[ David Champion Wrote On Sun 18.Nov'12 at 16:32:32 GMT ]
> 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:]))
I was wondering if I could ask about this script, as I am having a few
problems but it may well be due to how i'm using it rather than the
script itself, such as with the macro i've set up.
For example: this morning I set a message to expire on December 1st by
entering:
"Dec 1" at the prompt but without the quotes.
Then, I tried to expire another message using exactly the same date but
it just hung. I stopped the script using ^Cc and tried again, this time
just pressing CR to use the default date "today + 20" and again the
script hung and I had to cancel it again.
The macro i've got to run the script is this:
macro index E "<enter-command>set
editor=mutt_expiry_editor.py\n<edit><enter-command>set
editor=$EDITOR<enter>" "Add Expires Header"
This is all one, unbroken line, but i've set my line wrapping to 72
columns so when I pasted it, it displays as 3 lines in the email.
$EDITOR is set in the environment in ~/.profile = /usr/bin/vi
Could someone kindly help and point out if and what I might be doing
wrong?
Best wishes, Jamie.