Hi, all,
I'm trying to implement a simple plugin framework, with some
unexpected results. I'm using Python 2.3.4 on Windows 2000.
My script loader.py loads plugin packages from a directory (in this
case its own), and checks a variable defined in the package's
__init__.py for information on the plugin.
The directory in which loader.py sits is itself a package::
plugin_test
|
+ Plugin1
| |
| __init__.py
|
__init__.py
loader.py
I must be misunderstandin g how the imp module works, or something.
Although the correct package is returned by my helper method, its
member variables are those of the the parent package, plugin_test.
The output of this demo is::
<module 'Plugin1' from '__init__.pyc'>
Directory plugin_test
Which seems contradictory. What have I got wrong?
Best Regards,
Dave
<Code follows>
This is plugin_test/Plugin1/__init__.py::
dir = "plugin_tes t/Plugin1"
This is plugin_test/__init__.py::
dir = "plugin_tes t"
And here is the loader script, plugin_test/loader.py::
import os
import imp
def GetPackage(name , peerFile, relPath = '.'):
"""
Return the named Python package on a
relative path from the file given
"""
path = os.path.normpat h(
os.path.join(os .path.dirname(p eerFile), relPath))
try:
fObj, path, descr = imp.find_module (name, [path])
except ImportError:
return None
if descr[2] != imp.PKG_DIRECTO RY:
return None
try:
return imp.load_module (name, None, '', descr)
except:
return None
if __name__ == "__main__":
pkg = GetPackage("Plu gin1", __file__, '.')
print pkg
print "Directory ", pkg.dir
I'm trying to implement a simple plugin framework, with some
unexpected results. I'm using Python 2.3.4 on Windows 2000.
My script loader.py loads plugin packages from a directory (in this
case its own), and checks a variable defined in the package's
__init__.py for information on the plugin.
The directory in which loader.py sits is itself a package::
plugin_test
|
+ Plugin1
| |
| __init__.py
|
__init__.py
loader.py
I must be misunderstandin g how the imp module works, or something.
Although the correct package is returned by my helper method, its
member variables are those of the the parent package, plugin_test.
The output of this demo is::
<module 'Plugin1' from '__init__.pyc'>
Directory plugin_test
Which seems contradictory. What have I got wrong?
Best Regards,
Dave
<Code follows>
This is plugin_test/Plugin1/__init__.py::
dir = "plugin_tes t/Plugin1"
This is plugin_test/__init__.py::
dir = "plugin_tes t"
And here is the loader script, plugin_test/loader.py::
import os
import imp
def GetPackage(name , peerFile, relPath = '.'):
"""
Return the named Python package on a
relative path from the file given
"""
path = os.path.normpat h(
os.path.join(os .path.dirname(p eerFile), relPath))
try:
fObj, path, descr = imp.find_module (name, [path])
except ImportError:
return None
if descr[2] != imp.PKG_DIRECTO RY:
return None
try:
return imp.load_module (name, None, '', descr)
except:
return None
if __name__ == "__main__":
pkg = GetPackage("Plu gin1", __file__, '.')
print pkg
print "Directory ", pkg.dir
Comment