Changeset 802 for pykota/trunk
- Timestamp:
- 02/17/03 23:55:01 (22 years ago)
- Location:
- pykota/trunk/pykota
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/config.py
r800 r802 15 15 # 16 16 # $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 # 17 27 # Revision 1.14 2003/02/17 22:05:50 jalet 18 28 # Storage backend now supports admin and user passwords (untested) … … 91 101 raises PyKotaConfigError in case a problem is detected 92 102 """ 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 checks102 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 106 103 validmethods = [ "lazy" ] # TODO add more methods 107 104 if self.config.get("global", "method", raw=1).lower() not in validmethods : 108 105 raise PyKotaConfigError, _("Option method only supports values in %s") % str(validmethods) 109 110 # check all printers now111 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 requesters121 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))129 106 130 107 def getPrinterNames(self) : 131 108 """Returns the list of configured printers, i.e. all sections names minus 'global'.""" 132 109 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) 133 131 134 132 def getStorageBackend(self) : … … 139 137 "storageuser", \ 140 138 ] : 141 backendinfo[option] = self. config.get("global", option, raw=1)139 backendinfo[option] = self.getGlobalOption((option) 142 140 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) 147 142 return backendinfo 148 143 149 144 def getLoggingBackend(self) : 150 145 """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 152 151 153 152 def getRequesterBackend(self, printer) : 154 153 """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) 157 159 if args.endswith(')') : 158 160 args = args[:-1] 159 161 if not args : 160 162 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)) 161 166 return (requester, args) 162 167 163 168 def getPrinterPolicy(self, printer) : 164 169 """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 166 175 167 176 def getSMTPServer(self) : 168 177 """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") 170 179 171 def getAdminMail(self ) :180 def getAdminMail(self, printer) : 172 181 """Returns the Email address of the Print Quota Administrator.""" 173 return self. config.get("global", "adminmail", raw=1)182 return self.getPrinterOption(printer, "adminmail") 174 183 175 def getAdmin(self ) :184 def getAdmin(self, printer) : 176 185 """Returns the full name of the Print Quota Administrator.""" 177 return self. config.get("global", "admin", raw=1)186 return self.getPrinterOption(printer, "admin") 178 187 179 188 def getGraceDelay(self) : 180 189 """Returns the grace delay in days.""" 181 gd = self. config.get("global", "gracedelay", raw=1)190 gd = self.getPrinterOption(printer, "gracedelay") 182 191 try : 183 192 return int(gd) -
pykota/trunk/pykota/storage.py
r800 r802 15 15 # 16 16 # $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 # 17 27 # Revision 1.8 2003/02/17 22:05:50 jalet 18 28 # Storage backend now supports admin and user passwords (untested) … … 111 121 def openConnection(config, asadmin=0) : 112 122 """Returns a connection handle to the appropriate Quota Storage Database.""" 113 (backend, host, database, admin, user) = config.getStorageBackend()114 123 backendinfo = config.getStorageBackend() 115 124 backend = backendinfo["storagebackend"] -
pykota/trunk/pykota/tool.py
r788 r802 15 15 # 16 16 # $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 # 17 27 # Revision 1.19 2003/02/10 11:28:45 jalet 18 28 # Localization … … 116 126 self.printername = os.environ.get("PRINTER", None) 117 127 self.smtpserver = self.config.getSMTPServer() 118 self.admin = self.config.getAdmin()119 self.adminmail = self.config.getAdminMail()120 128 121 129 def display_version_and_quit(self) : … … 202 210 return 0 203 211 204 def sendMessage(self, touser, fullmessage) :212 def sendMessage(self, adminmail, touser, fullmessage) : 205 213 """Sends an email message containing headers to some user.""" 206 214 if "@" not in touser : 207 215 touser = "%s@%s" % (touser, self.smtpserver) 208 216 server = smtplib.SMTP(self.smtpserver) 209 server.sendmail( self.adminmail, [touser], fullmessage)217 server.sendmail(adminmail, [touser], fullmessage) 210 218 server.quit() 211 219 212 def sendMessageToUser(self, username, subject, message) :220 def sendMessageToUser(self, admin, adminmail, username, subject, message) : 213 221 """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) : 218 226 """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)) 220 228 221 229 def checkUserPQuota(self, username, printername) : … … 267 275 """Checks a user quota and send him a message if quota is exceeded on current printer.""" 268 276 pname = printername or self.printername 277 admin = self.config.getAdmin(pname) 278 adminmail = self.config.getAdminMail(pname) 269 279 (action, grace, gracedate) = self.checkUserPQuota(username, pname) 270 280 if action == "DENY" : … … 273 283 adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, pname) 274 284 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) 277 287 elif action == "WARN" : 278 288 adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, pname) 279 289 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) 282 292 return action 283 293