Posts Tagged ‘python’

Mutt aka Bash Is Still King

December 5th, 2011 subogero 4 comments

I’ve had problems with sending emails from the command line before. On Windows. That’s how mapis was born. This time it’s the same story on Linux. I want to send an email from the command line with an attachment. The rather lovely Evolution does not support it.

In comes mutt, the even more lovely terminal based email client. But I don’t want to loose features:

  • Gmail IMAP access
  • integration with my Gmail contacts
  • Hungarian spell-check
  • mutt as my default mail client in Gnome, so Firefox uses it when Sending Link…

And while I’m at it, I want some new features as well:

  • editing email with vim (with Hungarian spell-check, obviously)
  • bonus: email desktop notifications, even when the client is not running


apt-get install mutt  # as root

My ~/.muttrc config file:

set from = "your@email"
set realname = "Your Name"
set imap_user = "your@email"
set imap_pass = "yourpassword"
set folder = "imaps://"
set spoolfile = "+INBOX"
set postponed = "+[Gmail],Drafts"
set trash = "imaps://[Gmail]/Trash"
set header_cache = ~/.mutt/cache/headers
set message_cachedir = ~/.mutt/cache/bodies
set certificate_file = ~/.mutt/certificates
set smtp_url = "smtp://your@email:587/"
set smtp_pass = "your_password"
set editor  = "vi"
set query_command = "goobook query '%s'"
set sort = "threads"
set sort_aux = "last-date-received"

color normal white default
color index white default ~p
color index white default ~P
color index white default ~O
color index brightwhite default ~N
color tree red default
color hdrdefault cyan default
color quoted green default
color tilde white default
color signature blue default
color attachment brightyellow default
color status brightwhite green
color indicator black white

You’ve probably noticed vi as the editor, some options that don’t let you miss new posts to old email threads and a colour scheme that fits well into Linux Mint. Also goobook is mentioned, which can access your Gmail  contacts from the command line. Homework: find the goobook website on the internet! Then download and extract the source tarball, run

sudo ./ install

Then create its config file ~/.goobookrc

email: your@email
password: your_password

As the above config files store your password, don’t forget to change their permissions:

chmod 700 ~/.muttrc ~/.goobookrc

As to the Hungarian spell-check in vi, add a line to ~/.vimrc which maps it to <F7>, like some office tools:

map <F7> :setlocal spell spelllang=hu<Enter>

The first time you activate it, it will download the Hungarian database, then highlight the misspelled words with red. Move around them with [s and ]s and let vim suggest correct spelling with the z= key combo. It kicks ass indeed.

Mutt as Gnome’s default email client? Main Menu (I don’t use the Mint Menu), System, Preferences, Preferred Applications. Mail Reader. Choose Custom, then specify “/usr/bin/mutt %s” as the command. Run in terminal of course. Test with Send Link… in Firefox.

Now all is well. Just try to send a file attachment from the command line.

mutt -a foo.txt

opens mutt, which in turn asks for the mail recipient. Just type a part of the name or email, hit Ctrl-t, and mutt will complete the email address from your Gmail Contacts. Then enter a subject and voilá, vi opens up to edit the mail body. Edit, spellcheck, ZZ, y. Done.

And now for something completely different. Desktop notifications for new email, even when no email client is running. I’ve found a nice mutt-Gmail tutorial blogpost somewhere, I forget where. It contained a Python script for email notifications. I opened it. I got scared. 200 lines of code! I nearly gave up. But after  taking a courageous look I realized it just parsed the Gmail atom feed and called Gnome’s notify-send. Using a class and using an XML parsing module.

Python is a rather lovely language, but I have to say its adherents are famous for writing bloated code. I bet I can write a 10-line shell script for that. I ended up with 11 lines, but including the shebang and a configurable checking period. It’s among my Gnome Startup Applications:

SLEEP=`echo $1 | egrep '^[1-9][0-9]*$'`
[ -z "$SLEEP" ] && SLEEP=300
USER=`sed -rn 's/^set imap_user = "(.+)"$/\1/p' ~/.muttrc`
PASS=`sed -rn 's/^set imap_pass = "(.+)"$/\1/p' ~/.muttrc`
while true; do
 EMAILS=`curl -u ${USER}:${PASS} \
 | sed -rn 's/<name>(.+)<\/name>$/\1/p'`
 [ -n "$EMAILS" ] && notify-send "NEW EMAIL" "$EMAILS"
 sleep $SLEEP

Bash is still king.

Categories: Uncategorized Tags: , , , ,

Dive into Python

January 5th, 2010 subogero Comments off


I am very interested in the chronology of Biblical events (anything in the Bible, actually).
Problem: the Bible refers to the time of events always relative to other events. When was the Big Flood relative to Adam’s creation? All we are told is it happened when Noah was 600 years old.

The goal:

  • Create a program that calculates the time of biblical events relative to a single event.
  • The events shall be entered as specified in the Bible, that is, relative to other events.
  • Events should have an optional duration (e.g. the life of Methuselah).
  • Print the result in a nice table-like format
  • Create a timeline diagram

The means: Python

Events are entered into an xml-file, specifying a reference event and a start-time relative to that, and an optional duration. There must be a root event without a reference event, obviously. It looks like this:

  <event name="Adam" duration="930"/>
  <event name="Seth" ref="Adam" start="130" duration="912"/>
  <event name="Enos" ref="Seth" start="105" duration="905"/>
  <event name="Kenan" ref="Enos" start="90" duration="910"/>
  <event name="Mahalalel" ref="Kenan" start="70" duration="895"/>
  <event name="Jared" ref="Mahalalel" start="65" duration="962"/>
  <event name="Enoch" ref="Jared" start="162" duration="365"/>
  <event name="Methuselah" ref="Enoch" start="65" duration="969"/>
  <event name="Lamech" ref="Methuselah" start="187" duration="777"/>
  <event name="Noah" ref="Lamech" start="182" duration="950"/>
  <event name="Shem" ref="Noah" start="502" duration="600"/>
  <event name="Ham" ref="Noah" start="502"/>
  <event name="Japhet" ref="Noah" start="502"/>
  <event name="Flood" ref="Noah" start="600" duration="1"/>

The xml file is passed to the Python program as an argument, which parses it using the xml.dom.minidom package. An optional argument specifies the name of the event at zero years, otherwise everything is calculated from the root event (Adam).

$ ./ BibleTime.xml Adam

We get this on the console:

       Adam     0   930
       Seth   130  1042
       Enos   235  1140
      Kenan   325  1235
  Mahalalel   395  1290
      Jared   460  1422
      Enoch   622   987
 Methuselah   687  1656
     Lamech   874  1651
       Noah  1056  2006
       Shem  1558  2158
        Ham  1558
     Japhet  1558
      Flood  1656  1657

If you call, you also get a timeline window using the Tk-binding of Python, the Tkinter package:



Before, I did not know Python, never made GUI programs and never used XML. This program took me less than one day to develop. Any questions?

The source code: bible.7z