Show
Ignore:
Timestamp:
11/20/03 00:19:38 (20 years ago)
Author:
jalet
Message:

Code refactoring work.
Explicit redirection to /dev/null has to be set in external policy now, just
like in external mailto.

Files:
1 modified

Legend:

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

    r1193 r1196  
    2222# 
    2323# $Log$ 
     24# Revision 1.57  2003/11/19 23:19:38  jalet 
     25# Code refactoring work. 
     26# Explicit redirection to /dev/null has to be set in external policy now, just 
     27# like in external mailto. 
     28# 
    2429# Revision 1.56  2003/11/19 07:40:20  jalet 
    2530# Missing import statement. 
     
    234239 
    235240from pykota import version, config, storage, logger 
     241from pykota.accounter import openAccounter 
    236242 
    237243class PyKotaToolError(Exception): 
     
    514520        return action 
    515521     
    516     def externalMailTo(self, cmd, action, user, printername, message) : 
     522    def externalMailTo(self, cmd, action, user, printer, message) : 
    517523        """Warns the user with an external command.""" 
    518524        username = user.Name 
     525        printername = printer.Name 
    519526        email = user.Email or user.Name 
    520527        if "@" not in email : 
     
    522529        os.system(cmd % locals()) 
    523530     
     531    def formatCommandLine(self, cmd, user, printer) : 
     532        """Executes an external command.""" 
     533        username = user.Name 
     534        printername = printer.Name 
     535        return cmd % locals() 
     536         
    524537    def warnGroupPQuota(self, grouppquota) : 
    525538        """Checks a group quota and send messages if quota is exceeded on current printer.""" 
     
    542555                        self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printer.Name)) 
    543556                    else :     
    544                         self.externalMailTo(arguments, action, user, printer.Name, message) 
     557                        self.externalMailTo(arguments, action, user, printer, message) 
    545558        elif action == "WARN" :     
    546559            adminmessage = _("Print Quota low for group %s on printer %s") % (group.Name, printer.Name) 
     
    557570                        self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) 
    558571                    else :     
    559                         self.externalMailTo(arguments, action, user, printer.Name, message) 
     572                        self.externalMailTo(arguments, action, user, printer, message) 
    560573        return action         
    561574         
     
    578591                    self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) 
    579592                else :     
    580                     self.externalMailTo(arguments, action, user, printer.Name, message) 
     593                    self.externalMailTo(arguments, action, user, printer, message) 
    581594            if mailto in [ "BOTH", "ADMIN" ] : 
    582595                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
     
    592605                    self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message) 
    593606                else :     
    594                     self.externalMailTo(arguments, action, user, printer.Name, message) 
     607                    self.externalMailTo(arguments, action, user, printer, message) 
    595608            if mailto in [ "BOTH", "ADMIN" ] : 
    596609                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    597610        return action         
     611         
     612class PyKotaFilterOrBackend(PyKotaTool) :     
     613    """Class for the PyKota filter or backend.""" 
     614    def __init__(self) : 
     615        PyKotaTool.__init__(self) 
     616        (self.printingsystem, \ 
     617         self.printerhostname, \ 
     618         self.printername, \ 
     619         self.username, \ 
     620         self.jobid, \ 
     621         self.inputfile, \ 
     622         self.copies, \ 
     623         self.title, \ 
     624         self.options, \ 
     625         self.originalbackend) = self.extractInfoFromCupsOrLprng() 
     626        self.accounter = openAccounter(self) 
     627     
     628    def extractInfoFromCupsOrLprng(self) :     
     629        """Returns a tuple (printingsystem, printerhostname, printername, username, jobid, filename, title, options, backend). 
     630         
     631           Returns (None, None, None, None, None, None, None, None, None, None) if no printing system is recognized. 
     632        """ 
     633        # Try to detect CUPS 
     634        if os.environ.has_key("CUPS_SERVERROOT") and os.path.isdir(os.environ.get("CUPS_SERVERROOT", "")) : 
     635            if len(sys.argv) == 7 : 
     636                inputfile = sys.argv[6] 
     637            else :     
     638                inputfile = None 
     639                 
     640            # check that the DEVICE_URI environment variable's value is  
     641            # prefixed with "cupspykota:" otherwise don't touch it. 
     642            # If this is the case, we have to remove the prefix from  
     643            # the environment before launching the real backend in cupspykota 
     644            device_uri = os.environ.get("DEVICE_URI", "") 
     645            if device_uri.startswith("cupspykota:") : 
     646                fulldevice_uri = device_uri[:] 
     647                device_uri = fulldevice_uri[len("cupspykota:"):] 
     648                if device_uri.startswith("//") :    # lpd (at least) 
     649                    device_uri = device_uri[2:] 
     650                os.environ["DEVICE_URI"] = device_uri   # TODO : side effect ! 
     651            # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp 
     652            try : 
     653                (backend, destination) = device_uri.split(":", 1)  
     654            except ValueError :     
     655                raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri 
     656            while destination.startswith("/") : 
     657                destination = destination[1:] 
     658            printerhostname = destination.split("/")[0].split(":")[0] 
     659            return ("CUPS", \ 
     660                    printerhostname, \ 
     661                    os.environ.get("PRINTER"), \ 
     662                    sys.argv[2].strip(), \ 
     663                    sys.argv[1].strip(), \ 
     664                    inputfile, \ 
     665                    int(sys.argv[4].strip()), \ 
     666                    sys.argv[3], \ 
     667                    sys.argv[5], \ 
     668                    backend) 
     669        else :     
     670            # Try to detect LPRng 
     671            jseen = Pseen = nseen = rseen = Kseen = None 
     672            for arg in sys.argv : 
     673                if arg.startswith("-j") : 
     674                    jseen = arg[2:].strip() 
     675                elif arg.startswith("-n") :      
     676                    nseen = arg[2:].strip() 
     677                elif arg.startswith("-P") :     
     678                    Pseen = arg[2:].strip() 
     679                elif arg.startswith("-r") :     
     680                    rseen = arg[2:].strip() 
     681                elif arg.startswith("-K") or arg.startswith("-#") :     
     682                    Kseen = int(arg[2:].strip()) 
     683            if Kseen is None :         
     684                Kseen = 1       # we assume the user wants at least one copy... 
     685            if (rseen is None) and jseen and Pseen and nseen :     
     686                self.logger.log_message(_("Printer hostname undefined, set to 'localhost'"), "warn") 
     687                rseen = "localhost" 
     688            if jseen and Pseen and nseen and rseen :         
     689                # job is always in stdin (None) 
     690                return ("LPRNG", rseen, Pseen, nseen, jseen, None, Kseen, None, None, None) 
     691        self.logger.log_message(_("Printing system unknown, args=%s") % " ".join(sys.argv), "warn") 
     692        return (None, None, None, None, None, None, None, None, None, None)   # Unknown printing system