def _print(cls, fcns, header_filename): # useful when creating/updating a new wrapper class from msl.equipment.resources.utils import CHeader from msl.equipment.resources.utils import camelcase_to_underscore as convert def get_comment(lines, name): # used when creating a new wrapper class comments = [] found_it = False for line in lines: if name in line and '__cdecl' in line: found_it = True continue if found_it: if line.startswith('///'): comments.append(line[3:].strip()) else: break return ' """{}\n """'.format(' \n '.join(comments[::-1])) already_defined = (vars(cls)) header = CHeader('C:/Program Files/Thorlabs/Kinesis/' + header_filename, remove_comments=False) lines = header.get_lines()[::-1] for item in sorted(fcns): method_name = item[0].replace('MMIparams', 'MmiParams') method_name = method_name.replace('LEDswitches', 'LedSwitches') method_name = convert(method_name.split('_')[1]) args_p = '' args_c = '' for i, arg in enumerate(item[3]): if i == 0 and 'c_char_p' in str(arg[0]): args_c += 'self._serial, ' elif 'PyCPointerType' in str(type(arg[0])): args_c += 'byref({}), '.format(convert(arg[1])) else: a = convert(arg[1]) args_p += '{}, '.format(a) args_c += '{}, '.format(a) if method_name in already_defined: continue args_p = args_p[:-2] if args_p: print(' def {}(self, {}):'.format(method_name, args_p)) else: print(' def {}(self):'.format(method_name)) print(get_comment(lines, item[0])) print(' return self.sdk.{}({})\n'.format(item[0], args_c[:-2]))
'UnitType': c_short, } if __name__ == '__main__': # The following was used to automatically generate the above... import os from msl.equipment.resources.utils import CHeader enums = {} # dict of all enums enum_ctypes = {'UnitType': 'c_short'} # dict of enum data types root = r'C:\Program Files\Thorlabs\Kinesis' for f in os.listdir(root): if f.endswith('.h'): header = CHeader(os.path.join(root, f)) for key, value in header.enums().items(): if key in enums: continue enum_ctypes[key] = value[1] enums[key] = value[2] for class_name in enums: print('class {}(IntEnum):'.format(class_name)) for name, value in enums[class_name].items(): print(' {} = {}'.format(name, value)) print('\n') print('class UnitType(IntEnum):') print(' DISTANCE = 0') print(' VELOCITY = 1')
""" Example showing how to extract information from a C/C++ header file. """ # this "if" statement is used so that Sphinx does not execute this script when the docs are being built if __name__ == '__main__': from msl.equipment.resources.utils import CHeader path = r'C:\Program Files\Pico Technology\SDK\inc\ps5000aApi.h' fcn_regex = r'PREF0\s+PREF1\s+(\w+)\s+PREF2\s+PREF3\s+\((\w+)\)' header = CHeader(path) print('***** Constants *****') constants = header.constants() for key, value in constants.items(): print(key, value) print('') print('***** Enums *****') enums = header.enums() for key, value in enums.items(): print(key, value) print('') print('***** Structs *****') structs = header.structs() for key, value in structs.items(): print(key, value) print('')
# def set_timeout(self, timeout): # """Set the filter wheel's timeout value. # # Parameters # ---------- # timeout : :class:`int` # The timeout value, in seconds. # """ # self.sdk.SetTimeout(self._handle, timeout) if __name__ == '__main__': from msl.equipment.resources.utils import CHeader, camelcase_to_underscore header = CHeader( r'C:\Users\j.borbely\Desktop\AppNotes_FW102C_v400\AppNotes_FW102C\msvc\fw_cmd_library.h' ) fcns = header.functions(r'DllExport\s+(\w+)\s+(\w+)') for key, value in fcns.items(): print(' self.sdk.{name}.restype = {res}'.format(name=key, res=value[0])) print(' self.sdk.{name}.argtypes = [{args}]'.format( name=key, args=', '.join( [item[0] for item in value[1] if item[0] is not None]))) print(' self.sdk.{name}.errcheck = self.errcheck_code'.format( name=key)) print()
def multi_park(self): return self.lib.BI_multi_park() def start_log(self, c_list): return self.lib.BI_start_log(c_list) def stop_log(self, c_list): return self.lib.BI_stop_log(c_list) if __name__ == '__main__': from msl.equipment.resources.utils import CHeader, camelcase_to_underscore header = r'H:\Bentham\SDK\lang\c\bendll.h' header = CHeader(header) fcns = header.functions(r'typedef\s+(\w+)\s*\(WINAPI\*pf(\w+)\)') for key, value in fcns.items(): print(' self.lib.{name}.restype = {res}'.format(name=key, res=value[0])) print(' self.lib.{name}.argtypes = [{args}]'.format(name=key, args=', '.join([item[0] for item in value[1] if item[0] is not None]))) print() for key, value in fcns.items(): args_p = [camelcase_to_underscore(item[1]) for item in value[1] if item[0] is not None and not item[0].startswith('POINTER')] args_c = [camelcase_to_underscore(item[1]) for item in value[1] if item[0] is not None] ptrs = [[camelcase_to_underscore(item[1]), item[0]] for item in value[1] if item[0] is not None and item[0].startswith('POINTER')] if args_p: print(' def {}(self, {}):'.format(camelcase_to_underscore(key[3:]), ', '.join(args_p))) for p in ptrs: print(' {} = {}()'.format(p[0], p[1][8:-1]))