[Distutils] setuptools, how to read metadata programatically?

Jorge Vargas jorge.vargas at gmail.com
Sat Jun 30 22:31:37 CEST 2007


On 6/30/07, Phillip J. Eby <pje at telecommunity.com> wrote:
> At 02:02 PM 6/30/2007 -0400, Jorge Vargas wrote:
> >hi
> >
> >I'm building a plugin manager on top of setuptools and I'll like to
> >know how could I read the  package metadata if I got it's EntryPoint.
>
> Use entrypoint.dist.get_metadata('PKG-INFO') to get the PKG-INFO file
> as a string.  See the PKG-INFO format PEPs to understand what's in there.
>
> Use entrypoint.dist.version to get the version, without needing to
> parse PKG-INFO.  (Well, it may get parsed for you, if it's a
> "develop" egg.  But the point is, you don't need to explicitly mess
> with PKG-INFO unless you want to get other data than the version.)
>
I went with the parser since I'm building a UI for users to
enable/disable plugins I n eed all the metadata.
>
> >basically what I need is get the author, version,etc. I found a
> >pkg_resources.EggMetadata but i'm not sure how to instantiate it
> >because I don't know what a "importer" means in this context.
>
> You don't need to instantiate those metadata classes; that's done for
> you automatically by functions like 'find_distributions()'.  The
> entrypoint.dist attribute will give you a distribution with the right
> type of metadata already attached.  Likewise, distribution objects
> obtained from a WorkingSet or Environment will be properly initialized as well.
>
great thanks for the pointer, I have  worked an attr based class which
turned out to be a bit more complicated than what I though in the
first place but it does the job nice (and not that cleanly) here is a
copy if someone is interested.

import pkg_resources
import StringIO
import rfc822

fields= ['name' ,'version' ,'platform' ,'summary' ,'description'
        ,'keywords' ,'home_page' ,'author' ,'author_email' ,'license']

class pkg_info_parsed(object):
    def __init__(self,dist,missingMsg=None):
        metadata=StringIO.StringIO(dist.get_metadata('PKG-INFO'))
        messages=rfc822.Message(metadata)
        print messages.items()
        for field in fields:
            if field in ['home_page','author_email']:
                prop=field.replace('_','-')
            else:
                prop=field
            value=messages.getheader(prop)
            if missingMsg:
                if not value or value == 'UNKNOWN':
                    value=missingMsg
            setattr(self,field,value)

one=pkg_resources.iter_entry_points('deluge.plugin').next()
metadata=pkg_info_parsed(one.dist,'Not avaliable')

print metadata.name
print metadata.version
print metadata.platform
print metadata.summary
print metadata.description
print metadata.keywords
print metadata.home_page
print metadata.author
print metadata.author_email
print metadata.license



>


More information about the Distutils-SIG mailing list