diff --git a/osgar/record.py b/osgar/record.py index 77d85988a..e71b71bba 100644 --- a/osgar/record.py +++ b/osgar/record.py @@ -22,9 +22,14 @@ def __init__(self, config, logger): self.modules = {} self.bus = Bus(logger) + env = config.get('env') for module_name, module_config in config['modules'].items(): klass = get_class_by_name(module_config['driver']) - self.modules[module_name] = klass(module_config.get('init', {}), bus=self.bus.handle(module_name)) + module_config_init = module_config.get('init', {}) + if env is not None: + assert 'env' not in module_config_init, module_config_init + module_config_init['env'] = env.copy() + self.modules[module_name] = klass(module_config_init, bus=self.bus.handle(module_name)) for sender, receiver in config['links']: self.bus.connect(sender, receiver, self.modules) @@ -66,6 +71,10 @@ def request_stop(self, signum=None, frame=None): # pylint: disable=unused-argume def record(config, log_prefix, log_filename=None, duration_sec=None): with LogWriter(prefix=log_prefix, filename=log_filename, note=str(sys.argv)) as log: + # fill system variables + if 'env' in config['robot']: + for k in list(config['robot']['env'].keys()): + config['robot']['env'][k] = os.getenv(k) log.write(0, bytes(str(config), 'ascii')) g_logger.info(log.filename) with Recorder(config=config['robot'], logger=log) as recorder: diff --git a/osgar/replay.py b/osgar/replay.py index 470fb00d6..669019fca 100644 --- a/osgar/replay.py +++ b/osgar/replay.py @@ -64,8 +64,11 @@ def replay(args, application=None): driver_name = module_config['driver'] module_class = get_class_by_name(driver_name) - module_instance = module_class(module_config.get('init', {}), bus=bus) - + module_config_init = module_config.get('init', {}) + env = config['robot'].get('env') + if env is not None: + module_config_init['env'] = env + module_instance = module_class(module_config_init, bus=bus) bus.node = module_instance # needed for slots return module_instance diff --git a/osgar/test_record.py b/osgar/test_record.py index a1de9b550..17060111f 100644 --- a/osgar/test_record.py +++ b/osgar/test_record.py @@ -9,12 +9,15 @@ import tempfile import sys -from osgar.record import Recorder +from osgar.record import Recorder, record class Sleeper: def __init__(self, cfg, bus): + # dedicated part for test of environment variables + if cfg.get('assert_env'): + assert 'env' in cfg, cfg self.e = threading.Event() def start(self): @@ -132,5 +135,47 @@ def test_sigint_shell(self): self.assertEqual(len(stderr.splitlines()), 1, stderr) os.unlink(log_filename) + def test_config_env(self): + config = { + 'version': 2, + 'robot': { + 'env': { + 'OSGAR_LOG_PREFIX': None + }, + 'modules': { + "app": { + "driver": "osgar.test_record:Sleeper", + "init": { + 'assert_env': True + } + }, + }, 'links':[] + } + } + record(config, 'log-env-', duration_sec=0.1) + + def test_config_env_assert(self): + config = { + 'version': 2, + 'robot': { + 'env': { + 'OSGAR_LOGS_PREFIX': None + }, + 'modules': { + "app": { + "driver": "osgar.test_record:Sleeper", + "init": { + 'assert_env': True, + 'env': 'This will collide -> intentionally assert' + } + }, + }, 'links':[] + } + } + with self.assertRaises(AssertionError) as err: + record(config, 'log-env2-', duration_sec=0.1) + self.assertEqual(str(err.exception), + "{'assert_env': True, 'env': 'This will collide -> intentionally assert'}") + # vim: expandtab sw=4 ts=4