Example #1
0
    def find_imports(self):
        from pysource.api import parse_source

        try:
            source_ast = parse_source(self.content)
        except SyntaxError:
            logger.warn("`%s...` is not parsable!", self.content[:100])
            self.imports = []
            return

        import_names = set(get_import_names(source_ast))
        imports = []

        for import_name in import_names:
            try:
                try:
                    import_object = import_module(self, import_name)
                except ImportError:
                    # Probably because that the package is not installed
                    package_name = import_name.split(".")[0]
                    try:
                        import_object = AbsoluteImport.from_package(self.space.find_package(package_name), import_name)
                    except ImportError:
                        continue  # Just ignore this import for now.
            except FileExecutionError:
                continue
            imports.append(import_object)
        #
        self.imports = imports
Example #2
0
def test_extract_attr():
    code = '''
    import sys
    sys.stdin.write('hello, world!\\n')
    '''
    module_node = parse_source(code)
    module_accessors = list(get_module_accessors(module_node))
    #
    assert 'sys.stdin' in module_accessors
    assert 'sys.stdin.write' in module_accessors
Example #3
0
def test_extract_attr():
    code = '''
    import sys
    sys.stdin.write('hello, world!\\n')
    '''
    module_node = parse_source(code)
    module_accessors = list(get_module_accessors(module_node))
    #
    assert 'sys.stdin' in module_accessors
    assert 'sys.stdin.write' in module_accessors
Example #4
0
def test_get_import_names():
    code = '''
    import foo.bar
    def foo():
        def bar():
            from modulename import funcname
    '''
    module_node = parse_source(code)
    import_names = list(get_import_names(module_node))
    #
    assert 'foo.bar' in import_names
    assert 'modulename.funcname' in import_names
Example #5
0
def test_get_import_names():
    code = '''
    import foo.bar
    def foo():
        def bar():
            from modulename import funcname
    '''
    module_node = parse_source(code)
    import_names = list(get_import_names(module_node))
    #
    assert 'foo.bar' in import_names
    assert 'modulename.funcname' in import_names
Example #6
0
def main(argv):
    try:
        filename = argv[1]
    except IndexError:
        print 'usage: %s [filename]' % argv[0]
        return 1
    #
    with open(filename) as f:
        module_node = parse_source(f.read())
    import_names = list(get_import_names(module_node))
    accessors = list(get_module_accessors(module_node))
    print import_names
    print accessors
Example #7
0
def test_extract_attr_with_from_and_as():
    code = '''
    from sys import stdin as mystdin
    mystdin.write('hello, world!\\n')

    from os.path import dirname as mydirname
    print mydirname(__file__)
    '''
    module_node = parse_source(code)
    module_accessors = list(get_module_accessors(module_node))
    #
    assert 'sys.stdin.write' in module_accessors
    assert 'os.path.dirname' in module_accessors
Example #8
0
def test_extract_attr_with_from_and_as():
    code = '''
    from sys import stdin as mystdin
    mystdin.write('hello, world!\\n')

    from os.path import dirname as mydirname
    print mydirname(__file__)
    '''
    module_node = parse_source(code)
    module_accessors = list(get_module_accessors(module_node))
    #
    assert 'sys.stdin.write' in module_accessors
    assert 'os.path.dirname' in module_accessors
Example #9
0
def main(argv):
    try:
        filename = argv[1]
    except IndexError:
        print 'usage: %s [filename]' % argv[0]
        return 1
    #
    with open(filename) as f:
        module_node = parse_source(f.read())
    import_names = list(get_import_names(module_node))
    accessors = list(get_module_accessors(module_node))
    print import_names
    print accessors
Example #10
0
def parseCode(string):
    node = api.parse_source(string)
    import_names = list(api.get_import_names(node))
    package = []
    funcs = []
    for item in import_names:
        if len(item.split('.')) > 1:
            pac = item.split('.')[0]
            fun = item.split('.')[1]
            package.append(pac)
            funcs.append(fun)
        else:
            package.append(item)
    return set(package), set(funcs)
Example #11
0
def parseCode(string):
    node = api.parse_source(string)
    import_names = list(api.get_import_names(node))
    package = []
    funcs = []
    for item in import_names:
        if len(item.split('.')) >1:
            pac = item.split('.')[0]
            fun = item.split('.')[1]
            package.append(pac)
            funcs.append(fun)
        else:
            package.append(item)
    return set(package),set(funcs)
Example #12
0
def parseFile(filename):
    with open(filename) as f:
        node = api.parse_source(f.read())
    import_names = list(api.get_import_names(node))
    package = []
    funcs = []
    for item in import_names:
        if len(item.split('.')) > 1:
            pac = item.split('.')[0]
            fun = item.split('.')[1]
            package.append(pac)
            funcs.append(fun)
        else:
            package.append(item)
    return set(package), set(funcs)
Example #13
0
def parseFile(filename):
    with open(filename) as f: 
        node = api.parse_source(f.read())
    import_names = list(api.get_import_names(node))
    package = []
    funcs = []
    for item in import_names:
        if len(item.split('.')) >1:
            pac = item.split('.')[0]
            fun = item.split('.')[1]
            package.append(pac)
            funcs.append(fun)
        else:
            package.append(item)
    return set(package),set(funcs)
Example #14
0
def gen_subgraph_from_code(code):
    try:
        module = parse_source(code)
    except SyntaxError:
        return {}
    return gen_subgraph_from_keywords(get_import_names(module))
Example #15
0
def gen_subgraph_from_code(code):
    try:
        module = parse_source(code)
    except SyntaxError:
        return {}
    return gen_subgraph_from_keywords(get_import_names(module))