Package bitfling :: Module guihelper
[hide private]
[frames] | no frames]

Source Code for Module bitfling.guihelper

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2004 Roger Binns <rogerb@rogerbinns.com> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: guihelper.py 1638 2004-10-07 03:21:09Z n9yty $ 
  9   
 10  """Various convenience functions and widgets to assist the gui""" 
 11   
 12  import time 
 13  import os 
 14  import sys 
 15  import StringIO 
 16  import traceback 
 17   
 18   
 19  import wx 
 20   
 21  # some library routines 
22 -def IsMSWindows():
23 """Are we running on Windows? 24 25 @rtype: Bool""" 26 return wx.Platform=='__WXMSW__'
27
28 -def IsGtk():
29 """Are we running on GTK (Linux) 30 31 @rtype: Bool""" 32 return wx.Platform=='__WXGTK__'
33
34 -def IsMac():
35 """Are we running on Mac 36 37 @rtype: Bool""" 38 return wx.Platform=='__WXMAC__'
39
40 -class LogWindow(wx.Panel):
41
42 - def __init__(self, parent):
43 wx.Panel.__init__(self,parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE) 44 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) 45 f=wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL ) 46 ta=wx.TextAttr(font=f) 47 self.tb.SetDefaultStyle(ta) 48 self.sizer=wx.BoxSizer(wx.VERTICAL) 49 self.sizer.Add(self.tb, 1, wx.EXPAND) 50 self.SetSizer(self.sizer) 51 self.SetAutoLayout(True) 52 self.sizer.Fit(self) 53 wx.EVT_IDLE(self, self.OnIdle) 54 self.outstandingtext=""
55
56 - def Clear(self):
57 self.tb.Clear()
58
59 - def OnIdle(self,_):
60 if len(self.outstandingtext): 61 self.tb.AppendText(self.outstandingtext) 62 self.outstandingtext="" 63 self.tb.ScrollLines(-1)
64
65 - def log(self, str):
66 now=time.time() 67 t=time.localtime(now) 68 if len(str)==0 or str[0]=="&": # already has time etc stamps 69 self.outstandingtext+=str[1:]+"\r\n" 70 else: 71 self.outstandingtext+="%d:%02d:%02d.%03d: %s\r\n" % ( t[3], t[4], t[5], int((now-int(now))*1000), str)
72
73 - def logexception(self, excinfo=None):
74 str=formatexception(excinfo) 75 self.log(str)
76 77
78 -def formatexception(excinfo=None, lastframes=8):
79 """Pretty print exception, including local variable information. 80 81 See Python Cookbook, recipe 14.4. 82 83 @param excinfo: tuple of information returned from sys.exc_info when 84 the exception occurred. If you don't supply this then 85 information about the current exception being handled 86 is used 87 @param lastframes: local variables are shown for these number of 88 frames 89 @return: A pretty printed string 90 """ 91 if excinfo is None: 92 excinfo=sys.exc_info() 93 94 s=StringIO.StringIO() 95 traceback.print_exception(*excinfo, **{'file': s}) 96 tb=excinfo[2] 97 98 while True: 99 if not tb.tb_next: 100 break 101 tb=tb.tb_next 102 stack=[] 103 f=tb.tb_frame 104 while f: 105 stack.append(f) 106 f=f.f_back 107 stack.reverse() 108 if len(stack)>lastframes: 109 stack=stack[-lastframes:] 110 print >>s, "\nVariables by last %d frames, innermost last" % (lastframes,) 111 for frame in stack: 112 print >>s, "" 113 print >>s, "Frame %s in %s at line %s" % (frame.f_code.co_name, 114 frame.f_code.co_filename, 115 frame.f_lineno) 116 for key,value in frame.f_locals.items(): 117 # filter out modules 118 if type(value)==type(sys): 119 continue 120 print >>s,"%15s = " % (key,), 121 try: 122 print >>s,`value`[:160] 123 except: 124 print >>s,"(Exception occurred printing value)" 125 return s.getvalue()
126 127 128 # Where to find bitmaps etc 129 if IsMac(): 130 p=os.getcwd() 131 else: 132 p=sys.path[0] 133 if p.lower().endswith(".zip"): # zip importer in action 134 p=os.path.dirname(p) 135 resourcedirectory=os.path.join(os.path.abspath(p), "resources") 136
137 -def getresourcefile(filename):
138 """Returns name of file by adding it to resource directory pathname 139 140 No attempt is made to verify the file exists 141 @rtype: string 142 """ 143 return os.path.join(resourcedirectory, filename)
144
145 -def run(*args):
146 """Execute the command. 147 148 The path is searched""" 149 sl=os.spawnl 150 if sys.platform!='win32': 151 sl=os.spawnlp 152 ret=apply(sl, (os.P_WAIT,args[0])+args) 153 else: 154 # win98 was fine with above code, winxp just chokes 155 # so we call system() instead 156 str="" 157 for a in args: 158 if len(a)==0: 159 str+=' ""' 160 elif a.find(' ')>=0: 161 str+=' "'+a+'"' 162 else: 163 str+=" "+a 164 str=str[1:] # remove first space 165 # If you ever wanted proof how idiotic windows is, here it is 166 # if there is a value enclosed in double quotes, it is 167 # taken as the window title, even if it comes after all 168 # the switches, so i have to supply one, otherwise it mistakes 169 # the command to run as the window title 170 ret=os.system('start /b /wait "%s" %s' % (args[0], str)) 171 return ret
172