Show
Ignore:
Timestamp:
01/16/05 12:37:47 (19 years ago)
Author:
jerome
Message:

More cross-platform.
Planned for translations.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykoticon/trunk/bin/pykoticon

    r64 r65  
    2727import urllib 
    2828import urllib2 
    29  
    30 try : 
    31     import win32api 
    32     import win32net 
    33     import win32netcon 
    34     hasWindowsExtensions = 1 
    35 except ImportError :     
    36     hasWindowsExtensions = 0 
    37     raise ImportError, "Mark Hammond's Win32 Extensions are missing. Please install them." 
     29import locale 
     30import gettext 
     31 
     32if sys.platform == "win32" : 
     33    isWindows = 1 
     34    try : 
     35        import win32api 
     36        import win32net 
     37        import win32netcon 
     38    except ImportError :     
     39        raise ImportError, "Mark Hammond's Win32 Extensions are missing. Please install them." 
     40else :         
     41    isWindows = 0 
     42    import pwd 
    3843     
    3944try :     
     
    4853DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" 
    4954 
     55def getCurrentUserName() : 
     56    """Retrieves the current user's name.""" 
     57    if isWindows : 
     58        return win32api.GetUserName() 
     59    else :     
     60        try : 
     61            return pwd.getpwuid(os.geteuid())[0] 
     62        except : 
     63            return "** Unknown **" 
     64         
     65class Printer : 
     66    """A class for PyKota Printers.""" 
     67    def __init__(self, printername, priceperpage, priceperjob): 
     68        """Initialize printer datas.""" 
     69        self.PrinterName = printername 
     70        try : 
     71            self.PricePerPage = float(priceperpage) 
     72        except (ValueError, TypeError) : 
     73            self.PricePerPage = 0.0 
     74        try : 
     75            self.PricePerJob = float(priceperjob) 
     76        except (ValueError, TypeError) : 
     77            self.PricePerJob = 0.0 
     78 
    5079class UserQuota : 
    5180    """A class for PyKota User Print Quota entries.""" 
     
    71100    def __init__(self, username, userinfo, userquotas) : 
    72101        """Initialize user datas.""" 
    73         self.Name = username 
     102        self.UserName = username 
    74103        self.LimitBy = userinfo["limitby"][0].lower() 
    75104        try : 
     
    111140            lines = u.readlines() 
    112141        except IOError, msg :     
    113             raise IOError, "Unable to retrieve %s : %s" % (url, msg) 
     142            raise IOError, _("Unable to retrieve %s : %s") % (url, msg) 
    114143        else :     
    115144            u.close() 
     
    123152                                for fieldname in fieldnames ]) 
    124153            except :     
    125                 raise ValueError, "Invalid datas retrieved from %s" % url 
     154                raise ValueError, _("Invalid datas retrieved from %s") % url 
    126155        return answer 
    127156         
     
    162191    def __init__(self, parent, quotas) : 
    163192        gridlib.Grid.__init__(self, parent, -1) 
     193        sys.stderr.write("%s\n" % dir(self)) 
    164194        self.CreateGrid(len(quotas), 4) 
    165195        self.EnableEditing(False) 
    166         self.SetColLabelValue(0, "Page Counter") 
    167         self.SetColLabelValue(1, "Soft Limit") 
    168         self.SetColLabelValue(2, "Hard Limit") 
    169         self.SetColLabelValue(3, "Date Limit") 
     196        self.SetColLabelValue(0, _("Page Counter")) 
     197        self.SetColLabelValue(1, _("Soft Limit")) 
     198        self.SetColLabelValue(2, _("Hard Limit")) 
     199        self.SetColLabelValue(3, _("Date Limit")) 
    170200        attr = gridlib.GridCellAttr() 
    171201        attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_RIGHT) 
     
    195225            attr.SetBackgroundColour(colour)         
    196226            self.SetRowAttr(i, attr) 
     227        self.AutoSize() 
    197228             
    198229class PyKotIcon(wxPython.wx.wxFrame): 
     
    200231    def __init__(self, parent, id): 
    201232        wxPython.wx.wxFrame.__init__(self, parent, -1, \ 
    202                "PyKota Print Quota for user %s" % self.getCurrentUserName(), \ 
    203                # size=(640, 480), \ 
     233               _("PyKota Print Quota for user %s") % getCurrentUserName(), \ 
     234               size = (460, -1), \ 
    204235               style = wxPython.wx.wxDEFAULT_FRAME_STYLE | wxPython.wx.wxNO_FULL_REPAINT_ON_RESIZE) 
    205236        self.greenicon = wxPython.wx.wxIcon(os.path.join("..", "icons", "pykoticon-green.ico"), \ 
     
    223254                                          self.OnTaskBarClose) 
    224255        self.menu = wxPython.wx.wxMenu() 
    225         self.menu.Append(self.TBMENU_RESTORE, "Show Print Quota") 
    226         self.menu.Append(self.TBMENU_CLOSE, "Quit") 
     256        self.menu.Append(self.TBMENU_RESTORE, _("Show Print Quota")) 
     257        self.menu.Append(self.TBMENU_CLOSE, _("Quit")) 
    227258         
    228259        wxPython.wx.EVT_CLOSE(self, self.OnClose) 
     
    235266        self.networkInterface = CGINetworkInterface(DUMPYKOTA_URL) 
    236267        self.inTimer = False 
    237         self.chrono.Start(200) # first time in 0.25 second 
    238  
    239     def getCurrentUserName(self) : 
    240         """Returns the name of the current user.""" 
    241         return win32api.GetUserName() 
    242          
     268        self.chrono.Start(250) # first time in 0.25 second 
     269 
    243270    def OnChronoTimer(self, event) : 
    244271        """Retrieves user's data quota information.""" 
     
    248275        if self.inTimer is False : # avoids re-entrance 
    249276            self.inTimer = True 
    250             self.User = self.networkInterface.getUser(self.getCurrentUserName()) 
     277            self.User = self.networkInterface.getUser(getCurrentUserName()) 
    251278            if self.User.LimitBy == "balance" : 
    252279                if self.User.Balance <= 0.0 : 
     
    269296                else :     
    270297                    self.tbicon.SetIcon(self.greenicon, "PyKotIcon") 
    271                 if not self.IsIconized() :     
    272                     self.Iconize(True) 
    273                 if self.IsShown():     
    274                     self.Hide() 
    275298                if hasattr(self, "quotasgrid") :     
     299                    self.quotasgrid.Close() 
    276300                    self.quotasgrid.Destroy() 
    277301                    del self.quotasgrid 
    278302                self.quotasgrid = PyKotIconGrid(self, self.User.Quotas)     
    279                 if self.IsIconized() : 
    280                     self.Iconize(False) 
    281                 if not self.IsShown() : 
    282                     self.Show(True) 
    283303            self.inTimer = False 
    284304        # Now we want it every 3 minutes     
     
    327347def main(): 
    328348    """Program's entry point.""" 
     349    try : 
     350        locale.setlocale(locale.LC_ALL, "") 
     351    except (locale.Error, IOError) : 
     352        sys.stderr.write("Problem while setting locale.\n") 
     353    try : 
     354        gettext.install("pykoticon") 
     355    except : 
     356        gettext.NullTranslations().install() 
    329357    app = PyKotIconApp() 
    330358    app.MainLoop() 
     
    345373        username = sys.argv[2]  
    346374    else :     
    347         username = win32api.GetUserName() 
     375        username = getCurrentUserName() 
    348376    net = CGINetworkInterface(DUMPYKOTA_URL) 
    349377    user = net.getUser(username) 
    350     print "UserName : ", user.Name 
     378    print "UserName : ", user.UserName 
    351379    print "LimitBy : ", user.LimitBy 
    352380    print "Balance : ", user.Balance