def cmd_create_launch(data_central, argv): '''Creates ROS launch files for all combinations of agents and robots.''' parser = OptionParser(prog='create-launch', usage=cmd_create_launch.short_usage) parser.disable_interspersed_args() parser.add_option("-a", "--agent", dest='id_agent', help="Agent ID") parser.add_option("-r", "--robot", dest='id_robot', help="Robot ID") parser.add_option('-p', '--package', dest='package', default='bootstrapping_adapter', help='Which ROS package to put the launch file in [%default].') # TODO: bag (options, args) = parser.parse_args(argv) check_no_spurious(args) bo_config = data_central.get_bo_config() if options.id_agent is None: id_agents = bo_config.agents.keys() else: id_agents = [options.id_agent] # TODO: expand if options.id_robot is None: id_robots = bo_config.robots.keys() else: id_robots = [options.id_robot] # TODO: expand for id_agent in id_agents: check_contained(id_agent, bo_config.agents) for id_robot in id_robots: check_contained(id_robot, bo_config.robots) from roslib import packages #@UnresolvedImport out = packages.get_pkg_dir(options.package) outdir = os.path.join(out, 'launch', 'boot_olympics') root = os.path.realpath(data_central.root) for id_robot, id_agent in itertools.product(id_robots, id_agents): robot_ros_node = wrap_python_robot(bo_config.robots[id_robot], root) agent_ros_node = wrap_python_agent(bo_config.agents[id_agent], root) xml = create_launch_xml(agent_ros_node=agent_ros_node, robot_ros_node=robot_ros_node, agent_node_id='my_agent', robot_node_id='my_robot', namespace='boot_olympics', bag=None, output=None) basename = '%s-%s' % (id_robot, id_agent) filename = os.path.join(outdir, '%s.launch' % basename) make_sure_dir_exists(filename) with open(filename, 'w') as f: f.write('<!-- Created by %s on %s -->\n' % ('boot_olympics', isodate())) f.write(xml) logger.info('Writing on %r.' % filename)
def from_yaml(xo): try: if not isinstance(xo, dict): raise ValueError('Expected a dict, got %s' % xo) x = dict(**xo) # make a copy check_contained('observations', x) check_contained('commands', x) observations = StreamSpec.from_yaml(x.pop('observations')) commands = StreamSpec.from_yaml(x.pop('commands')) extra = x.pop('extra', None) desc = x.pop('desc', None) id_robot = x.pop('id', None) if x.keys(): logger.warning('While reading\n%s\nextra keys detected: %s' % (pformat(xo), x.keys())) return BootSpec(observations, commands, id_robot=id_robot, extra=extra, desc=desc) except: logger.error('Error while parsing the BootSpec:\n%s' % xo) raise
def get_log_filename(self, base_dir, id_agent, id_robot, id_stream, logs_format=None, add_unique=True): warn_good_identifier(id_stream) if logs_format is None: logs_format = self.log_format check_contained(logs_format, LogsFormat.formats, 'format') pattern = DirectoryStructure.pattern_logdir dirname = os.path.join(base_dir, substitute(pattern, id_agent=id_agent, id_robot=id_robot, id_stream=id_stream)) mkdirs_thread_safe(dirname) assert os.path.exists(dirname) # Add a unique string to the file if add_unique: tmpfile = tempfile.NamedTemporaryFile( suffix='.%s.active' % logs_format, prefix='%s-' % id_stream, dir=dirname, delete=False) tmpfile.write('To be used') tmp_filename = tmpfile.name warn_good_filename(tmp_filename) filename = tmp_filename.replace('.active', '') tmpfile.close() else: filename = os.path.join(dirname, '%s.%s' % (id_stream, logs_format)) # logger.debug('Writing on %r' % friendly_path(filename)) warn_good_filename(filename) return filename
def set_log_format(self, log_format): ''' Sets the log format to write logs with. ''' check_contained(log_format, LogsFormat.formats, 'format') self.log_format = log_format