[Qgis-developer] Python Plugin Repository - plugins app draft

Martin Dobias wonder.sk at gmail.com
Mon Nov 22 17:11:03 EST 2010


Hi Barry

On Mon, Nov 22, 2010 at 8:14 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> Hi guys,
>
>  I just had a quick look at the plugin repo spec. Cool.
>
>  It mentions putting plugin metadata in a config file rather than
> having it executable in __init__.py - I've been doing that for a while
> with my plugins. I have config.ini which looks like:
>
> [general]
> name: autosave
> version: 0.21
> description: save qgis projects as we go...
> author: Barry Rowlingson <b.rowlingson at lancaster.ac.uk>
> qgisMinimumVersion: 1.0
>
> and then __init__.py looks like this:
>
> import ConfigParser
> import os.path
> p = ConfigParser.ConfigParser()
> here = os.path.join(os.path.dirname(__file__),"config.ini")
> p.read(here)
>
> def name():
>  return p.get('general','name')
>
> [and so on for the other methods]

That's nice! I've been thinking about exactly this type of format. And
to make it even more awesome, I thought we could provide a method in
qgis.utils module with some dark magic that would inject the
__init__.py file with the metadata on the fly:

def parse_config(filename, dct):
  import ConfigParser
  import os.path
  p = ConfigParser.ConfigParser()
  p.optionxform = str  # don't convert keys to lower-case
  here = os.path.join(os.path.dirname(filename),"config.ini")
  p.read(here)
  inject(p.items('general'), dct)

def make_func(name, value):
    def innerfunc(): return value
    innerfunc.__name__ = name
    return innerfunc

def inject(items, dct):
  for name,value in items:
    dct[name] = make_func(name,value)

So that in __init__.py file you would end up with just:

import qgis.utils
qgis.utils.parse_config(__file__, globals())

# ... here comes the classFactory() method ...

> The advantage here is that it only uses standard python modules, the
> config file is easy for a human to edit (please no XML!), it's
> flexible (can have more than just the [general] section) and so on.

Exactly, thanks for pointing me to ConfigParser, I was nearly going to
create my own parser :)


>  Eventually I hope some kind of config.ini is mandated and I can
> delete the methods in __init__.py - but at the moment I can do either!

I think for 1.x releases we will have to provide the metadata in
__init__.py file - at least with that 2-lines wrapper. From 2.0 we
could start with cleaner __init__.py file.


>  A useful thing might be some python code to produce a config.ini from
> an existing __init__.py methods... Are there plugins with complex code
> in the __init__.py methods? I've just grepped all the __init__.py that
> I have in the qgis plugins I've installed and I can't find anything
> more complex than returning a string (except in my config.ini-driven
> plugins!).

Right - some methods for easier transition could be provided. Anyway,
it's crazy to return anything else than a string.

Cheers
Martin


More information about the Qgis-developer mailing list