Changeset 802 for pykota

Show
Ignore:
Timestamp:
02/17/03 23:55:01 (22 years ago)
Author:
jalet
Message:

More options can now be set per printer or globally :

admin
adminmail
gracedelay
requester

the printer option has priority when both are defined.

Location:
pykota/trunk/pykota
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/config.py

    r800 r802  
    1515# 
    1616# $Log$ 
     17# Revision 1.15  2003/02/17 22:55:01  jalet 
     18# More options can now be set per printer or globally : 
     19# 
     20#       admin 
     21#       adminmail 
     22#       gracedelay 
     23#       requester 
     24# 
     25# the printer option has priority when both are defined. 
     26# 
    1727# Revision 1.14  2003/02/17 22:05:50  jalet 
    1828# Storage backend now supports admin and user passwords (untested) 
     
    91101           raises PyKotaConfigError in case a problem is detected 
    92102        """ 
    93         for option in [ "storagebackend", "storageserver", \ 
    94                         "storagename", "storageadmin", \ 
    95                         "storageuser",  
    96                         "logger", "admin", "adminmail", 
    97                         "smtpserver", "method", "gracedelay" ] : 
    98             if not self.config.has_option("global", option) :             
    99                 raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) 
    100                  
    101         # more precise checks         
    102         validloggers = [ "stderr", "system" ]  
    103         if self.config.get("global", "logger", raw=1).lower() not in validloggers :              
    104             raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) 
    105              
    106103        validmethods = [ "lazy" ] # TODO add more methods             
    107104        if self.config.get("global", "method", raw=1).lower() not in validmethods :              
    108105            raise PyKotaConfigError, _("Option method only supports values in %s") % str(validmethods) 
    109              
    110         # check all printers now  
    111         for printer in self.getPrinterNames() : 
    112             for poption in [ "requester", "policy" ] :  
    113                 if not self.config.has_option(printer, poption) : 
    114                     raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename) 
    115                      
    116             validpolicies = [ "ALLOW", "DENY" ]      
    117             if self.config.get(printer, "policy", raw=1).upper() not in validpolicies : 
    118                 raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) 
    119              
    120             validrequesters = [ "snmp", "external" ] # TODO : add more requesters 
    121             fullrequester = self.config.get(printer, "requester", raw=1) 
    122             try : 
    123                 (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] 
    124             except ValueError :     
    125                 raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) 
    126             else : 
    127                 if requester not in validrequesters : 
    128                     raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters)) 
    129106                         
    130107    def getPrinterNames(self) :     
    131108        """Returns the list of configured printers, i.e. all sections names minus 'global'.""" 
    132109        return [pname for pname in self.config.sections() if pname != "global"] 
     110         
     111    def getGlobalOption(self, option, ignore=0) :     
     112        """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" 
     113        try : 
     114            return self.config.get("global", option, raw=1) 
     115        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :     
     116            if ignore : 
     117                return 
     118            else : 
     119                raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) 
     120                 
     121    def getPrinterOption(self, printer, option) :     
     122        """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" 
     123        globaloption = self.getGlobalOption(option, ignore=1) 
     124        try : 
     125            return self.config.get(printer, option, raw=1) 
     126        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :     
     127            if globaloption is not None : 
     128                return globaloption 
     129            else : 
     130                raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename) 
    133131         
    134132    def getStorageBackend(self) :     
     
    139137                        "storageuser", \ 
    140138                      ] : 
    141             backendinfo[option] = self.config.get("global", option, raw=1) 
     139            backendinfo[option] = self.getGlobalOption((option) 
    142140        for option in [ "storageadminpw", "storageuserpw" ] :     
    143             if self.config.has_option("global", option) : 
    144                 backendinfo[option] = self.config.get("global", option, raw=1) 
    145             else :     
    146                 backendinfo[option] = None 
     141            backendinfo[option] = self.getGlobalOption(option, ignore=1) 
    147142        return backendinfo 
    148143         
    149144    def getLoggingBackend(self) :     
    150145        """Returns the logging backend information.""" 
    151         return self.config.get("global", "logger", raw=1).lower() 
     146        validloggers = [ "stderr", "system" ]  
     147        logger = self.getGlobalOption("logger").lower() 
     148        if logger not in validloggers :              
     149            raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) 
     150        return logger     
    152151         
    153152    def getRequesterBackend(self, printer) :     
    154153        """Returns the requester backend to use for a given printer, with its arguments.""" 
    155         fullrequester = self.config.get(printer, "requester", raw=1) 
    156         (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] 
     154        fullrequester = self.getPrinterOption(printer, "requester") 
     155        try : 
     156            (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] 
     157        except ValueError :     
     158            raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) 
    157159        if args.endswith(')') : 
    158160            args = args[:-1] 
    159161        if not args : 
    160162            raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) 
     163        validrequesters = [ "snmp", "external" ] # TODO : add more requesters 
     164        if requester not in validrequesters : 
     165            raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters)) 
    161166        return (requester, args) 
    162167         
    163168    def getPrinterPolicy(self, printer) :     
    164169        """Returns the default policy for the current printer.""" 
    165         return self.config.get(printer, "policy", raw=1).upper() 
     170        validpolicies = [ "ALLOW", "DENY" ]      
     171        policy = self.getPrinterOption(printer, "policy").upper() 
     172        if policy not in validpolicies : 
     173            raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) 
     174        return policy 
    166175         
    167176    def getSMTPServer(self) :     
    168177        """Returns the SMTP server to use to send messages to users.""" 
    169         return self.config.get("global", "smtpserver", raw=1) 
     178        return self.getGlobalOption("smtpserver") 
    170179         
    171     def getAdminMail(self) :     
     180    def getAdminMail(self, printer) :     
    172181        """Returns the Email address of the Print Quota Administrator.""" 
    173         return self.config.get("global", "adminmail", raw=1) 
     182        return self.getPrinterOption(printer, "adminmail") 
    174183         
    175     def getAdmin(self) :     
     184    def getAdmin(self, printer) :     
    176185        """Returns the full name of the Print Quota Administrator.""" 
    177         return self.config.get("global", "admin", raw=1) 
     186        return self.getPrinterOption(printer, "admin") 
    178187         
    179188    def getGraceDelay(self) :     
    180189        """Returns the grace delay in days.""" 
    181         gd = self.config.get("global", "gracedelay", raw=1) 
     190        gd = self.getPrinterOption(printer, "gracedelay") 
    182191        try : 
    183192            return int(gd) 
  • pykota/trunk/pykota/storage.py

    r800 r802  
    1515# 
    1616# $Log$ 
     17# Revision 1.9  2003/02/17 22:55:01  jalet 
     18# More options can now be set per printer or globally : 
     19# 
     20#       admin 
     21#       adminmail 
     22#       gracedelay 
     23#       requester 
     24# 
     25# the printer option has priority when both are defined. 
     26# 
    1727# Revision 1.8  2003/02/17 22:05:50  jalet 
    1828# Storage backend now supports admin and user passwords (untested) 
     
    111121def openConnection(config, asadmin=0) : 
    112122    """Returns a connection handle to the appropriate Quota Storage Database.""" 
    113     (backend, host, database, admin, user) = config.getStorageBackend() 
    114123    backendinfo = config.getStorageBackend() 
    115124    backend = backendinfo["storagebackend"] 
  • pykota/trunk/pykota/tool.py

    r788 r802  
    1515# 
    1616# $Log$ 
     17# Revision 1.20  2003/02/17 22:55:01  jalet 
     18# More options can now be set per printer or globally : 
     19# 
     20#       admin 
     21#       adminmail 
     22#       gracedelay 
     23#       requester 
     24# 
     25# the printer option has priority when both are defined. 
     26# 
    1727# Revision 1.19  2003/02/10 11:28:45  jalet 
    1828# Localization 
     
    116126        self.printername = os.environ.get("PRINTER", None) 
    117127        self.smtpserver = self.config.getSMTPServer() 
    118         self.admin = self.config.getAdmin() 
    119         self.adminmail = self.config.getAdminMail() 
    120128         
    121129    def display_version_and_quit(self) : 
     
    202210        return 0 
    203211         
    204     def sendMessage(self, touser, fullmessage) : 
     212    def sendMessage(self, adminmail, touser, fullmessage) : 
    205213        """Sends an email message containing headers to some user.""" 
    206214        if "@" not in touser : 
    207215            touser = "%s@%s" % (touser, self.smtpserver) 
    208216        server = smtplib.SMTP(self.smtpserver) 
    209         server.sendmail(self.adminmail, [touser], fullmessage) 
     217        server.sendmail(adminmail, [touser], fullmessage) 
    210218        server.quit() 
    211219         
    212     def sendMessageToUser(self, username, subject, message) : 
     220    def sendMessageToUser(self, admin, adminmail, username, subject, message) : 
    213221        """Sends an email message to a user.""" 
    214         message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (self.admin, self.adminmail) 
    215         self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message)) 
    216          
    217     def sendMessageToAdmin(self, subject, message) : 
     222        message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail) 
     223        self.sendMessage(adminmail, username, "Subject: %s\n\n%s" % (subject, message)) 
     224         
     225    def sendMessageToAdmin(self, adminmail, subject, message) : 
    218226        """Sends an email message to the Print Quota administrator.""" 
    219         self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message)) 
     227        self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message)) 
    220228         
    221229    def checkUserPQuota(self, username, printername) : 
     
    267275        """Checks a user quota and send him a message if quota is exceeded on current printer.""" 
    268276        pname = printername or self.printername 
     277        admin = self.config.getAdmin(pname) 
     278        adminmail = self.config.getAdminMail(pname) 
    269279        (action, grace, gracedate) = self.checkUserPQuota(username, pname) 
    270280        if action == "DENY" : 
     
    273283                adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, pname) 
    274284                self.logger.log_message(adminmessage) 
    275                 self.sendMessageToUser(username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % pname) 
    276                 self.sendMessageToAdmin("Print Quota", adminmessage) 
     285                self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % pname) 
     286                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    277287        elif action == "WARN" :     
    278288            adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, pname) 
    279289            self.logger.log_message(adminmessage) 
    280             self.sendMessageToUser(username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % pname) 
    281             self.sendMessageToAdmin(_("Print Quota"), adminmessage) 
     290            self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % pname) 
     291            self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    282292        return action         
    283293