Open amogorkon opened 3 years ago
Would we need to add something to sys.meta_path to enable us to dynamically download and resolve these imports, probably at the very end of the list so that any possible local module would be preferred to the online version?
The added resolver would probably have the module import 'root' as a Path as an attribute of the resolver?
Clarifying my above comment with some API details:
Our special resolver is actually a finder
with one method:
def find_module(abs_name: str, parent_pkg_path: str = None) -> loader:
find_module()
method is called at least with the absolute name of the module being imported.package
then the parent package’s __path__
attribute is passed in as a second argument.None
if the module cannot be found, else returns a loader
. Hmm.. initially, I wasn't thinking of relative import
from online resources, but I won't complain if it's possible to adapt the mechanism from use(Path("foo.py"))
to import foo
, just keep in mind that use(Path(..))
is more general than import
, so we should make the mechanism work for that first and go from there.
Which means that I think that the aliases should live in Use(), not sys.meta_path primarily
With import_to_use #31 proposed, I think this one should be named "path_to_url" to make the distinction perfectly clear.
@thirteenpylons have fun and feel free to experiment ;) we could even introduce a new case for github urls like use(use.GIT("amogorkon/justuse"))
or somesuch if it helps.
use() on a github URL works for vanilla modules (no or only builtin imports), however it would and should not work "just like that" if classical relative imports or use() with a Path is applied to import modules from the same project/package. Attempting to resolve those relative paths could result in internal version conflicts or worse, in hijacked code. Instead, there should be an exception if attempting to do so, a guess at what the user might mean and supplying the user with that info (guessed_url, hash_algo, hash_valu) for them to insert into their calling code. This insertion should be done via an alias mapping that includes the source name, the path that should be mapped to an URL with the corresponding hashing details. Now, when use() is used from within the specified source, it should try to look up the path and map it to a call to URL.