- Timestamp:
- 04/20/06 10:50:32 (19 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykoticon/trunk/bin/pykoticon
r114 r119 1 1 #! /usr/bin/env python 2 2 # -*- coding: ISO-8859-15 -*- 3 4 """PyKotIcon is a generic, networked, cross-platform dialog box manager.""" 3 5 4 6 # PyKotIcon - Client side helper for PyKota … … 19 21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 20 22 # 21 # $Id$22 23 # 23 # 24 25 __version__ = "1.01" 26 __author__ = "Jerome Alet" 27 __author_email__ = "alet@librelogiciel.com" 28 __license__ = "GNU GPL" 29 __url__ = "http://www.librelogiciel.com/software/" 30 __revision__ = "$Id$" 24 31 25 32 import sys 26 33 import os 34 import time 27 35 import urllib 28 36 import urllib2 … … 34 42 import SimpleXMLRPCServer 35 43 36 import time37 44 38 45 if sys.platform == "win32" : 39 isWindows = 146 isWindows = True 40 47 try : 41 48 import win32api … … 45 52 iconsdir = os.path.split(sys.argv[0])[0] 46 53 else : 47 isWindows = 054 isWindows = False 48 55 iconsdir = "/usr/share/pykoticon" # TODO : change this 49 56 import pwd … … 51 58 try : 52 59 import wx 53 hasWxPython = 160 hasWxPython = True 54 61 except ImportError : 55 hasWxPython = 062 hasWxPython = False 56 63 raise ImportError, "wxPython is missing. Please install it." 57 64 58 aboutbox = """PyKotIcon v1.00 (c) 2003, 2004, 2005, 2006 Jerome Alet - alet@librelogiciel.com 59 60 PyKotIcon is a client side print quota notifier for PyKota, but it 65 aboutbox = """PyKotIcon v%(__version__)s (c) 2003-2006 %(__author__)s - %(__author_email__)s 66 67 PyKotIcon is generic, networked, cross-platform dialog box manager. 68 69 It is often used as a client side companion for PyKota, but it 61 70 can be used from other applications if you want. 62 71 … … 75 84 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.""" 76 85 77 def getCurrentUserName() :78 """Retrieves the current user's name."""79 if isWindows :80 return win32api.GetUserName()81 else :82 try :83 return pwd.getpwuid(os.geteuid())[0]84 except :85 return "** Unknown **"86 86 87 87 class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer) : … … 110 110 """Asks some textual datas defined by a list of labels, a list of variables' names and a list of variables values in a mapping.""" 111 111 values = {} 112 for (k , v) in varvalues.items() :113 values[k ] = self.frame.UTF8ToUserCharset(v.data)114 wx.CallAfter(self.frame.askDatas, [ self.frame.UTF8ToUserCharset( v.data) for vin labels ], \112 for (key, value) in varvalues.items() : 113 values[key] = self.frame.UTF8ToUserCharset(value.data) 114 wx.CallAfter(self.frame.askDatas, [ self.frame.UTF8ToUserCharset(label.data) for label in labels ], \ 115 115 varnames, \ 116 116 values) … … 119 119 time.sleep(0.1) 120 120 retcode = self.frame.dialogAnswer 121 for (k , v) in retcode.items() :122 if k != "isValid" :123 retcode[k ] = xmlrpclib.Binary(self.frame.userCharsetToUTF8(v))121 for (key, value) in retcode.items() : 122 if key != "isValid" : 123 retcode[key] = xmlrpclib.Binary(self.frame.userCharsetToUTF8(value)) 124 124 self.frame.dialogAnswer = None # prepare for next call, just in case 125 125 return retcode … … 164 164 self.server_close() 165 165 sys.exit(0) 166 166 167 167 168 class GenericInputDialog(wx.Dialog) : … … 203 204 self.Layout() 204 205 206 205 207 class PyKotIcon(wx.Frame): 206 208 """Main class.""" … … 208 210 self.dialogAnswer = None 209 211 wx.Frame.__init__(self, parent, id, \ 210 _("PyKotIcon info for %s") % getCurrentUserName(), \212 _("PyKotIcon info for %s") % self.getCurrentUserName(), \ 211 213 size = (1, 1), \ 212 214 style = wx.FRAME_NO_TASKBAR | wx.NO_FULL_REPAINT_ON_RESIZE) … … 239 241 self.Show(True) 240 242 243 def getCurrentUserName(self) : 244 """Retrieves the current user's name.""" 245 if isWindows : 246 return win32api.GetUserName() 247 else : 248 try : 249 return pwd.getpwuid(os.geteuid())[0] 250 except : 251 return "** Unknown **" 252 241 253 def OnIconify(self, event) : 242 254 if not self.IsIconized() : … … 265 277 def OnAbout(self, event) : 266 278 """Displays the about box.""" 267 dialog = wx.MessageDialog(self, aboutbox , _("About"), wx.OK | wx.ICON_INFORMATION)279 dialog = wx.MessageDialog(self, aboutbox % globals(), _("About"), wx.OK | wx.ICON_INFORMATION) 268 280 dialog.ShowModal() 269 281 dialog.Destroy() … … 319 331 self.charset = charset 320 332 self.port = localport 321 self.server = MyXMLRPCServer(self, printserver, localport , debug=True)333 self.server = MyXMLRPCServer(self, printserver, localport) 322 334 323 335 def UTF8ToUserCharset(self, text) : … … 325 337 if text is not None : 326 338 try : 327 return unicode(text, "UTF-8").encode(self.charset)328 except (UnicodeError, TypeError) :339 return text.decode("UTF-8").encode(self.charset, "replace") 340 except (UnicodeError, AttributeError) : 329 341 try : 330 # Incorrect locale settings ? 331 return unicode(text, "UTF-8").encode("ISO-8859-15") 332 except (UnicodeError, TypeError) : 333 try : 334 return text.encode(self.charset) 335 except (UnicodeError, TypeError, AttributeError) : 336 pass 342 # Maybe already in Unicode 343 return text.encode(self.charset, "replace") 344 except (UnicodeError, AttributeError) : 345 pass # Don't know what to do 337 346 return text 338 347 … … 341 350 if text is not None : 342 351 try : 343 return unicode(text, self.charset).encode("UTF-8") 344 except (UnicodeError, TypeError) : 352 # We don't necessarily trust the default charset, because 353 # xprint sends us titles in UTF-8 but CUPS gives us an ISO-8859-1 charset ! 354 # So we first try to see if the text is already in UTF-8 or not, and 355 # if it is, we delete characters which can't be converted to the user's charset, 356 # then convert back to UTF-8. PostgreSQL 7.3.x used to reject some unicode characters, 357 # this is fixed by the ugly line below : 358 return text.decode("UTF-8").encode(self.charset, "replace").decode(self.charset).encode("UTF-8", "replace") 359 except (UnicodeError, AttributeError) : 345 360 try : 346 # Incorrect locale settings ? 347 return unicode(text, "ISO-8859-15").encode("UTF-8") 348 except (UnicodeError, TypeError) : 361 return text.decode(self.charset).encode("UTF-8", "replace") 362 except (UnicodeError, AttributeError) : 349 363 try : 350 return text.encode("UTF-8") 351 except (UnicodeError, TypeError, AttributeError) : 352 pass 364 # Maybe already in Unicode 365 return text.encode("UTF-8", "replace") 366 except (UnicodeError, AttributeError) : 367 pass # Don't know what to do 353 368 return text 369 354 370 355 371 class PyKotIconApp(wx.App): … … 363 379 """Continues processing.""" 364 380 self.frame.postInit(charset, printserver, localport) 381 365 382 366 383 def main(printserver, localport): … … 400 417 app.MainLoop() 401 418 419 402 420 def crashed() : 403 421 """Minimal crash method.""" … … 410 428 sys.stderr.flush() 411 429 430 412 431 if __name__ == '__main__': 413 432 if len(sys.argv) >= 2 : 414 433 arg = sys.argv[1] 415 434 if arg in ("-v", "--version") : 416 print "0.3"435 print __version__ 417 436 elif arg in ("-h", "--help") : 418 437 sys.stderr.write("usage : pykoticon pykota_server_hostname_or_ip_address localTCPPort\n")