cengal.introspection.third_party.ctypes.versions.v_0.ctypes

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18
 19__all__ = ['cvalue_to_dict']
 20
 21
 22from ctypes import Structure, _Pointer, Array, c_char, c_wchar
 23from typing import Dict
 24
 25
 26"""
 27Module Docstring
 28Docstrings: http://www.python.org/dev/peps/pep-0257/
 29"""
 30
 31__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 32__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 33__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 34__license__ = "Apache License, Version 2.0"
 35__version__ = "4.4.1"
 36__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 37__email__ = "gtalk@butenkoms.space"
 38# __status__ = "Prototype"
 39__status__ = "Development"
 40# __status__ = "Production"
 41
 42
 43def cvalue_to_dict(item) -> Dict:
 44    """_summary_
 45        Example:
 46            from pprintpp import pprint
 47            
 48            def wnd_proc_impl(self, hwnd: wintypes.HWND, msg: wintypes.UINT, wparam: wintypes.WPARAM, lparam: wintypes.LPARAM) -> LRESULT:
 49                if win32con.WM_NCCALCSIZE == msg:
 50                    lpnccalcsize_params = ctypes.cast(lparam, LPNCCALCSIZE_PARAMS)
 51                    print(f'WM_NCCALCSIZE: lpnccalcsize_params=')
 52                    pprint(ctypes_to_dict(lpnccalcsize_params))
 53                    print()
 54        
 55        Output:
 56            WM_NCCALCSIZE: lpnccalcsize_params=
 57            {
 58                'LP_NCCALCSIZE_PARAMS': {
 59                    'contents': {
 60                        'NCCALCSIZE_PARAMS': {
 61                            'lppos': {
 62                                'LP_WINDOWPOS': {
 63                                    'contents': {
 64                                        'WINDOWPOS': {
 65                                            'cx': 416,
 66                                            'cy': 240,
 67                                            'flags': 6199,
 68                                            'hwnd': 22679738,
 69                                            'hwndInsertAfter': None,
 70                                            'x': 507,
 71                                            'y': 114,
 72                                        },
 73                                    },
 74                                },
 75                            },
 76                            'rgrc': {
 77                                'RECT_Array_3': [
 78                                    {
 79                                        'RECT': {
 80                                            'bottom': 354,
 81                                            'left': 507,
 82                                            'right': 923,
 83                                            'top': 114,
 84                                        },
 85                                    },
 86                                    {
 87                                        'RECT': {
 88                                            'bottom': 354,
 89                                            'left': 507,
 90                                            'right': 923,
 91                                            'top': 114,
 92                                        },
 93                                    },
 94                                    {
 95                                        'RECT': {
 96                                            'bottom': 354,
 97                                            'left': 507,
 98                                            'right': 923,
 99                                            'top': 114,
100                                        },
101                                    },
102                                ],
103                            },
104                        },
105                    },
106                },
107            }
108    
109    Args:
110        item (_type_): _description_
111
112    Returns:
113        Dict: _description_
114    """    
115    item_type_name = type(item).__name__
116    if isinstance(item, Structure):
117        st = item
118        data = dict()
119        fields = st._fields_
120        for field_name, field_type in fields:
121            field_value = cvalue_to_dict(getattr(st, field_name))
122            data[field_name] = field_value
123        
124        return {item_type_name: data}
125    elif isinstance(item, _Pointer):
126        field_name = 'contents'
127        field_value = cvalue_to_dict(item.contents)
128        return {item_type_name: {field_name: field_value}}
129    elif isinstance(item, Array):
130        array_type = item._type_
131        if c_char == array_type:
132            return item.value  # bytes
133        elif c_wchar == array_type:
134            return item.value  # str
135        else:
136            data = list()
137            array_len = len(item)
138            for sub_item in item:
139                data.append(cvalue_to_dict(sub_item))
140
141            return {item_type_name: data}
142    else:
143        return item
def cvalue_to_dict(item) -> Dict:
 44def cvalue_to_dict(item) -> Dict:
 45    """_summary_
 46        Example:
 47            from pprintpp import pprint
 48            
 49            def wnd_proc_impl(self, hwnd: wintypes.HWND, msg: wintypes.UINT, wparam: wintypes.WPARAM, lparam: wintypes.LPARAM) -> LRESULT:
 50                if win32con.WM_NCCALCSIZE == msg:
 51                    lpnccalcsize_params = ctypes.cast(lparam, LPNCCALCSIZE_PARAMS)
 52                    print(f'WM_NCCALCSIZE: lpnccalcsize_params=')
 53                    pprint(ctypes_to_dict(lpnccalcsize_params))
 54                    print()
 55        
 56        Output:
 57            WM_NCCALCSIZE: lpnccalcsize_params=
 58            {
 59                'LP_NCCALCSIZE_PARAMS': {
 60                    'contents': {
 61                        'NCCALCSIZE_PARAMS': {
 62                            'lppos': {
 63                                'LP_WINDOWPOS': {
 64                                    'contents': {
 65                                        'WINDOWPOS': {
 66                                            'cx': 416,
 67                                            'cy': 240,
 68                                            'flags': 6199,
 69                                            'hwnd': 22679738,
 70                                            'hwndInsertAfter': None,
 71                                            'x': 507,
 72                                            'y': 114,
 73                                        },
 74                                    },
 75                                },
 76                            },
 77                            'rgrc': {
 78                                'RECT_Array_3': [
 79                                    {
 80                                        'RECT': {
 81                                            'bottom': 354,
 82                                            'left': 507,
 83                                            'right': 923,
 84                                            'top': 114,
 85                                        },
 86                                    },
 87                                    {
 88                                        'RECT': {
 89                                            'bottom': 354,
 90                                            'left': 507,
 91                                            'right': 923,
 92                                            'top': 114,
 93                                        },
 94                                    },
 95                                    {
 96                                        'RECT': {
 97                                            'bottom': 354,
 98                                            'left': 507,
 99                                            'right': 923,
100                                            'top': 114,
101                                        },
102                                    },
103                                ],
104                            },
105                        },
106                    },
107                },
108            }
109    
110    Args:
111        item (_type_): _description_
112
113    Returns:
114        Dict: _description_
115    """    
116    item_type_name = type(item).__name__
117    if isinstance(item, Structure):
118        st = item
119        data = dict()
120        fields = st._fields_
121        for field_name, field_type in fields:
122            field_value = cvalue_to_dict(getattr(st, field_name))
123            data[field_name] = field_value
124        
125        return {item_type_name: data}
126    elif isinstance(item, _Pointer):
127        field_name = 'contents'
128        field_value = cvalue_to_dict(item.contents)
129        return {item_type_name: {field_name: field_value}}
130    elif isinstance(item, Array):
131        array_type = item._type_
132        if c_char == array_type:
133            return item.value  # bytes
134        elif c_wchar == array_type:
135            return item.value  # str
136        else:
137            data = list()
138            array_len = len(item)
139            for sub_item in item:
140                data.append(cvalue_to_dict(sub_item))
141
142            return {item_type_name: data}
143    else:
144        return item

