def test_device_builder(self): registry = DeviceRegistry(self._tmp_package_name) builder = registry.device_builder("some_file") self.assertEqual(builder.name, "some_file") self.assertRaises(LewisException, registry.device_builder, "invalid_device")
def test_devices(self): registry = DeviceRegistry(self._tmp_package_name) devices = registry.devices self.assertEqual(len(devices), 2) self.assertIn('some_file', devices) self.assertIn('some_dir', devices)
def do_run_simulation(argument_list=None): arguments = parser.parse_args(argument_list or sys.argv[1:]) if arguments.version: print(__version__) return if arguments.add_path is not None: sys.path.append(os.path.abspath(arguments.add_path)) device_registry = DeviceRegistry(arguments.device_package) if not arguments.device: devices = [ 'Please specify a device to simulate. The following devices are available:' ] for dev in device_registry.devices: devices.append(' ' + dev) print('\n'.join(devices)) return device_builder = device_registry.device_builder(arguments.device) if arguments.list_protocols: print('\n'.join(device_builder.protocols)) return device = device_builder.create_device(arguments.setup) interface = device_builder.create_interface( arguments.protocol, device=device, arguments=arguments.adapter_args) if arguments.show_interface: print(interface.documentation) return simulation = Simulation(device=device, adapter=interface, control_server=arguments.rpc_host) simulation.cycle_delay = arguments.cycle_delay simulation.speed = arguments.speed simulation.start()
def test_device_builder(self): registry = DeviceRegistry(self._tmp_package_name) with patch('lewis.core.devices.is_compatible_with_framework', return_value=True): builder = registry.device_builder('some_file') self.assertEqual(builder.name, 'some_file') with patch('lewis.core.devices.is_compatible_with_framework', return_value=False): builder = registry.device_builder('some_file', strict_versions=False) self.assertEqual(builder.name, 'some_file') self.assertRaises(LewisException, registry.device_builder, 'some_file') self.assertRaises(LewisException, registry.device_builder, 'some_file', strict_versions=True) with patch('lewis.core.devices.is_compatible_with_framework', return_value=None): builder = registry.device_builder('some_file', strict_versions=False) self.assertEqual(builder.name, 'some_file') builder = registry.device_builder('some_dir') self.assertEqual(builder.name, 'some_dir') self.assertRaises(LewisException, registry.device_builder, 'some_file', strict_versions=True) self.assertRaises(LewisException, registry.device_builder, 'invalid_device')
def __init__(self, devices_package, strict_versions=None): self._reg = DeviceRegistry(devices_package) self._rv = strict_versions
class SimulationFactory(object): """ This class is used to create :class:`Simulation`-objects according to a certain set of parameters, such as device, setup and protocol. To create a simulation, it needs to know where devices are stored and how strict device versions should be handled: .. sourcecode:: Python factory = SimulationFactory('lewis.devices') The actual creation happens via the :meth:`create`-method: .. sourcecode:: Python simulation = factory.create('device_name', protocol='protocol') The simulation can then be started and stopped as desired. .. warning:: This class is meant for internal use at the moment and may change frequently. """ def __init__(self, devices_package, strict_versions=None): self._reg = DeviceRegistry(devices_package) self._rv = strict_versions @property def devices(self): """Names of available devices.""" return self._reg.devices def get_protocols(self, device): """Returns a list of available protocols for the specified device.""" return self._reg.device_builder(device, self._rv).protocols def create(self, device, setup=None, protocols=None, control_server=None): """ Creates a :class:`Simulation` according to the supplied parameters. :param device: Name of device. :param setup: Name of the setup for device creation. :param protocols: Dictionary where each key is assigned a dictionary with options for the corresponding :class:`~lewis.core.adapters.Adapter`. For available protocols, see :meth:`get_protocols`. :param control_server: String to construct a control server (host:port). :return: Simulation object according to input parameters. """ device_builder = self._reg.device_builder(device, self._rv) device = device_builder.create_device(setup) adapters = [] if protocols is not None: for protocol, options in protocols.items(): interface = device_builder.create_interface(protocol) interface.device = device adapter = interface.adapter(options=options or {}) adapter.interface = interface adapters.append(adapter) return Simulation(device=device, adapters=adapters, device_builder=device_builder, control_server=control_server)
def __init__(self, devices_package): self._reg = DeviceRegistry(devices_package)
class SimulationFactory(object): """ This class is used to create :class:`Simulation`-objects according to a certain set of parameters, such as device, setup and protocol. To create a simulation, it needs to know where devices are stored and how strict device versions should be handled: .. sourcecode:: Python factory = SimulationFactory('lewis.devices') The actual creation happens via the :meth:`create`-method: .. sourcecode:: Python simulation = factory.create('device_name', protocol='protocol') The simulation can then be started and stopped as desired. .. warning:: This class is meant for internal use at the moment and may change frequently. """ def __init__(self, devices_package, strict_versions=None): self._reg = DeviceRegistry(devices_package) self._rv = strict_versions @property def devices(self): """Names of available devices.""" return self._reg.devices def get_protocols(self, device): """Returns a list of available protocols for the specified device.""" return self._reg.device_builder(device, self._rv).protocols def create(self, device, setup=None, protocols=None, control_server=None): """ Creates a :class:`Simulation` according to the supplied parameters. :param device: Name of device. :param setup: Name of the setup for device creation. :param protocols: Dictionary where each key is assigned a dictionary with options for the corresponding :class:`~lewis.core.adapters.Adapter`. For available protocols, see :meth:`get_protocols`. :param control_server: String to construct a control server (host:port). :return: Simulation object according to input parameters. """ device_builder = self._reg.device_builder(device, self._rv) device = device_builder.create_device(setup) adapters = [] if protocols is not None: for protocol, options in protocols.items(): interface = device_builder.create_interface(protocol) interface.device = device adapter = interface.adapter(options=options or {}) adapter.interface = interface adapters.append(adapter) return Simulation( device=device, adapters=adapters, device_builder=device_builder, control_server=control_server)