Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/model.py
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | def addNode(self, path: PosixPath, swhid: str, known: bool) -> None: | ||||
if new_path not in self.children: | if new_path not in self.children: | ||||
self.children[new_path] = Tree(new_path, self) | self.children[new_path] = Tree(new_path, self) | ||||
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.getTree(), indent=4, sort_keys=True)) | print(json.dumps(self.toDict(), indent=4, sort_keys=True)) | ||||
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 54 Lines • ▼ Show 20 Lines | def iterate(self) -> Iterable[Dict[str, Dict]]: | ||||
Software Heritage persistent identifier | Software Heritage persistent identifier | ||||
""" | """ | ||||
for _, child_node in self.children.items(): | for _, child_node in self.children.items(): | ||||
yield child_node.info | yield child_node.info | ||||
if child_node.otype == DIRECTORY: | if child_node.otype == DIRECTORY: | ||||
yield from child_node.iterate() | yield from child_node.iterate() | ||||
def getTree(self): | |||||
"""Walk through the tree to discover content or directory that have | |||||
a persistent identifier. If a persistent identifier is found it saves | |||||
the path with the relative PID. | |||||
Returns: | |||||
child_tree: the tree with the content/directory found | |||||
""" | |||||
child_tree = {} | |||||
for path, child_node in self.children.items(): | |||||
rel_path = str(child_node.path.relative_to(self.path)) | |||||
if child_node.swhid: | |||||
child_tree[rel_path] = child_node.swhid | |||||
else: | |||||
next_tree = child_node.getTree() | |||||
if next_tree: | |||||
child_tree[rel_path] = next_tree | |||||
return child_tree | |||||
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) | ||||
contents_info = child_node.count_contents() | contents_info = child_node.count_contents() | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |