Changeset 119 for pykoticon

Show
Ignore:
Timestamp:
04/20/06 10:50:32 (18 years ago)
Author:
jerome
Message:

Code cleaning.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykoticon/trunk/bin/pykoticon

    r114 r119  
    11#! /usr/bin/env python 
    22# -*- coding: ISO-8859-15 -*- 
     3 
     4"""PyKotIcon is a generic, networked, cross-platform dialog box manager.""" 
    35 
    46# PyKotIcon - Client side helper for PyKota 
     
    1921# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
    2022# 
    21 # $Id$ 
    2223# 
    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$" 
    2431 
    2532import sys 
    2633import os 
     34import time 
    2735import urllib 
    2836import urllib2 
     
    3442import SimpleXMLRPCServer 
    3543 
    36 import time 
    3744 
    3845if sys.platform == "win32" : 
    39     isWindows = 1 
     46    isWindows = True 
    4047    try : 
    4148        import win32api 
     
    4552        iconsdir = os.path.split(sys.argv[0])[0] 
    4653else :         
    47     isWindows = 0 
     54    isWindows = False 
    4855    iconsdir = "/usr/share/pykoticon"   # TODO : change this 
    4956    import pwd 
     
    5158try :     
    5259    import wx 
    53     hasWxPython = 1 
     60    hasWxPython = True 
    5461except ImportError :     
    55     hasWxPython = 0 
     62    hasWxPython = False 
    5663    raise ImportError, "wxPython is missing. Please install it." 
    5764     
    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  
     65aboutbox = """PyKotIcon v%(__version__)s (c) 2003-2006 %(__author__)s - %(__author_email__)s 
     66 
     67PyKotIcon is generic, networked, cross-platform dialog box manager. 
     68 
     69It is often used as a client side companion for PyKota, but it 
    6170can be used from other applications if you want. 
    6271 
     
    7584Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.""" 
    7685 
    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 **" 
    8686         
    8787class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer) : 
     
    110110        """Asks some textual datas defined by a list of labels, a list of variables' names and a list of variables values in a mapping.""" 
    111111        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 v in 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 ], \ 
    115115                                          varnames, \ 
    116116                                          values) 
     
    119119            time.sleep(0.1) 
    120120        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)) 
    124124        self.frame.dialogAnswer = None # prepare for next call, just in case 
    125125        return retcode 
     
    164164        self.server_close()     
    165165        sys.exit(0) 
     166     
    166167     
    167168class GenericInputDialog(wx.Dialog) : 
     
    203204        self.Layout() 
    204205         
     206         
    205207class PyKotIcon(wx.Frame): 
    206208    """Main class.""" 
     
    208210        self.dialogAnswer = None 
    209211        wx.Frame.__init__(self, parent, id, \ 
    210                _("PyKotIcon info for %s") % getCurrentUserName(), \ 
     212               _("PyKotIcon info for %s") % self.getCurrentUserName(), \ 
    211213               size = (1, 1), \ 
    212214               style = wx.FRAME_NO_TASKBAR | wx.NO_FULL_REPAINT_ON_RESIZE) 
     
    239241        self.Show(True) 
    240242         
     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             
    241253    def OnIconify(self, event) : 
    242254        if not self.IsIconized() : 
     
    265277    def OnAbout(self, event) :     
    266278        """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) 
    268280        dialog.ShowModal() 
    269281        dialog.Destroy() 
     
    319331        self.charset = charset 
    320332        self.port = localport 
    321         self.server = MyXMLRPCServer(self, printserver, localport, debug=True) 
     333        self.server = MyXMLRPCServer(self, printserver, localport) 
    322334         
    323335    def UTF8ToUserCharset(self, text) : 
     
    325337        if text is not None : 
    326338            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) :     
    329341                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 
    337346        return text 
    338347         
     
    341350        if text is not None : 
    342351            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) : 
    345360                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) :     
    349363                    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 
    353368        return text 
     369         
    354370 
    355371class PyKotIconApp(wx.App): 
     
    363379        """Continues processing.""" 
    364380        self.frame.postInit(charset, printserver, localport) 
     381         
    365382         
    366383def main(printserver, localport): 
     
    400417    app.MainLoop() 
    401418     
     419     
    402420def crashed() :     
    403421    """Minimal crash method.""" 
     
    410428    sys.stderr.flush() 
    411429     
     430     
    412431if __name__ == '__main__': 
    413432    if len(sys.argv) >= 2 : 
    414433        arg = sys.argv[1] 
    415434        if arg in ("-v", "--version") :     
    416             print "0.3" 
     435            print __version__ 
    417436        elif arg in ("-h", "--help") :     
    418437            sys.stderr.write("usage : pykoticon  pykota_server_hostname_or_ip_address  localTCPPort\n")