[Distutils] Enumerating packages in a distribution

Celvin read.beyond.data at gmx.net
Fri Dec 4 05:41:09 CET 2009


Hi,

I recently implemented a plugin system using setuptools, and now I'm wondering
how to enumerate the packages contained in a given Distribution object.


What I'm doing is basically this:
I use egg files as self-describing plugins to allow users to distribute
plugins containing several packages, with each package providing a set
of functions that are registered and loaded into a GUI's menu at
runtime.

The way each function in a given package is displayed in the GUI is
encoded into a XML file that is distributed with each package, i.e. users
can add several packages to the distribution, declare the entry points and
create an XML file that will describe each package's menu structure like
this:


foo/
  foo/
    __init__.py
    setup.py
    package1/
      menu.xml
      functions1.py
    package2/
      menu.xml
      functions2.py

    ...


Accessing the XML files via pkg_resource.resource_stream(__name__, "menu.xml")
works just fine from inside functionX.py, but since the combination of
working_set.find_plugins and working_set.iter_entry_points returns a
loose list of EntryPoint instances without preserving the package structure,
I have trouble mapping an EntryPoint to the matching menu.xml file,
while only parsing the XML file just once and not for each entry
point.

In the past, I used the module_name attribute of the EntryPoint
instances to build a path to be used with the IResourceProvider
API implemented in each Distribution instance like this:

dist.get_resource_stream(__name__, "%s/menu.xml" %
(entry_point.module_name, ))

...but since the user creating a plugin is free to nest the package
further, the module_name might not be identical to the package name
where the menu.xml file is stored, and I'm hesitant to just split
module_name at the dots and just use the first part, as it somehow
"doesn't feel right".

The whole thing somehow seems overly complicated, given that I'm
implementing it in Python, so maybe I'm artificially complicating things.
If someone could provide some insights or simplify my plugin scheme, I
would really appreciate it.


Cheers,
Celvin



More information about the Distutils-SIG mailing list