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(node.attributes for node in self.iterate())) | print( | ||||
ndjson.dumps( | |||||
{str(Path(k).relative_to(self.path)): v} | |||||
for node in self.iterate() | |||||
for k, v in node.attributes.items() | |||||
) | |||||
) | |||||
elif fmt == "text": | elif fmt == "text": | ||||
isatty = sys.stdout.isatty() | isatty = sys.stdout.isatty() | ||||
root_dir = self.path.relative_to(self.path.parent) | |||||
print(colorize(str(self.path), Color.blue) if isatty else str(self.path)) | print(colorize(str(root_dir), Color.blue) if isatty else str(root_dir)) | ||||
self.print_children(isatty) | self.print_children(isatty) | ||||
elif fmt == "sunburst": | elif fmt == "sunburst": | ||||
root = self.path | root = self.path | ||||
directories = self.get_directories_info(root) | directories = self.get_directories_info(root) | ||||
sunburst = generate_sunburst(directories, root) | sunburst = generate_sunburst(directories, root) | ||||
offline_plot(sunburst) | offline_plot(sunburst) | ||||
▲ Show 20 Lines • Show All 60 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 node in self.iterate() for k, v in node.attributes.items()} | return { | ||||
str(Path(k).relative_to(self.path)): v | |||||
for node in self.iterate() | |||||
for k, v in node.attributes.items() | |||||
} | |||||
zack: I understand this is not strictly speaking what was asked in the bug you're trying to fix with… | |||||
Done Inline ActionsI agree with the fact that the scanner.model should use Path objects everywhere. This should be changed when we will move all the visualization related functions to another sub-module (T2692). The new sub-module should change the Path objects to string before to output the information. @KShivendu Regarding this diff, as Stefano stated, i think it's for now safe to extract only the relative path with Path.relative_to. DanSeraf: I agree with the fact that the `scanner.model` should use Path objects everywhere. This should… | |||||
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: | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |
I understand this is not strictly speaking what was asked in the bug you're trying to fix with this diff, but looking at this I find that it's not correct that to_dict returns paths as strings, rather than keeping them as Path. (This is in turn due to what .attributes() does.)
Unless I'm missing something (cc: @DanSeraf for feedback), the model uses Path objects everywhere, and we should keep doing that for all model users. If someone needs to translate those paths to string, they should do that at the last minute.
Additionally, if we keep using path, what you are doing here could be done much more safely. Instead of using .replace you could (and should) use here something like PurePath.relative_to, which is much safer than string replacement.