Skip to content

Instantly share code, notes, and snippets.

@Taywee
Created December 6, 2016 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taywee/762cde4cdf1bd23a998f0bc882e4b321 to your computer and use it in GitHub Desktop.
Save Taywee/762cde4cdf1bd23a998f0bc882e4b321 to your computer and use it in GitHub Desktop.
CC0: A simple script that is used to load up the mailcap file and look up and call the appropriate program, for use when a filetype (especially pdf) is incorrectly given an application/octet-stream type
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# To the extent possible under law, Taylor C. Richberger has waived all
# copyright and related or neighboring rights to mutt_octet_lookup. This work
# is published from: United States.
import argparse
import locale
import mailcap
import mimetypes
import os
import shlex
def main():
locale.setlocale(locale.LC_ALL, '')
parser = argparse.ArgumentParser(description='Pull in an octet-stream argument and try to run the actual mailcap on it based on extension')
parser.add_argument('-V', '--version', action='version', version='0.1')
parser.add_argument('file', help='The file to process')
args = parser.parse_args()
mimetypes.init()
type = mimetypes.guess_type(args.file)[0]
caps = mailcap.getcaps()
view = mailcap.findmatch(caps, type, filename=args.file)[0]
command = shlex.split(view)
os.execvp(command[0], command)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment