* On 18 Nov 2012, Jamie Paul Griffin wrote: > Hi David, et al > > I wondered if you would mind showing me how I could use your python > code above to implement it into Gary's idea for creating the mail > Expiry date macro? > > I am a total python beginner and so can't quite make sense of what i > need to do to achieve what i need.
This is a quick hack and untested beyond the basics, but feel free to work from it. It is, or should be, a complete reimplementation of Gary's script in Python. #!/usr/bin/env python import os import sys import time try: from parsedatetime.parsedatetime import Calendar except ImportError: p = os.path.basename(sys.argv[0]) print >>sys.stderr, '%s: please install the parsedatetime module' % p sys.exit(255) def fmtdate(spec): '''Generate an rfc822 (GMT) time strong for a spec provided in the arguments. parsedatetime doesn't know anything about timezones, so the mktime and gmtime are just to adapt the struct_time value from c.parse() from local time to GMT, so that the RFC822 address can assume it. This lets the script work for anyone, without needing to calculate a zone offset for your locale. ''' rfc822gmt = '%a, %d %b %Y %H:%M:%S -0000' c = Calendar() st, flag = c.parse(spec) t = time.mktime(st) tm = time.gmtime(t) return time.strftime(rfc822gmt, tm) def main(args): defaultdate = 'today + 20 days' sys.stdout.write('Expiry date ["%s", "never" to remove]: ' % defaultdate) response = sys.stdin.readline() if response == '': # eof, ctrl-D return 10 spec = response.strip() if spec == '': # use default spec = defaultdate if spec.lower() == 'never': # remove header cmd = 'formail -i "Expires:"' else: date = fmtdate(spec) cmd = 'formail -i "Expires: %s"' % date if len(args): # if filename given, read from filename fp = open(args[0], 'r') data = fp.read() else: # else stdin data = sys.stdin.read() # write all data to pipe, read results back cin, cout = os.popen2(cmd) cin.write(data) cin.close() data = cout.read() cout.close() if len(args): # if filename given, read from filename fp = open(args[0], 'w') fp.write(data) fp.close() else: # write to stdout sys.stdout.write(data) return 0 if __name__ == '__main__': sys.exit(main(sys.argv[1:])) -- David Champion • d...@bikeshed.us