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

Untitled

By: a guest on Aug 9th, 2012  |  syntax: Python  |  size: 12.73 KB  |  views: 25  |  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. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2009, 2010, 2011  Roman Zimbelmann <romanz@lavabit.com>
  3. # This configuration file is licensed under the same terms as ranger.
  4. # ===================================================================
  5. # This is the configuration file for file type detection and application
  6. # handling.  It's all in python; lines beginning with # are comments.
  7. #
  8. # You can customize this in the file ~/.config/ranger/apps.py.
  9. # It has the same syntax as this file.  In fact, you can just copy this
  10. # file there with `ranger --copy-config=apps' and make your modifications.
  11. # But make sure you update your configs when you update ranger.
  12. #
  13. # In order to add application definitions "on top of" the default ones
  14. # in your ~/.config/ranger/apps.py, you should subclass the class defined
  15. # here like this:
  16. #
  17. #   from ranger.defaults.apps import CustomApplications as DefaultApps
  18. #   class CustomApplications(DeafultApps):
  19. #       <your definitions here>
  20. #
  21. # To override app_defaults, you can write something like:
  22. #
  23. #       def app_defaults(self, c):
  24. #           f = c.file
  25. #           if f.extension == 'lol':
  26. #               return "lolopener", c
  27. #           return DefaultApps.app_default(self, c)
  28. #
  29. # ===================================================================
  30. # This system is based on things called MODES and FLAGS.  You can read
  31. # in the man page about them.  To remind you, here's a list of all flags.
  32. # An uppercase flag inverts previous flags of the same name.
  33. #     s   Silent mode.  Output will be discarded.
  34. #     d   Detach the process.  (Run in background)
  35. #     p   Redirect output to the pager
  36. #     w   Wait for an Enter-press when the process is done
  37. #     c   Run the current file only, instead of the selection
  38. #     r   Run application with root privilege
  39. #     t   Run application in a new terminal window
  40. #
  41. # To implement flags in this file, you could do this:
  42. #     context.flags += "d"
  43. # Another example:
  44. #     context.flags += "Dw"
  45. #
  46. # To implement modes in this file, you can do something like:
  47. #     if context.mode == 1:
  48. #         <run in one way>
  49. #     elif context.mode == 2:
  50. #         <run in another way>
  51. #
  52. # ===================================================================
  53. # The methods are called with a "context" object which provides some
  54. # attributes that transfer information.  Relevant attributes are:
  55. #
  56. # mode -- a number, mainly used in determining the action in app_xyz()
  57. # flags -- a string with flags which change the way programs are run
  58. # files -- a list containing files, mainly used in app_xyz
  59. # filepaths -- a list of the paths of each file
  60. # file -- an arbitrary file from that list (or None)
  61. # fm -- the filemanager instance
  62. # popen_kws -- keyword arguments which are directly passed to Popen
  63. #
  64. # ===================================================================
  65. # The return value of the functions should be either:
  66. # 1. A reference to another app, like:
  67. #     return self.app_editor(context)
  68. #
  69. # 2. A call to the "either" method, which uses the first program that
  70. # is installed on your system.  If none are installed, None is returned.
  71. #     return self.either(context, "libreoffice", "soffice", "ooffice")
  72. #
  73. # 3. A tuple of arguments that should be run.
  74. #     return "mplayer", "-fs", context.file.path
  75. # If you use lists instead of strings, they will be flattened:
  76. #     args = ["-fs", "-shuf"]
  77. #     return "mplayer", args, context.filepaths
  78. # "context.filepaths" can, and will often be abbreviated with just "context":
  79. #     return "mplayer", context
  80. #
  81. # 4. "None" to indicate that no action was found.
  82. #     return None
  83. #
  84. # ===================================================================
  85. # When using the "either" method, ranger determines which program to
  86. # pick by looking at its dependencies.  You can set dependencies by
  87. # adding the decorator "depends_on":
  88. #     @depends_on("vim")
  89. #     def app_vim(self, context):
  90. #         ....
  91. # There is a special keyword which you can use as a dependence: "X"
  92. # This ensures that the program will only run when X is running.
  93. # ===================================================================
  94.  
  95. import ranger
  96. from ranger.api.apps import *
  97. from ranger.ext.get_executables import get_executables
  98.  
  99. class CustomApplications(Applications):
  100.     def app_default(self, c):
  101.         """How to determine the default application?"""
  102.         f = c.file
  103.  
  104.         if f.basename.lower() == 'makefile' and c.mode == 1:
  105.             made = self.either(c, 'make')
  106.             if made: return made
  107.  
  108.         if f.extension is not None:
  109.             if f.extension in ('pdf', ):
  110.                 return self.either(c, 'llpp', 'zathura', 'mupdf', 'apvlv',
  111.                         'evince', 'okular', 'epdfview')
  112.             if f.extension == 'djvu':
  113.                 return self.either(c, 'evince')
  114.             if f.extension in ('xml', 'csv'):
  115.                 return self.either(c, 'editor')
  116.             if f.extension == 'mid':
  117.                 return self.either(c, 'wildmidi')
  118.             if f.extension in ('html', 'htm', 'xhtml') or f.extension == 'swf':
  119.                 c.flags += 'd'
  120.                 handler = self.either(c,
  121.                         'luakit', 'uzbl', 'vimprobable', 'vimprobable2', 'jumanji',
  122.                         'firefox', 'seamonkey', 'iceweasel', 'opera',
  123.                         'surf', 'midori', 'epiphany', 'konqueror')
  124.                 # Only return if some program was found:
  125.                 if handler:
  126.                     return handler
  127.             if f.extension in ('html', 'htm', 'xhtml'):
  128.                 # These browsers can't handle flash, so they're not called above.
  129.                 c.flags += 'D'
  130.                 return self.either(c, 'elinks', 'links', 'links2', 'lynx', 'w3m')
  131.             if f.extension == 'nes':
  132.                 return self.either(c, 'fceux')
  133.             if f.extension in ('swc', 'smc', 'sfc'):
  134.                 return self.either(c, 'zsnes')
  135.             if f.extension == 'doc':
  136.                 c.flags += 'd'
  137.                 return self.either(c, 'abiword', 'libreoffice',
  138.                         'soffice', 'ooffice')
  139.             if f.extension in ('odt', 'ods', 'odp', 'odf', 'odg', 'sxc',
  140.                     'stc', 'xls', 'xlsx', 'xlt', 'xlw', 'gnm', 'gnumeric'):
  141.                 c.flags += 'd'
  142.                 return self.either(c, 'gnumeric', 'kspread',
  143.                         'libreoffice', 'soffice', 'ooffice')
  144.             if f.extension == 'odb':
  145.                 c.flags += 'd'
  146.                 return self.either(c, 'libreoffice')
  147.                 return 'setsid', 'libreoffice', c
  148.  
  149.         if f.mimetype is not None:
  150.             if INTERPRETED_LANGUAGES.match(f.mimetype):
  151.                 return self.either(c, 'edit_or_run')
  152.  
  153.         if f.container:
  154.             return self.either(c, 'aunpack', 'file_roller')
  155.  
  156.         if f.video or f.audio:
  157.             if f.video:
  158.                 c.flags += 'd'
  159.             return self.either(c, 'smplayer', 'gmplayer', 'mplayer2',
  160.                     'mplayer', 'vlc', 'totem')
  161.  
  162.         if f.image:
  163.             if c.mode in (11, 12, 13, 14):
  164.                 return self.either(c, 'set_bg_with_feh')
  165.             else:
  166.                 return self.either(c, 'sxiv', 'feh', 'eog', 'mirage')
  167.  
  168.         if f.document or f.filetype.startswith('text') or f.size == 0:
  169.             return self.either(c, 'editor')
  170.  
  171.         # You can put this at the top of the function and mimeopen will
  172.         # always be used for every file.
  173.         return self.either(c, 'mimeopen')
  174.  
  175.  
  176.     # ----------------------------------------- application definitions
  177.     # Note: Trivial application definitions are at the bottom
  178.     def app_pager(self, c):
  179.         return 'less', '-R', c
  180.  
  181.     def app_editor(self, c):
  182.         try:
  183.             default_editor = os.environ['EDITOR']
  184.         except KeyError:
  185.             pass
  186.         else:
  187.             parts = default_editor.split()
  188.             exe_name = os.path.basename(parts[0])
  189.             if exe_name in get_executables():
  190.                 return tuple(parts) + tuple(c)
  191.  
  192.         return self.either(c, 'vim', 'emacs', 'nano')
  193.  
  194.     def app_edit_or_run(self, c):
  195.         if c.mode is 1:
  196.             return self.app_self(c)
  197.         return self.app_editor(c)
  198.  
  199.     @depends_on('mplayer')
  200.     def app_mplayer(self, c):
  201.         if c.mode is 1:
  202.             return 'mplayer', '-fs', c
  203.  
  204.         elif c.mode is 2:
  205.             args = "mplayer -fs -sid 0 -vfm ffmpeg -lavdopts " \
  206.                     "lowres=1:fast:skiploopfilter=all:threads=8".split()
  207.             args.extend(c)
  208.             return args
  209.  
  210.         elif c.mode is 3:
  211.             return 'mplayer', '-mixer', 'software', c
  212.  
  213.         else:
  214.             return 'mplayer', c
  215.  
  216.     @depends_on('mplayer2')
  217.     def app_mplayer2(self, c):
  218.         args = list(self.app_mplayer(c))
  219.         args[0] += '2'
  220.         return args
  221.  
  222.     # A dependence on "X" means: this programs requires a running X server!
  223.     @depends_on('feh', 'X')
  224.     def app_set_bg_with_feh(self, c):
  225.         c.flags += 'd'
  226.         arg = {11: '--bg-scale', 12: '--bg-tile', 13: '--bg-center',
  227.                 14: '--bg-fill'}
  228.         if c.mode in arg:
  229.             return 'feh', arg[c.mode], c.file.path
  230.         return 'feh', arg[11], c.file.path
  231.  
  232.     @depends_on('feh', 'X')
  233.     def app_feh(self, c):
  234.         c.flags += 'd'
  235.         if c.mode is 0 and len(c.files) is 1 and self.fm.env.cwd:
  236.             # view all files in the cwd
  237.             images = [f.basename for f in self.fm.env.cwd.files if f.image]
  238.             return 'feh', '--start-at', c.file.basename, images
  239.         return 'feh', c
  240.  
  241.     @depends_on('sxiv', 'X')
  242.     def app_sxiv(self, c):
  243.         c.flags = 'd' + c.flags
  244.         if len(c.files) is 1 and self.fm.env.cwd:
  245.             images = [f.basename for f in self.fm.env.cwd.files if f.image]
  246.             try:
  247.                 position = images.index(c.file.basename) + 1
  248.             except:
  249.                 return None
  250.             return 'sxiv', '-n', str(position), images
  251.         return 'sxiv', c
  252.  
  253.     @depends_on('aunpack')
  254.     def app_aunpack(self, c):
  255.         if c.mode is 0:
  256.             c.flags += 'p'
  257.             return 'aunpack', '-l', c.file.path
  258.         return 'aunpack', c.file.path
  259.  
  260.     @depends_on('file-roller', 'X')
  261.     def app_file_roller(self, c):
  262.         c.flags += 'd'
  263.         return 'file-roller', c.file.path
  264.  
  265.     @depends_on('make')
  266.     def app_make(self, c):
  267.         if c.mode is 0:
  268.             return "make"
  269.         if c.mode is 1:
  270.             return "make", "install"
  271.         if c.mode is 2:
  272.             return "make", "clear"
  273.  
  274.     @depends_on('java')
  275.     def app_java(self, c):
  276.         def strip_extensions(file):
  277.             if '.' in file.basename:
  278.                 return file.path[:file.path.index('.')]
  279.             return file.path
  280.         files_without_extensions = map(strip_extensions, c.files)
  281.         return "java", files_without_extensions
  282.  
  283.     @depends_on('totem', 'X')
  284.     def app_totem(self, c):
  285.         if c.mode is 0:
  286.             return "totem", c
  287.         if c.mode is 1:
  288.             return "totem", "--fullscreen", c
  289.  
  290.     @depends_on('mimeopen')
  291.     def app_mimeopen(self, c):
  292.         if c.mode is 0:
  293.             return "mimeopen", c
  294.         if c.mode is 1:
  295.             # Will ask user to select program
  296.             # aka "Open with..."
  297.             return "mimeopen", "--ask", c
  298.  
  299.  
  300. # Often a programs invocation is trivial.  For example:
  301. #    vim test.py readme.txt [...]
  302. #
  303. # This could be implemented like:
  304. #    @depends_on("vim")
  305. #    def app_vim(self, context):
  306. #        return "vim", context
  307. #
  308. # But this is redundant and ranger does this automatically.  However, sometimes
  309. # you want to change some properties like flags or dependencies.
  310. # The method "generic" defines a generic method for the given programs which
  311. # looks like the one above, but you can add dependencies and flags here.
  312. # Add programs (that are not defined yet) here if they should only run in X:
  313. CustomApplications.generic('fceux', 'wine', 'zsnes', deps=['X'])
  314.  
  315. # Add those which should only run in X AND should be detached/forked here:
  316. CustomApplications.generic(
  317.     'luakit', 'uzbl', 'vimprobable', 'vimprobable2', 'jumanji',
  318.     'firefox', 'seamonkey', 'iceweasel', 'opera',
  319.     'surf', 'midori', 'epiphany', 'konqueror',
  320.     'evince', 'zathura', 'apvlv', 'okular', 'epdfview', 'mupdf', 'llpp',
  321.     'eog', 'mirage', 'gimp',
  322.     'libreoffice', 'soffice', 'ooffice', 'gnumeric', 'kspread', 'abiword',
  323.     'gmplayer', 'smplayer', 'vlc',
  324.             flags='d', deps=['X'])
  325.  
  326. # What filetypes are recognized as scripts for interpreted languages?
  327. # This regular expression is used in app_default()
  328. INTERPRETED_LANGUAGES = re.compile(r'''
  329.    ^(text|application)/x-(
  330.        haskell|perl|python|ruby|sh
  331.    )$''', re.VERBOSE)