Squeezing the sponge: How to implement a buffering SMTP Handler in Python

…when he needs what you have gleaned, it is but squeezing you,
and, sponge, you shall be dry again.
– Hamlet

One of the first things I implement in most Python projects is some quick stream logging so as not to dot too many print statements across the code. At some point file logging usually  follows as a feature so it can run by itself and I can check up on it from time to time.

Recently I got ambitious and added email logging to poca, my podcast client project. Which isn’t difficult as the logging module has an SMTPHandler built-in that you just stick on your logger and now your logs are airborne. It’s almost xkcd-like. The way it works though is that an email is sent off instantly every time something is logged, just like stream logging only over port 25. This was not what I wanted. To be specific I was looking for something that

  • Would gather up log entries as my script runs it course
  • At the end of the script it would evaluate if there was anything worthwhile sending
  • If so, send it; if not, save, don’t send

The way to do, as suggested by the logging module author himself, is to subclass the BufferingHandler which does a fine job of soaking up the entries and keeping them until its time to release them. He has written an example of how to do this himself and put it on Github.

I had, however, a further requirement. When a podcast feed fails, it’s not necessarily cause for alarm or notifying the user. Maybe the server’s in maintenance mode for a few minutes, maybe builders unplugged the cable for an hour, maybe you did. You only really need email logging to tell you when a podcast feed consistently fails because then it’s probably either a software failure or the feed is offline, either of which requires user intervention. Vinay Sajip’s script doesn’t allow for that. Also, using unicode is easier if we keep a clear distinction between body and headers which Sajip’s approach seems to fudge a bit.

My requirements meant that I would have to not just buffer entries in memory but save them over time and only release them when there was a sufficient number (and of sufficient severity) to worry about.


How to get domain-specific user stylesheets in Epiphany

Epiphany isn’t really at the top of my browser list. I guess I should say ‘Web’ as it’s now called but a) nobody can google that and b) if you open the Gnome acitivities overview and type web you still get Firefox first (quick tip: type ‘epi’ and you’ll get it). Still, it has a nice interface that fits right into GNOME and it does come with basics amenities, like adblocking and a somewhat rudimentary user style sheet support.

Sadly, this is no Stylish replacement. You simply type rules in one after the other with no specification of where to apply them and where not to. So if you want to get rid of the #rightcolumn on site A but not the similarly named #rightcolumn on site B you’re out of luck. Well, you can of course jump through hoops, specifying site specific hierarchies and nesting.  Not exactly an nice solution. Here’s one I like better.