[47] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
[119] | 4 | """PyKotIcon is a generic, networked, cross-platform dialog box manager.""" |
---|
| 5 | |
---|
[138] | 6 | # PyKotIcon - Client side helper for PyKota and other applications |
---|
[47] | 7 | # |
---|
[176] | 8 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[47] | 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | # |
---|
| 23 | # |
---|
| 24 | |
---|
[167] | 25 | __version__ = "1.03" |
---|
[119] | 26 | __author__ = "Jerome Alet" |
---|
| 27 | __author_email__ = "alet@librelogiciel.com" |
---|
| 28 | __license__ = "GNU GPL" |
---|
[137] | 29 | __url__ = "http://www.pykota.com/software/pykoticon" |
---|
[119] | 30 | __revision__ = "$Id$" |
---|
| 31 | |
---|
[47] | 32 | import sys |
---|
[63] | 33 | import os |
---|
[119] | 34 | import time |
---|
[47] | 35 | import urllib |
---|
| 36 | import urllib2 |
---|
[65] | 37 | import locale |
---|
| 38 | import gettext |
---|
[87] | 39 | import socket |
---|
| 40 | import threading |
---|
[88] | 41 | import xmlrpclib |
---|
[87] | 42 | import SimpleXMLRPCServer |
---|
[177] | 43 | import SocketServer |
---|
| 44 | |
---|
[123] | 45 | try : |
---|
| 46 | import optparse |
---|
| 47 | except ImportError : |
---|
| 48 | sys.stderr.write("You need Python v2.3 or higher for PyKotIcon to work.\nAborted.\n") |
---|
| 49 | sys.exit(-1) |
---|
[47] | 50 | |
---|
[65] | 51 | if sys.platform == "win32" : |
---|
[119] | 52 | isWindows = True |
---|
[65] | 53 | try : |
---|
| 54 | import win32api |
---|
| 55 | except ImportError : |
---|
| 56 | raise ImportError, "Mark Hammond's Win32 Extensions are missing. Please install them." |
---|
[76] | 57 | else : |
---|
[85] | 58 | iconsdir = os.path.split(sys.argv[0])[0] |
---|
[65] | 59 | else : |
---|
[119] | 60 | isWindows = False |
---|
[76] | 61 | iconsdir = "/usr/share/pykoticon" # TODO : change this |
---|
[65] | 62 | import pwd |
---|
[57] | 63 | |
---|
[58] | 64 | try : |
---|
| 65 | import wx |
---|
[119] | 66 | hasWxPython = True |
---|
[58] | 67 | except ImportError : |
---|
[119] | 68 | hasWxPython = False |
---|
[75] | 69 | raise ImportError, "wxPython is missing. Please install it." |
---|
[58] | 70 | |
---|
[119] | 71 | aboutbox = """PyKotIcon v%(__version__)s (c) 2003-2006 %(__author__)s - %(__author_email__)s |
---|
[111] | 72 | |
---|
[119] | 73 | PyKotIcon is generic, networked, cross-platform dialog box manager. |
---|
| 74 | |
---|
| 75 | It is often used as a client side companion for PyKota, but it |
---|
[111] | 76 | can be used from other applications if you want. |
---|
| 77 | |
---|
| 78 | This program is free software; you can redistribute it and/or modify |
---|
| 79 | it under the terms of the GNU General Public License as published by |
---|
| 80 | the Free Software Foundation; either version 2 of the License, or |
---|
| 81 | (at your option) any later version. |
---|
| 82 | |
---|
| 83 | This program is distributed in the hope that it will be useful, |
---|
| 84 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 85 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 86 | GNU General Public License for more details. |
---|
| 87 | |
---|
| 88 | You should have received a copy of the GNU General Public License |
---|
| 89 | along with this program; if not, write to the Free Software |
---|
| 90 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.""" |
---|
| 91 | |
---|
[177] | 92 | |
---|
| 93 | class ThreadedXMLRPCServer(SocketServer.ThreadingTCPServer, SimpleXMLRPCServer.SimpleXMLRPCDispatcher) : |
---|
| 94 | """Base class to have a threaded XMLRPC Server.""" |
---|
| 95 | def __init__(self, addr, requestHandler=SimpleXMLRPCServer.SimpleXMLRPCRequestHandler, logRequests=False) : |
---|
| 96 | """Imitate what is in SimpleXMLRPCServer.py but with a threaded TCP server instead.""" |
---|
| 97 | self.logRequests = logRequests |
---|
[179] | 98 | try : |
---|
| 99 | SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self) |
---|
| 100 | except TypeError : |
---|
| 101 | SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, False, None) |
---|
[177] | 102 | SocketServer.ThreadingTCPServer.__init__(self, addr, requestHandler) |
---|
[65] | 103 | |
---|
[177] | 104 | class MyXMLRPCServer(ThreadedXMLRPCServer) : |
---|
[87] | 105 | """My own server class.""" |
---|
[88] | 106 | allow_reuse_address = True |
---|
[123] | 107 | def __init__(self, frame, options, arguments) : |
---|
[177] | 108 | ThreadedXMLRPCServer.__init__(self, \ |
---|
| 109 | ('0.0.0.0', options.port), \ |
---|
| 110 | SimpleXMLRPCServer.SimpleXMLRPCRequestHandler, \ |
---|
| 111 | options.debug) |
---|
[87] | 112 | self.frame = frame |
---|
[123] | 113 | self.debug = options.debug |
---|
[146] | 114 | self.cacheduration = options.cache |
---|
| 115 | self.cache = {} |
---|
[123] | 116 | self.printServers = [ socket.gethostbyname(arg) for arg in arguments ] |
---|
[124] | 117 | if "127.0.0.1" not in self.printServers : |
---|
| 118 | self.printServers.append("127.0.0.1") # to allow clean shutdown |
---|
[87] | 119 | loop = threading.Thread(target=self.mainloop) |
---|
| 120 | loop.start() |
---|
[56] | 121 | |
---|
[88] | 122 | def logDebug(self, message) : |
---|
| 123 | """Logs a debug message if debug mode is active.""" |
---|
| 124 | if self.debug : |
---|
| 125 | sys.stderr.write("%s\n" % message) |
---|
| 126 | |
---|
[146] | 127 | def getAnswerFromCache(self, key) : |
---|
| 128 | """Tries to extract a value from the cache and returns it if still valid.""" |
---|
| 129 | cacheentry = self.cache.get(key) |
---|
| 130 | if cacheentry is not None : |
---|
| 131 | (birth, value) = cacheentry |
---|
| 132 | if (time.time() - birth) < self.cacheduration : |
---|
[147] | 133 | self.logDebug("Cache hit for %s" % str(key)) |
---|
[146] | 134 | return value # NB : we don't extend the life of this entry |
---|
| 135 | else : |
---|
| 136 | self.logDebug("Cache expired for %s" % str(key)) |
---|
| 137 | else : |
---|
| 138 | self.logDebug("Cache miss for %s" % str(key)) |
---|
| 139 | return None |
---|
[55] | 140 | |
---|
[146] | 141 | def storeAnswerInCache(self, key, value) : |
---|
| 142 | """Stores an entry in the cache.""" |
---|
| 143 | self.cache[key] = (time.time(), value) |
---|
[147] | 144 | self.logDebug("Cache store for %s" % str(key)) |
---|
[146] | 145 | |
---|
[103] | 146 | def export_askDatas(self, labels, varnames, varvalues) : |
---|
| 147 | """Asks some textual datas defined by a list of labels, a list of variables' names and a list of variables values in a mapping.""" |
---|
[107] | 148 | values = {} |
---|
[119] | 149 | for (key, value) in varvalues.items() : |
---|
| 150 | values[key] = self.frame.UTF8ToUserCharset(value.data) |
---|
[146] | 151 | cachekey = tuple(values.items()) |
---|
| 152 | retcode = self.getAnswerFromCache(cachekey) |
---|
[154] | 153 | if (retcode is None) or (not retcode["isValid"]) : |
---|
[146] | 154 | wx.CallAfter(self.frame.askDatas, [ self.frame.UTF8ToUserCharset(label.data) for label in labels ], \ |
---|
| 155 | varnames, \ |
---|
| 156 | values) |
---|
| 157 | # ugly, isn't it ? |
---|
| 158 | while self.frame.dialogAnswer is None : |
---|
| 159 | time.sleep(0.1) |
---|
| 160 | retcode = self.frame.dialogAnswer |
---|
| 161 | for (key, value) in retcode.items() : |
---|
| 162 | if key != "isValid" : |
---|
| 163 | retcode[key] = xmlrpclib.Binary(self.frame.userCharsetToUTF8(value)) |
---|
| 164 | self.frame.dialogAnswer = None # prepare for next call, just in case |
---|
| 165 | self.storeAnswerInCache(cachekey, retcode) |
---|
[105] | 166 | return retcode |
---|
[102] | 167 | |
---|
[146] | 168 | def export_quitApplication(self) : |
---|
| 169 | """Makes the application quit.""" |
---|
| 170 | self.frame.quitEvent.set() |
---|
| 171 | wx.CallAfter(self.frame.OnClose, None) |
---|
| 172 | return True |
---|
| 173 | |
---|
[100] | 174 | def export_showDialog(self, message, yesno) : |
---|
| 175 | """Opens a notification or confirmation dialog.""" |
---|
[112] | 176 | wx.CallAfter(self.frame.showDialog, self.frame.UTF8ToUserCharset(message.data), yesno) |
---|
[92] | 177 | # ugly, isn't it ? |
---|
[100] | 178 | while self.frame.dialogAnswer is None : |
---|
[97] | 179 | time.sleep(0.1) |
---|
[100] | 180 | retcode = self.frame.dialogAnswer |
---|
| 181 | self.frame.dialogAnswer = None # prepare for next call, just in case |
---|
[91] | 182 | return retcode |
---|
| 183 | |
---|
[88] | 184 | def export_nop(self) : |
---|
| 185 | """Does nothing, but allows a clean shutdown from the frame itself.""" |
---|
| 186 | return True |
---|
| 187 | |
---|
[138] | 188 | def _dispatch(self, method, params) : |
---|
| 189 | """Ensure that only export_* methods are available.""" |
---|
| 190 | return getattr(self, "export_%s" % method)(*params) |
---|
| 191 | |
---|
| 192 | def handle_error(self, request, client_address) : |
---|
| 193 | """Doesn't display an ugly traceback in case an error occurs.""" |
---|
| 194 | self.logDebug("An exception occured while handling an incoming request from %s:%s" % (client_address[0], client_address[1])) |
---|
| 195 | |
---|
[87] | 196 | def verify_request(self, request, client_address) : |
---|
| 197 | """Ensures that requests which don't come from the print server are rejected.""" |
---|
| 198 | (client, port) = client_address |
---|
[123] | 199 | if client in self.printServers : |
---|
[88] | 200 | self.logDebug("%s accepted." % client) |
---|
[87] | 201 | return True |
---|
| 202 | else : |
---|
| 203 | # Unauthorized access ! |
---|
[88] | 204 | self.logDebug("%s rejected." % client) |
---|
[87] | 205 | return False |
---|
[55] | 206 | |
---|
[87] | 207 | def mainloop(self) : |
---|
| 208 | """XML-RPC Server's main loop.""" |
---|
[102] | 209 | self.register_function(self.export_askDatas) |
---|
[100] | 210 | self.register_function(self.export_showDialog) |
---|
[87] | 211 | self.register_function(self.export_quitApplication) |
---|
[88] | 212 | self.register_function(self.export_nop) |
---|
[87] | 213 | while not self.frame.quitEvent.isSet() : |
---|
| 214 | self.handle_request() |
---|
[88] | 215 | self.server_close() |
---|
[87] | 216 | sys.exit(0) |
---|
[47] | 217 | |
---|
[119] | 218 | |
---|
[105] | 219 | class GenericInputDialog(wx.Dialog) : |
---|
[104] | 220 | """Generic input dialog box.""" |
---|
| 221 | def __init__(self, parent, id, labels, varnames, varvalues): |
---|
[105] | 222 | wx.Dialog.__init__(self, parent, id, \ |
---|
[104] | 223 | _("PyKotIcon data input"), \ |
---|
[105] | 224 | style = wx.CAPTION \ |
---|
| 225 | | wx.THICK_FRAME \ |
---|
[104] | 226 | | wx.STAY_ON_TOP \ |
---|
[105] | 227 | | wx.DIALOG_MODAL) |
---|
| 228 | |
---|
[106] | 229 | self.variables = [] |
---|
[104] | 230 | vsizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 231 | for i in range(len(varnames)) : |
---|
| 232 | varname = varnames[i] |
---|
| 233 | try : |
---|
| 234 | label = labels[i] |
---|
| 235 | except IndexError : |
---|
| 236 | label = "" |
---|
| 237 | labelid = wx.NewId() |
---|
| 238 | varid = wx.NewId() |
---|
[136] | 239 | labelst = wx.StaticText(self, labelid, label) |
---|
[106] | 240 | if varname.lower().find("password") != -1 : |
---|
| 241 | variable = wx.TextCtrl(self, varid, varvalues.get(varname, ""), style=wx.TE_PASSWORD) |
---|
| 242 | else : |
---|
| 243 | variable = wx.TextCtrl(self, varid, varvalues.get(varname, "")) |
---|
| 244 | self.variables.append(variable) |
---|
[104] | 245 | hsizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[136] | 246 | hsizer.Add(labelst, 0, wx.ALIGN_CENTER | wx.ALIGN_RIGHT | wx.ALL, 5) |
---|
[104] | 247 | hsizer.Add(variable, 0, wx.ALIGN_CENTER | wx.ALIGN_LEFT | wx.ALL, 5) |
---|
| 248 | vsizer.Add(hsizer, 0, wx.ALIGN_CENTER | wx.ALL, 5) |
---|
| 249 | |
---|
[105] | 250 | okbutton = wx.Button(self, wx.ID_OK, "OK") |
---|
[104] | 251 | vsizer.Add(okbutton, 0, wx.ALIGN_CENTER | wx.ALL, 5) |
---|
[172] | 252 | if self.variables : |
---|
| 253 | self.variables[0].SetFocus() |
---|
[104] | 254 | self.SetAutoLayout(True) |
---|
| 255 | self.SetSizerAndFit(vsizer) |
---|
| 256 | self.Layout() |
---|
| 257 | |
---|
[119] | 258 | |
---|
[87] | 259 | class PyKotIcon(wx.Frame): |
---|
[63] | 260 | """Main class.""" |
---|
| 261 | def __init__(self, parent, id): |
---|
[100] | 262 | self.dialogAnswer = None |
---|
[104] | 263 | wx.Frame.__init__(self, parent, id, \ |
---|
[119] | 264 | _("PyKotIcon info for %s") % self.getCurrentUserName(), \ |
---|
[124] | 265 | size = (0, 0), \ |
---|
[111] | 266 | style = wx.FRAME_NO_TASKBAR | wx.NO_FULL_REPAINT_ON_RESIZE) |
---|
[114] | 267 | |
---|
[119] | 268 | def getCurrentUserName(self) : |
---|
| 269 | """Retrieves the current user's name.""" |
---|
[180] | 270 | try : |
---|
| 271 | if isWindows : |
---|
| 272 | return win32api.GetUserName() |
---|
| 273 | else : |
---|
[119] | 274 | return pwd.getpwuid(os.geteuid())[0] |
---|
[180] | 275 | except : |
---|
| 276 | return "** Unknown **" |
---|
[119] | 277 | |
---|
[59] | 278 | def OnIconify(self, event) : |
---|
[124] | 279 | """Iconify/De-iconify the application.""" |
---|
[104] | 280 | if not self.IsIconized() : |
---|
| 281 | self.Iconize(True) |
---|
[111] | 282 | self.Hide() |
---|
[58] | 283 | |
---|
[59] | 284 | def OnTaskBarActivate(self, event) : |
---|
[124] | 285 | """Show the application if it is minimized.""" |
---|
[104] | 286 | if self.IsIconized() : |
---|
| 287 | self.Iconize(False) |
---|
[59] | 288 | if not self.IsShown() : |
---|
[58] | 289 | self.Show(True) |
---|
| 290 | self.Raise() |
---|
| 291 | |
---|
[64] | 292 | def OnClose(self, event) : |
---|
[124] | 293 | """Cleanly quit the application.""" |
---|
[160] | 294 | if (event is None) \ |
---|
| 295 | or self.options.allowquit : |
---|
[138] | 296 | self.closeServer() |
---|
| 297 | self.menu.Destroy() |
---|
| 298 | self.tbicon.Destroy() |
---|
| 299 | self.Destroy() |
---|
[160] | 300 | return True |
---|
[138] | 301 | else : |
---|
[160] | 302 | # self.quitIsForbidden() |
---|
| 303 | return False |
---|
[58] | 304 | |
---|
[91] | 305 | def OnTaskBarMenu(self, event) : |
---|
[124] | 306 | """Open the taskbar menu.""" |
---|
[114] | 307 | self.tbicon.PopupMenu(self.menu) |
---|
[58] | 308 | |
---|
[91] | 309 | def OnTaskBarClose(self, event) : |
---|
[124] | 310 | """React to close from the taskbar.""" |
---|
[160] | 311 | self.Close() |
---|
[124] | 312 | |
---|
[127] | 313 | def quitIsForbidden(self) : |
---|
[124] | 314 | """Displays a message indicating that quitting the application is not allowed.""" |
---|
[138] | 315 | message = _("Sorry, this was forbidden by your system administrator.") |
---|
| 316 | caption = _("Information") |
---|
| 317 | style = wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP |
---|
| 318 | dialog = wx.MessageDialog(self, message, caption, style) |
---|
| 319 | dialog.ShowModal() |
---|
| 320 | dialog.Destroy() |
---|
[91] | 321 | |
---|
[111] | 322 | def OnAbout(self, event) : |
---|
| 323 | """Displays the about box.""" |
---|
[128] | 324 | dialog = wx.MessageDialog(self, aboutbox % globals(), \ |
---|
| 325 | _("About"), \ |
---|
| 326 | wx.OK | wx.ICON_INFORMATION) |
---|
[174] | 327 | dialog.Raise() |
---|
[111] | 328 | dialog.ShowModal() |
---|
| 329 | dialog.Destroy() |
---|
| 330 | |
---|
[100] | 331 | def showDialog(self, message, yesno) : |
---|
| 332 | """Opens a notification dialog.""" |
---|
| 333 | self.dialogAnswer = None |
---|
| 334 | if yesno : |
---|
| 335 | caption = _("Confirmation") |
---|
| 336 | style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION |
---|
| 337 | else : |
---|
| 338 | caption = _("Information") |
---|
| 339 | style = wx.OK | wx.ICON_INFORMATION |
---|
| 340 | style |= wx.STAY_ON_TOP |
---|
| 341 | dialog = wx.MessageDialog(self, message, caption, style) |
---|
[174] | 342 | dialog.Raise() |
---|
[100] | 343 | self.dialogAnswer = ((dialog.ShowModal() == wx.ID_NO) and "CANCEL") or "OK" |
---|
| 344 | dialog.Destroy() |
---|
[91] | 345 | |
---|
[103] | 346 | def askDatas(self, labels, varnames, varvalues) : |
---|
[102] | 347 | """Opens a dialog box asking for data entry.""" |
---|
[105] | 348 | self.dialogAnswer = None |
---|
| 349 | dialog = GenericInputDialog(self, wx.ID_ANY, labels, varnames, varvalues) |
---|
[174] | 350 | dialog.Raise() |
---|
[106] | 351 | retvalues = {} |
---|
| 352 | if dialog.ShowModal() == wx.ID_OK : |
---|
| 353 | retvalues["isValid"] = True |
---|
| 354 | for i in range(len(varnames)) : |
---|
| 355 | retvalues[varnames[i]] = dialog.variables[i].GetValue() |
---|
| 356 | else : |
---|
| 357 | retvalues["isValid"] = False |
---|
| 358 | for k in varvalues.keys() : |
---|
| 359 | retvalues[k] = "" |
---|
| 360 | self.dialogAnswer = retvalues |
---|
[105] | 361 | dialog.Destroy() |
---|
[104] | 362 | |
---|
| 363 | def closeServer(self) : |
---|
| 364 | """Tells the xml-rpc server to exit.""" |
---|
| 365 | if not self.quitEvent.isSet() : |
---|
| 366 | self.quitEvent.set() |
---|
[124] | 367 | server = xmlrpclib.ServerProxy("http://localhost:%s" % self.options.port) |
---|
[104] | 368 | try : |
---|
| 369 | # wake the server with an empty request |
---|
| 370 | # for it to see the event object |
---|
| 371 | # which has just been set |
---|
| 372 | server.nop() |
---|
| 373 | except : |
---|
| 374 | # Probably already stopped |
---|
| 375 | pass |
---|
| 376 | |
---|
[123] | 377 | def postInit(self, charset, options, arguments) : |
---|
[104] | 378 | """Starts the XML-RPC server.""" |
---|
[112] | 379 | self.charset = charset |
---|
[124] | 380 | self.options = options |
---|
[160] | 381 | |
---|
| 382 | self.tbicon = wx.TaskBarIcon() |
---|
| 383 | self.greenicon = wx.Icon(os.path.join(iconsdir, "pykoticon-green.ico"), \ |
---|
| 384 | wx.BITMAP_TYPE_ICO) |
---|
| 385 | self.redicon = wx.Icon(os.path.join(iconsdir, "pykoticon-red.ico"), \ |
---|
| 386 | wx.BITMAP_TYPE_ICO) |
---|
| 387 | self.tbicon.SetIcon(self.greenicon, "PyKotIcon") |
---|
| 388 | |
---|
| 389 | wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate) |
---|
| 390 | wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu) |
---|
| 391 | |
---|
| 392 | self.TBMENU_ABOUT = wx.NewId() |
---|
| 393 | self.TBMENU_RESTORE = wx.NewId() |
---|
| 394 | self.TBMENU_CLOSE = wx.NewId() |
---|
| 395 | wx.EVT_MENU(self.tbicon, self.TBMENU_ABOUT, \ |
---|
| 396 | self.OnAbout) |
---|
| 397 | wx.EVT_MENU(self.tbicon, self.TBMENU_RESTORE, \ |
---|
| 398 | self.OnTaskBarActivate) |
---|
| 399 | wx.EVT_MENU(self.tbicon, self.TBMENU_CLOSE, \ |
---|
| 400 | self.OnTaskBarClose) |
---|
| 401 | self.menu = wx.Menu() |
---|
| 402 | self.menu.Append(self.TBMENU_ABOUT, _("About"), _("About this software")) |
---|
| 403 | if options.allowquit : |
---|
| 404 | self.menu.Append(self.TBMENU_CLOSE, _("Quit"), \ |
---|
| 405 | _("Exit the application")) |
---|
| 406 | wx.EVT_ICONIZE(self, self.OnIconify) |
---|
| 407 | wx.EVT_CLOSE(self, self.OnClose) |
---|
| 408 | self.Show(True) |
---|
| 409 | self.Hide() |
---|
| 410 | |
---|
| 411 | self.quitEvent = threading.Event() |
---|
[123] | 412 | self.server = MyXMLRPCServer(self, options, arguments) |
---|
[112] | 413 | |
---|
| 414 | def UTF8ToUserCharset(self, text) : |
---|
| 415 | """Converts from UTF-8 to user's charset.""" |
---|
| 416 | if text is not None : |
---|
| 417 | try : |
---|
[119] | 418 | return text.decode("UTF-8").encode(self.charset, "replace") |
---|
| 419 | except (UnicodeError, AttributeError) : |
---|
[112] | 420 | try : |
---|
[119] | 421 | # Maybe already in Unicode |
---|
| 422 | return text.encode(self.charset, "replace") |
---|
| 423 | except (UnicodeError, AttributeError) : |
---|
| 424 | pass # Don't know what to do |
---|
[112] | 425 | return text |
---|
| 426 | |
---|
| 427 | def userCharsetToUTF8(self, text) : |
---|
| 428 | """Converts from user's charset to UTF-8.""" |
---|
| 429 | if text is not None : |
---|
| 430 | try : |
---|
[174] | 431 | return text.decode(self.charset).encode("UTF-8") |
---|
[119] | 432 | except (UnicodeError, AttributeError) : |
---|
[112] | 433 | try : |
---|
[174] | 434 | return text.decode(self.charset, "replace").encode("UTF-8") |
---|
[119] | 435 | except (UnicodeError, AttributeError) : |
---|
[113] | 436 | try : |
---|
[119] | 437 | # Maybe already in Unicode |
---|
| 438 | return text.encode("UTF-8", "replace") |
---|
| 439 | except (UnicodeError, AttributeError) : |
---|
| 440 | pass # Don't know what to do |
---|
[112] | 441 | return text |
---|
[119] | 442 | |
---|
[92] | 443 | |
---|
[111] | 444 | class PyKotIconApp(wx.App): |
---|
[58] | 445 | def OnInit(self) : |
---|
[104] | 446 | self.frame = PyKotIcon(None, wx.ID_ANY) |
---|
[111] | 447 | self.frame.Show(False) |
---|
[114] | 448 | self.SetTopWindow(self.frame) |
---|
[58] | 449 | return True |
---|
| 450 | |
---|
[123] | 451 | def postInit(self, charset, options, arguments) : |
---|
[81] | 452 | """Continues processing.""" |
---|
[123] | 453 | self.frame.postInit(charset, options, arguments) |
---|
[81] | 454 | |
---|
[119] | 455 | |
---|
[123] | 456 | def main() : |
---|
[63] | 457 | """Program's entry point.""" |
---|
[174] | 458 | # locale stuff |
---|
[65] | 459 | try : |
---|
[174] | 460 | locale.setlocale(locale.LC_ALL, ("", None)) |
---|
[65] | 461 | except (locale.Error, IOError) : |
---|
[174] | 462 | locale.setlocale(locale.LC_ALL, None) |
---|
| 463 | (language, charset) = locale.getlocale() |
---|
| 464 | language = language or "C" |
---|
[175] | 465 | charset = ((sys.platform != "win32") and charset) or locale.getpreferredencoding() |
---|
[174] | 466 | |
---|
| 467 | # translation stuff |
---|
[65] | 468 | try : |
---|
[174] | 469 | try : |
---|
| 470 | trans = gettext.translation("pykoticon", languages=["%s.%s" % (language, charset)], codeset=charset) |
---|
| 471 | except TypeError : # Python <2.4 |
---|
| 472 | trans = gettext.translation("pykoticon", languages=["%s.%s" % (language, charset)]) |
---|
| 473 | trans.install() |
---|
[65] | 474 | except : |
---|
| 475 | gettext.NullTranslations().install() |
---|
[112] | 476 | |
---|
[174] | 477 | |
---|
[170] | 478 | parser = optparse.OptionParser(usage="pykoticon [options] server1 [server2 ...]") |
---|
[123] | 479 | parser.add_option("-v", "--version", |
---|
| 480 | action="store_true", |
---|
| 481 | dest="version", |
---|
[146] | 482 | help=_("show PyKotIcon's version number and exit.")) |
---|
| 483 | parser.add_option("-c", "--cache", |
---|
| 484 | type="int", |
---|
| 485 | default=0, |
---|
| 486 | dest="cache", |
---|
| 487 | help=_("the duration of the cache in seconds to keep input forms' datas in memory. Defaults to 0 second, meaning no cache.")) |
---|
[123] | 488 | parser.add_option("-d", "--debug", |
---|
| 489 | action="store_true", |
---|
| 490 | dest="debug", |
---|
[146] | 491 | help=_("activate debug mode.")) |
---|
[123] | 492 | parser.add_option("-p", "--port", |
---|
| 493 | type="int", |
---|
| 494 | default=7654, |
---|
| 495 | dest="port", |
---|
[146] | 496 | help=_("the TCP port PyKotIcon will listen to, default is 7654.")) |
---|
[123] | 497 | parser.add_option("-q", "--allowquit", |
---|
| 498 | action="store_true", |
---|
| 499 | dest="allowquit", |
---|
[146] | 500 | help=_("allow the end user to close the application.")) |
---|
[123] | 501 | (options, arguments) = parser.parse_args() |
---|
| 502 | if options.version : |
---|
| 503 | print "PyKotIcon v%(__version__)s" % globals() |
---|
| 504 | else : |
---|
[146] | 505 | if not (1024 <= options.port <= 65535) : |
---|
| 506 | sys.stderr.write(_("The TCP port number specified for --port must be between 1024 and 65535.\n")) |
---|
| 507 | elif not (0 <= options.cache <= 86400) : |
---|
| 508 | sys.stderr.write(_("The duration specified for --cache must be between 0 and 86400 seconds.\n")) |
---|
| 509 | else : |
---|
| 510 | app = PyKotIconApp() |
---|
| 511 | app.postInit(charset, options, arguments) |
---|
| 512 | app.MainLoop() |
---|
[63] | 513 | |
---|
[119] | 514 | |
---|
[58] | 515 | if __name__ == '__main__': |
---|
[123] | 516 | main() |
---|
| 517 | |
---|