Show
Ignore:
Timestamp:
04/24/03 00:13:57 (21 years ago)
Author:
jalet
Message:

Preliminary support for LPRng added BUT STILL UNTESTED.

Files:
1 modified

Legend:

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

    r929 r952  
    11#! /usr/bin/env python 
    22 
    3 # PyKota - Print Quotas for CUPS 
     3# PyKota - Print Quotas for CUPS and LPRng 
    44# 
    55# (c) 2003 Jerome Alet <alet@librelogiciel.com> 
     
    2121# 
    2222# $Log$ 
     23# Revision 1.35  2003/04/23 22:13:57  jalet 
     24# Preliminary support for LPRng added BUT STILL UNTESTED. 
     25# 
    2326# Revision 1.34  2003/04/17 09:26:21  jalet 
    2427# repykota now reports account balances too. 
     
    183186        self.logger = logger.openLogger(self.config) 
    184187        self.storage = storage.openConnection(self.config, asadmin=asadmin) 
    185         self.printername = os.environ.get("PRINTER", None) 
    186188        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           
    187231         
    188232    def display_version_and_quit(self) : 
     
    296340        """Checks the group quota on a printer and deny or accept the job.""" 
    297341        printerid = self.storage.getPrinterId(printername) 
     342        policy = self.config.getPrinterPolicy(printername) 
    298343        groupid = self.storage.getGroupId(groupname) 
    299344        limitby = self.storage.getGroupLimitBy(groupid) 
     
    301346            balance = self.storage.getGroupBalance(groupid) 
    302347            if balance is None : 
    303                 policy = self.config.getPrinterPolicy(printername) 
    304348                if policy in [None, "ALLOW"] : 
    305349                    action = "POLICY_ALLOW" 
     
    318362            if quota is None : 
    319363                # Unknown group or printer or combination 
    320                 policy = self.config.getPrinterPolicy(printername) 
    321364                if policy in [None, "ALLOW"] : 
    322365                    action = "POLICY_ALLOW" 
     
    373416        # then we check the user's own quota 
    374417        printerid = self.storage.getPrinterId(printername) 
     418        policy = self.config.getPrinterPolicy(printername) 
    375419        limitby = self.storage.getUserLimitBy(userid) 
    376420        if limitby == "balance" :  
    377421            balance = self.storage.getUserBalance(userid) 
    378422            if balance is None : 
    379                 policy = self.config.getPrinterPolicy(printername) 
    380423                if policy in [None, "ALLOW"] : 
    381424                    action = "POLICY_ALLOW" 
     
    394437            if quota is None : 
    395438                # Unknown user or printer or combination 
    396                 policy = self.config.getPrinterPolicy(printername) 
    397439                if policy in [None, "ALLOW"] : 
    398440                    action = "POLICY_ALLOW" 
     
    438480        return action 
    439481     
    440     def warnGroupPQuota(self, groupname, printername=None) : 
     482    def warnGroupPQuota(self, groupname, printername) : 
    441483        """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) 
    447488        groupmembers = self.storage.getGroupMembersNames(groupname) 
    448489        if action.startswith("POLICY_") : 
    449490            action = action[7:] 
    450491        if action == "DENY" : 
    451             adminmessage = _("Print Quota exceeded for group %s on printer %s") % (groupname, pname) 
     492            adminmessage = _("Print Quota exceeded for group %s on printer %s") % (groupname, printername) 
    452493            self.logger.log_message(adminmessage) 
    453494            if mailto in [ "BOTH", "ADMIN" ] : 
     
    455496            for username in groupmembers : 
    456497                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.") % pname) 
     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) 
    458499        elif action == "WARN" :     
    459             adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (groupname, pname) 
     500            adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (groupname, printername) 
    460501            self.logger.log_message(adminmessage) 
    461502            if mailto in [ "BOTH", "ADMIN" ] : 
     
    463504            for username in groupmembers : 
    464505                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.") % pname) 
     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) 
    466507        return action         
    467508         
    468     def warnUserPQuota(self, username, printername=None) : 
     509    def warnUserPQuota(self, username, printername) : 
    469510        """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) 
    475515        if action.startswith("POLICY_") : 
    476516            action = action[7:] 
    477517        if action == "DENY" : 
    478             adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, pname) 
     518            adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, printername) 
    479519            self.logger.log_message(adminmessage) 
    480520            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.") % pname) 
     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) 
    482522            if mailto in [ "BOTH", "ADMIN" ] : 
    483523                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    484524        elif action == "WARN" :     
    485             adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, pname) 
     525            adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, printername) 
    486526            self.logger.log_message(adminmessage) 
    487527            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.") % pname) 
     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) 
    489529            if mailto in [ "BOTH", "ADMIN" ] : 
    490530                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)