Extending the import mechanism - what is recommended?
Steve Holden
steve at holdenweb.com
Tue Jan 29 14:36:03 EST 2008
dbr517 at gmail.com wrote:
> I need to extend the import mechanism to support another file type.
> I've already written the necessary C library to read the file and
> return a python code object.
>
> I found one example which just sub-classed imputil.ImportManager like
> this:
>
> from myLib import pye_code as pye_code
> class MyImporter(imputil.ImportManager):
> def __init__(self):
> imputil.ImportManager.__init__(self)
> self.add_suffix('.pye', self.import_pye)
> self.install()
>
> def import_pye(self, filepath, fileinfo, filename):
> data = pye_code(filepath)
> return 0, data, {}
>
> This actually works fine if the module is just a few lines of code,
> but it won't chain to the "built-in" importers; if the module that I'm
> importing does something as simple as 'import re', it fails.
>
> It may be that my confusion here is because (even after reading the
> code), I'm not clear on the purposes of imputil.ImportManager vs.
> imputil.Importer :-(
>
> What is the "preferred" way to do this type of extension? One other
> note; at this time, I just need to import individual module files with
> this extension; I don't need to import packages.
>
Here's an importer I wrote some time ago to bring modules in from a
relational database. I won't trouble you with the code to compile the
modules/packages and add them into the database, but perhaps the
attached code will be enough to show you what you are doing that's not
working.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: dbimp.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20080129/73312bcd/attachment-0001.ksh>
More information about the Python-list
mailing list