On 9/21/06, Guido van Rossum <[EMAIL PROTECTED]> wrote:
On 9/21/06, Paul Moore <[EMAIL PROTECTED]> wrote:
> On 9/21/06, Guido van Rossum <[EMAIL PROTECTED]> wrote:
> > I think one missing feature is a mechanism whereby you can say "THIS
> > package (gives top-level package name) lives HERE (gives filesystem
> > location of package)" without adding the parent of HERE to sys.path
> > for all module searches. I think Phillip Eby's egg system might
> > benefit from this.
>
> This is pretty easy to do with a custom importer on sys.meta_path.
> Getting the details right is a touch fiddly, but it's conceptually
> straightforward.
Isn't the main problem how to specify a bunch of these in the
environment? Or can this be done through .pkg files? Those aren't
cheap either though -- it would be best if the work was only done when
the package is actually needed.
Hmm, I wasn't thinking of the environment. I pretty much never use
PYTHONPATH, so I tend to forget about that aspect. I was assuming an
importer object with a "register(package_name, filesystem_path)"
method. Then register the packages you want in your code, or in
site.py.
I've attached a trivial proof of concept.
But yes, you'd need to consider the environment. Possibly just have an
initialisation function called at load time (I'm assuming the new hook
is defined in a system module of some sort - I mean when that system
module is loaded) which parses an environment variable and issues a
set of register() calls.
Paul.
import sys
import imp
class Importer(object):
def __init__(self):
self.registered_modules = {}
def register(self, fullname, filename):
self.registered_modules[fullname] = filename
def find_module(self, fullname, path=None):
if path is None:
return Loader(self)
class Loader(object):
def __init__(self, importer):
self.importer = importer
def load_module(self, fullname):
print "Trying", fullname
filename = self.importer.registered_modules.get(fullname)
if filename:
print fullname, "was registered - load from", filename
# Create a dummy for now, as I don't have time to code the full
# filesystem lookup.
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
mod.__file__ = "<%s>" % self.__class__.__name__
mod.__loader__ = self
mod.__path__ = [filename]
exec "pass" in mod.__dict__
return mod
importer = Importer()
sys.meta_path.append(importer)
# Put code here to register module/path pairs from an environment variable
# ...
if __name__ == '__main__':
# Trivial test
importer.register("a", "C:\\Whatever")
# C:\Whatever needs to contain a subdir b, containing __init__.py and c.py
import a.b.c
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com