Skip to content

Commit

Permalink
Merge pull request #30 from abdelrahman0w/code-refacor
Browse files Browse the repository at this point in the history
Code refacor
  • Loading branch information
abdelrahman0w committed Jan 29, 2024
2 parents 814e11b + e79ac1a commit 49857ec
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 52 deletions.
2 changes: 1 addition & 1 deletion ouro/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def main():
)

for possible_origin in possible_origins:
logger.info(f" ==> {possible_origin}")
logger.warn(f" ==> {possible_origin}")

if args.verbose:
logger.info("PRINTING REPORT TO CONSOLE")
Expand Down
51 changes: 19 additions & 32 deletions ouro/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def _handle_node_cycle_only(
self._cycles[node.name] = []

in_def = any(
[
def_begin <= lineno <= def_end
for def_begin, def_end in node.defs
]
def_begin <= lineno <= def_end for def_begin, def_end in node.defs
)
path_from_import_to_file = [node.name for node in path]

Expand Down Expand Up @@ -69,10 +66,7 @@ def _handle_node_cycle_categorize(
}

in_def = any(
[
def_begin <= lineno <= def_end
for def_begin, def_end in node.defs
]
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 = {
Expand Down Expand Up @@ -127,18 +121,21 @@ def cycles(self) -> Dict:
def get_possible_origins(
self, cycles: Dict, num_possibilities: int = 3
) -> List[str]:
paths = []

if self.categorize:
for cycle in cycles.values():
for category in cycle.values():
for cycle_info in category:
paths.extend(cycle_info["path_from_import_to_file"])
cycle_iter = (
cycle_info
for cycle in cycles.values()
for category in cycle.values()
for cycle_info in category
)
else:
for cycle in cycles.values():
for cycle_info in cycle:
paths.extend(cycle_info["path_from_import_to_file"])
cycle_iter = (
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
]
if not paths:
return []

Expand All @@ -152,20 +149,10 @@ def get_possible_origins(

possible_origins = []
for path in most_common_paths:
if self.categorize:
if path in cycles and any(
path in cycle_info["path_from_import_to_file"]
for cycle in cycles.values()
for category in cycle.values()
for cycle_info in category
):
possible_origins.append(path)
else:
if path in cycles and any(
path in cycle_info["path_from_import_to_file"]
for cycle in cycles.values()
for cycle_info in cycle
):
possible_origins.append(path)
if path in cycles and any(
path in cycle_info["path_from_import_to_file"]
for cycle_info in cycle_iter
):
possible_origins.append(path)

return possible_origins
15 changes: 0 additions & 15 deletions ouro/imports_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,5 @@ def find_path(
path_list.reverse()
return path_list

def is_cyclic(
self, first_node: "Node", second_node: "Node"
) -> Tuple[bool, List["Node"], List["Node"]]:
is_reachable_first_to_second, path_first_to_second = self.is_reachable(
first_node, second_node
)
is_reachable_second_to_first, path_second_to_first = self.is_reachable(
second_node, first_node
)

if is_reachable_first_to_second and is_reachable_second_to_first:
return True, path_first_to_second, path_second_to_first
else:
return False, [], []

def __iter__(self):
return iter(self.graph.items())
13 changes: 9 additions & 4 deletions ouro/nodes_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _get_imports(
return [
node
for node in ast.walk(ast.parse(content))
if isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom)
if isinstance(node, (ast.Import, ast.ImportFrom))
]

def _get_defs(
Expand All @@ -52,9 +52,14 @@ def _get_defs(
return [
node
for node in ast.walk(ast.parse(content))
if isinstance(node, ast.FunctionDef)
or isinstance(node, ast.AsyncFunctionDef)
or isinstance(node, ast.ClassDef)
if isinstance(
node,
(
ast.FunctionDef,
ast.AsyncFunctionDef,
ast.ClassDef,
),
)
]

def _get_module_path(
Expand Down

0 comments on commit 49857ec

Please sign in to comment.