![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I've been trying python recently as a replacement for my perl scripts. Mostly because I don't like how perl looks, yes stupid reason I know.
To start some things annoy me. To convert a string into a number you have to do int('2'), but then they tell you everything is an object, so to append an item to a list you do aList.append(2). So why not '2'.int()?
Then I explore the wonderful library. It covers a lot, but immediately I run into problems: there is a library providing maildir support. But the only method it understands is next() and it only returns an email object. Nice if you want to look at your emails, but not if like me you want to write an archiving script(!). So in the end I return to iterating over the files.
All in all I get the feeling of "almost" and "nearly". The few Common Lisp libraries available are much better in quality, but most of the time lacking in documentation. Elegant, documented, broad: select 2 :-(.
For you reading pleasure (and amusement) here is the archive scripts:
It moves all the files in my maildir tree (Except those in the main INBOX) into folder with .. appended. It works for me.
To start some things annoy me. To convert a string into a number you have to do int('2'), but then they tell you everything is an object, so to append an item to a list you do aList.append(2). So why not '2'.int()?
Then I explore the wonderful library. It covers a lot, but immediately I run into problems: there is a library providing maildir support. But the only method it understands is next() and it only returns an email object. Nice if you want to look at your emails, but not if like me you want to write an archiving script(!). So in the end I return to iterating over the files.
All in all I get the feeling of "almost" and "nearly". The few Common Lisp libraries available are much better in quality, but most of the time lacking in documentation. Elegant, documented, broad: select 2 :-(.
For you reading pleasure (and amusement) here is the archive scripts:
import re,email,string,time,os,dircache,commands from email.Utils import parsedate_tz, parsedate year, month, day, hour, minute, seconds, hun, tz, dls = time.localtime() dateLimit = (year*100)+month-2 nilPattern = re.compile('.*NIL.*') basedir = '/home/pvaneynd/Maildir/' sorted = {} for foldername in dircache.listdir(basedir): if foldername[0] != '.': continue if foldername[1] == '.': continue if string.find(foldername[2:],'.') != -1: continue dir = basedir + foldername + '/cur/' for filename in dircache.listdir(dir): try: fd = PrivoxyWindowOpen(dir + filename,'rb') except: print 'Could not open file ' + dir + filename continue try: message = email.message_from_file(fd) except: print 'Parsing of header failed!\n' fd.close() continue fd.close() dateString = message['Date'] if not dateString: if message.get_unixfrom(): dateString = message.get_unixfrom() else: continue if nilPattern.search(dateString): print 'found NIL in the envelope, not touching it\n' continue try: date = parsedate_tz(dateString) except: data = None if not date: try: data = parsedate(dateString) except: data = None if not date: month = 0 if re.compile('Jan').search(dateString): month = 1 elif re.compile('Feb').search(dateString): month = 2 elif re.compile('Mar').search(dateString): month = 3 elif re.compile('Apr').search(dateString): month = 4 elif re.compile('May').search(dateString): month = 5 elif re.compile('Jun').search(dateString): month = 6 elif re.compile('Jul').search(dateString): month = 7 elif re.compile('Aug').search(dateString): month = 8 elif re.compile('Sep').search(dateString): month = 9 elif re.compile('Oct').search(dateString): month = 10 elif re.compile('Nov').search(dateString): month = 11 elif re.compile('Dec').search(dateString): month = 12 else: print 'Could not find a month in %s\n' % dateString continue year = re.compile('([12][0-9][0-9][0-9])').search(dateString) if year: try: year = int(year.group(1)) except: continue else: year = re.compile(' (9[4-9]) ').search(dateString) if year: try: year = int(year.group(1)) year = year + 1900 except: print 'Could not emergency parse year %s ' % year.group(1) continue else: print 'Could not parse year %s' % year.group(1) continue date = [ year, month] #else: ##print 'parsedate worked' year = date[0] month = date[1] #print '-> %d %d\n' % (month, year) if (year < 100): year = year + 1900 if ((year*100)+month) <= dateLimit: folder = basedir + foldername + ( '.%d.%d/' % (year,month)) if not sorted.has_key(folder): sorted[folder] = [ (basedir + foldername + '/cur/' + filename, filename) ] else: sorted[folder].append( (basedir + foldername + '/cur/' + filename, filename) ) for subfolder in sorted.keys(): print 'I want to create the subfolder "%s"\n' % subfolder print 'and move these messages to it: "%10s"\n' % sorted[subfolder] (status, output) = commands.getstatusoutput('/usr/bin/maildirmake "' + subfolder + '"') if status != 0: print 'Got error from maildirmake: %s message: %s' % (status, output) continue else: for (orig, name) in sorted[subfolder]: print 'renaming: ' + orig + ' to ' + subfolder + "cur/" name os.rename(orig, subfolder + "cur/" + name )
It moves all the files in my maildir tree (Except those in the main INBOX) into folder with .