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
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
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
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
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
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)
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)
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)
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)
def gen_subgraph_from_code(code): try: module = parse_source(code) except SyntaxError: return {} return gen_subgraph_from_keywords(get_import_names(module))