Quote filenames before substituting into mailcap rules in mailcap-test.py.
[blog.git] / posts / mailcap / mailcap-test.py
index 8990b7c3c9cd974012d3441b9a429efedd0f638c..ee2b8efd7b7216d090e1648e085f6c9aee211436 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# Copyright (C) 2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2011-2012 W. Trevor King <wking@drexel.edu>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as
 
 import mailcap as _mailcap
 import mimetypes as _mimetypes
+import shlex as _shlex
+if not hasattr(_shlex, 'quote'):  # Python < 3.3
+    import pipes as _pipes
+    _shlex.quote = _pipes.quote
 import subprocess as _subprocess
 
 
@@ -31,7 +35,7 @@ def view(filename, mime=None):
         if mime is None:
             return 1
         print('guessed {} for {}'.format(mime, filename))
-    match = _mailcap.findmatch(_CAPS, mime, filename=filename)
+    match = _mailcap.findmatch(_CAPS, mime, filename=_shlex.quote(filename))
     if match[0] is None:
         return 1
     print('view {} with {}'.format(filename, match[0]))