diff --git a/swh/scanner/dashboard/dashboard.py b/swh/scanner/dashboard/dashboard.py --- a/swh/scanner/dashboard/dashboard.py +++ b/swh/scanner/dashboard/dashboard.py @@ -23,7 +23,7 @@ the file and the relative SoftWare Heritage persistent IDentifier (SWHID). """ data = [] - for file_info in source.getFilesFromDir(dir_path): + for file_info in source.get_files_from_dir(dir_path): for file_path, attr in file_info.items(): file_path = Path(file_path) file_name = file_path.parts[len(file_path.parts) - 1] diff --git a/swh/scanner/model.py b/swh/scanner/model.py --- a/swh/scanner/model.py +++ b/swh/scanner/model.py @@ -42,7 +42,7 @@ self.known = False self.children: Dict[Path, Tree] = {} - def addNode(self, path: Path, swhid: str, known: bool) -> None: + def add_node(self, path: Path, swhid: str, known: bool) -> None: """Recursively add a new path. """ relative_path = path.relative_to(self.path) @@ -56,35 +56,35 @@ if new_path not in self.children: self.children[new_path] = Tree(new_path, self) - self.children[new_path].addNode(path, swhid, known) + self.children[new_path].add_node(path, swhid, known) - def show(self, format) -> None: + def show(self, fmt) -> None: """Show tree in different formats""" - if format == "json": - print(json.dumps(self.toDict(), indent=4, sort_keys=True)) + if fmt == "json": + print(json.dumps(self.to_dict(), indent=4, sort_keys=True)) - if format == "ndjson": - print(ndjson.dumps(dict_path for dict_path in self.__iterNodesAttr())) + if fmt == "ndjson": + print(ndjson.dumps(dict_path for dict_path in self._iter_nodes_attr())) - elif format == "text": + elif fmt == "text": isatty = sys.stdout.isatty() print(colorize(str(self.path), Color.blue) if isatty else str(self.path)) - self.printChildren(isatty) + self.print_children(isatty) - elif format == "sunburst": + elif fmt == "sunburst": root = self.path - directories = self.getDirectoriesInfo(root) + directories = self.get_directories_info(root) sunburst = generate_sunburst(directories, root) offline_plot(sunburst) - def printChildren(self, isatty: bool, inc: int = 1) -> None: + def print_children(self, isatty: bool, inc: int = 1) -> None: for path, node in self.children.items(): - self.printNode(node, isatty, inc) + self.print_node(node, isatty, inc) if node.children: - node.printChildren(isatty, inc + 1) + node.print_children(isatty, inc + 1) - def printNode(self, node: Any, isatty: bool, inc: int) -> None: + def print_node(self, node: Any, isatty: bool, inc: int) -> None: rel_path = str(node.path.relative_to(self.path)) begin = "│ " * inc end = "/" if node.otype == DIRECTORY else "" @@ -111,7 +111,7 @@ """ return {str(self.path): {"swhid": self.swhid, "known": self.known,}} - def toDict(self) -> Dict[str, Dict[str, Any]]: + def to_dict(self) -> Dict[str, Dict[str, Any]]: """ Recursively flatten the current tree nodes into a dictionary. @@ -146,7 +146,7 @@ """ - return {k: v for d in self.__iterNodesAttr() for k, v in d.items()} + return {k: v for d in self._iter_nodes_attr() for k, v in d.items()} def iterate(self) -> Iterator[Tree]: """ @@ -158,7 +158,7 @@ if child_node.otype == DIRECTORY: yield from child_node.iterate() - def __iterNodesAttr(self) -> Iterator[Dict[str, Dict[str, Any]]]: + 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 @@ -170,9 +170,9 @@ for child_node in self.iterate(): yield child_node.attributes if child_node.otype == DIRECTORY: - yield from child_node.__iterNodesAttr() + yield from child_node._iter_nodes_attr() - def getFilesFromDir(self, dir_path: Path) -> List: + def get_files_from_dir(self, dir_path: Path) -> List: """ Retrieve files information about a specific directory path @@ -181,7 +181,7 @@ in input """ - def getFiles(node): + def get_files(node): files = [] for _, node in node.children.items(): if node.otype == CONTENT: @@ -189,16 +189,16 @@ return files if dir_path == self.path: - return getFiles(self) + return get_files(self) else: for node in self.iterate(): if node.path == dir_path: - return getFiles(node) + return get_files(node) raise InvalidDirectoryPath( "The directory provided doesn't match any stored directory" ) - def __getSubDirsInfo(self, root, directories): + def _get_sub_dirs_info(self, root, directories): """Fills the directories given in input with the contents information stored inside the directory child, only if they have contents. """ @@ -213,9 +213,9 @@ if not contents_info[0] == 0: directories[rel_path] = contents_info if child_node.has_dirs(): - child_node.__getSubDirsInfo(root, directories) + child_node._get_sub_dirs_info(root, directories) - def getDirectoriesInfo(self, root: Path) -> Dict[Path, Tuple[int, int]]: + def get_directories_info(self, root: Path) -> Dict[Path, Tuple[int, int]]: """Get information about all directories under the given root. Returns: @@ -224,7 +224,7 @@ """ directories = {root: self.count_contents()} - self.__getSubDirsInfo(root, directories) + self._get_sub_dirs_info(root, directories) return directories def count_contents(self) -> Tuple[int, int]: @@ -241,7 +241,7 @@ if not self.otype == DIRECTORY: raise InvalidObjectType( - "Can't calculate contents of the " "object type: %s" % self.otype + "Can't count contents of the object type: %s" % self.otype ) if self.known: diff --git a/swh/scanner/scanner.py b/swh/scanner/scanner.py --- a/swh/scanner/scanner.py +++ b/swh/scanner/scanner.py @@ -179,9 +179,9 @@ obj_type = parse_swhid(obj_swhid).object_type if obj_type == CONTENT: - source_tree.addNode(path, obj_swhid, known) + source_tree.add_node(path, obj_swhid, known) elif obj_type == DIRECTORY and directory_filter(path, exclude_patterns): - source_tree.addNode(path, obj_swhid, known) + source_tree.add_node(path, obj_swhid, known) if not known: await _scan(path, session, api_url, source_tree, exclude_patterns) @@ -238,7 +238,7 @@ if interactive: root = Path(root_path) - directories = source_tree.getDirectoriesInfo(root) + directories = source_tree.get_directories_info(root) figure = generate_sunburst(directories, root) run_app(figure, source_tree) else: diff --git a/swh/scanner/tests/conftest.py b/swh/scanner/tests/conftest.py --- a/swh/scanner/tests/conftest.py +++ b/swh/scanner/tests/conftest.py @@ -116,11 +116,11 @@ for path, swhid in temp_folder["paths"].items(): if path in known_paths: - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) else: - example_tree.addNode(path, swhid, False) + example_tree.add_node(path, swhid, False) - return example_tree.getDirectoriesInfo(root) + return example_tree.get_directories_info(root) @pytest.fixture diff --git a/swh/scanner/tests/test_dashboard.py b/swh/scanner/tests/test_dashboard.py --- a/swh/scanner/tests/test_dashboard.py +++ b/swh/scanner/tests/test_dashboard.py @@ -12,7 +12,7 @@ subdir_path = temp_folder["subdir"] for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) generated_body = generate_table_body(subdir_path, example_tree) diff --git a/swh/scanner/tests/test_model.py b/swh/scanner/tests/test_model.py --- a/swh/scanner/tests/test_model.py +++ b/swh/scanner/tests/test_model.py @@ -12,7 +12,7 @@ avail_paths = temp_folder["paths"].keys() for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, False) + example_tree.add_node(path, swhid, False) for path, node in example_tree.children.items(): assert path in avail_paths @@ -23,9 +23,9 @@ def test_to_json_no_one_present(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, False) + example_tree.add_node(path, swhid, False) - result = example_tree.toDict() + result = example_tree.to_dict() assert len(result) == 6 @@ -35,9 +35,9 @@ def test_get_json_tree_all_present(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) - result = example_tree.toDict() + result = example_tree.to_dict() assert len(result) == 6 @@ -50,9 +50,9 @@ filesample_path = temp_folder["filesample"] for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, True if path == filesample_path else False) + example_tree.add_node(path, swhid, True if path == filesample_path else False) - result = example_tree.toDict() + result = example_tree.to_dict() assert len(result) == 6 @@ -72,11 +72,11 @@ for path, swhid in temp_folder["paths"].items(): if path == filesample_path or path == filesample2_path: - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) else: - example_tree.addNode(path, swhid, False) + example_tree.add_node(path, swhid, False) - directories = example_tree.getDirectoriesInfo(example_tree.path) + directories = example_tree.get_directories_info(example_tree.path) assert subsubdir_path not in directories assert directories[subdir_path] == (2, 2) @@ -86,23 +86,23 @@ subdir_path = temp_folder["subdir"] for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) - files = example_tree.getFilesFromDir(subdir_path) + files = example_tree.get_files_from_dir(subdir_path) assert len(files) == 2 def test_get_files_source_path(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) - files = example_tree.getFilesFromDir(example_tree.path) + files = example_tree.get_files_from_dir(example_tree.path) assert len(files) == 1 def test_get_files_from_dir_raise_exception(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): - example_tree.addNode(path, swhid, True) + example_tree.add_node(path, swhid, True) with pytest.raises(InvalidDirectoryPath): - example_tree.getFilesFromDir("test/") + example_tree.get_files_from_dir("test/")