'Add profiling code into the givne module AST.'

def _profiling(self, astmod, options):

    if (not astor):
        _logger.warning('Please    install    astor    to    display    the    code    profiling')
        return
    code_line = astor.to_source(astmod)
    astmod.body.insert(0, ast.Assign(targets=[ast.Name(id='code', ctx=ast.Store())], value=ast.Call(func=ast.Attribute(value=ast.Str(code_line), attr='split', ctx=ast.Load()), args=[ast.Str('\n')], keywords=[], starargs=None, kwargs=None)))
    code_line = [[l, False] for l in code_line.split('\n')]
    astmod.body.insert(0, ast.Assign(targets=[ast.Name(id='profiling', ctx=ast.Store())], value=ast.Dict(keys=[], values=[])))
    astmod.body.insert(0, ast.parse('from    time    import    time').body[0])
    line_id = [0]
    def prof(code, time):
        line_id[0] += 1
        return ast.Expr(ast.Call(func=ast.Attribute(value=ast.Name(id='profiling', ctx=ast.Load()), attr='setdefault', ctx=ast.Load()), args=[ast.Num(line_id[0]), ast.BinOp(left=ast.Call(func=ast.Name(id='time', ctx=ast.Load()), args=[], keywords=[], starargs=None, kwargs=None), op=ast.Sub(), right=ast.Name(id=time, ctx=ast.Load()))], keywords=[], starargs=None, kwargs=None))
    def profile(body):
        profile_body = []
        for code in body:
            time = self._make_name('time')
            profile_body.append(ast.Assign(targets=[ast.Name(id=time, ctx=ast.Store())], value=ast.Call(func=ast.Name(id='time', ctx=ast.Load()), args=[], keywords=[], starargs=None, kwargs=None)))
            profile_body.append(code)
            profline = prof(code, time)
            if hasattr(code, 'body'):
                code.body = ([profline] + profile(code.body))
                if hasattr(code, 'orelse'):
                    code.orelse = ([profline] + profile(code.orelse))
            profile_body.append(profline)
        return profile_body
    for call in options['ast_calls']:
        call.body = profile(call.body)
    options['ast_calls'][0].body = (ast.parse(dedent('\n                                                global    profiling\n                                                profiling    =    {}\n                                                ')).body + options['ast_calls'][0].body)
    p = float(options.get('profile'))
    options['ast_calls'][0].body.extend(ast.parse(dedent(('\n                                                total    =    0\n                                                prof_total    =    0\n                                                code_profile    =    []\n                                                line_id    =    0\n                                                for    line    in    code:\n                                                                if    not    line:\n                                                                                if    %s    <=    0:    print    ""\n                                                                                continue\n                                                                if    line.startswith(\'def    \')    or    line.startswith(\'from    \')    or    line.startswith(\'import    \'):\n                                                                                if    %s    <=    0:    print    "                         DCTB ",    line\n                                                                                continue\n                                                                line_id    +=    1\n                                                                total    +=    profiling.get(line_id,    0)\n                                                                dt    =    round(profiling.get(line_id,    -1)*1000000)/1000\n                                                                if    %s    <=    dt:\n                                                                                prof_total    +=    profiling.get(line_id,    0)\n                                                                                display    =    "%%.2f DCTB "    %%    dt\n                                                                                print    ("    "    *    (7    -    len(display)))    +    display,    line\n                                                                elif    dt    <    0    and    %s    <=    0:\n                                                                                print    "                    ? DCTB ",    line\n                                                print    "\'%s\'    Total:    %%d/%%d"    %%    (round(prof_total*1000),    round(total*1000))\n                                                ' % (p, p, p, p, str(options['template']).replace('"', '    '))))).body)
