Hi, everybody,
How can I go about writing an import hook that shows or hides (i.e.,
does not allow import of) modules within a package based on a defined
setting?
Here's a basic example of what I'm trying to do, given a package
/usr/lib/python2.3/site-packages/hidden/ that has three modules in it
(one.py, two.py, three.py):
[color=blue][color=green][color=darkred]
>>> ALLOWED_MODULES = ['one', 'two']
>>> from hidden import one
>>> from hidden import two
>>> from hidden import three[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named three[color=blue][color=green][color=darkred]
>>> ALLOWED_MODULES = ['one', 'two', 'three']
>>> from hidden import three
>>> # Because 'three' is in ALLOWED_MODULES , it worked.[/color][/color][/color]
The ALLOWED_MODULES setting wouldn't necessarily be changed at runtime;
I did that here just to demonstrate what I'm trying to do. I imagine
the import-hook code would do something like "from settings import
ALLOWED_MODULES " and take it from there.
Is this even possible? I'm on Python 2.3, if that helps.
Thanks,
Adrian
How can I go about writing an import hook that shows or hides (i.e.,
does not allow import of) modules within a package based on a defined
setting?
Here's a basic example of what I'm trying to do, given a package
/usr/lib/python2.3/site-packages/hidden/ that has three modules in it
(one.py, two.py, three.py):
[color=blue][color=green][color=darkred]
>>> ALLOWED_MODULES = ['one', 'two']
>>> from hidden import one
>>> from hidden import two
>>> from hidden import three[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named three[color=blue][color=green][color=darkred]
>>> ALLOWED_MODULES = ['one', 'two', 'three']
>>> from hidden import three
>>> # Because 'three' is in ALLOWED_MODULES , it worked.[/color][/color][/color]
The ALLOWED_MODULES setting wouldn't necessarily be changed at runtime;
I did that here just to demonstrate what I'm trying to do. I imagine
the import-hook code would do something like "from settings import
ALLOWED_MODULES " and take it from there.
Is this even possible? I'm on Python 2.3, if that helps.
Thanks,
Adrian