The Artima Developer Community
Sponsored Link

Python Buzz Forum
Python plugins continued : resources management

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Philippe Normand

Posts: 78
Nickname: philn
Registered: Aug, 2005

Philippe Normand is a developer
Python plugins continued : resources management Posted: Feb 2, 2006 10:11 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Philippe Normand.
Original Post: Python plugins continued : resources management
Feed Title: Base-Art / Python
Feed URL: http://base-art.net/Sections/5/rss.xml
Feed Description: Phil's blog
Latest Python Buzz Posts
Latest Python Buzz Posts by Philippe Normand
Latest Posts From Base-Art / Python

Advertisement

Plugin developers often want to supply data files (web templates, images, etc) with their package, let's see how to do this using setuptools resource management system.

If there are still some lazy people reading this, get an updated version of the code. So, on your plugin's setup.py, add a package_data argument to setup():

setup(
  # ...
  package_data = {
     'foo_plugin': ['data/*.tmpl'],
  },
  # ...
)

Create a data directory in foo's plugin directory, and create some files:

$ mkdir plugins/foo/data
$ echo "Hello this is foo\n" > plugins/foo/data/foobar.tmpl

Don't forget to re-run python setup.py develop in the foo plugin directory. The next step is to fetch those data files via our plugin_loader:

for entrypoint in pkg_resources.iter_entry_points("my.plugins"):
    plugin_class = entrypoint.load()
    print 'Plugin %s: %s' % (repr(entrypoint.name), repr(plugin_class))

    requirement = pkg_resources.Requirement.parse(plugin_class.__name__)
    tmpl = 'data/foobar.tmpl'
      
    data_contents = pkg_resources.resource_listdir(requirement, 'data')
    print 'data contents : %s' % repr(data_contents)

    # let's find and read a data file in our plugin
    if pkg_resources.resource_exists(requirement, tmpl):    
        foo_bar = pkg_resources.resource_string(requirement, tmpl)

        # contents of foobar.tmpl as a string
        print 'tmpl contents : %s' % repr(foo_bar)
    else:
        print '%s not found :-(' % repr(tmpl)

Given the plugin's name, we tell pkg_resources to find the plugin's resources given their relative location from the plugin point of view. That's fun, no ? You don't need to care about a global variable storing the full path of the data files, or whatever crappy monkey-code !

I promise, next time i'll start playing with plugin API management, probably with interfaces :-)

Read: Python plugins continued : resources management

Topic: Java is a pain in the ass (or is it just eBay's Java SDK?) Previous Topic   Next Topic Topic: Python plugins egg cooking mini-howto

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use