You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The simplest way to make your custom importer, should also be the easiest : reusing existing classes directly should not come with surprises.
We should integrate the pattern I use in most FileFinder even if I they dont do much, only to integrate with existing python and work around some pitfalls :
classMyFileFinder(FileFinder):
def__init__(self, path, *loader_details):
super(MyFileFinder, self).__init__(path, *loader_details)
def__repr__(self):
return'MyFileFinder({!r})'.format(self.path)
@classmethoddefpath_hook(cls, *loader_details):
"""A class method which returns a closure to use on sys.path_hook which will return an instance using the specified loaders and the path called on the closure. If the path called on the closure is not a directory, or doesnt contain any files with the supported extension, ImportError is raised. This is different from default python behavior but prevent polluting the cache with custom finders """defpath_hook_for_MyFileFinder(path):
"""Path hook for importlib.machinery.FileFinder."""ifnot (os.path.isdir(path)):
raiseImportError('only directories are supported')
exts= [xforldinloader_detailsforxinld[1]]
ifnotany(fname.endswith(ext) forfnameinos.listdir(path) forextinexts):
raiseImportError(
'only directories containing {ext} files are supported'.format(ext=", ".join(exts)))
returncls(path, *loader_details)
returnpath_hook_for_MyFileFinderdeffind_spec(self, fullname, target=None):
""" Try to find a spec for the specified module. :param fullname: the name of the package we are trying to import :return: the matching spec, or None if not found. """# We attempt to load a .my file as a moduletail_module=fullname.rpartition('.')[2]
base_path=os.path.join(self.path, tail_module)
forsuffix, loader_classinself._loaders:
full_path=base_path+suffixifos.path.isfile(full_path): # maybe we need more checks here (importlib filefinder checks its cache...)returnself._get_spec(loader_class, fullname, full_path, None, target)
# Otherwise, we try find python modulesreturnsuper(MyFileFinder, self).find_spec(fullname=fullname, target=target)
[...]
There are probably similar changes that would be useful in PathFinder. I haven't used it enough yet to know which ones would be useful.
The text was updated successfully, but these errors were encountered:
For clarity : This issue is about testing the behavior when one makes a custom importer (without defining his own class).
It is important, especially in this case, to add tests before starting to change existing behavior.
The simplest way to make your custom importer, should also be the easiest : reusing existing classes directly should not come with surprises.
We should integrate the pattern I use in most FileFinder even if I they dont do much, only to integrate with existing python and work around some pitfalls :
There are probably similar changes that would be useful in PathFinder. I haven't used it enough yet to know which ones would be useful.
The text was updated successfully, but these errors were encountered: