Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Simple example of how to use importlib to create a loader

Reply
Thread Tools

Simple example of how to use importlib to create a loader

 
 
Paul Moore
Guest
Posts: n/a
 
      03-26-2013
I'm trying to write my own loader using importlib. And frankly, I'm getting nowhere. I'm struggling to understand precisely which methods of the various ABCs I need to implement, and in some cases what they should do.

Could anyone point me to a simple example (say, something that implements zip imports using the zipfile module - that was what I initially tried to get working as a proof of concept).

Where I've got to is the following stub implementation to try to log the sequence of calls:

from importlib.abc import PathEntryFinder, SourceLoader

class MyFinder(PathEntryFinder):
def __init__(self, pathentry):
"""Create a finder for the given `pathentry`"""
if pathentry == 'foo':
return
raise ImportError
def find_loader(self, fullname):
"""Return a loader for the module `fullname`"""
print("find_loader('{}')".format(fullname))
return MyLoader(), ['foo']

class MyLoader(SourceLoader):
def module_repr(self):
print("module_repr()")
return True
def is_package(self, fullname):
print("is_package('{}')".format(fullname))
return True
def get_data(self, path):
"""Return an open binary file object for `path`"""
print("get_data('{}')".format(path))
return b"print('hello from foo!')"
def get_filename(self, fullname):
"""Return the filename for module `fullname`"""
print("get_filename('{}')".format(fullname))
return '/this/is/foo'

if __name__ == '__main__':
import sys
sys.path_hooks.append(lambda p: MyFinder(p))
sys.path.insert(0, 'foo')
import a
import a.b
from a.b.c import x


It prints out

>py .\loader.py

find_loader('a')
is_package('a')
get_filename('a')
get_data('/this/is/foo')
get_filename('a')
is_package('a')
hello from foo!
Traceback (most recent call last):
File ".\loader.py", line 46, in <module>
import a.b
ImportError: No module named 'a.b'

which is a good start, but I don't know why it isn't willing to check for 'a.b' (after all, I tell it that 'a' is a package).

I'm quite probably doing something trivially wrong here - after all, I'm not even *trying* to implement a sane view of a package structure here. But I'd have expected at least a find_loader('a.b') call...

Thanks for any help,
Paul.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
importlib: what is it, and why is it important? excord80 Python 1 02-01-2009 04:08 AM
Library class loader (setting system class loader parent) Karsten Wutzke Java 0 06-29-2007 04:44 AM
Delete GRUB loader and use boot.ini Use.Netuser.ntl Computer Support 2 08-10-2005 08:31 AM
How to use this simple XML example to display? Grace Tsai XML 3 04-01-2005 09:43 PM



Advertisments