Skip to main content

Ralsina.Me — Roberto Alsina's website

Making your app modular: Yapsy

That a plug­in ar­chi­tec­ture for a com­plex app is a good idea is one of those things that most peo­ple kin­da agree on. One thing we don't quite agree is how the heck are we go­ing to make out app mod­u­lar?

One way to do it (if you are cod­ing python) is us­ing Yap­sy.

Yap­sy is awe­some. Al­so, yap­sy is a bit un­der­doc­u­ment­ed. Let's see if this post fix­es that a bit and leaves just the awe­some.

Up­date: I had not seen the new Yap­sy doc­s, re­leased a few days ago. They are much bet­ter than what was there be­fore :-)

Here's the gen­er­al idea be­hind yap­sy:

  • You cre­ate a Plug­in Man­ag­er that can find and load plug­ins from a list of places (for ex­am­­ple, from ["/us­r/share/ap­p­­name/­­plu­g­in­s", "~/.ap­p­­name/­­plu­g­in­s"]).

  • A plug­in cat­e­­go­ry is a class.

  • There is a map­ping be­tween cat­e­­go­ry names and cat­e­­go­ry class­es.

  • A plug­in is a mod­­ule and a meta­­da­­ta file. The mod­­ule de­fines a class that in­­her­its from a cat­e­­go­ry class, and be­­longs to that cat­e­­go­ry.

    The meta­­da­­ta file has stuff like the plug­in's name, de­scrip­­tion, URL, ver­­sion, etc.

One of the great things about Yap­sy is that it does­n't spec­i­fy too much. A plug­in will be just a python ob­jec­t, you can put what­ev­er you want there, or you can nar­row it down by spec­i­fy­ing the cat­e­go­ry class.

In fac­t, the way I have been do­ing the cat­e­go­ry class­es is:

  • Start with an em­p­­ty class

  • Im­­ple­­ment two plug­ins of that cat­e­­go­ry

  • If there is a chunk that's much alike in both, move it in­­­to the cat­e­­go­ry class.

But trust me, this will all be clear­er with an ex­am­ple :-)

I will be do­ing it with a graph­i­cal PyQt ap­p, but Yap­sy works just as well for head­less of CLI app­s.

Let's start with a sim­ple ap­p: an HTML ed­i­tor with a pre­view wid­get.

//ralsina.me/static/yapsy/editor1.jpeg

A sim­ple ed­i­tor with pre­view

Here's the code for the ap­p, which is re­al­ly sim­ple (it does­n't save or do any­thing, re­al­ly, it's just an ex­am­ple):

But this ap­pli­ca­tion has an ob­vi­ous lim­it: you have to type HTML in it. Why not type python code in it and have it con­vert to HTML for dis­play? Or Wi­ki markup, or re­struc­tured tex­t?

You could, in prin­ci­ple, just im­ple­ment all those mod­es, but then you are as­sum­ing the re­spon­s­abil­i­ty of sup­port­ing ev­ery thing-that-­can-be-­turned-in­to-HTM­L. Your app would be a mono­lith. That's where yap­sy en­ters the scene.

So, let's cre­ate a plug­in cat­e­go­ry, called "For­mat­ter" which takes plain text and re­turns HTM­L. Then we add stuff in the UI so the us­er can choose what for­mat­ter he wants, and im­ple­ment two of those.

Here's our plug­in cat­e­go­ry class:

Of course what good is a plug­in ar­chi­tec­ture with­out any plug­ins for it? So, let's cre­ate two plug­ins.

First: a plug­in that takes python code and re­turns HTM­L, thanks to pyg­ments.

See how it goes in­to a plug­ins fold­er? Lat­er on we will tell yap­sy to search there for plug­ins.

To be rec­og­nized as a plug­in, it needs a meta­da­ta file, too:

And re­al­ly, that's all there is to mak­ing a plug­in. Here's an­oth­er one for com­par­ison, which us­es do­cu­tils to for­mat re­Struc­tured Tex­t:

And here they are in ac­tion:

//ralsina.me/static/yapsy/editor2.jpeg

reSt mode

//ralsina.me/static/yapsy/editor3.jpeg

Python mode

Of course us­ing cat­e­gories you can do things like a "Tool­s" cat­e­go­ry, where the plug­ins get added to a Tools menu, too.

And here's the ap­pli­ca­tion code:

In short: this is easy to do, and it leads to fix­ing your ap­pli­ca­tion's in­ter­nal struc­ture, so it helps you write bet­ter code.

Full source code for ev­ery­thing.

I would be a sellout, but there's not much demand.

For Sale Portobello Mkt
For Sale Portobello Mkt by Jason Jones, under a CC-by-nc-sa license.

It would be nat­u­ral to any­one who does­n't know me to be­lieve I live a life of lux­u­ry, in­ter­na­tion­al trav­el and ex­ot­ic plea­sures [1] but there is a small thing I am kin­da ashamed of:

I hard­ly ev­er got paid to code.

Re­al­ly! Most of the mon­ey I live on has ab­so­lute­ly noth­ing to do with what­ev­er you read about on my blog.

I make my liv­ing do­ing things like in­stalling As­ter­isk in call cen­ter­s, or con­fig­ur­ing MySQL repli­ca­tion, or con­fig­ur­ing VP­Ns us­ing as­sort­ed piece-of-crap routers and by all means if you need that kind of work done, please con­tact Net Man­agers we are freaky good at it and charge rea­son­able rates.

But while I like that kind of thing [2] I like oth­er things bet­ter. I like pro­gram­ming much more than I like con­fig­ur­ing As­ter­isk!

Then again, I am not a re­al­ly great pro­gram­mer. I am just about an av­er­age coder, ex­cept that I am faster than most.

And late­ly, an in­ter­est­ing phe­nom­e­non has tak­en place.

How much mon­ey I earned thanks to my first 14 years of open source and free soft­ware de­vel­op­men­t? $0 [3]

How much mon­ey have I earned in the last year of FLOSS de­vel­op­men­t? $500 (and it is bound to be more than that soon).

To a first-­worlder that may seem like peanut­s, but down here in the land of cheap beef, that's... about 100 pounds of good meat! Or 10 nice din­ners in fine restau­rants for me and my wife. [4]

I am won­der­ing, is this part of a trend? Have oth­ers been re­ceiv­ing more ap­pre­ci­a­tion in the form of mon­ey late­ly?

In any case, it's a great feel­ing when a work of love (and trust me, noone does some­thing for 14 years for free if he does­n't love it) is ap­pre­ci­at­ed.

Just in case: this does­n't mean you have to pay me to get a bug fixed, or to get a fea­ture im­ple­ment­ed. How­ev­er, it does mean that if you like or use any of my pro­grams and want me to feel good about the time I spent writ­ing them... well, you can just send me a nice email, I will be just as hap­py. Re­al­ly!


Virtualenv and friends (video)

I am one of the speak­ers in the cy­cle of open python lec­tures at La Tribu in Buenos Aires, and here is the one where I speak about vir­tualen­v, build­out, nose, hud­son and oth­er things.

Long video!


Contents © 2000-2023 Roberto Alsina