Python Desktop: Configuring and Modularizing

I’ve got a little desktop application I’ve been building in Python, and a few of the tools I’ve come across are helpful, so here’s a short discussion of YAPSY, argparse, and ConfigParser.

I’ve got a little desktop application I’ve been building in Python, and a few of the tools I’ve come across are helpful, so here’s a discussion.

First, there’s YAPSY; to build plugins of discrete types into the application.  YAPSY comes pretty bare, which is great.  It gives you the essentials, and you build the functionality on based on what your plugins need to look like.

It’s versatile enough that reworking my plugin types after I already had a basic design wasn’t that hard.  The only mistake I think I’ve made so far was not deriving the plugins I have from the categories as much as possible (but improving that won’t be hard either).

One of the best things about using a plugin system is that it can be integrated with argparse, using ArgumentParser.add_argument_group(title, description).

That separates the commands per group when --help is invoked to give usage. But, the downside here is it doesn’t actually separate those commands in the parsed arguments. It’s a minor inconvenience to separate them. It also doesn’t magically namespace things for you, so coming up with a convention is important.

And that leads to the third component to discuss today: ConfigParser. ConfigParser lets you read from a configuration file.

This time, groups are no problem, as the file format is equivalent to a classic .ini file (ie, it has headed sections to it). But there’s still some work to handle the integration between arguments and configuration. There is some overlap, but there are some options that are found in one or the other, but not both.

For example, an option of which configuration file to use probably wouldn’t be specified in the configuration file itself, but only via the program arguments. Similarly, you wouldn’t want to have an argument for every option, so some options would only come from the configuration file.

Thus, having a single class that handles both configuration files and arguments is useful. First, it gets the data from both, then it unifies the overlap, providing all three sets of options to the program as a whole (some of which go to the program, some to the plugins).


One response to “Python Desktop: Configuring and Modularizing”

  1. […] une mystérieuse application de bureau […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.