plumber

Plumber – a modern approach to plumbing
git clone git://r-36.net/plumber
Log | Files | Refs | README | LICENSE

see (868B)


      1 #!/usr/bin/env python
      2 # coding=utf-8
      3 #
      4 # Copy me, if you can.
      5 # by 20h
      6 #
      7 
      8 import os
      9 import sys
     10 import getopt
     11 import mailcap
     12 import mimetypes
     13 import subprocess
     14 
     15 def usage(app):
     16 	app = os.path.basename(app)
     17 	print("usage: %s [-h] file" % (app), file=sys.stderr)
     18 	sys.exit(1)
     19 
     20 def main(args):
     21 	try:
     22 		opts, largs = getopt.getopt(args[1:], "h")
     23 	except getopt.GetoptError as err:
     24 		print(str(err))
     25 		usage(args[0])
     26 
     27 	for o, a in opts:
     28 		if o == "-h":
     29 			usage(args[0])
     30 		else:
     31 			assert False, "unhandled option"
     32 
     33 	if len(largs) < 1:
     34 		usage(args[0])
     35 
     36 	caps = mailcap.getcaps()
     37 	(typ, enc) = mimetypes.guess_type(largs[0], strict=False)
     38 	if typ == None:
     39 		return 1
     40 	(cmd, ent) = mailcap.findmatch(caps, typ, 'view',
     41 			 largs[0])
     42 	if cmd == None:
     43 		return 1
     44 
     45 	return subprocess.call(cmd, shell=True, stdin=sys.stdin)
     46 
     47 if __name__ == "__main__":
     48 	sys.exit(main(sys.argv))
     49