Changeset 952 for pykota/trunk/pykota/tool.py
- Timestamp:
- 04/24/03 00:13:57 (21 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/tool.py
r929 r952 1 1 #! /usr/bin/env python 2 2 3 # PyKota - Print Quotas for CUPS 3 # PyKota - Print Quotas for CUPS and LPRng 4 4 # 5 5 # (c) 2003 Jerome Alet <alet@librelogiciel.com> … … 21 21 # 22 22 # $Log$ 23 # Revision 1.35 2003/04/23 22:13:57 jalet 24 # Preliminary support for LPRng added BUT STILL UNTESTED. 25 # 23 26 # Revision 1.34 2003/04/17 09:26:21 jalet 24 27 # repykota now reports account balances too. … … 183 186 self.logger = logger.openLogger(self.config) 184 187 self.storage = storage.openConnection(self.config, asadmin=asadmin) 185 self.printername = os.environ.get("PRINTER", None)186 188 self.smtpserver = self.config.getSMTPServer() 189 190 def extractInfoFromCupsOrLprng(self) : 191 """Returns a tuple (printingsystem, printerhostname, printername, username, jobid, filename) depending on the printing system in use (as seen by the print filter). 192 193 Returns (None, None, None, None, None, None) if no printing system is recognized. 194 """ 195 # Try to detect CUPS 196 if os.environ.has_key("CUPS_SERVERROOT") and os.path.isdir(os.environ.get("CUPS_SERVERROOT", "")) : 197 if len(sys.argv) == 7 : 198 inputfile = sys.argv[6] 199 else : 200 inputfile = None 201 202 device_uri = os.environ.get("DEVICE_URI", "") 203 # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp 204 try : 205 (backend, destination) = device_uri.split(":", 1) 206 except ValueError : 207 raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri 208 while destination.startswith("/") : 209 destination = destination[1:] 210 printerhostname = destination.split("/")[0].split(":")[0] 211 return ("CUPS", printerhostname, os.environ.get("PRINTER"), sys.argv[2].strip(), sys.argv[1].strip(), inputfile) 212 else : 213 # Try to detect LPRng 214 jseen = Jseen = Pseen = nseen = rseen = None 215 for arg in sys.argv : 216 if arg.startswith("-j") : 217 jseen = arg[2:].strip() 218 elif arg.startswith("-J") : 219 Jseen = arg[2:].strip() 220 if Jseen == "(STDIN)" : 221 Jseen = None 222 elif arg.startswith("-n") : 223 nseen = arg[2:].strip() 224 elif arg.startswith("-P") : 225 Pseen = arg[2:].strip() 226 elif arg.startswith("-r") : 227 rseen = arg[2:].strip() 228 if jseen and Jseen and Pseen and nseen and rseen : 229 return ("LPRNG", rseen, Pseen, nseen, jseen, Jseen) 230 return (None, None, None, None, None, None) # Unknown printing system 187 231 188 232 def display_version_and_quit(self) : … … 296 340 """Checks the group quota on a printer and deny or accept the job.""" 297 341 printerid = self.storage.getPrinterId(printername) 342 policy = self.config.getPrinterPolicy(printername) 298 343 groupid = self.storage.getGroupId(groupname) 299 344 limitby = self.storage.getGroupLimitBy(groupid) … … 301 346 balance = self.storage.getGroupBalance(groupid) 302 347 if balance is None : 303 policy = self.config.getPrinterPolicy(printername)304 348 if policy in [None, "ALLOW"] : 305 349 action = "POLICY_ALLOW" … … 318 362 if quota is None : 319 363 # Unknown group or printer or combination 320 policy = self.config.getPrinterPolicy(printername)321 364 if policy in [None, "ALLOW"] : 322 365 action = "POLICY_ALLOW" … … 373 416 # then we check the user's own quota 374 417 printerid = self.storage.getPrinterId(printername) 418 policy = self.config.getPrinterPolicy(printername) 375 419 limitby = self.storage.getUserLimitBy(userid) 376 420 if limitby == "balance" : 377 421 balance = self.storage.getUserBalance(userid) 378 422 if balance is None : 379 policy = self.config.getPrinterPolicy(printername)380 423 if policy in [None, "ALLOW"] : 381 424 action = "POLICY_ALLOW" … … 394 437 if quota is None : 395 438 # Unknown user or printer or combination 396 policy = self.config.getPrinterPolicy(printername)397 439 if policy in [None, "ALLOW"] : 398 440 action = "POLICY_ALLOW" … … 438 480 return action 439 481 440 def warnGroupPQuota(self, groupname, printername =None) :482 def warnGroupPQuota(self, groupname, printername) : 441 483 """Checks a group quota and send messages if quota is exceeded on current printer.""" 442 pname = printername or self.printername 443 admin = self.config.getAdmin(pname) 444 adminmail = self.config.getAdminMail(pname) 445 mailto = self.config.getMailTo(pname) 446 action = self.checkGroupPQuota(groupname, pname) 484 admin = self.config.getAdmin(printername) 485 adminmail = self.config.getAdminMail(printername) 486 mailto = self.config.getMailTo(printername) 487 action = self.checkGroupPQuota(groupname, printername) 447 488 groupmembers = self.storage.getGroupMembersNames(groupname) 448 489 if action.startswith("POLICY_") : 449 490 action = action[7:] 450 491 if action == "DENY" : 451 adminmessage = _("Print Quota exceeded for group %s on printer %s") % (groupname, p name)492 adminmessage = _("Print Quota exceeded for group %s on printer %s") % (groupname, printername) 452 493 self.logger.log_message(adminmessage) 453 494 if mailto in [ "BOTH", "ADMIN" ] : … … 455 496 for username in groupmembers : 456 497 if mailto in [ "BOTH", "USER" ] : 457 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour group Print Quota is exceeded on printer %s.") % p name)498 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour group Print Quota is exceeded on printer %s.") % printername) 458 499 elif action == "WARN" : 459 adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (groupname, p name)500 adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (groupname, printername) 460 501 self.logger.log_message(adminmessage) 461 502 if mailto in [ "BOTH", "ADMIN" ] : … … 463 504 for username in groupmembers : 464 505 if mailto in [ "BOTH", "USER" ] : 465 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour group Print Quota is almost reached on printer %s.") % p name)506 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour group Print Quota is almost reached on printer %s.") % printername) 466 507 return action 467 508 468 def warnUserPQuota(self, username, printername =None) :509 def warnUserPQuota(self, username, printername) : 469 510 """Checks a user quota and send him a message if quota is exceeded on current printer.""" 470 pname = printername or self.printername 471 admin = self.config.getAdmin(pname) 472 adminmail = self.config.getAdminMail(pname) 473 mailto = self.config.getMailTo(pname) 474 action = self.checkUserPQuota(username, pname) 511 admin = self.config.getAdmin(printername) 512 adminmail = self.config.getAdminMail(printername) 513 mailto = self.config.getMailTo(printername) 514 action = self.checkUserPQuota(username, printername) 475 515 if action.startswith("POLICY_") : 476 516 action = action[7:] 477 517 if action == "DENY" : 478 adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, p name)518 adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, printername) 479 519 self.logger.log_message(adminmessage) 480 520 if mailto in [ "BOTH", "USER" ] : 481 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % p name)521 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printername) 482 522 if mailto in [ "BOTH", "ADMIN" ] : 483 523 self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 484 524 elif action == "WARN" : 485 adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, p name)525 adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, printername) 486 526 self.logger.log_message(adminmessage) 487 527 if mailto in [ "BOTH", "USER" ] : 488 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.") % p name)528 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.") % printername) 489 529 if mailto in [ "BOTH", "ADMIN" ] : 490 530 self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)