From 6d44f5c608c7d1f0354980f544905df7fddac53f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 1 Dec 2011 20:42:55 -0500 Subject: [PATCH] Add mailcap post. --- posts/mailcap.mdwn | 13 +++++++++ posts/mailcap/mailcap-test.py | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 posts/mailcap.mdwn create mode 100755 posts/mailcap/mailcap-test.py diff --git a/posts/mailcap.mdwn b/posts/mailcap.mdwn new file mode 100644 index 0000000..23e4f54 --- /dev/null +++ b/posts/mailcap.mdwn @@ -0,0 +1,13 @@ +Mailcap files, defined in [RFC 1524][rfc1524], allow you to tell you +mail clients, web browsers, and other programs how you want to view +and edit various MIME types. Since interaction with your mailcap +files often occurs deep within the bowels of your program, I've +written up a very simple [[Python]] script to test your mailcap +entries: [[mailcap-test.py]]. Enjoy! + +[rfc1524]: http://tools.ietf.org/html/rfc1524 + +[[!tag tags/code]] +[[!tag tags/linux]] +[[!tag tags/programming]] +[[!tag tags/python]] diff --git a/posts/mailcap/mailcap-test.py b/posts/mailcap/mailcap-test.py new file mode 100755 index 0000000..8990b7c --- /dev/null +++ b/posts/mailcap/mailcap-test.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see +# . + +"View files using mailcap-specified commands." + +import mailcap as _mailcap +import mimetypes as _mimetypes +import subprocess as _subprocess + + +_CAPS = _mailcap.getcaps() + +def view(filename, mime=None): + if mime is None: + mime,encoding = _mimetypes.guess_type(filename) + if mime is None: + return 1 + print('guessed {} for {}'.format(mime, filename)) + match = _mailcap.findmatch(_CAPS, mime, filename=filename) + if match[0] is None: + return 1 + print('view {} with {}'.format(filename, match[0])) + return _subprocess.call(match[0], shell=True) + + +if __name__ == '__main__': + import argparse + import sys + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + 'files', metavar='FILE', nargs='+', help='file to view') + + args = parser.parse_args() + + for filename in args.files: + rc = view(filename) + if rc != 0: + sys.exit(rc) -- 2.26.2