Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/model.py
Show First 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | def addNode(self, path: PosixPath, swhid: str, known: bool) -> None: | ||||
self.children[new_path].addNode(path, swhid, known) | self.children[new_path].addNode(path, swhid, known) | ||||
def show(self, format) -> None: | def show(self, format) -> None: | ||||
"""Show tree in different formats""" | """Show tree in different formats""" | ||||
if format == "json": | if format == "json": | ||||
print(json.dumps(self.toDict(), indent=4, sort_keys=True)) | print(json.dumps(self.toDict(), indent=4, sort_keys=True)) | ||||
if format == "ndjson": | if format == "ndjson": | ||||
print(ndjson.dumps(dict_path for dict_path in self.iterate())) | print(ndjson.dumps(dict_path for dict_path in self.__iterNodesAttr())) | ||||
elif format == "text": | elif format == "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.printChildren(isatty) | self.printChildren(isatty) | ||||
elif format == "sunburst": | elif format == "sunburst": | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | def toDict(self, dict_nodes={}) -> Dict[str, Dict[str, Dict]]: | ||||
"root/subdir/file.txt": { | "root/subdir/file.txt": { | ||||
"swhid": "...", | "swhid": "...", | ||||
"known": True/False | "known": True/False | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
for node_dict in self.iterate(): | for node_dict in self.__iterNodesAttr(): | ||||
dict_nodes.update(node_dict) | dict_nodes.update(node_dict) | ||||
return dict_nodes | return dict_nodes | ||||
def iterate(self) -> Iterable[Dict[str, Dict]]: | def iterate(self) -> Iterable[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(): | |||||
yield child_node | |||||
if child_node.otype == DIRECTORY: | |||||
yield from child_node.iterate() | |||||
def __iterNodesAttr(self) -> Iterable[Dict[str, Dict]]: | |||||
""" | |||||
Recursively iterate through the children of the current node returning | |||||
an iterable of the children nodes attributes | |||||
Yields: | Yields: | ||||
a dictionary containing a path with its known/unknown status and the | a dictionary containing a path with its known/unknown status and the | ||||
Software Heritage persistent identifier | Software Heritage persistent identifier | ||||
""" | """ | ||||
for _, child_node in self.children.items(): | for child_node in self.iterate(): | ||||
yield child_node.attributes | yield child_node.attributes | ||||
if child_node.otype == DIRECTORY: | if child_node.otype == DIRECTORY: | ||||
yield from child_node.iterate() | yield from child_node.__iterNodesAttr() | ||||
def __getSubDirsInfo(self, root, directories): | def __getSubDirsInfo(self, root, directories): | ||||
"""Fills the directories given in input with the contents information | """Fills the directories given in input with the contents information | ||||
stored inside the directory child, only if they have contents. | stored inside the directory child, only if they have contents. | ||||
""" | """ | ||||
for path, child_node in self.children.items(): | for path, child_node in self.children.items(): | ||||
if child_node.otype == DIRECTORY: | if child_node.otype == DIRECTORY: | ||||
rel_path = path.relative_to(root) | rel_path = path.relative_to(root) | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |