Advertisement
Guest User

Python script for fvwm 'applications' submenu

a guest
Mar 10th, 2011
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. #!/usr/bin/python2
  2. #
  3. # Author: Victor Ananjevsky, 2007 - 2010
  4. # based on xdg-menu.py, written by Piotr Zielinski (http://www.cl.cam.ac.uk/~pz215/)
  5. # License: GPL
  6. #
  7. # This script takes names of menu files conforming to the XDG Desktop
  8. # Menu Specification, and outputs their FVWM equivalents to the
  9. # standard output.
  10. #
  11. #   http://standards.freedesktop.org/menu-spec/latest/
  12. #
  13. # Requirements:
  14. #   pyxdg, pygtk, gnome-menus
  15. #
  16. # Syntax:
  17. #   fvwm-xdg-menu.py [-d Menu] menufile1 menufile2 menufile3 ...
  18. #
  19. # Each menufile is an XDG menu description file.
  20. # Icons of menu entries cached in $XDG_CACHE_HOME/fvwm/icons/menu
  21. #
  22. # For menufile name `recent' will be generated menu of recently opened files
  23. #
  24. # -d mean not print headers for toplevel menu (useful in DynamicPopupAction)
  25. #
  26. # Example:
  27. #   fvwm-xdg-menu.py /etc/xdg/menus/applications.menu
  28. #   fvwm-xdg-menu.py applications
  29. #
  30.  
  31.  
  32. import sys
  33. import os
  34. from optparse import OptionParser
  35.  
  36. import xdg.Menu
  37. from xdg.DesktopEntry import *
  38. from xdg.RecentFiles import *
  39. from xdg.BaseDirectory import xdg_config_dirs, xdg_cache_home
  40.  
  41. import gtk
  42.  
  43. # fix for correct output of unicode chars without terminal
  44. sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
  45.  
  46. def cache_icon (icon):
  47.     ''' cache an icon  '''
  48.     icon_file = "%s/%s.png" % (cache_path, os.path.basename(icon))
  49.     if os.path.exists(icon_file):
  50.         return
  51.     full_icon = "%s.png" % icon
  52.     if os.path.exists(full_icon):
  53.         gtk.gdk.pixbuf_new_from_file_at_size(full_icon, options.icon_size, options.icon_size).save(icon_file, 'png')
  54.         return
  55.     try:
  56.         icon_theme.load_icon(icon, options.icon_size, gtk.ICON_LOOKUP_NO_SVG).save(icon_file, "png")
  57.     except:
  58.         pass
  59.  
  60. def parse_menu (menu, fvwm_menu = None):
  61.     ''' parse menu file '''
  62.     prefix = "+"
  63.     if fvwm_menu == None:
  64.         print ''
  65.         print 'DestroyMenu "%s"' % menu
  66.         print 'AddToMenu "%s"' % menu
  67.     else:
  68.         print 'DestroyMenu recreate %s' % fvwm_menu
  69.         prefix = "AddToMenu %s" % fvwm_menu
  70.  
  71.     for entry in menu.getEntries():
  72.     if isinstance(entry, xdg.Menu.Menu):
  73.             icon = entry.getIcon()
  74.             print u'%s "%s%%menu/folder.png%%" Popup "%s"' % (prefix, entry.getName(), entry)
  75.     elif isinstance(entry, xdg.Menu.MenuEntry):
  76.             desktop = DesktopEntry(entry.DesktopEntry.getFileName())
  77.             icon = desktop.getIcon()
  78.             ind = icon.rfind('.')
  79.             if ind != -1:
  80.                 icon = icon[0:ind]
  81.             cmd = desktop.getExec().rstrip('%FUfu')
  82.             cache_icon(icon)
  83.             print u'%s "%s%%menu/%s.png%%" Exec exec %s' % (prefix, desktop.getName(), os.path.basename(icon), cmd)
  84.     else:
  85.         pass
  86.  
  87.     for entry in menu.getEntries():
  88.     if isinstance(entry, xdg.Menu.Menu):
  89.         parse_menu(entry)
  90.  
  91. def parse_recent (fvwm_menu = None):
  92.     ''' parse recently opened files '''
  93.     prefix = "+"
  94.     if fvwm_menu == None:
  95.         print ''
  96.         print 'DestroyMenu "Recent"'
  97.         print 'AddToMenu "Recent"'
  98.     else:
  99.         print 'DestroyMenu recreate %s' % fvwm_menu
  100.         prefix="AddToMenu %s" % fvwm_menu
  101.    
  102.     rm = gtk.RecentManager()
  103.     for rf in rm.get_items():
  104.         print '%s "%s" Exec exec xdg-open "%s"' % (prefix, rf.get_display_name(), rf.get_uri())
  105.  
  106. # Start
  107.  
  108. cache_path = "%s/fvwm/menu" % xdg_cache_home
  109. icon_theme = gtk.icon_theme_get_default()
  110.  
  111. if not os.path.exists(cache_path):
  112.     os.makedirs(cache_path)
  113.  
  114. # Parse commandline
  115.  
  116. parser = OptionParser()
  117. parser.add_option("-d", "--dynamic", dest="fvwm_menu", default=None, help="Use in DynamicPopupAction", metavar="MENU")
  118. parser.add_option("-i", "--icons", dest="icon_size", default=16, help="Set icons size", metavar="SIZE")
  119. (options, args) = parser.parse_args()
  120.  
  121. for arg in args:
  122.     filename = ""
  123.     if os.path.exists(arg) or arg == "recent":
  124.         filename = arg
  125.     else:
  126.         tmpfile = "%s/menus/%s.menu" % (xdg_config_home, arg)
  127.         if os.path.exists(tmpfile):
  128.             filename = tmpfile
  129.         else:
  130.             for dir in xdg_config_dirs:
  131.                 tmpfile = "%s/menus/%s.menu" % (dir, arg)
  132.                 if os.path.exists(tmpfile):
  133.                     filename = tmpfile
  134.                     break
  135.     if filename == "":
  136.         continue
  137.     elif filename == "recent":
  138.         parse_recent (options.fvwm_menu)
  139.     else:
  140.         parse_menu(xdg.Menu.parse(filename), options.fvwm_menu)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement