def get_prebuilt_op_set(self): non_reusable_type_set = self.get_non_reusable_type_set() op_set = set() # Try to build as many configurations instances from all the files we # are given conf_map = MultiSrcConf.from_yaml_map_list(self.args.conf) for conf_cls, conf in conf_map.items(): op_set.add(PrebuiltOperator( conf_cls, [conf], non_reusable_type_set=non_reusable_type_set )) # Inject serialized objects as root operators for path in self.args.inject: obj = Serializable.from_path(path) op_set.add(PrebuiltOperator(type(obj), [obj], non_reusable_type_set=non_reusable_type_set )) # Inject a dummy empty TargetConf if self.args.inject_empty_target_conf: op_set.add(PrebuiltOperator(TargetConf, [TargetConf(conf={})], non_reusable_type_set=non_reusable_type_set )) return op_set
def get_prebuilt_op_set(self): non_reusable_type_set = self.get_non_reusable_type_set() op_set = set() # Try to build as many configurations instances from all the files we # are given conf_cls_set = set(get_subclasses(MultiSrcConf, only_leaves=True)) conf_list = [] for conf_path in self.args.conf: for conf_cls in conf_cls_set: try: # Do not add the default source, to avoid overriding user # configuration with the default one. conf = conf_cls.from_yaml_map(conf_path, add_default_src=False) except ValueError: continue else: conf_list.append((conf, conf_path)) def keyfunc(conf_and_path): cls = type(conf_and_path[0]) # We use the ID since classes are not comparable return id(cls), cls # Then aggregate all the conf from each type, so they just act as # alternative sources. for (_, conf_cls), conf_and_path_seq in groupby(conf_list, key=keyfunc): conf_and_path_list = list(conf_and_path_seq) # Get the default configuration, and stack all user-defined keys conf = conf_cls() for conf_src, conf_path in conf_and_path_list: src = os.path.basename(conf_path) conf.add_src(src, conf_src) op_set.add( PrebuiltOperator(conf_cls, [conf], non_reusable_type_set=non_reusable_type_set)) # Inject serialized objects as root operators for path in self.args.inject: obj = Serializable.from_path(path) op_set.add( PrebuiltOperator(type(obj), [obj], non_reusable_type_set=non_reusable_type_set)) # Inject a dummy empty TargetConf if self.args.inject_empty_target_conf: op_set.add( PrebuiltOperator(TargetConf, [TargetConf(conf={})], non_reusable_type_set=non_reusable_type_set)) return op_set
def show_db(self, db): parse_uuid_attr = self._parse_uuid_attr def indent(s): idt = ' ' * 4 return idt + s.replace('\n', '\n' + idt) def get_uuid(uuid): try: froz_val = db.get_by_uuid(uuid) except KeyError: raise KeyError('UUID={} not found in the database'.format(uuid)) else: return froz_val def get_attr_key(obj, attr_key): # parse "attr[key1][key2][...]" attr = attr_key.split('[', 1)[0] keys = re.findall(r'\[(.*?)\]', attr_key) if attr: obj = getattr(obj, attr) return get_nested_key(obj, keys) def resolve_attr(obj, attr_key): if attr_key is None: return obj try: attr_key, remainder = attr_key.split('.', 1) except ValueError: return get_attr_key(obj, attr_key) else: obj = get_attr_key(obj, attr_key) return resolve_attr(obj, remainder) args = self.args if not (args.show or args.show_yaml): super().show_db(db) attr_map = {} for uuid, attr in args.show: attr_map.setdefault(uuid, set()).add(attr) if len(args.show) == 1: show_format = '{val}' else: show_format = 'UUID={uuid} {type}{attr}{eq}{val}' serialize_spec_list = args.serialize yaml_show_spec_list = args.show_yaml for uuid, attr_set in attr_map.items(): attr_list = sorted(attr_set) froz_val = get_uuid(uuid) value = froz_val.value for attr in attr_list: attr_value = resolve_attr(value, attr) attr_str = str(attr_value) if '\n' in attr_str: attr_str = '\n' + indent(attr_str) eq = ':' else: eq = '=' print(show_format.format( uuid=froz_val.uuid, type=get_name(type(value)), attr='.' + attr if attr else '', val=attr_str, eq=eq, )) if len(yaml_show_spec_list) == 1: yaml_show_format = '{yaml}' yaml_indent = lambda x: x else: yaml_show_format = 'UUID={uuid} {type}:\n\n{yaml}' yaml_indent = indent for uuid, attr in yaml_show_spec_list: froz_val = get_uuid(uuid) value = froz_val.value value = resolve_attr(value, attr) if isinstance(value, Serializable): yaml_str = value.to_yaml() else: yaml_str = Serializable._to_yaml(value) print(yaml_show_format.format( uuid=uuid, type=get_name(type(value)), yaml=yaml_indent(yaml_str), )) for uuid_attr, path in serialize_spec_list: uuid, attr = parse_uuid_attr(uuid_attr) froz_val = get_uuid(uuid) value = froz_val.value value = resolve_attr(value, attr) if isinstance(value, Serializable): value.to_path(path) else: Serializable._to_path(value, path, fmt='yaml') return 0