11 Python UAVCAN package.
12 Supported Python versions: 3.2+, 2.7.
15 from __future__
import division, absolute_import, print_function, unicode_literals
21 from logging
import getLogger
26 except AttributeError:
33 return monotonic.monotonic()
34 time.monotonic = monotonic_wrapper
36 time.monotonic = time.time
37 print(
'''The package 'monotonic' is not available, the library will use real time instead of monotonic time.
38 This implies that the library may misbehave if system clock is adjusted while the library is running.
39 In order to fix this problem, consider either option:
40 1. Switch to Python 3.
41 2. Install the missing package, e.g. using pip:
42 pip install monotonic''', file=sys.stderr)
49 from .version
import __version__
56 get_active_union_field, switch_union_field, is_union, \
57 get_constants, get_fields, \
58 is_request, is_response
62 TRANSFER_PRIORITY_LOWEST = 31
63 TRANSFER_PRIORITY_HIGHEST = 0
66 logger = getLogger(__name__)
74 """Provides a nice object-based way to look up UAVCAN data types."""
81 """Returns the namespace object at the given .-separated path,
82 creating any namespaces in the path that don't already exist."""
84 attr, _, subpath = attrpath.partition(
".")
85 if attr
not in self.__dict__:
90 return self.__dict__[attr].
_path(subpath)
92 return self.__dict__[attr]
95 """Returns the top-level namespaces in this object"""
107 Loads the DSDL files under the given directory/directories, and creates
108 types for each of them in the current module's namespace.
110 If the exclude_dist argument is not present, or False, the DSDL
111 definitions installed with this package will be loaded first.
113 Also adds entries for all datatype (ID, kind)s to the DATATYPES
114 dictionary, which maps datatype (ID, kind)s to their respective type
117 global DATATYPES, TYPENAMES
125 if not args.get(
"exclude_dist",
None):
126 dsdl_path = pkg_resources.resource_filename(__name__,
"dsdl_files")
127 paths = [os.path.join(dsdl_path,
"uavcan")] + paths
128 custom_path = os.path.join(os.path.expanduser(
"~"),
"uavcan_vendor_specific_types")
129 if os.path.isdir(custom_path):
130 paths += [f
for f
in [os.path.join(custom_path, f)
for f
in os.listdir(custom_path)]
136 dtypes = dsdl.parse_namespaces(paths)
138 namespace, _, typename = dtype.full_name.rpartition(
".")
139 root_namespace._path(namespace).__dict__[typename] = dtype
140 TYPENAMES[dtype.full_name] = dtype
142 if dtype.default_dtid:
143 DATATYPES[(dtype.default_dtid, dtype.kind)] = dtype
145 dtype.base_crc = dsdl.crc16_from_bytes(struct.pack(
"<Q", dtype.get_data_type_signature()))
146 logger.debug(
"DSDL Load {: >30} DTID: {: >4} base_crc:{: >8}"
147 .
format(typename, dtype.default_dtid, hex(dtype.base_crc)))
149 def create_instance_closure(closure_type, _mode=None):
151 def create_instance(*args, **kwargs):
153 assert '_mode' not in kwargs,
'Mode cannot be supplied to service type instantiation helper'
154 kwargs[
'_mode'] = _mode
156 return create_instance
158 dtype._instantiate = create_instance_closure(dtype)
160 if dtype.kind == dtype.KIND_SERVICE:
161 dtype.Request = create_instance_closure(dtype, _mode=
'request')
162 dtype.Response = create_instance_closure(dtype, _mode=
'response')
164 namespace = root_namespace._path(
"uavcan")
165 for top_namespace
in namespace._namespaces():
166 MODULE.__dict__[
str(top_namespace)] = namespace.__dict__[top_namespace]
168 MODULE.__dict__[
"thirdparty"] =
Namespace()
169 for ext_namespace
in root_namespace._namespaces():
170 if str(ext_namespace) !=
"uavcan":
172 MODULE.thirdparty.__dict__[
str(ext_namespace)] = root_namespace.__dict__[ext_namespace]
175 __all__ = [
"dsdl",
"transport",
"load_dsdl",
"DATATYPES",
"TYPENAMES"]
181 MODULE.__dict__ = globals()
182 MODULE._module = sys.modules[MODULE.__name__]
183 MODULE._pmodule = MODULE
184 sys.modules[MODULE.__name__] = MODULE