def eva_import(import_ast, local_namespace): global_namespace = local_namespace.global_namespace cur_fpath = os.path.dirname(global_namespace.fpath) # TODO: os.path = module path是一个属性 实际上没有 os.path 这个文件 需要支持这种写法 module = None for alias in import_ast.names: module_name = alias.name asname = alias.asname if alias.asname else module_name # 通过把 a.b.c 转换成 文件名查找对应的代码 if module_name in sys_modules: module = sys_modules[module_name] else: if module_name not in filter_module_names: module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace) if module_name in sys_modules: module = sys_modules[module_name] elif module_fpath: source = file_text(module_fpath) module_ast = ast.parse(source) add_fpath_attr(module_ast, module_fpath) module = eva_module(module_ast, module_name, fpath=module_fpath) else: module = None if module: local_namespace.set(asname, module) # 通过把 a.b 转换成 文件名查找对应的代码 然后再获取 .c属性 if "." in module_name: module_name, attr = module_name.rsplit(".", 1) if module_name in sys_modules: module = sys_modules[module_name] else: if module_name not in filter_module_names: module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace) if module_name in sys_modules: module = sys_modules[module_name] elif module_fpath: source = file_text(module_fpath) module_ast = ast.parse(source) add_fpath_attr(module_ast, module_fpath) module = eva_module(module_ast, module_name, fpath=module_fpath) else: print("_____", module_fpath, module_name) import pdb; pdb.set_trace() if module: local_namespace.set(asname, module)
def builtin_init(): for fname in os.listdir(r"mock\builtins"): if fname not in ["list.py", "dict.py", "builtins.py", "string.py"]: continue if fname.endswith(".py"): mname = fname[:-3] mfpath = r"mock\builtins\%s" % fname source = file_text(mfpath) mast = ast.parse(source) from utils import add_fpath_attr add_fpath_attr(mast, mfpath) module = eva_module(mast, "b_%s" % mname, mfpath) if fname == "dict.py": cimpl.impl_dict_methods(module) if fname == "list.py": cimpl.impl_list_methods(module) if fname == "builtins.py": cimpl.impl_builtins_methods(module) if fname == "string.py": cimpl.impl_string_methods(module) sys_modules['__builtin__'] = sys_modules["b_builtins"]
import time from eva import eva_module from utils import add_fpath_attr filename = "../test/%s.py" % sys.argv[1] now = time.time begin = now() with open(filename) as pyfile: source = pyfile.read() module_ast = ast.parse(source) add_fpath_attr(module_ast, filename) eva_module(module_ast, "__main__", filename) end = now() print "use time %0.8f" % (end - begin) # import pdb; pdb.set_trace() # 补全测试 # import domain # domain.debug() # 覆盖率测试 # from coverage import q, fs, a # a()
def handle(self): data, socket = self.request args = data.split("[|||]") command = args[0] args = args[1:] path, line, column, lineno = args column = int(column) - 2 lineno = int(lineno) package_path = find_top_package(path) module_name = get_module_name(path, package_path) print command, args print package_path, module_name, column from eva import sys_modules from eva import eva_module from utils import add_fpath_attr import ast filename = path if module_name not in sys_modules: with open(filename) as pyfile: source = pyfile.read() module_ast = ast.parse(source) add_fpath_attr(module_ast, filename) eva_module(module_ast, "__main__", filename) import completion name, extra = completion.word(line, column) if extra: resp_index = line.find(extra) else: resp_index = line.find(name) print 9998, name, extra, resp_index module = ast.parse(name) from eva import eva from domain import files fpath = path prims = [] from py import Function def canko(name, v): if isinstance(v, Function): return dict(word=v.signature, abbr=v.signature, info=v.signature) return dict(word=name, abbr=name, info=name+"pre") # name if extra: print files[fpath][lineno].names.names.keys() wc = files[fpath][lineno].names.names wa = list(wc["self"])[0] # import pdb; pdb.set_trace() values = eva(module.body[0].value, files[fpath][lineno]) print 1111, name, values cl = [] for v in values: try: print 88888, v.get_attr(extra), extra if v.get_attr(extra): for xxx in v.get_attr(extra): prims.append(canko(extra, xxx)) cl.append(extra) else: # 实例域 for key in v.namespace.names.names: if key.startswith(extra): for xxx in v.namespace.names.names[key]: print xxx, 1 prims.append(canko(key, xxx)) cl.append(key) # 类域 for key in v.klass.namespace.names.names: if key.startswith(extra): for xxx in v.klass.namespace.names.names[key]: print xxx, 2 prims.append(canko(key, xxx)) cl.append(key) # 基类 for bases in v.klass.base_class: for b in bases: for key in b.namespace.names.names: if key.startswith(extra): for xxx in b.namespace.names.names[key]: print xxx, 3 prims.append(canko(key, xxx)) cl.append(key) # 属性??? property # 先尝试获取名字 # 获取不到再拿其他的迭代namespace # TODO except: import traceback traceback.print_exc() print "err", v, extra print "COM>>>>...", cl else: # 先尝试获取名字 cl = [] values = eva(module.body[0].value, files[fpath][lineno]) if values: prims.append(dict(word=name, abbr=name, info=name)) cl.append(name) else: ns = files[fpath][lineno] for key in ns.names.names: if key.startswith(name): for xxx in ns.names.names[key]: prims.append(canko(key, xxx)) cl.append(key) for key in ns.global_namespace.names.names: if key.startswith(name): for xxx in ns.global_namespace.names.names[key]: prims.append(canko(key, xxx)) prims.append(dict(word=key, abbr=key, info=key)) cl.append(key) # buildints? # 获取不到再拿其他的迭代namespace print "COM", cl # word 用于插入的完整文本 # abbr 用于在弹出选项中显示的文本 # info 用于在preview窗口显示的文本 # c = {'word', 'abbr', 'info'} prims.sort(key=lambda x:x["word"]) dictstr = '[' for cmpl in prims: dictstr += '{' for x in cmpl: dictstr += '"%s":"%s",' % (x,cmpl[x]) dictstr += '"icase":0},' if dictstr[-1] == ',': dictstr = dictstr[:-1] dictstr += ']' print "***************" print dictstr socket.sendto("[|||]".join(["Done", dictstr, str(resp_index)]), self.client_address)
def eva_importfrom(import_ast, local_namespace): module_name = import_ast.module module = None # if "achieve" in module_name: # import pdb; pdb.set_trace() global_namespace = local_namespace.global_namespace cur_fpath = os.path.dirname(global_namespace.fpath) # 相对路径的import if import_ast.level == 1: module_fpath = os.path.join(cur_fpath, "%s.py" % module_name) if os.path.exists(module_fpath): cur_module_name = local_namespace.global_namespace.module_name if local_namespace.global_namespace.module_anko.ast.is_package: prefix = cur_module_name elif "." in cur_module_name: prefix = cur_module_name.rsplit(".", 1)[0] else: prefix = "" if prefix: module_name = "%s.%s" % (prefix, module_name) if module_name in sys_modules: module = sys_modules[module_name] else: source = file_text(module_fpath) module_ast = ast.parse(source) add_fpath_attr(module_ast, module_fpath) module = eva_module(module_ast, module_name, fpath=module_fpath) elif import_ast.level > 1: import pdb; pdb.set_trace() elif module_name not in filter_module_names: if module_name in sys_modules: module = sys_modules[module_name] else: module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace) if module_name in sys_modules: module = sys_modules[module_name] elif module_fpath: source = file_text(module_fpath) module_ast = ast.parse(source) add_fpath_attr(module_ast, module_fpath) if module_name in sys_modules: module = sys_modules[module_name] else: module = eva_module(module_ast, module_name, fpath=module_fpath) else: # 通过把 a.b 转换成 文件名查找对应的代码 然后再获取 .c属性 if "." in module_name: module_name, attr = module_name.rsplit(".", 1) if module_name in sys_modules: module = sys_modules[module_name] else: module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace) if module_name in sys_modules: module = sys_modules[module_name] elif module_fpath: source = file_text(module_fpath) module_ast = ast.parse(source) add_fpath_attr(module_ast, module_fpath) if module_name in sys_modules: module = sys_modules[module_name] else: module = eva_module(module_ast, module_name, fpath=module_fpath) else: print("_____", module_fpath, module_name) import pdb; pdb.set_trace() module_fpath, is_cur_dir = module_file_path(cur_fpath, module_name, local_namespace) if module is None: return for alias in import_ast.names: asname = alias.asname if alias.asname else alias.name values = module.get(alias.name) for value in values: local_namespace.set(asname, value)