Skip to content

Commit 364d851

Browse files
authored
Merge pull request #3 from xnuinside/dev
add get_lines_numbers method
2 parents d247b90 + ade6a47 commit 364d851

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

codegraph/core.py

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5186
def 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-
174183
def 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-

codegraph/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def cli():
1919

2020

2121
def main(args):
22-
modules_entities = core.create_graph(args)
23-
pprint.pprint(modules_entities)
22+
usage_graph = core.CodeGraph(args).usage_graph()
23+
pprint.pprint(usage_graph)
2424
if not args.object_only:
2525
# to make more quick work if not needed to visualize
2626
import codegraph.vizualyzer as vz
27-
vz.draw_graph(modules_entities)
27+
vz.draw_graph(usage_graph)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "codegraph"
3-
version = "0.0.5"
3+
version = "0.0.6"
44
license = "MIT"
55
readme = "README.rst"
66
homepage = "https://github.com/xnuinside/codegraph"

0 commit comments

Comments
 (0)