_summary_ Example: from pprintpp import pprint

    def wnd_proc_impl(self, hwnd: wintypes.HWND, msg: wintypes.UINT, wparam: wintypes.WPARAM, lparam: wintypes.LPARAM) -> LRESULT:
        if win32con.WM_NCCALCSIZE == msg:
            lpnccalcsize_params = ctypes.cast(lparam, LPNCCALCSIZE_PARAMS)
            print(f'WM_NCCALCSIZE: lpnccalcsize_params=')
            pprint(ctypes_to_dict(lpnccalcsize_params))
            print()

Output:
    WM_NCCALCSIZE: lpnccalcsize_params=
    {
        'LP_NCCALCSIZE_PARAMS': {
            'contents': {
                'NCCALCSIZE_PARAMS': {
                    'lppos': {
                        'LP_WINDOWPOS': {
                            'contents': {
                                'WINDOWPOS': {
                                    'cx': 416,
                                    'cy': 240,
                                    'flags': 6199,
                                    'hwnd': 22679738,
                                    'hwndInsertAfter': None,
                                    'x': 507,
                                    'y': 114,
                                },
                            },
                        },
                    },
                    'rgrc': {
                        'RECT_Array_3': [
                            {
                                'RECT': {
                                    'bottom': 354,
                                    'left': 507,
                                    'right': 923,
                                    'top': 114,
                                },
                            },
                            {
                                'RECT': {
                                    'bottom': 354,
                                    'left': 507,
                                    'right': 923,
                                    'top': 114,
                                },
                            },
                            {
                                'RECT': {
                                    'bottom': 354,
                                    'left': 507,
                                    'right': 923,
                                    'top': 114,
                                },
                            },
                        ],
                    },
                },
            },
        },
    }

Args: item (_type_): _description_

Returns: Dict: _description_