def _set_subcomponents(self, match_categories): """Set subcomponents for the top component from the match categories.""" data = CommentedMap(self.top_component.as_yaml()) data.yaml_set_start_comment(TOP_LEVEL_COMMENT) temp_list = CommentedSeq() # Set the subcomponents and comments for top_comment, start_index, matches in match_categories: components = self._matches_to_components(matches) for subcomponent in components: try: # Extract inline comment before it's removed inline_comment = subcomponent.inline_comment except AttributeError: inline_comment = None d2 = CommentedMap(subcomponent.as_yaml()) if inline_comment: # Apply inline comment to data d2.yaml_add_eol_comment(comment=inline_comment, key='name') temp_list.append(d2) temp_list.yaml_set_comment_before_after_key(key=start_index, before=top_comment, indent=OFFSET) data['subcomponents'] = temp_list return data
def cli(sections, input, output, quiet): # Set YAML parameters yaml = YAML(typ="rt") # Round-trip mode allows for comment insertion indent_offset = 2 yaml.indent(offset=indent_offset) # Load environment file template with open(os.path.join("requirements", "environment.in")) as f: env_yml = yaml.load(f.read()) # Extract dependency section contents if not quiet: print(f"Reading dependencies from {input}") setup_config = read_configuration(input) sections = [x.strip() for x in sections.split(",")] section_indices = dict() dep_list = (deepcopy(env_yml["dependencies"]) if "dependencies" in env_yml else []) i = len(dep_list) for section in sections: section_indices[section] = i try: packages = (setup_config["options"]["install_requires"] if section == "main" else setup_config["options"]["extras_require"][section]) except KeyError: raise RuntimeError(f"Cannot fetch dependencies from {input}") for package in packages: if package not in dep_list: # Do not duplicate dep_list.append(package) i += 1 # Format dependency list lst = CS(dep_list) for section, i in section_indices.items(): lst.yaml_set_comment_before_after_key(i, section, indent_offset) env_yml["dependencies"] = lst # Output to terminal if not quiet: yaml.dump(env_yml, sys.stdout) # Output to file if output is not None: with open(output, "w") as outfile: if not quiet: print() print(f"Saving to {output}") yaml.dump(env_yml, outfile)
def keymap_to_easy(keymap): def get_condition(condition): quantifier_string = 'all ' if condition.get('match_all', False) else '' return "{quantifier}{key} {operator} {operand}".format( quantifier=quantifier_string, key=condition['key'], operator=condition['operator'], operand=yaml_dumps(condition['operand']), ) def get_entry(entry): ret = {} if len(entry['keys']) == 1: ret['keys'] = entry['keys'][0] else: ret['keys'] = entry['keys'] if 'args' in entry: if entry['command'] == 'chain': ret['command'] = [ cmd[0] if len(cmd) == 1 else { cmd[0]: cmd[1] } for cmd in entry['args']['commands'] ] else: ret['command'] = {entry['command']: entry['args']} else: ret['command'] = entry['command'] if 'context' in entry: ret['context'] = [ get_condition(condition) for condition in entry['context'] ] return sort_dict(ret, KEY_ORDER) ret = CommentedSeq() for i, entry in enumerate(keymap): ret.append(get_entry(entry)) if i > 0: ret.yaml_set_comment_before_after_key(i, before='\n\n') return ret