jstokes75

user_applications.py

Feb 16th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import os
  4. import ConfigParser
  5. import xdg.IconTheme
  6.  
  7. class my_apps:
  8.     def __init__(self,  name ,  execute ,  icon=None):
  9.         self.name = name
  10.         self.execute = execute
  11.         self.icon = icon
  12.  
  13. class application_list:
  14.     def __init__(self):
  15.         self.known_cats = ['Audio',  'Audio Video', 'Development',  'Education',  
  16.                                         'Game' ,   'Graphics',  'Network' ,  'Office' , 'Science' ,
  17.                                         'Settings' ,  'System' , 'Utility', 'Other']
  18.         self.list = {'Audio':[],  'Audio Video':[], 'Development':[],  'Education':[],  
  19.                                         'Game':[] ,   'Graphics':[],  'Network':[] ,  'Office':[] , 'Science':[] ,
  20.                                         'Settings':[] ,  'System':[] , 'Utility':[], 'Other':[]}
  21.         self.get_app_list()
  22.         #for i in self.list.items():
  23.             #i.sort(key = operator.attrgetter('name'))
  24.            
  25.    
  26.     def get_menu_items(self,  app):
  27.         #path = '/usr/share/applications/'
  28.         section = 'Desktop Entry'
  29.         config = ConfigParser.ConfigParser()
  30.         config.readfp(open(app))
  31.  
  32.         try:
  33.             exe = config.get(section,'Exec')
  34.         except:
  35.             exe = '\dev\Null'    
  36.  
  37.         try:
  38.             name = config.get(section, 'Name')
  39.         except:
  40.             name = 'N/A'
  41.            
  42.         try:
  43.             t = config.get(section, 'Type')
  44.         except :
  45.             t = None
  46.         try:    
  47.             categories = config.get(section, 'Categories')
  48.             cats = categories.split(";")
  49.         except:
  50.             cats = ['Other']
  51.  
  52.         try:
  53.             icon = config.get(section, 'Icon')
  54.         except:
  55.             icon = None
  56.  
  57.         #print ("Name:{0}\n\tExec:{1}\n\tType:{2}\n\tCat:{3}").format(name, exe, t, cats)
  58.         return name, exe , t,  cats , icon
  59.        
  60.     def get_cats(self, categories):
  61.         rt = []
  62.         if categories:
  63.             if  len(categories) > 0:
  64.                 for i in categories:
  65.                     if i in self.known_cats:
  66.                         rt.append(i)
  67.         return rt
  68.        
  69.     def get_app_list(self):
  70.         apps=[]
  71.         path = '/home/jason/.local/share/applications/'
  72.         for (path, dir ,  filename) in os.walk(path):
  73.             for fn in filename:
  74.                 full_path = os.path.join(path, fn)
  75.                 filen,  filee = os.path.splitext(fn)
  76.                 if filee == ".desktop":
  77.                     name, exe,  t ,  cats , icon = self.get_menu_items(full_path)
  78.                     cat = self.get_cats(cats)
  79.                     exe = self.check_execute(exe)
  80.                     icon = self.check_icon(icon)
  81.                     if cat is not None:
  82.                         for d in cat:
  83.                             my_a = my_apps(name, exe, icon)
  84.                             self.list[d].append(my_a)
  85.     #print("Done ------------")
  86.  
  87.     def print_list(self):
  88.         print " outputing List"
  89.         for i in self.list.items():
  90.             for j in i[1]:
  91.                 print("{0} : {1} ".format( j.name,  j.execute))
  92.                
  93.     def check_execute(self, execute):
  94.         checklist = ['%U' , '%u', '%F' ,  '%f',  '%i' ,  '%c',  '%k']
  95.         for i in checklist:
  96.             execute = execute.replace(i, "")
  97.            
  98.         execute = execute.strip()
  99.         #print execute
  100.         return execute
  101.        
  102.     def check_icon(self,icon):
  103.         if icon is not None:
  104.             if os.path.isfile(icon):
  105.                 return icon
  106.             else:
  107.                 try:
  108.                     icon = xdg.IconTheme.getIconPath(icon)
  109.                 except:
  110.                     icon = ""
  111.                 return icon
  112.         else:
  113.             return ""      
  114.  
  115. ml = application_list()
  116. print "<openbox_pipe_menu>"
  117. for j in ml.known_cats:
  118.     if len(ml.list[j]) > 0 :
  119.         print "<menu id=\"user-{0}\" label=\"{0}\">".format(j)
  120.         for i in ml.list[j]:
  121.             print "<item label=\"{0}\" icon=\"{2}\">\n\t<action name=\"Execute\">\n\t\t<execute>{1}</execute>\n\t</action>\n</item>".format(i.name,i.execute,i.icon)
  122.         print "</menu>"
  123. print "</openbox_pipe_menu>"
Add Comment
Please, Sign In to add comment