<?xml version="1.0" encoding="iso-8859-1"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Comments on article "Python plugins continued : resources management"</title>
<link rel="alternate" type="text/html" href="http://base-art.net/Articles/65/" />
<updated>February 03, 2006 08:52 AM</updated>
<author>
  <name>Philippe Normand</name>
</author>
<id>urn:md5:11329</id>
<generator uri="http://pythonfr.org/">Alinea</generator>



<entry>
<title>Phil on Python plugins continued : resources management</title>
<author>
<name>Phil</name>
</author>
<link rel="alternate" type="text/html" href="http://base-art.net/Comments/184/"/>
<id>http://base-art.net/Comments/184/</id>
<updated>February 03, 2006 08:52 AM</updated>
<content type="html">
&lt;p&gt;Thanks for the tip Phillip ! The code looks better using this ;) I'll continue to play with setuptools, it's really helpful and a great step for Python packages distribution :)&lt;/p&gt;

</content>
</entry>

<entry>
<title>Phillip J. Eby on Python plugins continued : resources management</title>
<author>
<name>Phillip J. Eby</name>
</author>
<link rel="alternate" type="text/html" href="http://base-art.net/Comments/183/"/>
<id>http://base-art.net/Comments/183/</id>
<updated>February 02, 2006 10:15 PM</updated>
<content type="html">
&lt;p&gt;Quick tip: if you use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;entrypoint.module_name&lt;/span&gt;&lt;/tt&gt; as the first argument to the various &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;resource_foo()&lt;/span&gt;&lt;/tt&gt; APIs, your code won't be dependent on the plugin class' name being the same as the plugin egg's project name, and you won't have to mess around with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Requirement.parse()&lt;/span&gt;&lt;/tt&gt; for the use cases shown.  Instead, it will always look for resources relative to the package containing the plugin:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
for entrypoint in pkg_resources.iter_entry_points(&amp;quot;my.plugins&amp;quot;):
   plugin_class = entrypoint.load()
   print 'Plugin %s: %s' % (repr(entrypoint.name), repr(plugin_class))

   tmpl = 'data/foobar.tmpl'
     
   data_contents = pkg_resources.resource_listdir(entrypoint.module_name, 'data')
   print 'data contents : %s' % repr(data_contents)

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

       # contents of foobar.tmpl as a string
       print 'tmpl contents : %s' % repr(foo_bar)
   else:
       print '%s not found :-(' % repr(tmpl)
&lt;/pre&gt;
&lt;p&gt;Also, instead of bundling data with packages, you can bundle it in the egg-info directories and get access to it with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;entrypoint.dist.get_metadata()&lt;/span&gt;&lt;/tt&gt; and the like.  But that might be out of scope for this tutorial.&lt;/p&gt;
&lt;p&gt;Nice series, by the way, keep up the good work!&lt;/p&gt;

</content>
</entry>


</feed>
