예제 #1
0
파일: main.py 프로젝트: prim/anko
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()
예제 #2
0
    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)