Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/model.py
Show First 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | def add_node(self, path: Path, swhid: str, known: bool) -> None: | ||||
self.children[new_path].add_node(path, swhid, known) | self.children[new_path].add_node(path, swhid, known) | ||||
def show(self, fmt) -> None: | def show(self, fmt) -> None: | ||||
"""Show tree in different formats""" | """Show tree in different formats""" | ||||
if fmt == "json": | if fmt == "json": | ||||
print(json.dumps(self.to_dict(), indent=4, sort_keys=True)) | print(json.dumps(self.to_dict(), indent=4, sort_keys=True)) | ||||
if fmt == "ndjson": | if fmt == "ndjson": | ||||
print(ndjson.dumps(dict_path for dict_path in self._iter_nodes_attr())) | print(ndjson.dumps(node.attributes for node in self.iterate())) | ||||
elif fmt == "text": | elif fmt == "text": | ||||
isatty = sys.stdout.isatty() | isatty = sys.stdout.isatty() | ||||
print(colorize(str(self.path), Color.blue) if isatty else str(self.path)) | print(colorize(str(self.path), Color.blue) if isatty else str(self.path)) | ||||
self.print_children(isatty) | self.print_children(isatty) | ||||
elif fmt == "sunburst": | elif fmt == "sunburst": | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | def to_dict(self) -> Dict[str, Dict[str, Any]]: | ||||
"root/subdir/file.txt": { | "root/subdir/file.txt": { | ||||
"swhid": "...", | "swhid": "...", | ||||
"known": True/False | "known": True/False | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
return {k: v for d in self._iter_nodes_attr() for k, v in d.items()} | return {k: v for node in self.iterate() for k, v in node.attributes.items()} | ||||
def iterate(self) -> Iterator[Tree]: | def iterate(self) -> Iterator[Tree]: | ||||
""" | """ | ||||
Recursively iterate through the children of the current node | Recursively iterate through the children of the current node | ||||
""" | """ | ||||
for _, child_node in self.children.items(): | for _, child_node in self.children.items(): | ||||
yield child_node | yield child_node | ||||
if child_node.otype == DIRECTORY: | if child_node.otype == DIRECTORY: | ||||
yield from child_node.iterate() | yield from child_node.iterate() | ||||
def _iter_nodes_attr(self) -> Iterator[Dict[str, Dict[str, Any]]]: | |||||
""" | |||||
Recursively iterate through the children of the current node returning | |||||
an iterable of the children nodes attributes | |||||
Yields: | |||||
a dictionary containing a path with its known/unknown status and the | |||||
SWHID | |||||
""" | |||||
for child_node in self.iterate(): | |||||
yield child_node.attributes | |||||
if child_node.otype == DIRECTORY: | |||||
yield from child_node._iter_nodes_attr() | |||||
def get_files_from_dir(self, dir_path: Path) -> List: | def get_files_from_dir(self, dir_path: Path) -> List: | ||||
""" | """ | ||||
Retrieve files information about a specific directory path | Retrieve files information about a specific directory path | ||||
Returns: | Returns: | ||||
A list containing the files attributes present inside the directory given | A list containing the files attributes present inside the directory given | ||||
in input | in input | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 83 Lines • Show Last 20 Lines |