jstokes75

appliactions.py

Feb 26th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import shlex
  4. import subprocess
  5. import os
  6. import sys
  7. import pyglet
  8. from pyglet.window import mouse
  9. import ConfigParser
  10. import operator
  11. import fnmatch
  12.  
  13. import xdg.IconTheme
  14.  
  15.  
  16.  
  17.  
  18. class my_apps:
  19.     def __init__(self,  name ,  execute ,  icon=None):
  20.         self.name = name
  21.         self.execute = execute
  22.         self.icon = icon
  23.  
  24. class application_list:
  25.     def __init__(self):
  26.         self.known_cats = ['Favorites', 'Audio',  'Audio Video', 'Development',  'Education',  
  27.                                         'Game' ,   'Graphics',  'Network' ,  'Office' , 'Science' ,
  28.                                         'Settings' ,  'System' , 'Utility']
  29.         self.list = {'Favorites':[],  'Audio':[],  'Audio Video':[], 'Development':[],  'Education':[],  
  30.                                         'Game':[] ,   'Graphics':[],  'Network':[] ,  'Office':[] , 'Science':[] ,
  31.                                         'Settings':[] ,  'System':[] , 'Utility':[]}
  32.         self.get_app_list()
  33.         for i in self.list.items():
  34.             i[1].sort(key = operator.attrgetter('name'))
  35.         self.known_cats.sort()
  36.        
  37.     def launch_ap(self,  application):
  38.         a = shlex.split(application)
  39.         subprocess.Popen(a)
  40.    
  41.     def get_menu_items(self,  app):
  42.         #path = '/usr/share/applications/'
  43.         section = 'Desktop Entry'
  44.         config = ConfigParser.ConfigParser()
  45.         config.readfp(open(app))
  46.         exe = config.get(section,'Exec')
  47.         name = config.get(section, 'Name')
  48.         try:
  49.             t = config.get(section, 'Type')
  50.         except :
  51.             t = None
  52.         try:    
  53.             categories = config.get(section, 'Categories')
  54.             cats = categories.split(";")
  55.         except:
  56.             cats = None
  57.         try:
  58.             icon = config.get(section, 'Icon')
  59.         except :
  60.             icon = None
  61.            
  62.         return name, exe , t,  cats, icon
  63.        
  64.     def get_cats(self, categories):
  65.         rt = []
  66.         if categories:
  67.             if  len(categories) > 0:
  68.                 for i in categories:
  69.                     if i in self.known_cats:
  70.                         rt.append(i)
  71.         return rt
  72.        
  73.     def find_icon_path(self):
  74.         pass
  75.        
  76.     def get_app_list(self):
  77.         apps=[]
  78.         path = '/usr/share/applications/'
  79.         for (path, dir ,  filename) in os.walk(path):
  80.             for fn in filename:
  81.                 full_path = os.path.join(path, fn)
  82.                 filen,  filee = os.path.splitext(fn)
  83.                 if filee == ".desktop":
  84.                     name, exe,  t ,  cats, icon = self.get_menu_items(full_path)
  85.                     cat = self.get_cats(cats)
  86.                     exe = self.check_execute(exe)
  87.                     iconp = self.get_icon_path(icon)
  88.                     if cat is not None:
  89.                         for d in cat:
  90.                             my_a = my_apps(name, exe , iconp)
  91.                             self.list[d].append(my_a)
  92.        
  93.     def print_list(self):
  94.         for i in self.list.items():
  95.             for j in i[1]:
  96.                 print("{0} : {1}".format( j.name,  j.execute,  j.icon))
  97.                
  98.     def check_execute(self, execute):
  99.         checklist = ['%U' , '%u', '%F' ,  '%f',  '%i' ,  '%c',  '%k']
  100.         for i in checklist:
  101.             execute = execute.replace(i, "")            
  102.         execute = execute.strip()
  103.         return execute
  104.        
  105.     def get_icon_path_old(self, icon):
  106.         file_found = None
  107.         icon_path = None
  108.         icon_path_home = '/home/jason/.icons/ProphetIcon13/apps/'
  109.         knonw_file_icon_type = ['.png', '.jpg']
  110.         if icon:
  111.             try:
  112.                 with open(icon) as f:
  113.                     file_found = True
  114.                     icon_p = icon
  115.             except IOError as e:
  116.                     file_found = False
  117.            
  118.         if not file_found:
  119.             file_found = False
  120.             icon_path = None
  121.        
  122.         matches = []
  123.         if icon:
  124.             for root, dirnames, filenames in os.walk(icon_path_home):
  125.                 for filename in fnmatch.filter(filenames, icon+'.*'):
  126.                     print filename
  127.                     matches.append(os.path.join(root, filename))
  128.                     print len(matches)
  129.             if len(matches) > 0:        
  130.                 icon_p = matches[0]
  131.                 fileName, fileExtension = os.path.splitext(icon_p)
  132.                 if fileExtension in knonw_file_icon_type:
  133.                         file_found = True
  134.                         icon_path = icon_p
  135.                 else:
  136.                         icon_path = None
  137.         else:
  138.             icon_path = None
  139.            
  140.         print icon ,  icon_path
  141.         return icon_path
  142.    
  143.     def get_icon_path(self, icon):
  144.         size = 64
  145.         if icon:
  146.             return xdg.IconTheme.getIconPath(icon,size)
  147.         else:
  148.             return None
Add Comment
Please, Sign In to add comment