@@ -34,18 +34,53 @@ def get_code_objects(paths_list: List) -> Dict:
3434 return all_data
3535
3636
37- def create_graph (args : Namespace ) -> Dict :
38- """
39- method to create list of objects from py modules
40- :param args:
41- :return:
42- """
43- # get py modules list
44- paths_list = get_paths_list (args .paths )
45- # get py modules list
46- add_dict = get_code_objects (paths_list )
47- modules_entities = usage_graph (add_dict )
48- return modules_entities
37+ class CodeGraph :
38+
39+ def __init__ (self , args : Namespace ):
40+ self .paths_list = get_paths_list (args .paths )
41+ # get py modules list data
42+ self .modules_data = get_code_objects (self .paths_list )
43+
44+ def get_lines_numbers (self ):
45+ """
46+ return data with entities names and start and end line
47+ :return: Example: {'/Users/user/package/module_name.py':
48+ {'function': (1, 2), 'function_with_constant_return_int': (5, 6),
49+ 'function_with_constant_return_float': (9, 10),
50+ 'function_with_statement_return': (13, 14)..}}
51+
52+ first number in tuple - start line, second - last line
53+ """
54+ data = {}
55+ for module in self .modules_data :
56+ data [module ] = {}
57+ for func in self .modules_data [module ]:
58+ data [module ][func .name ] = (func .lineno , func .endno )
59+ return data
60+
61+ def usage_graph (self ) -> Dict :
62+ """
63+ module name: function
64+ :return:
65+ """
66+ entities_lines , imports , modules_names_map = get_imports_and_entities_lines (self .modules_data )
67+ entities_usage_in_modules = collect_entities_usage_in_modules (self .modules_data , imports , modules_names_map )
68+ # create edges
69+ dependencies = defaultdict (dict )
70+ for module in entities_usage_in_modules :
71+ dependencies [module ] = defaultdict (list )
72+ for method_that_used in entities_usage_in_modules [module ]:
73+ method_usage_lines = entities_usage_in_modules [module ][method_that_used ]
74+ for method_usage_line in method_usage_lines :
75+ for entity in entities_lines [module ]:
76+ if entity [0 ] <= method_usage_line <= entity [1 ]:
77+ dependencies [module ][entities_lines [module ][entity ]].append (method_that_used )
78+ break
79+ else :
80+ # mean in global of module
81+ dependencies [module ]['_' ].append (method_that_used )
82+ dependencies = populate_free_nodes (self .modules_data , dependencies )
83+ return dependencies
4984
5085
5186def get_module_name (code_path : Text ) -> Text :
@@ -145,32 +180,6 @@ def populate_free_nodes(code_objects: Dict, dependencies: Dict) -> Dict:
145180 return dependencies
146181
147182
148- def usage_graph (code_objects : Dict ) -> Dict :
149- """
150- module name: function
151- :param code_objects:
152- :return:
153- """
154- entities_lines , imports , modules_names_map = get_imports_and_entities_lines (code_objects )
155- entities_usage_in_modules = collect_entities_usage_in_modules (code_objects , imports , modules_names_map )
156- # create edges
157- dependencies = defaultdict (dict )
158- for module in entities_usage_in_modules :
159- dependencies [module ] = defaultdict (list )
160- for method_that_used in entities_usage_in_modules [module ]:
161- method_usage_lines = entities_usage_in_modules [module ][method_that_used ]
162- for method_usage_line in method_usage_lines :
163- for entity in entities_lines [module ]:
164- if entity [0 ] <= method_usage_line <= entity [1 ]:
165- dependencies [module ][entities_lines [module ][entity ]].append (method_that_used )
166- break
167- else :
168- # mean in global of module
169- dependencies [module ]['_' ].append (method_that_used )
170- dependencies = populate_free_nodes (code_objects , dependencies )
171- return dependencies
172-
173-
174183def search_entity_usage (module_name : Text , name : Text , line : Text ) -> bool :
175184 """ check exist method or entity usage in line or not """
176185 method_call = name + "("
@@ -182,4 +191,3 @@ def search_entity_usage(module_name: Text, name: Text, line: Text) -> bool:
182191 if aliases [module_name ] + '.' + method_call in line :
183192 return True
184193 return False
185-
0 commit comments