MCPcopy Index your code
hub / github.com/bpython/bpython / find_modules

Method find_modules

bpython/importcompletion.py:169–239  ·  view source on GitHub ↗

Find all modules (and packages) for a given directory.

(self, path: Path)

Source from the content-addressed store, hash-verified

167 return None
168
169 def find_modules(self, path: Path) -> Generator[str | None, None, None]:
170 """Find all modules (and packages) for a given directory."""
171 if not path.is_dir():
172 # Perhaps a zip file
173 return
174 if any(fnmatch.fnmatch(path.name, entry) for entry in self.skiplist):
175 # Path is on skiplist
176 return
177
178 finder = importlib.machinery.FileFinder(str(path), *LOADERS) # type: ignore
179 try:
180 for p in path.iterdir():
181 if p.name.startswith(".") or p.name == "__pycache__":
182 # Impossible to import from names starting with . and we can skip __pycache__
183 continue
184 elif any(
185 fnmatch.fnmatch(p.name, entry) for entry in self.skiplist
186 ):
187 # Path is on skiplist
188 continue
189 elif not any(p.name.endswith(suffix) for suffix in SUFFIXES):
190 # Possibly a package
191 if "." in p.name:
192 continue
193 elif p.is_dir():
194 # Unfortunately, CPython just crashes if there is a directory
195 # which ends with a python extension, so work around.
196 continue
197 name = p.name
198 for suffix in SUFFIXES:
199 if name.endswith(suffix):
200 name = name[: -len(suffix)]
201 break
202 if name == "badsyntax_pep3120":
203 # Workaround for issue #166
204 continue
205
206 package_pathname = None
207 try:
208 with warnings.catch_warnings():
209 warnings.simplefilter("ignore", ImportWarning)
210 spec = finder.find_spec(name)
211 if spec is None:
212 continue
213 if spec.submodule_search_locations is not None:
214 package_pathname = spec.submodule_search_locations[
215 0
216 ]
217 except (ImportError, OSError, SyntaxError, UnicodeEncodeError):
218 # UnicodeEncodeError happens with Python 3 when there is a filename in some invalid encoding
219 continue
220
221 if package_pathname is not None:
222 path_real = Path(package_pathname).resolve()
223 try:
224 stat = path_real.stat()
225 except OSError:
226 continue

Callers 1

find_all_modulesMethod · 0.95

Calls 2

_LoadedInodeClass · 0.85
addMethod · 0.45

Tested by

no test coverage detected