def module_node_from_path(path):
    try:
        node = astor.parsefile(path)
    except:
        return None
    else:
        return node
Example #2
0
 def match(self, selector, filename):
     module = astor.parsefile(filename)
     for match in super(ASTMatchEngine, self).match(selector, module.body):
         lineno = match.lineno
         if isinstance(match, (ast.ClassDef, ast.FunctionDef)):
             for d in match.decorator_list:
                 lineno += 1
         yield match, lineno
Example #3
0
 def match(self, selector, filename):
     module = astor.parsefile(filename)
     for match in super(ASTMatchEngine, self).match(selector, module.body):
         lineno = match.lineno
         if isinstance(match, (ast.ClassDef, ast.FunctionDef)):
             for d in match.decorator_list:
                 lineno += 1
         yield match, lineno
Example #4
0
def rebuild_bbscript(sourceFilename, outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    f = open(outFilename + ".txt", "w")
    f.write(astor.dump(sourceAST))
    f.close()
    output = open(outFilename, "wb")
    Rebuilder().visit(sourceAST)
    output.close()
def rebuild_dbzfscript(sourceFilename,outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    if(os.path.isfile(sourceFilename+".txt")):
        os.remove(sourceFilename+".txt")
    f = open(sourceFilename+".txt","w")
    f.write(astor.dump(sourceAST))
    f.close()
    output = open(outFilename,"wb")
    Rebuilder().visit(sourceAST)
    output.close()
Example #6
0
def rebuild_dbzfscript(sourceFilename, outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    if (os.path.isfile(sourceFilename + ".txt")):
        os.remove(sourceFilename + ".txt")
    f = open(sourceFilename + ".txt", "w")
    f.write(astor.dump(sourceAST))
    f.close()
    output = open(outFilename, "wb")
    Rebuilder().visit(sourceAST)
    output.close()
Example #7
0
    def test_deprecation(self):
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            ast1 = astor.code_to_ast.parse_file(__file__)
            src1 = astor.to_source(ast1)
            ast2 = astor.parsefile(__file__)
            src2 = astor.codegen.to_source(ast2)
            self.assertEqual(len(w), 2)
            w = [warnings.formatwarning(x.message, x.category,
                                        x.filename, x.lineno) for x in w]
            w = [x.rsplit(':', 1)[-1].strip() for x in w]
            self.assertEqual(w[0], 'astor.parsefile is deprecated.  '
                             'Please use astor.code_to_ast.parse_file.\n'
                             '  ast2 = astor.parsefile(__file__)')
            self.assertEqual(w[1], 'astor.codegen is deprecated.  '
                             'Please use astor.code_gen.\n'
                             '  src2 = astor.codegen.to_source(ast2)')

        self.assertEqual(src1, src2)
Example #8
0
def analy_pygrammar(ruta):
    resultado = []
    if not op.exists(ruta):
        raise Exception('PyGrammarAnaly Error: no existe ' + str(ruta))
    try:
        # PARA LAS PRACTICAS DE PYTHON2, LAS RECODIFICAMOS Y PASAMOS A PY3
        # PARA PODER ANALIZAR SU ARBOL
        (status0, out0) = subprocess.getstatusoutput(" echo '\n' >> " + ruta)
        (status1, out1) = subprocess.getstatusoutput(
            """cat {0} | sed "s/á/a/g;s/é/e/g;s/í/i/g;s/ó/o/g;s/ú/u/g" \
            > {0}.bak && mv {0}.bak {0}""".format(ruta))
        (status2, out2) = subprocess.getstatusoutput(" 2to3 -w " + ruta)
    
        if any((status0, status1, status2)):
            LogError(descripcion='analy_pygrammar fichero no se analiza'
                                 'por problemas codificacion ' + ruta)
            return resultado
            #raise Exception('Error al convertir a py3 ' + ruta)
        # Analisis del abstract source tree para py3
        ast_tree = astor.parsefile(ruta)
        for nd in ast.walk(ast_tree):
            if isinstance(nd, (ast.ClassDef, ast.FunctionDef)):
                # print(astor.code_to_ast.get_file_info(nd))
                # print(type(nd), nd.name, nd.lineno, nd.col_offset)
                coor = Udfuentecoor(filaini=nd.lineno, col=nd.col_offset)
                elem = Udfuente(nombre=nd.name, ubicacion=coor)
                
                if isinstance(nd, ast.ClassDef):
                    elem.tipo = Udfuente.TIPO_CLASE
                elif isinstance(nd, ast.FunctionDef):
                    elem.tipo = Udfuente.TIPO_FUNC
                
                elem.snipped = snippedfichero(ruta, nd.lineno,
                                              NUMLINEAS_SNIPPED)
                
                resultado.append(elem)
        return resultado
    except Exception as exc:
        raise
    
    return
Example #9
0
def testone(fname, f1=None, f2=None):
    try:
        ast1 = astor.parsefile(fname)
    except (SyntaxError, UnicodeDecodeError):
        print("IGNORED %s" % fname)
        return
    dump1 = astor.dump(ast1)
    reconstitute = '# -*- coding: utf-8 -*-\n' + astor.to_source(ast1)
    ast2 = ast.parse(reconstitute, fname)
    dump2 = astor.dump(ast2)
    ok = dump1 == dump2
    print('%-8s%s' % ('OK' if dump1 == dump2 else 'FAIL', fname))
    if not ok and f1 is not None and f2 is not None:
        f1.write('\n\n***************************************\n%s\n***************************************\n\n\n' % fname)
        f2.write('\n\n***************************************\n%s\n***************************************\n\n\n' % fname)
        f1.write(dump1)
        f2.write(dump2)
        f = open('bad.txt', 'w')
        f.write(reconstitute)
        f.close()
    return ok
Example #10
0
def create_py_modules(target_swagger_domain_name='loanapp-dev.sl.com'):
    # 生成或切换目录
    temp_module_directory = re.sub('restful_service_entity_generator',
                                   'temp_modules', os.getcwd())
    if not os.path.isdir(temp_module_directory):
        os.mkdir(temp_module_directory)
        os.path.join(temp_module_directory)

    module_entity = SwaggerEntity(target_swagger_domain_name)
    module_entity.send_request()
    node = astor.parsefile(COMPLETE_FILE_NAME)
    paths = module_entity.paths

    for i in range(len(paths)):
        module_dict = module_entity.next()

        for module_item in module_dict.values():
            _node = deepcopy(node)
            handle_cls_name(_node.body, 'Foo', module_item['cls_name'])
            # set global assignment
            handle_global_assignment(_node.body, 'PATH', module_item['path'])
            handle_global_assignment(_node.body, 'METHOD_TYPE',
                                     module_item['method_type'])

            content_type = module_item['content_type']
            if content_type:
                print content_type
                content_type = str(
                    re.search(r'json|form', content_type).group())
                handle_global_assignment(_node.body, 'CONTENT_TYPE',
                                         content_type)

            handle_global_assignment(_node.body, 'HAS_DATA_PATTERN',
                                     module_item['has_data_pattern'])
            # set request data
            if 'parameters_in_path' in module_item:
                handle_path_parameters(_node.body,
                                       module_item['parameters_in_path'])
            if 'body_data' in module_item:
                if content_type == 'form':
                    handle_global_assignment(
                        _node.body, 'BODY_DATA',
                        _format_query_data(module_item['body_data'].keys()))
                else:
                    handle_global_assignment(_node.body, 'BODY_DATA',
                                             module_item['body_data'])
            if 'query_data' in module_item:
                handle_global_assignment(
                    _node.body, 'QUERY_DATA',
                    '?' + _format_query_data(module_item['query_data']))
            # set response doc
            _handle_class_doc(_node.body, module_item['response'])
            # Written to the .py file
            file_name = re.sub(r'(?P<first>^[A-Z])|[A-Z]', _to_lower,
                               module_item['cls_name']) + '.py'
            # temp_lines = astor.to_source(_node)
            # lines = re.sub(r"\\n[ ]+'", '\n    """',
            #                re.sub(r"'\\n", '"""\n', temp_lines))
            # print re.sub(r"\\n([ ]*)", lambda m: '\n' + m.group(1), lines)
            # assert 0
            with open(temp_module_directory + os.sep + file_name,
                      'w') as py_module:
                temp_lines = astor.to_source(_node)
                lines = re.sub(r"\\n[ ]+'", '\n    """',
                               re.sub(r"'\\n", '"""\n', temp_lines))
                py_module.write(
                    re.sub(r"\\n([ ]*)", lambda m: '\n' + m.group(1), lines))
Example #11
0
def inject_tail(scope):
    path = os.path.join(os.path.dirname(__file__), 'tail.py')
    scope['callables']['tail'] = None
    return astor.parsefile(path).body
Example #12
0
 for node in ast.walk(a):
     print(node)
 print()
 for node in ast.iter_child_nodes(a):
     print(node)
 '''
 
 # print(b)
 
 zz = False
 if zz:
 
     # a = astor.parsefile('/home/chilli-mint/Dropbox/_PFC_MISTURE
     # /001_codigos_py_sh_etc/humansize.py')
     a = astor.parsefile('/home/chilli-mint/Dropbox/MiStuRe/misture_core/'
                         + \
                         'MISTURE/analyx/git_utils.py')
     # print(a)
     # b = astor.to_source(a)
     print(type(ast.walk(a)))
     for node in ast.walk(a):
         # print(type(node), '->\t', astor.strip_tree(node))
         if isinstance(node, (ast.ClassDef, ast.FunctionDef)):
             # print(astor.code_to_ast.get_file_info(node))
             print(type(node), node.name, node.lineno, node.col_offset)
             print(node._fields)
             # if isinstance(node, ast.FunctionDef):
             # print(node.name, node.lineno, node.col_offset, node._fields)
             # print('####')
             # print(ast.dump(node, annotate_fields=True,
             # include_attributes=False))
Example #13
0
def load_ast_from_py_file(filename):
    node = astor.parsefile(filename)
    return node
def rebuild_bbscript(sourceFilename,outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    output = open(outFilename,"wb")
    Rebuilder().visit(sourceAST)
    output.close()
Example #15
0
def inject_tail(scope):
    path = os.path.join(os.path.dirname(__file__), 'tail.py')
    scope['callables']['tail'] = None
    return astor.parsefile(path).body
Example #16
0
def create_har_py_modules(entity_instance, mode='file'):
    # mode: 'file'/'json'
    # 生成或切换目录
    temp_module_directory = re.sub('restful_service_entity_generator',
                                   'temp_modules', os.getcwd())
    if not os.path.isdir(temp_module_directory):
        os.mkdir(temp_module_directory)
        os.path.join(temp_module_directory)

    module_entity = entity_instance
    node = astor.parsefile(COMPLETE_FILE_NAME)
    next_entity = module_entity.next_entity()
    json_template = {}

    while True:
        try:
            module_dict = next_entity.next()

            for module_item in module_dict.values():
                _node = deepcopy(node)
                handle_cls_name(_node.body, 'Foo', module_item['cls_name'])
                # set global assignment
                # set value for PATH
                handle_global_assignment(_node.body, 'PATH',
                                         module_item['path'])
                # set value for METHOD_TYPE
                handle_global_assignment(_node.body, 'METHOD_TYPE',
                                         module_item['method_type'])

                content_type = module_item['content_type']
                if content_type:
                    content_type = str(
                        re.search(r'json|form', content_type).group())
                    # set value for CONTENT_TYPE
                    handle_global_assignment(_node.body, 'CONTENT_TYPE',
                                             content_type)
                # set value for domain_name
                handle_global_assignment(_node.body, 'DOMAIN_NAME',
                                         module_item['host'])
                # set value for url
                handle_global_assignment(_node.body, 'URL',
                                         module_item['url_pattern'])
                # set value for request_headers
                handle_global_assignment(_node.body, 'REQUEST_HEADERS',
                                         module_item['request_headers'])
                # set value for HAS_DATA_PATTERN
                handle_global_assignment(_node.body, 'HAS_DATA_PATTERN',
                                         module_item['has_data_pattern'])
                # set request data
                # set value for BODY_DATA
                handle_global_assignment(_node.body, 'BODY_DATA',
                                         module_item['body_data'])

                # set value for QUERY_DATA
                handle_global_assignment(_node.body, 'QUERY_DATA',
                                         module_item['query_data'])
                # Written to the .py file
                file_name = re.sub(r'(?P<first>^[A-Z])|[A-Z]', _to_lower,
                                   module_item['cls_name']) + '.py'
                # prepare code lines to inject
                temp_lines = astor.to_source(_node)
                lines = re.sub(r"\\n[ ]+'", '\n    """',
                               re.sub(r"'\\n", '"""\n', temp_lines))
                lines = re.sub(r"\\n([ ]*)", lambda m: '\n' + m.group(1),
                               lines)

                if mode == 'file':
                    with open(temp_module_directory + os.sep + file_name,
                              'w') as py_module:
                        py_module.write(lines)

                if mode == 'json':
                    json_template.update({module_item['cls_name']: lines})

        except StopIteration:
            if json_template:
                return json_template
            break
Example #17
0
def rebuild_bbscript(sourceFilename, outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    output = open(outFilename, "wb")
    Rebuilder().visit(sourceAST)
    output.close()
import astor

ORIGINAL = 'main.py'

a = astor.parsefile(ORIGINAL)

print(astor.dump(a))
Example #19
0
import astor

f = "test.py"

node = astor.parsefile(f)
print node
print astor.dump(node)
print astor.to_source(node)