File: tuto_1_2.py

package info (click to toggle)
doit 0.36.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,704 kB
  • sloc: python: 11,863; makefile: 33; ansic: 14; javascript: 3; sh: 1
file content (48 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pathlib
import pygraphviz

from import_deps import PyModule, ModuleSet

def get_imports(module_path):
    module = PyModule(module_path)
    base_path = module.pkg_path().resolve()
    mset = ModuleSet(base_path.glob('**/*.py'))
    imports = mset.get_imports(module, return_fqn=True)
    return {'modules': list(sorted(imports))}

def task_imports():
    """find imports from a python module"""
    module_path = 'projects/requests/requests/models.py'
    return {
        'file_dep': [module_path],
        'actions': [(get_imports, [module_path])],
    }


def module_to_dot(source, sinks, targets):
    graph = pygraphviz.AGraph(strict=False, directed=True)
    graph.node_attr['color'] = 'lightblue2'
    graph.node_attr['style'] = 'filled'
    for sink in sinks:
        graph.add_edge(source, sink)
    graph.write(targets[0])


def task_dot():
    """generate a graphviz's dot graph from module imports"""
    return {
        'targets': ['requests.models.dot'],
        'actions': [(module_to_dot, (), {'source': 'requests.models'})],
        'getargs': {'sinks': ('imports', 'modules')},
        'clean': True,
    }


def task_draw():
    """generate image from a dot file"""
    return {
        'file_dep': ['requests.models.dot'],
        'targets': ['requests.models.png'],
        'actions': ['dot -Tpng %(dependencies)s -o %(targets)s'],
        'clean': True,
    }