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.

(more…)

Setting default user session in GDM (default != latest)

Throughout most of my linux life I have had two desktops installed: Gnome and Openbox. The first for most everyday uses, the second specifically to create a no-nonsense environment for games. Openbox just works better for a lot of wine as well as native inux games – less screen-, keyboard and ressources-hogging etc.

So when I log out of Openbox it’s in order toget back to business. GDM sees it differently: You’re always set to log into the last desktop you logged out of. This is just plain wrong. To set it right I found this Fedora forum post pretty helpful. Here’s how I put it to use to set a default desktop for my user, regardless of what I was last logged in to.

(more…)

Stackexchange Answers: Systemd and PIDs

Systemd tracks PIDs. This is kind of obvious but I never thought to investigate before today. These two commands have been and I suspect, will be, very useful:

journalctl _PID = 1234

Get info on anything logged by the process in question. Technically, I suppose (?) it shouldn’t tell you anything that systemctl -u whatever.service doesn’t but it helped me get the exact error that caused the service not to work. Thanks, Rahul Khimasia.

systemctl status 1234

How exactly does pulseaudio get started on your desktop? Simply use htop to find the PID and ask systemctl for a status on the it and you will get told what systemd unit is associated with it. session-27.scope, it seems. Didn’t even know there was such a thing as ‘scopes’. Thanks, larsks.

 

A Systemd service unit for Redshift

Redshift is a small utility that gradually – as day turns into night – shifts the colour composition of the light from your monitor. The later it gets, the ‘warmer’ (i.e. redder) the colour becomes. This may sound odd but the effect is really pleasant and (literally) easy on the eyes. Once you’ve grown used to it, looking at a screen late at night you’ll really notice how harsh and cold the light will appear without it. I owe this discovery to Graham Morrison from Linux Voice. You can read more about it in Graham’s piece on page 78 in the now free issue 14 of Linux Voice (you should also subscribe to LV as it’s a really good magazine).

Redshift is the sort of thing you want to have running as soon as you log into your desktop but occasionally, you want it to turn it off. I occasionally play Spelunky into the wee hours of the night and running redshift while playing the game makes dark levels, well dark. Pitch black, more like. So a classic Gnome autostart file is not ideal as we can’t interact with it (apart from killing the resulting process). Redshift does come with a small tray utility that should allow for turning on and off but I really dislike tray litter and anyway Gnome’s trying to do away with this sort of thing. So systemd to the rescue.

Sadly the redshift package in Ubuntu 16.04 comes without a systemd .service file so I put together one myself.

(more…)

Link

“How To Satisfy Debian Dependencies Without Installing The Stupid Package”

I paid for a mindmapping program a couple of years back as I found the open source options somewhat lacking. The program uses java and the provided .deb has a flexible dependency on java 5 or 6, oracle-or-open-take-your-pick but later version of java – like the openjdk-8-jre package I have on my system – fail to satisfy. Installing the .deb was easy – just tell dpkg to –ignore-depends, brush your hands and think ‘job well done’. And then the next apt-get upgrade fails because there’s a missing dependency. I don’t get the logic here: I can’t upgrade packages A and B because completely unrelated package C has unresolved dependencies? However, the issue was resolved with the help of the satisfyingly titled post “How To Satisfy Debian Dependencies Without Installing The Stupid Package”.  Go check it out.

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.

(more…)

These go to 11

The PulseAudio daemon has the ability to set it’s niceness level after launch. This can be helpful if the process isn’t prioritised properly and sound cuts out now and again. The man page states:

nice-level= The nice level to acquire for the daemon, if  high-priority is  enabled.  Note:  on  some  distributions  X11  uses -10 by default. Defaults to -11.

Which makes sense, right? It’s one more, right. It’s not ten, it’s eleven. If you need that extra push, put it up to eleven. So that it gets a little more priority than the default process.

Oh come on, Lennart, that has to be a Spinal Tap joke, right?