def main(): functions = [] include_paths = [ '/usr/include', '/usr/local/include', '/opt/include', '/opt/local/include' ] if sys.platform == 'win32': include_paths = [r'c:\ta-lib\c\include'] header_found = False for path in include_paths: ta_func_header = os.path.join(path, 'ta-lib', 'ta_func.h') if os.path.exists(ta_func_header): header_found = True break if not header_found: print('Error: ta-lib/ta_func.h not found', file=sys.stderr) sys.exit(1) print("parsing %r" % ta_func_header) from pyclibrary import CParser ta_func_header = "/usr/local/include/ta-lib/ta_func.h" parser = CParser(ta_func_header) print("parsed") parser.print_all() d_functions = parser.defs['functions'] for funcname in d_functions.keys(): shortname = funcname[3:] d = dict() d['parsed'] = d_functions[funcname] is_float = funcname.startswith('TA_S_') d['is_float'] = is_float #is_indicator = not proto.startswith('TA_RetCode TA_Set') and not proto.startswith('TA_RetCode TA_Restore') #d['is_indicator'] = is_indicator is_lookback = '_Lookback' in funcname d['is_lookback'] = is_lookback #if not is_float and not is_lookback and is_indicator: try: func_info = abstract.Function(shortname).info except: print("can't get info for %r" % shortname) func_info = {} d['info'] = func_info d_functions[funcname] = d filename = 'functions.json' print("save to %r" % filename) with open(filename, 'w') as fd: json.dump(d_functions, fd, indent=4) filename = 'functions.yaml' print("save to %r" % filename) with open(filename, 'w') as fd: yaml.dump(d_functions, fd, indent=4)
def main(): functions = [] include_paths = ['/usr/include', '/usr/local/include', '/opt/include', '/opt/local/include'] if sys.platform == 'win32': include_paths = [r'c:\ta-lib\c\include'] header_found = False for path in include_paths: ta_func_header = os.path.join(path, 'ta-lib', 'ta_func.h') if os.path.exists(ta_func_header): header_found = True break if not header_found: print('Error: ta-lib/ta_func.h not found', file=sys.stderr) sys.exit(1) print("parsing %r" % ta_func_header) from pyclibrary import CParser ta_func_header="/usr/local/include/ta-lib/ta_func.h" parser = CParser(ta_func_header) print("parsed") parser.print_all() d_functions = parser.defs['functions'] for funcname in d_functions.keys(): shortname = funcname[3:] d = dict() d['parsed'] = d_functions[funcname] is_float = funcname.startswith('TA_S_') d['is_float'] = is_float #is_indicator = not proto.startswith('TA_RetCode TA_Set') and not proto.startswith('TA_RetCode TA_Restore') #d['is_indicator'] = is_indicator is_lookback = '_Lookback' in funcname d['is_lookback'] = is_lookback #if not is_float and not is_lookback and is_indicator: try: func_info = abstract.Function(shortname).info except: print("can't get info for %r" % shortname) func_info = {} d['info'] = func_info d_functions[funcname] = d filename = 'functions.json' print("save to %r" % filename) with open(filename, 'w') as fd: json.dump(d_functions, fd, indent=4) filename = 'functions.yaml' print("save to %r" % filename) with open(filename, 'w') as fd: yaml.dump(d_functions, fd, indent=4)
def parse_c_file(self): if not self.__check_file_exists(self.development_certificate) or \ not self.__check_file_exists(self.update_resource): return None values = {} values.update( CParser([self.development_certificate]).defs.get('values')) values.update( CParser([self.update_resource], macros={ 'MBED_CLOUD_DEV_UPDATE_ID': 1, 'MBED_CLOUD_DEV_UPDATE_CERT': 1 }).defs.get('values')) return values
def readHeader(self, file_name): p = CParser([file_name]) h = p.defs['structs'][p.defs['types'][p.defs['types'][self.handler][0]] [1]]['members'] input = self.findMember(h, 'input') if input == None: self.nof_inputs = 0 self.input_type = None self.input_size = 0 else: self.nof_inputs = input[1][2][0] self.input_type = input[1][0] size = self.findSize(self.findLinesHandler(file_name), 'input') if size == None: size = '2048*20' self.input_size = size output = self.findMember(h, 'output') if output == None: self.nof_outputs = 0 self.output_type = None self.output_size = 0 else: self.nof_outputs = output[1][2][0] self.output_type = output[1][0] size = self.findSize(self.findLinesHandler(file_name), 'output') if size == None: size = '2048*20' self.output_size = size initpm = p.defs['structs'].get(self.init_pm_struct) if (initpm != None): for m in initpm['members']: default = self.findDefault(self.findLinesStruct(\ self.findLinesHandler(file_name),\ self.init_pm_struct), m[0]) if default == None: default = '0' self.init_params.append({'name':m[0],'variable':self.name+'.init.'+m[0],\ 'type':m[1][0],'default':default}) inputpm = p.defs['structs'].get(self.input_pm_struct) if (inputpm != None): for m in inputpm['members']: default = self.findDefault(self.findLinesStruct(\ self.findLinesHandler(file_name),\ self.input_pm_struct), m[0]) self.input_params.append({'name':m[0],'variable':self.name+'.ctrl_in.'+m[0],\ 'type':m[1][0],'default':default}) outputpm = p.defs['structs'].get(self.output_pm_struct) if (outputpm != None): for m in outputpm['members']: self.output_params.append({'name':m[0],'variable':self.name+'.ctrl_out.'+m[0],\ 'type':m[1][0]})
def main(): if (len(sys.argv) > 1): try: parser = CParser([str(sys.argv[1])]) except: print("Unable to parse the header file") sys.exit() data_structure = [] d_structs = [ "veryHighPriorityData_t", "highPriorityData_t", "normalPriorityData_t", "lowPriorityData_t", "veryLowPriorityData_t", "asyncData_t" ] try: for i in range(0, 6): data_structure.append(parser.defs['types'][str(d_structs[i])]) except: print("Fundamental structure key name error") sys.exit() struct_data = [0] * 6 member_name = [0] * 6 for i in range(0, len(data_structure)): val = data_structure[i].type_spec.split(' ') temp = parser.defs['structs'][str(val[1])] struct_data[i] = [] member_name[i] = [] ''' Generate the structure string ''' for j in range(0, len(temp.members)): temp_struct_string = " " temp_type = temp.members[j][1].type_spec temp_arrLen = temp.members[j][1].declarators if (len(temp_arrLen) > 0): temp_struct_string = str( temp_type) + " " + temp.members[j][0] + str( temp_arrLen[0]) + ";" else: temp_struct_string = str( temp_type) + " " + temp.members[j][0] + ";" member_name[i].append(temp.members[j][0]) struct_data[i].append(temp_struct_string) print(struct_data[i]) print("\n") print(member_name) path = pathlib.Path(__file__).parent.absolute() filename = os.path.join(str(path), "jfw_structs.py") with open(filename, "w+") as f: f.write("import cstruct\n") for i in range(0, len(d_structs)): f.write( generateClass(str(d_structs[i]), struct_data[i], member_name[i])) f.write("\n") f.close() else: print("Please provide header filepath")
def parseHeaders(self, headers): self.parser = CParser(headers) self.dtypes = {} for (struct_name, s) in self.parser.defs['structs'].items(): members = s['members'] for (name, type, default_Value) in members: c_typename = type[0] if c_typename in self.parser.defs[ 'structs'] and c_typename not in self.dtypes: self.add_type(c_typename, self.parser.defs['structs'][c_typename]) self.add_dtype(struct_name, members)
def transpose_files(paths: list, macros: dict): """ Parses the header files in paths and returns header file of macros :param paths: list of path of headers to create macros for. should be ordered in topological ordering regarding dependency :param macros: dictionary of macros to define before parsing the header :return: transposed header file content and dictionary of parsed macros :rtype: str """ parser = CParser(paths, macros=macros) enum_macros = create_enum_macros(parser.defs['enums']) define_macros = create_define_macros(parser.defs['macros']) path = paths[ -1] # only the last header is needed to be included, as it will #include all the others return create_output(os.path.basename(path), enum_macros, define_macros)
def init_drv(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.sigStatusMessage.emit('Loading driver...') try: drv_path = os.environ['ANDORSDK'] except KeyError: raise Exception('Camera driver not found. Check if envionment variable ANDORSDK exist') ANDOR_HEADER = os.path.join(drv_path, 'ATMCD32D.H') ANDOR_LIB = os.path.join(drv_path, 'atmcd32d.dll') ANDOR_CACHE = os.path.join(drv_path, 'ATMCD32D.cache') self.header = CParser([ANDOR_HEADER], cache=ANDOR_CACHE, macros={'WINAPI':''}) self.camera = CLibrary(ANDOR_LIB, self.header, convention='windll') self.DRV_SUCCESS = self.camera.DRV_SUCCESS self.values = dict((v, k) for k, v in self.header.defs['values'].items()) QApplication.restoreOverrideCursor()
def __init__(self, cache_path="cache", lib_path=None, header_path=None): if not header_path: header_path = r"C:\Program Files\Instrument Systems\CAS4x64-SDK\VC2013\CAS4.h" if not lib_path: lib_path = r"C:\Windows\System32\CAS4x64.dll" lib_name = Path(lib_path).name if cache_path: cache_name = lib_name + ".cache" cache_path_ = Path(cache_path) cache_path_.mkdir(parents=True, exist_ok=True) cache_file_path = str(cache_path_ / cache_name) else: cache_file_path = None self.parser = CParser( header_path, cache=cache_file_path, replace={"#define __callconv __stdcall": "#define __callconv"}) self.clib = CLibrary(lib_path, self.parser)
typeSpec = 'long' quals = '' if len(type.type_quals) >= 1: quals = ' '.join(type.type_quals[0]) + ' ' return quals + typeSpec + ' '.join(type.declarators) def argToStr(arg): argName = arg[0] if argName == 'body': argName = '_body' argType = arg[1] return typeToStr(argType) + ' ' + argName parser = CParser(files=['Newton.h'], cache='Newton.h.cache') funcs = parser.defs['functions'] binding = '' binding += ''' /* Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:
# definitions. Exports that data to a C file for the introspection API. # # Requires: https://github.com/MatthieuDartiailh/pyclibrary # import os import sys import argparse from pyclibrary import CParser argp = argparse.ArgumentParser() argp.add_argument("-o", "--outfile") argp.add_argument("infiles", nargs="*") args = argp.parse_args() parser = CParser(args.infiles) source = """\ /* * Wireshark - Network traffic analyzer * By Gerald Combs <*****@*****.**> * Copyright 1998 Gerald Combs * * SPDX-License-Identifier: GPL-2.0-or-later * * Generated automatically from %s. * * It can be re-created using "make gen-enums". * * It is fine to edit this file by hand. Particularly if a symbol * disappears from the API it can just be removed here. There is no
# Functions _txt += "{.push dynlib:libName,importc.}\n" for txt1, txt2, comment, level, _ in data["functions"]: if not comment: _txt += f"{txt1}\n" _txt += "{.pop.}\n" return _txt if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("header", nargs=1, type=argparse.FileType('r'), help="the header file") parser.add_argument("library", nargs=1, type=argparse.FileType('r'), help="the library file") parser.add_argument("output", nargs='?', type=argparse.FileType('w'), default=sys.stdout, help="the output file") args = parser.parse_args() headerFileName = args.header[0].name libFileName = args.library[0].name #----- data = add_header_and_libname(headerFileName, libFileName) parser = CParser([headerFileName]) data = parse_macros(data, parser) data = parse_enums( data, parser ) data = create_types(data, parser) data = get_function_signatures_types(data, parser) data = create_functions(data, parser) #----- txt = create_text(data) args.output.write(txt)
args = argp.parse_args() if args.infiles: infiles = args.infiles else: infiles = default_infiles if args.outfile: outfile = args.outfile else: outfile = default_outfile print("input: {}".format(infiles)) print("output: {}".format(outfile)) parser = CParser(infiles) source = """\ /* * Wireshark - Network traffic analyzer * By Gerald Combs <*****@*****.**> * Copyright 1998 Gerald Combs * * SPDX-License-Identifier: GPL-2.0-or-later * * Generated automatically from %s. * * It can be re-created using "make gen-enums". * * It is fine to edit this file by hand. Particularly if a symbol * disappears from the API it can just be removed here. There is no
def parse_c_file(self): parser = CParser([self.development_certificate]) return parser.defs.get('values')
def create_dev_file(name): path = '../' + name.lower() + '/' + name.lower() + '.h' devpath = '../' + name.lower() + '/' + name.lower() + '.devlib' libpath = '../' + name.lower() + '/' + name.lower() + '.lib' funcnames = [] ''' Determine order of functions ''' libfile = open(libpath, 'r') line = libfile.readline().strip() columns = line.split() lib_name = columns[1] lib_version = columns[2] line = libfile.readline().strip() while line: columns = line.split() funcnames.append(columns[1]) line = libfile.readline().strip() devfile = open(devpath, 'w') parser = CParser([path]) functions = parser.defs['functions'] devfile.write('lib_%s:\n' % name.lower()) devfile.write('\tdb %s0,%s ; library name / version\n' % (lib_name, lib_version)) devfile.write('\tdb %d ; number of functions\n' % len(funcnames)) index = 0 for i in range(len(funcnames)): try: ret = functions[funcnames[i]][0] params = functions[funcnames[i]][1] except: print('no function prototype: ', funcnames[i]) ret = [] params = [] ''' Parse return value ''' retvalue = 0 if len(ret) > 0: retvalue = get_bytes_type(ret[0]) if len(ret) > 1: if ret[1] == "*": retvalue = 131 devfile.write('\tdb %d ; return value (%s [%d])\n' % (retvalue, funcnames[i], index)) ''' Parse parameters ''' if len(params) == 0 or (len(params) == 1 and str(params[0][1][0]) == "void"): devfile.write('\tdb 0 ; num params\n') devfile.write('\tdb 0,') else: devfile.write('\tdb %d ; num params\n' % len(params)) devfile.write('\tdb ') for arg in params: if len(arg[1]) > 0: argvalue = get_bytes_type(arg[1][0]) if len(arg[1]) > 1: if arg[1][1] == "*": argvalue = 131 devfile.write('%d,' % argvalue) for i in range(1, 14 - len(params)): devfile.write('0,') devfile.write('0\n') index += 3 devfile.close()
def _load_bifrost_lib(): import os import glob library_name = "libbifrost.so" api_prefix = "bf" header_paths = ["/usr/local/include/bifrost", "../src/bifrost"] # TODO: Remove this one? include_env = 'BIFROST_INCLUDE_PATH' # PYCLIBRARY ISSUE # TODO: Would it make sense to build this into PyCLibrary? library_env = 'LD_LIBRARY_PATH' home_dir = os.path.expanduser("~") parser_cache = os.path.join(home_dir, ".cache/bifrost.parse") def _get_env_paths(env): paths = os.getenv(env) if paths is None: return [] return [p for p in paths.split(':') if len(p.strip())] import pyclibrary from pyclibrary import CParser, CLibrary import ctypes # PYCLIBRARY ISSUE Should these be built in? Optional extra? # Note: This is needed because pyclibrary starts with only # the fundamental types (short, int, float etc.). #extra_types = {} #extra_types = {'uint64_t': ctypes.c_uint64} extra_types = { 'uint8_t': ctypes.c_uint8, 'int8_t': ctypes.c_int8, 'uint16_t': ctypes.c_uint16, 'int16_t': ctypes.c_int16, 'uint32_t': ctypes.c_uint32, 'int32_t': ctypes.c_int32, 'uint64_t': ctypes.c_uint64, 'int64_t': ctypes.c_int64, 'size_t': ctypes.c_ulong } try: pyclibrary.auto_init(extra_types=extra_types) except RuntimeError: pass # WAR for annoying "Can only initialise the parser once" header_paths = _get_env_paths(include_env) + header_paths valid_header_paths = [p for p in header_paths if os.path.exists(p)] # HACK TODO: Decide on what to do here valid_header_paths = valid_header_paths[:1] if len(valid_header_paths) == 0: raise ValueError( "No valid Bifrost header paths found. Run make install or set BIFROST_INCLUDE_PATH." ) header_path = valid_header_paths[0] headers = glob.glob(os.path.join(header_path, '*.h')) pyclibrary.utils.add_header_locations(valid_header_paths) try: _parser = CParser(headers, cache=unicode(parser_cache, "utf-8")) except AttributeError: # # PYCLIBRARY ISSUE WAR for "'tuple' has no attribute 'endswith'" bug raise ValueError("Could not find Bifrost headers.\nSearch paths: " + str(header_paths)) pyclibrary.utils.add_library_locations(_get_env_paths(library_env)) lib = CLibrary(library_name, _parser, prefix=api_prefix) return lib