comparison roundup/pygettext.py @ 8082:a4127d7afaa9

fix: remove references to imp in untouched code CI runs flake8 to check for syntax errors and it threw errors on old imp.XYZ code that was not actually called. It looks like the code was only called when resolving modules like ssl, _ssl, importlib, importlib.resources .. rather than files/directory paths. In Roundup's use case it only runs over files/directories.
author John Rouillard <rouilj@ieee.org>
date Sat, 13 Jul 2024 21:15:50 -0400
parents d1c29284ccd9
children 60e92a540ca7
comparison
equal deleted inserted replaced
8081:95f91b6f0386 8082:a4127d7afaa9
267 267
268 Return the file system path to a file for a module, and to a directory for 268 Return the file system path to a file for a module, and to a directory for
269 a package. Return None if the name is not found, or is a builtin or 269 a package. Return None if the name is not found, or is a builtin or
270 extension module. 270 extension module.
271 """ 271 """
272 # split off top-most name 272 pathname = None
273 parts = dotted_name.split('.', 1) 273 r = importlib.util.find_spec(dotted_name, pathlist)
274 274
275 if len(parts) > 1: 275 if r.loader.is_package(dotted_name):
276 # we have a dotted path, import top-level package 276 pathname = r.submodule_search_locations[0]
277 try: 277 elif issubclass(r.loader.__class__,(importlib.abc.SourceLoader)):
278 file, pathname, description = importlib.find_module(parts[0], pathlist) 278 pathname = r.origin
279 if file: file.close()
280 except ImportError:
281 return None
282
283 # check if it's indeed a package
284 if description[2] == imp.PKG_DIRECTORY:
285 # recursively handle the remaining name parts
286 pathname = _get_modpkg_path(parts[1], [pathname])
287 else:
288 pathname = None
289 else:
290 # plain name
291 try:
292 file, pathname, description = imp.find_module(
293 dotted_name, pathlist)
294 if file:
295 file.close()
296 if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]:
297 pathname = None
298 except ImportError:
299 pathname = None
300
301 return pathname 279 return pathname
302 280
303 281
304 def getFilesForName(name): 282 def getFilesForName(name):
305 """Get a list of module files for a filename, a module or package name, 283 """Get a list of module files for a filename, a module or package name,
323 # find all python files in directory 301 # find all python files in directory
324 list = [] 302 list = []
325 # get extension for python source files 303 # get extension for python source files
326 if '_py_ext' not in globals(): 304 if '_py_ext' not in globals():
327 global _py_ext 305 global _py_ext
328 _py_ext = [triple[0] for triple in imp.get_suffixes() 306 _py_ext = importlib.machinery.SOURCE_SUFFIXES
329 if triple[2] == imp.PY_SOURCE][0]
330 for root, dirs, files in os.walk(name): 307 for root, dirs, files in os.walk(name):
331 # don't recurse into CVS directories 308 # don't recurse into CVS directories
332 if 'CVS' in dirs: 309 if 'CVS' in dirs:
333 dirs.remove('CVS') 310 dirs.remove('CVS')
334 # add all *.py files to list 311 # add all *.py files to list

Roundup Issue Tracker: http://roundup-tracker.org/