ExternalAppsList.py (3296B)
1 import os 2 import FreeCAD 3 import FreeCADGui as Gui 4 import subprocess 5 import PySide 6 import re 7 from PySide import QtGui 8 from PySide import QtCore 9 from xml.etree import ElementTree 10 11 from MyX11Utils import * 12 from ToolXML import * 13 14 class Tool(): 15 def __init__(self, *, appName, toolName, xForms, toolTip, icon, extendedDescription, openHelpFile): 16 self.AppName = appName 17 self.ToolName = toolName 18 self.XForms = xForms 19 self.ToolTip = toolTip 20 self.Icon = icon 21 self.ExtendedDescription = extendedDescription 22 self.OpenHelpFile = openHelpFile 23 24 @staticmethod 25 def fromXForms(*, appName, xForms): 26 # TODO: implement a tool cache which avoids parsing the XML and memorizes the name and icon 27 from xml.etree import ElementTree 28 xml = ElementTree.parse(xForms).getroot() 29 30 return Tool(appName=appName, 31 toolName = getSingletonFromXML(xml, './XternalApps:name').text, 32 xForms = xForms, 33 toolTip = getSingletonFromXML(xml, './XternalApps:tooltip').text, 34 icon = os.path.dirname(__file__) + '/icons/' + appName + '/' + getSingletonFromXML(xml, './XternalApps:icon').text, 35 extendedDescription = getSingletonFromXML(xml, './XternalApps:extended-description').text, 36 openHelpFile = None) 37 38 class ToolsClass(): 39 def __init__(self, tools): 40 # TODO: make this private 41 self.AllTools = {tool.ToolName: tool for tool in tools} 42 def __getitem__(self, k): 43 return self.AllTools[k] 44 def __iter__(self): 45 return self.AllTools.__iter__() 46 47 class App(): 48 def __init__(self, name, *, start_command_and_args, xwininfo_filter_re, extra_xprop_filter, tools): 49 self.name = name 50 self.Icon = os.path.dirname(__file__) + '/icons/' + self.name + '.svg' 51 self.start_command_and_args = start_command_and_args 52 self.xwininfo_filter_re = re.compile(xwininfo_filter_re) 53 self.extra_xprop_filter = extra_xprop_filter 54 self.Tools = ToolsClass([Tool.fromXForms(appName=self.name, xForms=os.path.dirname(__file__) + '/' + t) for t in tools]) 55 56 class Apps(): 57 def __init__(self, *apps): 58 # TODO: make this private 59 self.AllApps = {app.name: app for app in apps} 60 def __getitem__(self, k): 61 return self.AllApps[k] 62 def __iter__(self): 63 return self.AllApps.__iter__() 64 65 # app-specific infos: 66 apps = Apps( 67 App('Mousepad', 68 start_command_and_args = ['mousepad', '--disable-server'], 69 xwininfo_filter_re = r'mousepad', 70 extra_xprop_filter = lambda processId, windowId, i: True, 71 tools = []), 72 App('Inkscape', 73 start_command_and_args = ['inkscape'], 74 xwininfo_filter_re = r'inkscape', 75 extra_xprop_filter = lambda processId, windowId, i: x11prop(windowId, 'WM_STATE', 'WM_STATE') is not None, 76 tools = [ 77 "myTool.xforms" 78 ]), 79 App('GIMP', 80 start_command_and_args = ['env', '-i', 'DISPLAY=:0', '/home/suzanne/perso/dotfiles/nix/result/bin/gimp', '--new-instance'], 81 xwininfo_filter_re = r'gimp', 82 extra_xprop_filter = lambda processId, windowId, i: x11prop(windowId, 'WM_STATE', 'WM_STATE') is not None, 83 tools = []))