Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion osgar/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions osgar/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 46 additions & 1 deletion osgar/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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