Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

mimeo $MIMEO mimetype determination patch

By: a guest on Jun 5th, 2012  |  syntax: None  |  size: 2.05 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --- Mimeo.py    2012-06-02 15:26:00.000000000 +0100
  2. +++ Mimeo_patched.py    2012-06-05 17:21:15.000000000 +0100
  3. @@ -722,11 +722,23 @@ class Mimeo():
  4.      if os.path.isdir(rpath):
  5.        return DIRECTORY_MIMETYPE
  6.      elif os.path.isfile(rpath):
  7. -      mimetype = mimetypes.guess_type(rpath)[0]
  8. -      if not mimetype:
  9. -        proc = subprocess.Popen(['file',  '--mime-type', rpath], stdout=subprocess.PIPE)
  10. +      method = os.getenv("MIMEO")
  11. +      if not method:
  12. +        method = "python"
  13. +      if 'file' in method:
  14. +        proc = subprocess.Popen(['file', '--mime-type', rpath], stdout=subprocess.PIPE)
  15. +        output = proc.communicate()[0]
  16. +        mimetype = output[len(rpath)+2:].rstrip()
  17. +      elif 'mimetype' in method:
  18. +        proc = subprocess.Popen(['mimetype', '-i', rpath], stdout=subprocess.PIPE)
  19.          output = proc.communicate()[0]
  20.          mimetype = output[len(rpath)+2:].rstrip()
  21. +      elif 'rox' in method:
  22. +        proc = subprocess.Popen(['rox', '-m', rpath], stdout=subprocess.PIPE)
  23. +        output = proc.communicate()[0]
  24. +        mimetype = output[0:].rstrip()
  25. +      else:
  26. +        mimetype = mimetypes.guess_type(rpath)[0]
  27.        if not mimetype:
  28.          self.error_msg("error: failed to detect MIME-type of %s" % arg)
  29.        return mimetype
  30. @@ -2026,7 +2038,7 @@ def main():
  31.  
  32.  
  33.  
  34. -  conf_group = parser.add_argument_group('Configuration', 'Various configuration options.')
  35. +  conf_group = parser.add_argument_group('Configuration', 'Various configuration options. Optionally, set the environment variable \'MIMEO\' to contain one of the following command names which will be used to determine MIME-types (assuming the relevant packages are installed): file, mimetype, rox. If none is specified, Python\'s \'mimetypes\' module is used instead.')
  36.    conf_group.add_argument('-a', '--assoc', dest='assoc', metavar='<filepath>', action='append',
  37.                      help='Specify a file that associates regular expressions with custom commands. This can be used for opening URLs, for example. See "--assoc-help" for details.')