def run_parser(source_dirs, search_dirs): try: types = dsdl.parse_namespaces(source_dirs, search_dirs) except dsdl.DsdlException as ex: logger.info('Parser failure', exc_info=True) die(ex) return types
def load_dsdl(paths): """Loads the DSDL files under the given directory/directories, and creates types for each of them in the current module's namespace. Also adds entries for all datatype (ID, kind)s to the DATATYPES dictionary, which maps datatype (ID, kind)s to their respective type classes.""" global DATATYPES, TYPENAMES if isinstance(paths, basestring): paths = [paths] root_namespace = Namespace() dtypes = dsdl.parse_namespaces(paths, []) for dtype in dtypes: namespace, _, typename = dtype.full_name.rpartition(".") root_namespace._path(namespace).__dict__[typename] = dtype TYPENAMES[dtype.full_name] = dtype if dtype.default_dtid: DATATYPES[(dtype.default_dtid, dtype.kind)] = dtype # Add the base CRC to each data type capable of being transmitted dtype.base_crc = dsdl.common.crc16_from_bytes( struct.pack("<Q", dtype.get_data_type_signature())) logging.debug( "DSDL Load {: >30} DTID: {: >4} base_crc:{: >8}".format( typename, dtype.default_dtid, hex(dtype.base_crc))) def create_instance_closure(closure_type): def create_instance(*args, **kwargs): return transport.CompoundValue(closure_type, tao=True, *args, **kwargs) return create_instance dtype.__call__ = create_instance_closure(dtype) namespace = root_namespace._path("uavcan") for top_namespace in namespace._namespaces(): MODULE.__dict__[str(top_namespace)] = namespace.__dict__[top_namespace] MODULE.__dict__["thirdparty"] = Namespace() for ext_namespace in root_namespace._namespaces(): if str(ext_namespace) != "uavcan": MODULE.thirdparty.__dict__[str(ext_namespace)] = \ root_namespace.__dict__[ext_namespace]
def load_dsdl(paths): """Loads the DSDL files under the given directory/directories, and creates types for each of them in the current module's namespace. Also adds entries for all datatype (ID, kind)s to the DATATYPES dictionary, which maps datatype (ID, kind)s to their respective type classes.""" global DATATYPES, TYPENAMES if isinstance(paths, basestring): paths = [paths] root_namespace = Namespace() dtypes = dsdl.parse_namespaces(paths, []) for dtype in dtypes: namespace, _, typename = dtype.full_name.rpartition(".") root_namespace._path(namespace).__dict__[typename] = dtype TYPENAMES[dtype.full_name] = dtype if dtype.default_dtid: DATATYPES[(dtype.default_dtid, dtype.kind)] = dtype # Add the base CRC to each data type capable of being transmitted dtype.base_crc = dsdl.common.crc16_from_bytes( struct.pack("<Q", dtype.get_data_type_signature())) logging.debug("DSDL Load {: >30} DTID: {: >4} base_crc:{: >8}". format(typename, dtype.default_dtid, hex(dtype.base_crc))) def create_instance_closure(closure_type): def create_instance(*args, **kwargs): return transport.CompoundValue(closure_type, tao=True, *args, **kwargs) return create_instance dtype.__call__ = create_instance_closure(dtype) namespace = root_namespace._path("uavcan") for top_namespace in namespace._namespaces(): MODULE.__dict__[str(top_namespace)] = namespace.__dict__[top_namespace] MODULE.__dict__["thirdparty"] = Namespace() for ext_namespace in root_namespace._namespaces(): if str(ext_namespace) != "uavcan": MODULE.thirdparty.__dict__[str(ext_namespace)] = \ root_namespace.__dict__[ext_namespace]
def load_dsdl(*paths, **args): """ Loads the DSDL files under the given directory/directories, and creates types for each of them in the current module's namespace. If the exclude_dist argument is not present, or False, the DSDL definitions installed with this package will be loaded first. Also adds entries for all datatype (ID, kind)s to the DATATYPES dictionary, which maps datatype (ID, kind)s to their respective type classes. """ global DATATYPES, TYPENAMES paths = list(paths) # Try to prepend the built-in DSDL files # TODO: why do we need try/except here? # noinspection PyBroadException try: if not args.get("exclude_dist", None): dsdl_path = pkg_resources.resource_filename(__name__, "dsdl_files") # @UndefinedVariable paths = [os.path.join(dsdl_path, "uavcan")] + paths except Exception: pass root_namespace = Namespace() dtypes = dsdl.parse_namespaces(paths) for dtype in dtypes: namespace, _, typename = dtype.full_name.rpartition(".") root_namespace._path(namespace).__dict__[typename] = dtype TYPENAMES[dtype.full_name] = dtype if dtype.default_dtid: DATATYPES[(dtype.default_dtid, dtype.kind)] = dtype # Add the base CRC to each data type capable of being transmitted dtype.base_crc = dsdl.crc16_from_bytes(struct.pack("<Q", dtype.get_data_type_signature())) logger.debug("DSDL Load {: >30} DTID: {: >4} base_crc:{: >8}" .format(typename, dtype.default_dtid, hex(dtype.base_crc))) def create_instance_closure(closure_type, _mode=None): # noinspection PyShadowingNames def create_instance(*args, **kwargs): if _mode: assert '_mode' not in kwargs, 'Mode cannot be supplied to service type instantiation helper' kwargs['_mode'] = _mode return transport.CompoundValue(closure_type, *args, **kwargs) return create_instance dtype._instantiate = create_instance_closure(dtype) if dtype.kind == dtype.KIND_SERVICE: dtype.Request = create_instance_closure(dtype, _mode='request') dtype.Response = create_instance_closure(dtype, _mode='response') namespace = root_namespace._path("uavcan") for top_namespace in namespace._namespaces(): MODULE.__dict__[str(top_namespace)] = namespace.__dict__[top_namespace] MODULE.__dict__["thirdparty"] = Namespace() for ext_namespace in root_namespace._namespaces(): if str(ext_namespace) != "uavcan": # noinspection PyUnresolvedReferences MODULE.thirdparty.__dict__[str(ext_namespace)] = root_namespace.__dict__[ext_namespace]
def load_dsdl(*paths, **args): """ Loads the DSDL files under the given directory/directories, and creates types for each of them in the current module's namespace. If the exclude_dist argument is not present, or False, the DSDL definitions installed with this package will be loaded first. Also adds entries for all datatype (ID, kind)s to the DATATYPES dictionary, which maps datatype (ID, kind)s to their respective type classes. """ global DATATYPES, TYPENAMES paths = list(paths) # Try to prepend the built-in DSDL files # TODO: why do we need try/except here? # noinspection PyBroadException try: if not args.get("exclude_dist", None): dsdl_path = pkg_resources.resource_filename(__name__, "dsdl_files") # @UndefinedVariable paths = [os.path.join(dsdl_path, "uavcan")] + paths custom_path = os.path.join(os.path.expanduser("~"), "uavcan_vendor_specific_types") if os.path.isdir(custom_path): paths += [f for f in [os.path.join(custom_path, f) for f in os.listdir(custom_path)] if os.path.isdir(f)] except Exception: pass root_namespace = Namespace() dtypes = dsdl.parse_namespaces(paths) for dtype in dtypes: namespace, _, typename = dtype.full_name.rpartition(".") root_namespace._path(namespace).__dict__[typename] = dtype TYPENAMES[dtype.full_name] = dtype if dtype.default_dtid: DATATYPES[(dtype.default_dtid, dtype.kind)] = dtype # Add the base CRC to each data type capable of being transmitted dtype.base_crc = dsdl.crc16_from_bytes(struct.pack("<Q", dtype.get_data_type_signature())) logger.debug("DSDL Load {: >30} DTID: {: >4} base_crc:{: >8}" .format(typename, dtype.default_dtid, hex(dtype.base_crc))) def create_instance_closure(closure_type, _mode=None): # noinspection PyShadowingNames def create_instance(*args, **kwargs): if _mode: assert '_mode' not in kwargs, 'Mode cannot be supplied to service type instantiation helper' kwargs['_mode'] = _mode return transport.CompoundValue(closure_type, *args, **kwargs) return create_instance dtype._instantiate = create_instance_closure(dtype) if dtype.kind == dtype.KIND_SERVICE: dtype.Request = create_instance_closure(dtype, _mode='request') dtype.Response = create_instance_closure(dtype, _mode='response') namespace = root_namespace._path("uavcan") for top_namespace in namespace._namespaces(): MODULE.__dict__[str(top_namespace)] = namespace.__dict__[top_namespace] MODULE.__dict__["thirdparty"] = Namespace() for ext_namespace in root_namespace._namespaces(): if str(ext_namespace) != "uavcan": # noinspection PyUnresolvedReferences MODULE.thirdparty.__dict__[str(ext_namespace)] = root_namespace.__dict__[ext_namespace]
#!/usr/bin/env python3 import sys import logging logging.basicConfig(stream=sys.stderr, level='DEBUG', format='%(levelname)s: %(message)s') from uavcan import dsdl parsed = dsdl.parse_namespaces(['uavcan','com','ardupilot', 'dronecan']); if parsed: logging.info('%d data types parsed successfully', len(parsed))