def _discover_interfaces(self, interface_package): all_interfaces = [] if interface_package is not None: for interface_module in get_submodules(interface_package).values(): all_interfaces += list( get_members(interface_module, is_interface).values() ) all_interfaces += list(get_members(self._module, is_interface).values()) interfaces = {} for interface in all_interfaces: existing_interface = interfaces.get(interface.protocol) if existing_interface is not None: raise RuntimeError( "The protocol '{}' is defined in two interfaces for device '{}':\n" " {} (in {})\n" " {} (in {})\n" "One of the protocol names needs to be changed.".format( interface.protocol, self.name, existing_interface.__name__, existing_interface.__module__, interface.__name__, interface.__module__, ) ) interfaces[interface.protocol] = interface return interfaces
def _discover_interfaces(self, interface_package): all_interfaces = [] if interface_package is not None: for interface_module in get_submodules(interface_package).values(): all_interfaces += list(get_members(interface_module, is_interface).values()) all_interfaces += list(get_members(self._module, is_interface).values()) interfaces = {} for interface in all_interfaces: existing_interface = interfaces.get(interface.protocol) if existing_interface is not None: raise RuntimeError( 'The protocol \'{}\' is defined in two interfaces for device \'{}\':\n' ' {} (in {})\n' ' {} (in {})\n' 'One of the protocol names needs to be changed.'.format( interface.protocol, self.name, existing_interface.__name__, existing_interface.__module__, interface.__name__, interface.__module__)) interfaces[interface.protocol] = interface return interfaces
def _discover_setups(self, setups_package): setups = getattr(self._module, "setups", {}) all_setups = setups if isinstance(setups, dict) else {} if setups_package is not None: for name, setup_module in get_submodules(setups_package).items(): existing_setup = all_setups.get(name) if existing_setup is not None: raise RuntimeError( "The setup '{}' is defined twice in device '{}'.".format( existing_setup, self.name ) ) all_setups[name] = { "device_type": getattr( setup_module, "device_type", self.default_device_type ), "parameters": getattr(setup_module, "parameters", {}), } if "default" not in all_setups: all_setups["default"] = {"device_type": self.default_device_type} return all_setups
def _discover_setups(self, setups_package): setups = getattr(self._module, 'setups', {}) all_setups = setups if isinstance(setups, dict) else {} if setups_package is not None: for name, setup_module in get_submodules(setups_package).items(): existing_setup = all_setups.get(name) if existing_setup is not None: raise RuntimeError( 'The setup \'{}\' is defined twice in device \'{}\'.'. format(existing_setup, self.name)) all_setups[name] = { 'device_type': getattr(setup_module, 'device_type', self.default_device_type), 'parameters': getattr(setup_module, 'parameters', {}) } if 'default' not in all_setups: all_setups['default'] = {'device_type': self.default_device_type} return all_setups
def __init__(self, module): self._module = module self._device_types = list(get_members(self._module, is_device).values()) submodules = get_submodules(self._module) self._setups = self._discover_setups(submodules.get('setups')) self._interfaces = self._discover_interfaces(submodules.get('interfaces'))
def _discover_devices(self, devices_package): devices = list(get_members(self._module, is_device).values()) if devices_package is None: return devices for module in get_submodules(devices_package).values(): devices += list(get_members(module, is_device).values()) return devices
def _discover_devices(self, devices_package): devices = list(get_members(self._module, is_device).values()) if devices_package is None: return devices for module in get_submodules(devices_package).values(): devices += list(get_members(module, is_device).values()) return devices
def __init__(self, device_module): try: self._device_module = importlib.import_module(device_module) except ImportError: raise LewisException( 'Failed to import module \'{}\' for device discovery. ' 'Make sure that it is in the PYTHONPATH.\n' 'See also the -a option of lewis.'.format(device_module)) self._devices = {name: DeviceBuilder(module) for name, module in get_submodules(self._device_module).items()}
def __init__(self, device_module): try: self._device_module = importlib.import_module(device_module) except ImportError: raise LewisException( 'Failed to import module \'{}\' for device discovery. ' 'Make sure that it is in the PYTHONPATH.\n' 'See also the -a option of lewis.'.format(device_module)) self._devices = {name: DeviceBuilder(module) for name, module in get_submodules(self._device_module).items()} self.log.debug('Devices loaded from \'%s\': %s', device_module, ', '.join(self._devices.keys()))
def __init__(self, module): self._module = module submodules = get_submodules(self._module) self._device_types = self._discover_devices(submodules.get('devices')) self._setups = self._discover_setups(submodules.get('setups')) self._interfaces = self._discover_interfaces(submodules.get('interfaces')) self.log.debug( 'Discovered the following items in \'%s\': Devices: %s; Setups: %s; Interfaces: %s', self._module.__name__, ', '.join(device_t.__name__ for device_t in self._device_types), ', '.join(self._setups.keys()), ', '.join('(%s: %s)' % (k, v.__name__) for k, v in self._interfaces.items()))
def __init__(self, module): self._module = module submodules = get_submodules(self._module) self._device_types = self._discover_devices(submodules.get('devices')) self._setups = self._discover_setups(submodules.get('setups')) self._interfaces = self._discover_interfaces( submodules.get('interfaces')) self.log.debug( 'Discovered the following items in \'%s\': Devices: %s; Setups: %s; Interfaces: %s', self._module.__name__, ', '.join(device_t.__name__ for device_t in self._device_types), ', '.join(self._setups.keys()), ', '.join('(%s: %s)' % (k, v.__name__) for k, v in self._interfaces.items()))
def __init__(self, module): self._module = module submodules = get_submodules(self._module) self._device_types = self._discover_devices(submodules.get("devices")) self._setups = self._discover_setups(submodules.get("setups")) self._interfaces = self._discover_interfaces(submodules.get("interfaces")) self.log.debug( "Discovered the following items in '%s': Devices: %s; Setups: %s; Interfaces: %s", self._module.__name__, ", ".join(device_t.__name__ for device_t in self._device_types), ", ".join(self._setups.keys()), ", ".join( "(%s: %s)" % (k, v.__name__) for k, v in self._interfaces.items() ), )
def __init__(self, device_module): try: self._device_module = importlib.import_module(device_module) except ImportError: raise LewisException( "Failed to import module '{}' for device discovery. " "Make sure that it is in the PYTHONPATH.\n" "See also the -a option of lewis.".format(device_module)) self._devices = { name: DeviceBuilder(module) for name, module in get_submodules(self._device_module).items() } self.log.debug( "Devices loaded from '%s': %s", device_module, ", ".join(self._devices.keys()), )
def _discover_setups(self, setups_package): setups = getattr(self._module, 'setups', {}) all_setups = setups if isinstance(setups, dict) else {} if setups_package is not None: for name, setup_module in get_submodules(setups_package).items(): existing_setup = all_setups.get(name) if existing_setup is not None: raise RuntimeError( 'The setup \'{}\' is defined twice in device \'{}\'.'.format( existing_setup, self.name)) all_setups[name] = { 'device_type': getattr(setup_module, 'device_type', self.default_device_type), 'parameters': getattr(setup_module, 'parameters', {}) } if 'default' not in all_setups: all_setups['default'] = {'device_type': self.default_device_type} return all_setups
def test_correct_modules_are_returned(self): submodules = get_submodules( importlib.import_module(self._tmp_package_name)) self.assertEqual(sorted(submodules.keys()), sorted(self._expected_modules))
def test_correct_modules_are_returned(self): submodules = get_submodules(importlib.import_module(self._tmp_package_name)) self.assertEqual(sorted(submodules.keys()), sorted(self._expected_modules))