| 611 | |
| 612 | class 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 |