[Zconfig] Re: [pymon] python eggs and ZConfig

Duncan McGreggor duncan.mcgreggor at gmail.com
Mon Nov 14 12:33:20 EST 2005


On Nov 14, 2005, at 9:58 AM, Phillip J. Eby wrote:

> At 10:40 AM 11/14/2005 -0500, Fred L. Drake, Jr. wrote:
>
>> It looks like you need to mark those files as "eager resources" or 
>> some such;
>> I'm not sure of the exact spelling and terminology.
>
> That's certainly an option (eager_resources in setup()).  Of course, 
> if you use a custom URL scheme for package-relative files, then you 
> can use resource_string or resource_stream instead of 
> resource_filename, and never have to unpack the config files to the 
> filesystem in the first place.
>
> PEAK's ZConfig support does this with a 'pkgfile:' URL scheme.  More 
> precisely, PEAK's ZConfig support just uses PEAK's URL resolution 
> machinery, which includes support for a pkgfile: scheme.  But you 
> could also just support pkgfile: URLs natively in ZConfig.  If you're 
> not extracting such files to the filesystem, you get the additional 
> advantage of being able to do it without the pkg_resources module.
>
> For example:
>
> try:
>     from pkg_resources import resource_string
> except ImportError:
>     def resource_string(module_name, path):
>         __import__(module_name)
>         module = sys.modules[module_name]
>         path = 
> os.path.join(os.path.dirname(module.__file__),*path.split('/'))
>         if hasattr(module,'__loader__):
>              return module.__loader__.get_data(path)
>         else:
>              f = open(path,'rb')
>              data = f.read()
>              f.close()
>              return data
>
> (Or the analagous code for resource_stream by wrapping get_data in a 
> StringIO.)
>
> Anyway, if you use this approach with pkgfile: URLs you're basically 
> good to go; the rest of ZConfig works beautifully with PEAK's pkgfile: 
> URLs for this stuff, although I don't think I've done anything with it 
> since ZConfig 2.0.
>
> Also - an important point!  If you use resource_string() or 
> resource_stream() (or hand-rolled equivalents thereof), your code will 
> work with *any* zipfiles, not just eggs!  This is handy for 
> py2exe-packaged files.  It's only resource_filename() that needs the 
> full egg runtime support, because of the cache directory and the need 
> for unique names within it.  If you just use data directly from a 
> zipfile, it's pretty simple (as you can see above).

Thanks Phillip -- this is fantastic!



More information about the ZConfig mailing list