Debian Codegraph @

doc/tutorial/tuto_1_3.py

raw symbols SCIP
1 import pathlib
2 import pygraphviz
3
4 from import_deps import PyModule, ModuleSet
5
6
7
8 def get_imports(pkg_modules, module_path):
9 module = pkg_modules.by_path[module_path]
10 imports = pkg_modules.get_imports(module, return_fqn=True)
11 return {'modules': list(sorted(imports))}
12
13 def task_imports():
14 """find imports from a python module"""
15 base_path = pathlib.Path('projects/requests/requests')
16 pkg_modules = ModuleSet(base_path.glob('**/*.py'))
17 for name, module in pkg_modules.by_name.items():
18 yield {
19 'name': name,
20 'file_dep': [module.path],
21 'actions': [(get_imports, (pkg_modules, module.path))],
22 }
23
24
25 def module_to_dot(imports, targets):
26 graph = pygraphviz.AGraph(strict=False, directed=True)
27 graph.node_attr['color'] = 'lightblue2'
28 graph.node_attr['style'] = 'filled'
29 for source, sinks in imports.items():
30 for sink in sinks:
31 graph.add_edge(source, sink)
32 graph.write(targets[0])
33
34 def task_dot():
35 """generate a graphviz's dot graph from module imports"""
36 return {
37 'targets': ['requests.dot'],
38 'actions': [module_to_dot],
39 'getargs': {'imports': ('imports', 'modules')},
40 'clean': True,
41 }
42
43
44 def task_draw():
45 """generate image from a dot file"""
46 return {
47 'file_dep': ['requests.dot'],
48 'targets': ['requests.png'],
49 'actions': ['dot -Tpng %(dependencies)s -o %(targets)s'],
50 'clean': True,
51 }