def test_add_item(): sec = Section("MAIN_SEC") sec.add_item("pkg1") sec.add_item("pkg2 # [win]") sec.add_item("pkg3") assert "pkg1" in sec assert "pkg2" in sec assert "pkg3" in sec
def populate_metadata_from_dict(self, metadata: Any, section: Section) -> Section: if not isinstance(metadata, bool) and not metadata: return section if isinstance(metadata, list): section.add_items(metadata) return section if isinstance(metadata, dict): for name, value in metadata.items(): if isinstance(value, bool) or value: self.populate_metadata_from_dict( value, Section(name, section.yaml_obj)) else: section.add_item(metadata) return section
def __getitem__(self, item: str) -> Any: if item in self.ALL_SECTIONS: if self._yaml.get(item) is None: self._yaml[item] = CommentedMap() return Section(item, parent_yaml=self._yaml) else: raise KeyError(f"Section {item} not found.")
def test_section_children(): commented_map = CommentedMap({ "section1": CommentedMap({"subsection1": CommentedMap({"subsection2": "item"})}) }) sec = Section("section1", parent_yaml=commented_map) assert sec.section_name == "section1" assert sec.values[0].section_name == "subsection1"
def test_section_yaml_obj(): commented_map = CommentedMap( {"section1": { "subsection1": { "subsection2": "item" } }}) sec = Section("MAIN_SECTION", parent_yaml=commented_map) assert sec.yaml_obj == commented_map.update({"MAIN_SECTION": None})
def test_reduce_section(): commented_map = CommentedMap({}) sec = Section("section", commented_map) sec.add_items(["item", None]) sec.values[0].selector = "# [win]" assert sec.values == ["item", None] assert sec._get_parent()[sec.section_name] == ["item", None] sec.reduce_section() assert sec._get_parent()[sec.section_name] == "item" assert sec.values == ["item"]
def test_repr(): commented_map = CommentedMap( {"section1": { "subsection1": { "subsection2": "item" } }}) sec = Section("section1", commented_map) assert repr( sec) == "Section(section_name=section1, subsection=['subsection1'])"
def test_section_load(): commented_map = CommentedMap( {"section1": { "subsection1": { "subsection2": "item" } }}) sec = Section("section1", commented_map) assert sec.section_name == "section1" assert sec["subsection1"].section_name == "subsection1" assert sec["subsection1"]["subsection2"].section_name == "subsection2" assert sec["subsection1"]["subsection2"][0].value == "item"
def __init__(self, name=None, version=None, load_recipe: str = ""): if load_recipe: with open(load_recipe, "r") as yaml_file: self._yaml = yaml.load(yaml_file) else: self._yaml = yaml.load(f'{{% set name = "{name}" %}}\n' "package:\n name: {{ name|lower }}\n") for section in self.ALL_SECTIONS[1:]: Section(section, self._yaml) if version: self.add_jinja_var("version", version, quote=True) self["package"]["version"] = "<{ version }}" self["build"]["number"] = 0 self.__files_copy: List = [] self.update_all_recipe() super(AbstractRecipeModel, self).__init__()
def __reduce_list(self, name, recipe: CommentedMap): for section in Section(name, recipe): if section == "entry_points": continue section.reduce_section()
def test_hash(): commented_map = CommentedMap() sec = Section("section1", commented_map) assert hash(sec) == hash("section1-[]") sec.add_item("item") assert hash(sec) == hash("section1-['item']")
def test_str(): commented_map = CommentedMap() sec = Section("section1", commented_map) assert str(sec) == "section1"
def test_add_subsection(): sec = Section("MAIN_SEC") sec.add_subsection("SUBSECTION") assert "SUBSECTION" in sec assert sec.section_name == "MAIN_SEC"
def __reduce_list(self, name, recipe: CommentedMap): for section in Section(name, recipe): section.reduce_section()