def check(self): try: d = get_duckiefleet_root() return "Duckiefleet root is detected as %r" % d except DTConfigException as e: msg = 'Could not get the duckiefleet root in any way.' raise_wrapped(CheckFailed, e, msg)
def r(m): try: yield except EvaluationError as e: msg = 'Cannot evaluate binary operation: error during %s' % m msg += '\n' + str(self) raise_wrapped(EvaluationError, e, msg, compact=True)
def parse_date_spec(d): from dateutil.parser import parse try: return parse(d) except ValueError as e: msg = 'Cannot parse date %s.' % d.__repr__() raise_wrapped(RTParseError, e, msg, compact=True)
def __call__(self, a, b): try: expect_float(a) expect_float(b) val = self.f(a, b) desc = '%s %s %s' % (a, self.which, b) return ResultWithDescription(val, desc) except EvaluationError as e: msg = 'While evaluating %s(%s, %s)' % (self.f.__name__, a, b) raise_wrapped(EvaluationError, e, msg, compact=True)
def read_scuderia(scuderia): """ Returns the contents of scuderia as a dict, with validated fields.""" if not os.path.exists(scuderia): msg = 'Scuderia file %s does not exist' % scuderia raise ScuderiaException(msg) import yaml yaml_string = open(scuderia).read() try: values = yaml.load(yaml_string) except yaml.YAMLError as e: msg = 'Yaml file is invalid:\n---\n%s' % e raise ScuderiaException(msg) if not isinstance(values, dict): msg = 'Invalid content: %s' % values raise ScuderiaException(msg) n = len(values) names = ", ".join(sorted(values)) logger.info('I found %d Duckiebots in the scuderia: %s.' % (n, names)) results = {} for k, value in values.items(): try: check_good_name(k) if not isinstance(value, dict): msg = 'Entry should be a dict, found %s' % k raise_desc(ScuderiaException, msg) fields = ['owner_duckietown_id', 'username', 'owner_duckietown_id'] for f in fields: if not f in value: msg = 'Could not find field "%s".' % f raise ScuderiaException(msg) owner_duckietown_id = value['owner_duckietown_id'] username = value['username'] robot_name = k results[robot_name] = ScuderiaEntry(robot_name=robot_name, username=username, owner_duckietown_id=owner_duckietown_id) except ScuderiaException as e: msg = 'Invalid entry "%s" in scuderia.' % k msg += '\n\n' + indent(yaml.dump(value).strip(), '> ') + '\n ' raise_wrapped(ScuderiaException, e, msg, compact=True) return results
def check(self): try: path = get_machines_files_path() except DTConfigException as e: msg = 'Could not get path to machines.' raise_wrapped(CheckError, e, msg) if not os.path.exists(path): msg = 'Machines file does not exist.' l = 'File does not exist: %s ' % path raise CheckFailed(msg, l)
def get_scuderia_contents(): ''' Returns the contents of the scuderia file from $(DUCKIEFLEET_ROOT)/scuderia.yaml ''' fn_scuderia = get_scuderia_path() try: scuderia_contents = read_scuderia(fn_scuderia) except ScuderiaException as e: msg = 'Invalid scuderia file %r.' % fn_scuderia raise_wrapped(ScuderiaException, e, msg, compact=True) logger.info('Read %d entries from scuderia %s' % (len(scuderia_contents), fn_scuderia)) return scuderia_contents
def _parse_regression_test_check(line): line = line.strip() tokens = line.split(' ') if len(tokens) != 3: msg = 'I expect exactly 3 tokens.\nLine: "%s"\nTokens: %s' % (line, tokens) raise DTConfigException(msg) try: ref1 = parse_reference(tokens[0]) binary = parse_binary(tokens[1]) ref2 = parse_reference(tokens[2]) evaluable = BinaryEval(ref1, binary, ref2) except RTParseError as e: msg = 'Cannot parse string "%s".' % line raise_wrapped(RTParseError, e, msg, compact=True) return Wrapper(evaluable)
def check(self): try: path = get_machines_files_path() except DTConfigException as e: msg = 'Could not get path to machines.' raise_wrapped(CheckError, e, msg) if not os.path.exists(path): msg = 'Machines file does not exist.' l = 'File does not exist: %s ' % path raise CheckFailed(msg, l) hostname = socket.gethostname() contents = open(path).read() if not '"%s"' % hostname in contents: msg = 'This robot, "%s" is not mentioned in the machines file.' % hostname raise CheckFailed(msg)
def check(self): # find a try: python_files = locate_files(self.dirname, '*.py') for filename in python_files: try: check_no_half_merges(filename) if DuckietownConstants.enforce_no_tabs: check_no_tabs(filename) if DuckietownConstants.enforce_naming_conventions: check_good_name(filename) except CheckFailed as e: msg = 'Check failed for file %s:' % filename raise_wrapped(CheckFailed, e, msg, compact=True) except CheckFailed as e: msg = 'Checks failed for package %s.' % self.package_name l = str(e) raise CheckFailed(msg, l)
def check(self): try: path_machines = get_machines_files_path() path_scuderia = get_scuderia_path() except DTConfigException as e: msg = 'Could not get path to machines.' raise_wrapped(CheckError, e, msg) if not os.path.exists(path_machines): msg = 'Machines file does not exist.' raise CheckError(msg) if not os.path.exists(path_scuderia): msg = 'Scuderia file does not exist.' raise CheckError(msg) f1 = os.path.getmtime(path_machines) f2 = os.path.getmtime(path_scuderia) if f2 > f1: msg = 'The machines file is older than the scuderia file.' raise CheckFailed(msg)
def process_log(self, bag_in, bag_out): algo_db = get_easy_algo_db() line_detector = algo_db.create_instance(FAMILY_LINE_DETECTOR, self.line_detector) image_prep = algo_db.create_instance('image_prep', self.image_prep) vehicle = which_robot(bag_in) topic = '/%s/camera_node/image/compressed' % vehicle context = FakeContext() transform = None frame = 0 for compressed_img_msg in d8n_bag_read_with_progress(bag_in, topic): with context.phase('decoding'): try: image_cv = image_cv_from_jpg(compressed_img_msg.data) except ValueError as e: msg = 'Could not decode image: %s' % e raise_wrapped(ValueError, e, msg) segment_list = image_prep.process(context, image_cv, line_detector, transform) rendered = vs_fancy_display(image_prep.image_cv, segment_list) rendered = d8_image_zoom_linear(rendered, 2) log_name = 'log_name' time = 12 rendered = add_duckietown_header(rendered, log_name, time, frame) out = d8n_image_msg_from_cv_image( rendered, "bgr8", same_timestamp_as=compressed_img_msg) # Write to the bag bag_out.write('processed', out) # out = d8n_image_msg_from_cv_image(image_cv, "bgr8", same_timestamp_as=compressed_img_msg) bag_out.write('image', compressed_img_msg) frame += 1
def interpret_config_file(filename): """ Returns a ConfigInfo. """ try: basename = os.path.basename(filename) base = basename.replace(SUFFIX, '') # now we have something like # package-node.config_name.date # or # package-node.config_name if not '.' in base: msg = 'Invalid filename %r.' % filename raise DTConfigException(msg) tokens = base.split('.') if len(tokens) > 3: msg = 'Too many periods/tokens (tokens=%s)' % tokens raise DTConfigException(msg) if len(tokens) <= 2: # package-node.config_name package_node = tokens[0] if not '-' in package_node: msg = 'Expected a "-" in "%s".' % package_node raise DTConfigException(msg) i = package_node.index('-') package_name = package_node[:i] node_name = package_node[i + 1:] config_name = tokens[1] if len(tokens) == 3: # package-node.config_name.date date_effective = tokens[2] else: date_effective = '20170101' from dateutil.parser import parse try: date_effective = parse(date_effective) except: msg = 'Cannot interpret "%s" as a date.' % date_effective raise DTConfigException(msg) # now read file contents = open(filename).read() try: try: data = yaml.load(contents) except YAMLError as e: raise_wrapped(DTConfigException, e, 'Invalid YAML', compact=True) if not isinstance(data, dict): msg = 'Expected a dictionary inside.' raise DTConfigException(msg) for field in ['description', 'values']: if not field in data: msg = 'Missing field "%s".' % field raise DTConfigException(msg) description = data.pop('description') if not isinstance(description, str): msg = 'I expected that "description" is a string, obtained %r.' % description raise DTConfigException(msg) extends = data.pop('extends', []) if not isinstance(extends, list): msg = 'I expected that "extends" is a list, obtained %r.' % extends raise DTConfigException(msg) values = data.pop('values') if not isinstance(values, dict): msg = 'I expected that "values" is a dictionary, obtained %s.' % type( values) raise DTConfigException(msg) # Freeze the data extends = tuple(extends) values = frozendict(values) except DTConfigException as e: msg = 'Could not interpret the contents of the file\n' msg += ' %s\n' % friendly_path(filename) msg += 'Contents:\n' + indent(contents, ' > ') raise_wrapped(DTConfigException, e, msg, compact=True) return ConfigInfo( filename=filename, package_name=package_name, node_name=node_name, config_name=config_name, date_effective=date_effective, extends=extends, description=description, values=values, # not decided valid=None, error_if_invalid=None) except DTConfigException as e: msg = 'Invalid file %s' % friendly_path(filename) raise_wrapped(DTConfigException, e, msg, compact=True)