##// END OF EJS Templates
Fix 'import '... completion for py3 & egg files....
Bradley M. Froehle -
Show More
@@ -48,7 +48,10 b' TIMEOUT_STORAGE = 2'
48 TIMEOUT_GIVEUP = 20
48 TIMEOUT_GIVEUP = 20
49
49
50 # Regular expression for the python import statement
50 # Regular expression for the python import statement
51 import_re = re.compile(r'.*(\.so|\.py[cod]?)$')
51 import_re = re.compile(r'(?P<name>[a-zA-Z_][a-zA-Z0-9_]*?)'
52 r'(?P<package>[/\\]__init__)?'
53 r'(?P<suffix>%s)$' %
54 r'|'.join(re.escape(s[0]) for s in imp.get_suffixes()))
52
55
53 # RE for the ipython %run command (python + ipython scripts)
56 # RE for the ipython %run command (python + ipython scripts)
54 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
57 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
@@ -66,36 +69,35 b' def module_list(path):'
66 if path == '':
69 if path == '':
67 path = '.'
70 path = '.'
68
71
69 if os.path.isdir(path):
70 folder_list = os.listdir(path)
71 elif path.endswith('.egg'):
72 try:
73 folder_list = [f for f in zipimporter(path)._files]
74 except:
75 folder_list = []
76 else:
77 folder_list = []
78
79 if not folder_list:
80 return []
81
82 # A few local constants to be used in loops below
72 # A few local constants to be used in loops below
83 isfile = os.path.isfile
84 pjoin = os.path.join
73 pjoin = os.path.join
85 basename = os.path.basename
86
87 def is_importable_file(path):
88 """Returns True if the provided path is a valid importable module"""
89 name, extension = os.path.splitext( path )
90 return import_re.match(path) and py3compat.isidentifier(name)
91
74
92 # Now find actual path matches for packages or modules
75 if os.path.isdir(path):
93 folder_list = [p for p in folder_list
76 # Build a list of all files in the directory and all files
94 if any(isfile(pjoin(path, p, '__init__' + suffix[0])) for
77 # in its subdirectories. For performance reasons, do not
95 suffix in imp.get_suffixes())
78 # recurse more than one level into subdirectories.
96 or is_importable_file(p) ]
79 files = []
80 for root, dirs, nondirs in os.walk(path):
81 subdir = root[len(path)+1:]
82 if subdir:
83 files.extend(pjoin(subdir, f) for f in nondirs)
84 dirs[:] = [] # Do not recurse into additional subdirectories.
85 else:
86 files.extend(nondirs)
97
87
98 return [basename(p).split('.')[0] for p in folder_list]
88 else:
89 try:
90 files = list(zipimporter(path)._files.keys())
91 except:
92 files = []
93
94 # Build a list of modules which match the import_re regex.
95 modules = []
96 for f in files:
97 m = import_re.match(f)
98 if m:
99 modules.append(m.group('name'))
100 return list(set(modules))
99
101
100 def get_root_modules():
102 def get_root_modules():
101 """
103 """
General Comments 0
You need to be logged in to leave comments. Login now