OpenCyphal / pycyphal

Python implementation of the Cyphal protocol stack.
https://pycyphal.readthedocs.io/
MIT License
119 stars 106 forks source link

Default DSDL-generated package directory path #85

Closed pavel-kirienko closed 4 years ago

pavel-kirienko commented 4 years ago

I realized that it is extremely unlikely that one will ever need to maintain several separate directories with DSDL-generated packages due to the inherent stability and immutability of DSDL definitions. The addition of a library-default path removes a substantial chunk of boilerplate from interactive sessions and simple scripts.

This PR makes the following possible, assuming that the package has been generated beforehand using the CLI tool:

import sys
import pyuavcan
sys.path.insert(0, str(pyuavcan.dsdl.DEFAULT_GENERATED_PACKAGES_DIR))
import pyuavcan.application

Without this change, the functional equivalent would be:

import sys
import pathlib
import tempfile
import importlib
import pyuavcan
dsdl_generated_dir = pathlib.Path(tempfile.gettempdir(), 'dsdl-for-my-program', f'pyuavcan-v{pyuavcan.__version__}')
dsdl_generated_dir.mkdir(parents=True, exist_ok=True)
sys.path.insert(0, str(dsdl_generated_dir))
try:
    import pyuavcan.application
except (ImportError, AttributeError):
    script_path = os.path.abspath(os.path.dirname(__file__))
    pyuavcan.dsdl.generate_package(
        package_parent_directory=dsdl_generated_dir,
        root_namespace_directory=os.path.join(script_path, '../public_regulated_data_types/uavcan')
    )
    importlib.invalidate_caches()
    import pyuavcan.application

This PR also brings a bunch of automatic cosmetic changes to the codebase, hence the large diff.

coveralls commented 4 years ago

Pull Request Test Coverage Report for Build 685


Files with Coverage Reduction New Missed Lines %
pyuavcan/presentation/_port/_subscriber.py 1 81.14%
pyuavcan/transport/can/media/socketcan/_socketcan.py 1 82.57%
.test_dsdl_generated/uavcan/si/sample/angle/Scalar_1_0.py 1 84.0%
.test_dsdl_generated/uavcan/si/sample/duration/Scalar_1_0.py 1 84.0%
.test_dsdl_generated/uavcan/si/sample/voltage/Scalar_1_0.py 1 84.0%
.test_dsdl_generated/uavcan/si/sample/volume/Scalar_1_0.py 1 84.0%
.test_dsdl_generated/uavcan/si/unit/energy/Scalar_1_0.py 1 84.31%
.test_dsdl_generated/uavcan/si/unit/length/Scalar_1_0.py 1 84.31%
.test_dsdl_generated/uavcan/si/unit/voltage/Scalar_1_0.py 1 84.31%
.test_dsdl_generated/uavcan/si/unit/volumetric_flow_rate/Scalar_1_0.py 1 84.31%
<!-- Total: 148 -->
Totals Coverage Status
Change from base Build 678: -0.07%
Covered Lines: 19316
Relevant Lines: 20410

💛 - Coveralls