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

spupy

By: a guest on Dec 5th, 2009  |  syntax: Python  |  size: 2.75 KB  |  views: 139  |  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. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import gtk, os, sys
  5.  
  6. def print_usage():
  7.     print "Usage:"
  8.     print "PyMenu label:command[:icon] ..."
  9.     print "The label:command:icon tuple can appear multiple times."
  10.     print "icon is a string identifier of a gtk stock icon. You can, for example, use either 'new' or 'gtk-new'."
  11.     exit()
  12.  
  13. def _generate_callback(command):
  14.     """
  15.        Generates a callback for a menu item.
  16.    """
  17.     def callback(menuitem):
  18.         os.system(command+" &")
  19.         exit()
  20.     return callback
  21.  
  22. def _create_normal_menu_item(text):
  23.     """
  24.        Internal method; creates standart gtk.MenuItem
  25.    """
  26.     item = gtk.MenuItem(text)
  27.     return item
  28.  
  29. def _create_stock_menu_item(text, stock_id):
  30.     """
  31.        Internal method; creates a menu item from stock - gtk.ImageMenuItem, but replaces the stock text
  32.    """
  33.     if not stock_id.startswith("gtk-"):
  34.         stock_id = "gtk-"+stock_id
  35.     stock_lookup = gtk.stock_lookup(stock_id)
  36.     if not stock_lookup:
  37.         print "Warning: Stock_id", stock_id, "not found"
  38.     item = gtk.ImageMenuItem(stock_id)
  39.     label = item.get_children()[0]
  40.     label.set_text(text)
  41.     return item
  42.  
  43. def create_menu_item(entry):
  44.     """
  45.        Creates a menu item, with or without a stock icon.
  46.    """
  47.     label = entry["label"]
  48.     command = entry["command"]
  49.     if "icon" in entry:
  50.         icon = entry["icon"]
  51.     else:
  52.         icon = None
  53.  
  54.     item = None
  55.     if icon:
  56.         item = _create_stock_menu_item(label, icon)
  57.     else:
  58.         item = _create_normal_menu_item(label)
  59.  
  60.     item.connect("activate", _generate_callback(command))
  61.     return item
  62.  
  63. def parse_args(args):
  64.     """
  65.        Parses the command line arguments. Each entry is saved as a dict.
  66.    """
  67.     entries = []
  68.     for arg in args:
  69.         entry = {}
  70.         if ":" not in arg:
  71.             print "Misformatted argument:", arg
  72.             print_usage()
  73.         entry_raw = arg.split(':')
  74.         if len(entry_raw)!=2 and len(entry_raw)!=3:
  75.             print "Misformatted argument:", arg
  76.             print_usage()
  77.  
  78.         entry["label"] = entry_raw[0]
  79.         entry["command"] = entry_raw[1]
  80.         if len(entry_raw)==3:
  81.             entry["icon"] = entry_raw[2]
  82.  
  83.         entries.append(entry)
  84.  
  85.     return entries
  86.  
  87.  
  88. def create_menu(entries):
  89.     menu = gtk.Menu()
  90.     for entry in entries:
  91.         menu.append(create_menu_item(entry))
  92.  
  93.     menu.connect("deactivate", gtk.main_quit)
  94.     menu.show_all()
  95.     return menu
  96.  
  97. if __name__ == "__main__":
  98.     if len(sys.argv)==1 or sys.argv[1]=="-h" or sys.argv[1]=="--help":
  99.         print_usage()
  100.     entries = parse_args(sys.argv[1:])
  101.     menu = create_menu(entries)
  102.     menu.popup(None, None, None, 0, 0)
  103.     gtk.main()