##// END OF EJS Templates
Key the root modules cache by sys.path entries....
Antony Lee -
Show More
@@ -98,39 +98,44 b' def module_list(path):'
98 modules.append(m.group('name'))
98 modules.append(m.group('name'))
99 return list(set(modules))
99 return list(set(modules))
100
100
101
101 def get_root_modules():
102 def get_root_modules():
102 """
103 """
103 Returns a list containing the names of all the modules available in the
104 Returns a list containing the names of all the modules available in the
104 folders of the pythonpath.
105 folders of the pythonpath.
106
107 ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
105 """
108 """
106 ip = get_ipython()
109 ip = get_ipython()
107
110 rootmodules_cache = ip.db.get('rootmodules_cache', {})
108 if 'rootmodules' in ip.db:
111 rootmodules = list(sys.builtin_module_names)
109 return ip.db['rootmodules']
112 start_time = time()
110
111 t = time()
112 store = False
113 store = False
113 modules = list(sys.builtin_module_names)
114 for path in sys.path:
114 for path in sys.path:
115 modules += module_list(path)
115 try:
116 if time() - t >= TIMEOUT_STORAGE and not store:
116 modules = rootmodules_cache[path]
117 store = True
117 except KeyError:
118 print("\nCaching the list of root modules, please wait!")
118 modules = module_list(path)
119 print("(This will only be done once - type '%rehashx' to "
119 try:
120 "reset cache!)\n")
120 modules.remove('__init__')
121 sys.stdout.flush()
121 except ValueError:
122 if time() - t > TIMEOUT_GIVEUP:
122 pass
123 print("This is taking too long, we give up.\n")
123 if path not in ('', '.'): # cwd modules should not be cached
124 ip.db['rootmodules'] = []
124 rootmodules_cache[path] = modules
125 return []
125 if time() - start_time > TIMEOUT_STORAGE and not store:
126
126 store = True
127 modules = set(modules)
127 print("\nCaching the list of root modules, please wait!")
128 if '__init__' in modules:
128 print("(This will only be done once - type '%rehashx' to "
129 modules.remove('__init__')
129 "reset cache!)\n")
130 modules = list(modules)
130 sys.stdout.flush()
131 if time() - start_time > TIMEOUT_GIVEUP:
132 print("This is taking too long, we give up.\n")
133 return []
134 rootmodules.extend(modules)
131 if store:
135 if store:
132 ip.db['rootmodules'] = modules
136 ip.db['rootmodules_cache'] = rootmodules_cache
133 return modules
137 rootmodules = list(set(rootmodules))
138 return rootmodules
134
139
135
140
136 def is_importable(module, attr, only_modules):
141 def is_importable(module, attr, only_modules):
@@ -148,7 +148,7 b' class OSMagics(Magics):'
148 from IPython.core.alias import InvalidAliasError
148 from IPython.core.alias import InvalidAliasError
149
149
150 # for the benefit of module completer in ipy_completers.py
150 # for the benefit of module completer in ipy_completers.py
151 del self.shell.db['rootmodules']
151 del self.shell.db['rootmodules_cache']
152
152
153 path = [os.path.abspath(os.path.expanduser(p)) for p in
153 path = [os.path.abspath(os.path.expanduser(p)) for p in
154 os.environ.get('PATH','').split(os.pathsep)]
154 os.environ.get('PATH','').split(os.pathsep)]
General Comments 0
You need to be logged in to leave comments. Login now