ficapy
7/21/2016 - 12:57 AM

精简pycallgraph代码,生成调用关系图 python3.5.1 代码解读https://ficapy.github.io/2016/07/22/pycallgraph_note/

精简pycallgraph代码,生成调用关系图 python3.5.1 代码解读https://ficapy.github.io/2016/07/22/pycallgraph_note/

===================================func_name====================================   nums
requests.sessions.Session.resolve_redirects                                         12
requests.adapters.HTTPAdapter.send                                                  9
requests.sessions.Session.send                                                      7
requests.packages.urllib3.connectionpool.HTTPConnectionPool.urlopen                 7
requests.packages.urllib3.connectionpool.HTTPSConnectionPool.urlopen                7

=====================================model======================================   nums
requests.sessions                                                                   46
urllib.parse                                                                        32
http.cookiejar                                                                      29
requests.cookies                                                                    22
requests.packages.urllib3.connectionpool                                            20
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Ficapy
# Create: '20/7/2016'
import inspect
import os
import re
import tempfile
import textwrap
import pkgutil
from collections import defaultdict
from distutils.sysconfig import get_python_lib
from functools import lru_cache


class Output():
    def __init__(self, filename='call.png'):
        self.filename = filename

    @property
    def edges(self):
        output = []
        for src_func, dests in self.data.items():
            for dst_func, calls in dests.items():
                singal = '"{}" -> "{}" ["label"={}];'.format(src_func, dst_func, calls)
                output.append(singal)
        return output

    @property
    def groups(self):
        output = []
        for group, funcs in self.group.items():
            # 排除一个funcs的
            if len(funcs) == 1:
                continue
            func = '" "'.join(funcs)
            output.append(
                'subgraph "cluster_{group}" {{ '
                '"{func}"; '
                'label = "{group}"; '
                'fontcolor = "black"; '
                'style = "bold"; }}'.format(group=group, func=func))
        return output

    def generate(self):
        '''Returns a string with the contents of a DOT file for Graphviz to
        parse.
        '''
        indent_join = '\n' + ' ' * 12

        return textwrap.dedent('''\
        digraph G {{

            // Attributes
            node [ style = "filled", fontname = "Verdana", shape = "rect", fontsize = "7", fontcolor = "#000000ff" ];
            edge