0001 ### BITPIM 0002 ### 0003 ### Copyright (C) 2004 Roger Binns <rogerb@rogerbinns.com> 0004 ### 0005 ### This program is free software; you can redistribute it and/or modify 0006 ### it under the terms of the BitPim license as detailed in the LICENSE file. 0007 ### 0008 ### $Id: guihelper.py 1638 2004-10-07 03:21:09Z n9yty $ 0009 0010 """Various convenience functions and widgets to assist the gui""" 0011 0012 import time 0013 import os 0014 import sys 0015 import StringIO 0016 import traceback 0017 0018 0019 import wx 0020 0021 # some library routines 0022 def IsMSWindows(): 0023 """Are we running on Windows? 0024 0025 @rtype: Bool""" 0026 return wx.Platform=='__WXMSW__' 0027 0028 def IsGtk(): 0029 """Are we running on GTK (Linux) 0030 0031 @rtype: Bool""" 0032 return wx.Platform=='__WXGTK__' 0033 0034 def IsMac(): 0035 """Are we running on Mac 0036 0037 @rtype: Bool""" 0038 return wx.Platform=='__WXMAC__' 0039 0040 class LogWindow(wx.Panel): 0041 0042 def __init__(self, parent): 0043 wx.Panel.__init__(self,parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE) 0044 self.tb=wx.TextCtrl(self, 1, style=wx.TE_MULTILINE|wx.TE_RICH2|wx.NO_FULL_REPAINT_ON_RESIZE|wx.TE_DONTWRAP|wx.TE_READONLY) 0045 f=wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL ) 0046 ta=wx.TextAttr(font=f) 0047 self.tb.SetDefaultStyle(ta) 0048 self.sizer=wx.BoxSizer(wx.VERTICAL) 0049 self.sizer.Add(self.tb, 1, wx.EXPAND) 0050 self.SetSizer(self.sizer) 0051 self.SetAutoLayout(True) 0052 self.sizer.Fit(self) 0053 wx.EVT_IDLE(self, self.OnIdle) 0054 self.outstandingtext="" 0055 0056 def Clear(self): 0057 self.tb.Clear() 0058 0059 def OnIdle(self,_): 0060 if len(self.outstandingtext): 0061 self.tb.AppendText(self.outstandingtext) 0062 self.outstandingtext="" 0063 self.tb.ScrollLines(-1) 0064 0065 def log(self, str): 0066 now=time.time() 0067 t=time.localtime(now) 0068 if len(str)==0 or str[0]=="&": # already has time etc stamps 0069 self.outstandingtext+=str[1:]+"\r\n" 0070 else: 0071 self.outstandingtext+="%d:%02d:%02d.%03d: %s\r\n" % ( t[3], t[4], t[5], int((now-int(now))*1000), str) 0072 0073 def logexception(self, excinfo=None): 0074 str=formatexception(excinfo) 0075 self.log(str) 0076 0077 0078 def formatexception(excinfo=None, lastframes=8): 0079 """Pretty print exception, including local variable information. 0080 0081 See Python Cookbook, recipe 14.4. 0082 0083 @param excinfo: tuple of information returned from sys.exc_info when 0084 the exception occurred. If you don't supply this then 0085 information about the current exception being handled 0086 is used 0087 @param lastframes: local variables are shown for these number of 0088 frames 0089 @return: A pretty printed string 0090 """ 0091 if excinfo is None: 0092 excinfo=sys.exc_info() 0093 0094 s=StringIO.StringIO() 0095 traceback.print_exception(*excinfo, **{'file': s}) 0096 tb=excinfo[2] 0097 0098 while True: 0099 if not tb.tb_next: 0100 break 0101 tb=tb.tb_next 0102 stack=[] 0103 f=tb.tb_frame 0104 while f: 0105 stack.append(f) 0106 f=f.f_back 0107 stack.reverse() 0108 if len(stack)>lastframes: 0109 stack=stack[-lastframes:] 0110 print >>s, "\nVariables by last %d frames, innermost last" % (lastframes,) 0111 for frame in stack: 0112 print >>s, "" 0113 print >>s, "Frame %s in %s at line %s" % (frame.f_code.co_name, 0114 frame.f_code.co_filename, 0115 frame.f_lineno) 0116 for key,value in frame.f_locals.items(): 0117 # filter out modules 0118 if type(value)==type(sys): 0119 continue 0120 print >>s,"%15s = " % (key,), 0121 try: 0122 print >>s,`value`[:160] 0123 except: 0124 print >>s,"(Exception occurred printing value)" 0125 return s.getvalue() 0126 0127 0128 # Where to find bitmaps etc 0129 if IsMac(): 0130 p=os.getcwd() 0131 else: 0132 p=sys.path[0] 0133 if p.lower().endswith(".zip"): # zip importer in action 0134 p=os.path.dirname(p) 0135 resourcedirectory=os.path.join(os.path.abspath(p), "resources") 0136 0137 def getresourcefile(filename): 0138 """Returns name of file by adding it to resource directory pathname 0139 0140 No attempt is made to verify the file exists 0141 @rtype: string 0142 """ 0143 return os.path.join(resourcedirectory, filename) 0144 0145 def run(*args): 0146 """Execute the command. 0147 0148 The path is searched""" 0149 sl=os.spawnl 0150 if sys.platform!='win32': 0151 sl=os.spawnlp 0152 ret=apply(sl, (os.P_WAIT,args[0])+args) 0153 else: 0154 # win98 was fine with above code, winxp just chokes 0155 # so we call system() instead 0156 str="" 0157 for a in args: 0158 if len(a)==0: 0159 str+=' ""' 0160 elif a.find(' ')>=0: 0161 str+=' "'+a+'"' 0162 else: 0163 str+=" "+a 0164 str=str[1:] # remove first space 0165 # If you ever wanted proof how idiotic windows is, here it is 0166 # if there is a value enclosed in double quotes, it is 0167 # taken as the window title, even if it comes after all 0168 # the switches, so i have to supply one, otherwise it mistakes 0169 # the command to run as the window title 0170 ret=os.system('start /b /wait "%s" %s' % (args[0], str)) 0171 return ret 0172
Generated by PyXR 0.9.4