def _migrate_dependency_groups(self, pipenv_key: str, group_name: str): if "group" not in self._pyproject["tool"]["poetry"]: self._pyproject["tool"]["poetry"]["group"] = Table( Container(), Trivia(), False, is_super_table=True ) if group_name not in self._pyproject["tool"]["poetry"]["group"]: self._pyproject["tool"]["poetry"]["group"][group_name] = Table( Container(), Trivia(), False, is_super_table=True ).add("dependencies", table()) group = self._pyproject["tool"]["poetry"]["group"][group_name] for name, properties in self._pipenv.get(pipenv_key, {}).items(): name, extras = self._split_extras(name) if name in group["dependencies"]: continue properties = self._reformat_dependency_properties(extras, properties) group["dependencies"].add(name, properties) self._pyproject["tool"]["poetry"]["group"][group_name] = group
def convert_tomlkit_buggy_types(in_value: object, parent: Item) -> Item: """Fix buggy items while iterating through tomlkit items Iterating through tomlkit items can often be buggy; this function fixes it """ if isinstance(in_value, Item): return in_value if isinstance(in_value, Container): return Table(in_value, trivia=Trivia(), is_aot_element=False) return item(in_value, parent)
def sorted_children_table(self, parent: Table) -> Iterable[Tuple[str, Item]]: """Get the sorted children of a table NOTE: non-tables are wrapped in an item to ensure that they are, in fact, items. Tables and AoT's are definitely items, so the conversion is not necessary. """ table_items = [(key, convert_tomlkit_buggy_types(parent[key], parent)) for key in parent.keys()] tables = ((key, value) for key, value in table_items if isinstance(value, (Table, AoT))) non_tables = ((key, value) for key, value in table_items if not isinstance(value, (Table, AoT))) non_tables_final = (sorted(non_tables, key=lambda x: x[0]) if not self.only_sort_tables else non_tables) return itertools.chain(non_tables_final, sorted(tables, key=lambda x: x[0]))
def toml_elements_sorted(self, original: Item) -> Item: """Returns a sorted item, recursing collections to their base.""" if isinstance(original, Table): original.trivia.indent = "\n" new_table = Table( Container(), trivia=original.trivia, is_aot_element=original.is_aot_element(), is_super_table=original.is_super_table(), ) for key, value in self.sorted_children_table(original): new_table[key] = self.toml_elements_sorted(value) return new_table if isinstance(original, AoT): new_aot = aot() for aot_item in original: new_aot.append(self.toml_elements_sorted(aot_item)) return new_aot if isinstance(original, Item): original.trivia.indent = "" return original raise TypeError("Invalid TOML; " + str(type(original)) + " is not an Item.")
def load_pipfile(p=None): # type: (Optional[path.Path]) -> TOMLDocument if p is None: p = path.Path(ROOT_PIPFILE) with open(p, "r") as fp: doc = loads(fp.read()) doc._parsed = True sources = [] have_default = False for key in ("source", "sources"): try: item = doc.item(key) # type: Union[AoT, Table] except KeyError: continue doc.remove(key) if isinstance(item, AoT): items = item.body else: items = item.value for source in items: if not isinstance(source, Table): source = Table(source, Trivia(trail=""), is_aot_element=True) container = source.value reorder_container(container, pipfile_source_key, key_type=KeyType.Basic) if not have_default and is_default(container): have_default = True sources.append(source) if not have_default: source = Table(Container(True), Trivia(), True) for k, v in DEFAULT_SOURCE: source.append(k, v) sources.insert(0, source) doc.append("source", AoT(sources, parsed=True)) return doc
def test_source_to_table(source: Source, table_body: dict[str, str | bool]): table = Table(Container(), Trivia(), False) table._value = table_body assert source_to_table(source) == table