diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4f5573b..bf44847 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,12 +30,15 @@ repos: rev: 23.12.1 hooks: - id: black - args: - - --line-length=79 language_version: python3 - repo: https://github.com/PyCQA/flake8 rev: 7.0.0 hooks: - id: flake8 + args: + - --max-line-length=88 language_version: python3 - additional_dependencies: [flake8-print] + - repo: https://github.com/abdelrahman0w/ouro + rev: v0.1.1 + hooks: + - id: ouro diff --git a/README.md b/README.md index 90c227f..9332902 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ ouro [-h] [-v] [--verbose] [--no-categorize] [-e] [-i IGNORE [IGNORE ...]] [path ```yaml - repo: https://github.com/abdelrahman0w/ouro - rev: v0.1.0 + rev: v0.1.1 hooks: - id: ouro ``` diff --git a/ouro/__init__.py b/ouro/__init__.py index 64c236b..d9ac6ba 100644 --- a/ouro/__init__.py +++ b/ouro/__init__.py @@ -1,3 +1,3 @@ __author__ = """Abdelrahman Abdelkhalek""" __email__ = "abdelrahman.abdelkhalek@thndr.app" -__version__ = "0.1.0" +__version__ = "0.1.1" diff --git a/ouro/__main__.py b/ouro/__main__.py index 50f1bba..ec016ba 100644 --- a/ouro/__main__.py +++ b/ouro/__main__.py @@ -78,16 +78,12 @@ def main(): checker = Checker( path=args.path, ignore=args.ignore, categorize=(not args.no_categorize) ) - cycles = checker.cycles - - if cycles: + if cycles := checker.cycles: retv = 1 possible_origins = checker.get_possible_origins(cycles) logger.fail("FOUND CIRCULAR IMPORT(S)!", highlight=True) - logger.warn( - "PROBABLY ONE OF THE FOLLOWING IS THE ORIGIN", highlight=True - ) + logger.warn("PROBABLY ONE OF THE FOLLOWING IS THE ORIGIN", highlight=True) for possible_origin in possible_origins: logger.warn(f" ==> {possible_origin}") @@ -100,22 +96,16 @@ def main(): export_path = os.path.join(os.getcwd(), "ouro-report.json") with open("ouro-report.json", "w") as f: - logger.info( - f"EXPORTING REPORT TO: {export_path}", highlight=True - ) + logger.info(f"EXPORTING REPORT TO: {export_path}", highlight=True) json.dump(cycles, f, indent=4) - logger.success( - f"REPORT EXPORTED TO: {export_path}", highlight=True - ) + logger.success(f"REPORT EXPORTED TO: {export_path}", highlight=True) else: retv = 0 logger.success("WHOA! NO CIRCULAR IMPORT(S) FOUND!", highlight=True) - logger.info( - f"ELAPSED TIME: {time() - start_time:.2f} seconds", highlight=True - ) + logger.info(f"ELAPSED TIME: {time() - start_time:.2f} seconds", highlight=True) return retv diff --git a/ouro/checker.py b/ouro/checker.py index 13f4835..127db4d 100644 --- a/ouro/checker.py +++ b/ouro/checker.py @@ -33,9 +33,7 @@ def _handle_node_cycle_only( if not self._cycles.get(node.name): self._cycles[node.name] = [] - in_def = any( - def_begin <= lineno <= def_end for def_begin, def_end in node.defs - ) + in_def = any(def_begin <= lineno <= def_end for def_begin, def_end in node.defs) path_from_import_to_file = [node.name for node in path] self._cycles[node.name].append( @@ -65,9 +63,7 @@ def _handle_node_cycle_categorize( "direct_import_in_def": [], } - in_def = any( - def_begin <= lineno <= def_end for def_begin, def_end in node.defs - ) + in_def = any(def_begin <= lineno <= def_end for def_begin, def_end in node.defs) path_from_import_to_file = [node.name for node in path] categories_map = { "critical": (is_from and not in_def), @@ -93,9 +89,7 @@ def _check_node( self, node: "Node", node_imports: List[Tuple["Node", bool, int]] ) -> None: for node_import, is_from, lineno in node_imports: - is_cyclic, path = self._imports_graph.is_reachable( - node_import, node - ) + is_cyclic, path = self._imports_graph.is_reachable(node_import, node) if not is_cyclic: continue @@ -105,9 +99,7 @@ def _check_node( node, node_import, lineno, path, is_from ) else: - self._handle_node_cycle_only( - node, node_import, lineno, path, is_from - ) + self._handle_node_cycle_only(node, node_import, lineno, path, is_from) def _check_all(self): for node, node_imports in self._imports_graph: @@ -133,9 +125,7 @@ def get_possible_origins( cycle_info for cycle in cycles.values() for cycle_info in cycle ) - paths = [ - cycle_info["path_from_import_to_file"] for cycle_info in cycle_iter - ] + paths = [cycle_info["path_from_import_to_file"] for cycle_info in cycle_iter] paths = [path for path_list in paths for path in path_list] if not paths: return [] @@ -148,9 +138,4 @@ def get_possible_origins( most_common_paths = sorted_paths[:num_possibilities] most_common_paths.reverse() - possible_origins = [] - for path in most_common_paths: - if path in cycles: - possible_origins.append(path) - - return possible_origins + return [path for path in most_common_paths if path in cycles] diff --git a/ouro/nodes_initializer.py b/ouro/nodes_initializer.py index 3bc599a..d937d52 100644 --- a/ouro/nodes_initializer.py +++ b/ouro/nodes_initializer.py @@ -14,9 +14,7 @@ def __init__( name: str, ) -> None: self.name = name - self.imports: List[ - Tuple["Node", bool, int] - ] = [] # (node, is_from, lineno) + self.imports: List[Tuple["Node", bool, int]] = [] # (node, is_from, lineno) self.defs: List[Tuple[int, int]] = [] def add(self, item: "Node", is_from: bool, lineno: int) -> None: @@ -37,9 +35,7 @@ def _get_node(self, file: str) -> "Node": return self.nodes[file] - def _get_imports( - self, content: str - ) -> List[Union[ast.Import, ast.ImportFrom]]: + def _get_imports(self, content: str) -> List[Union[ast.Import, ast.ImportFrom]]: return [ node for node in ast.walk(ast.parse(content)) @@ -74,8 +70,7 @@ def _get_module_path( ).with_suffix(".py") if not Path.exists(path): path = Path( - Path(self._prg_path).parent - / Path(*(node.names[0].name.split("."))) + Path(self._prg_path).parent / Path(*(node.names[0].name.split("."))) ).with_suffix(".py") return (str(path), str(path)) @@ -88,8 +83,7 @@ def _get_module_path( ).with_suffix(".py") if not Path.exists(path_1): path_1 = Path( - Path(self._prg_path).parent - / Path(*(node.module.split("."))) + Path(self._prg_path).parent / Path(*(node.module.split("."))) ).with_suffix(".py") path_2 = Path( @@ -112,16 +106,17 @@ def _initialize(self) -> None: for file, content in self._prj: node = self._get_node(file) node.defs = [ - (def_.lineno, def_.end_lineno) - if def_.end_lineno - else (def_.lineno, def_.lineno) + ( + (def_.lineno, def_.end_lineno) + if def_.end_lineno + else (def_.lineno, def_.lineno) + ) for def_ in self._get_defs(content) ] imports = self._get_imports(content) for import_ in imports: - imported_module_paths = self._get_module_path(import_) - if imported_module_paths: + if imported_module_paths := self._get_module_path(import_): path_1, path_2 = imported_module_paths imported_node_1 = self._get_node(path_1) imported_node_2 = self._get_node(path_2) diff --git a/ouro/reader.py b/ouro/reader.py index f9a38d3..0092d55 100644 --- a/ouro/reader.py +++ b/ouro/reader.py @@ -63,9 +63,7 @@ def _valid_files(self) -> Generator: for file in files: file_path = os.path.join(dir, file) - if self._is_checkable(file_path) and not self._is_ignored( - file_path - ): + if self._is_checkable(file_path) and not self._is_ignored(file_path): yield file_path @property diff --git a/setup.cfg b/setup.cfg index f827ee9..8609cdb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.1.0 +current_version = 0.1.1 commit = True tag = True diff --git a/setup.py b/setup.py index c332758..81e90e7 100644 --- a/setup.py +++ b/setup.py @@ -62,6 +62,6 @@ test_suite="tests", tests_require=test_requirements, url="https://github.com/abdelrahman0w/ouro", - version="0.1.0", + version="0.1.1", zip_safe=False, ) diff --git a/tox.ini b/tox.ini index b3af777..3869a3f 100644 --- a/tox.ini +++ b/tox.ini @@ -4,12 +4,12 @@ envlist = py38, flake8, black [testenv:flake8] basepython = python deps = flake8 -commands = flake8 ouro tests +commands = flake8 --max-line-length 88 ouro tests [testenv:black] basepython = python deps = black -commands = black --check ouro tests +commands = black ouro tests [testenv] setenv =