Back to index

system-config-printer  1.3.9+20120706
Classes | Functions
cupshelpers.cupshelpers Namespace Reference

Classes

class  Printer
class  Device
class  _GetDevicesCall

Functions

def getPrinters
def parseDeviceID
def getDevices
def activateNewPrinter
def copyPPDOptions
def setPPDPageSize
def missingExecutables
def missingPackagesAndExecutables
def _main

Function Documentation

def cupshelpers.cupshelpers._main ( ) [private]

Definition at line 833 of file cupshelpers.py.

00833 
00834 def _main():
00835     c = cups.Connection()
00836     #printers = getPrinters(c)
00837     for device in getDevices(c).itervalues():
00838         print device.uri, device.id_dict

Here is the call graph for this function:

def cupshelpers.cupshelpers.activateNewPrinter (   connection,
  name 
)
Set a new printer enabled, accepting jobs, and (if necessary) the
default printer.

@param connection: CUPS connection
@type connection: cups.Connection object
@param name: printer name
@type name: string
@raise cups.IPPError: IPP error

Definition at line 618 of file cupshelpers.py.

00618 
00619 def activateNewPrinter(connection, name):
00620     """
00621     Set a new printer enabled, accepting jobs, and (if necessary) the
00622     default printer.
00623 
00624     @param connection: CUPS connection
00625     @type connection: cups.Connection object
00626     @param name: printer name
00627     @type name: string
00628     @raise cups.IPPError: IPP error
00629     """
00630     connection.enablePrinter (name)
00631     connection.acceptJobs (name)
00632 
00633     # Set as the default if there is not already a default printer.
00634     if connection.getDefault () == None:
00635         connection.setDefault (name)

def cupshelpers.cupshelpers.copyPPDOptions (   ppd1,
  ppd2 
)
Copy default options between PPDs.

@param ppd1: source PPD
@type ppd1: cups.PPD object
@param ppd2: destination PPD
@type ppd2: cups.PPD object

Definition at line 636 of file cupshelpers.py.

00636 
00637 def copyPPDOptions(ppd1, ppd2):
00638     """
00639     Copy default options between PPDs.
00640 
00641     @param ppd1: source PPD
00642     @type ppd1: cups.PPD object
00643     @param ppd2: destination PPD
00644     @type ppd2: cups.PPD object
00645     """
00646     def getPPDGroupOptions(group):
00647        options = group.options[:]
00648         for g in group.subgroups:
00649             options.extend(getPPDGroupOptions(g))
00650         return options
00651 
00652     def iteratePPDOptions(ppd):
00653        for group in ppd.optionGroups:
00654             for option in getPPDGroupOptions(group):
00655               yield option
00656 
00657     for option in iteratePPDOptions(ppd1):
00658         if option.keyword == "PageRegion":
00659             continue
00660         new_option = ppd2.findOption(option.keyword)
00661         if new_option and option.ui==new_option.ui:
00662             value = option.defchoice
00663             for choice in new_option.choices:
00664                 if choice["choice"]==value:
00665                     ppd2.markOption(new_option.keyword, value)
00666                     _debugprint ("set %s = %s" % (new_option.keyword, value))
                    
def cupshelpers.cupshelpers.getDevices (   connection,
  kw 
)
Obtain a list of available CUPS devices.

@param connection: CUPS connection
@type connection: cups.Connection object
@returns: a list of L{Device} objects
@raise cups.IPPError: IPP Error

Definition at line 606 of file cupshelpers.py.

00606 
00607 def getDevices(connection, **kw):
00608     """
00609     Obtain a list of available CUPS devices.
00610 
00611     @param connection: CUPS connection
00612     @type connection: cups.Connection object
00613     @returns: a list of L{Device} objects
00614     @raise cups.IPPError: IPP Error
00615     """
00616     op = _GetDevicesCall ()
00617     return op.call (connection, kw)

Here is the caller graph for this function:

Obtain a list of printers.

@param connection: CUPS connection
@type connection: CUPS.Connection object
@returns: L{Printer} list

Definition at line 462 of file cupshelpers.py.

00462 
00463 def getPrinters(connection):
00464     """
00465     Obtain a list of printers.
00466 
00467     @param connection: CUPS connection
00468     @type connection: CUPS.Connection object
00469     @returns: L{Printer} list
00470     """
00471     printers = connection.getPrinters()
00472     classes = connection.getClasses()
00473     for name, printer in printers.iteritems():
00474         printer = Printer(name, connection, **printer)
00475         printers[name] = printer
00476         if classes.has_key(name):
00477             printer.class_members = classes[name]
00478             printer.class_members.sort()
00479     return printers

Check that all relevant executables for a PPD are installed.

@param ppd: PPD
@type ppd: cups.PPD object
@returns: string list, representing missing executables

Definition at line 690 of file cupshelpers.py.

00690 
00691 def missingExecutables(ppd):
00692     """
00693     Check that all relevant executables for a PPD are installed.
00694 
00695     @param ppd: PPD
00696     @type ppd: cups.PPD object
00697     @returns: string list, representing missing executables
00698     """
00699 
00700     # First, a local function.  How to check that something exists
00701     # in a path:
00702     def pathcheck (name, path="/usr/bin:/bin"):
00703         if name == "-":
00704             # A filter of "-" means that no filter is required,
00705             # i.e. the device accepts the given format as-is.
00706             return "builtin"
00707         # Strip out foomatic '%'-style place-holders.
00708         p = name.find ('%')
00709         if p != -1:
00710             name = name[:p]
00711         if len (name) == 0:
00712             return "true"
00713         if name[0] == '/':
00714             if os.access (name, os.X_OK):
00715                 _debugprint ("%s: found" % name)
00716                 return name
00717             else:
00718                 _debugprint ("%s: NOT found" % name)
00719                 return None
00720         if name.find ("=") != -1:
00721             return "builtin"
00722         if name in [ ":", ".", "[", "alias", "bind", "break", "cd",
00723                      "continue", "declare", "echo", "else", "eval",
00724                      "exec", "exit", "export", "fi", "if", "kill", "let",
00725                      "local", "popd", "printf", "pushd", "pwd", "read",
00726                      "readonly", "set", "shift", "shopt", "source",
00727                      "test", "then", "trap", "type", "ulimit", "umask",
00728                      "unalias", "unset", "wait" ]:
00729             return "builtin"
00730         for component in path.split (':'):
00731             file = component.rstrip (os.path.sep) + os.path.sep + name
00732             if os.access (file, os.X_OK):
00733                 _debugprint ("%s: found" % file)
00734                 return file
00735         _debugprint ("%s: NOT found in %s" % (name,path))
00736         return None
00737 
00738     exes_to_install = []
00739 
00740     def add_missing (exe):
00741         # Strip out foomatic '%'-style place-holders.
00742         p = exe.find ('%')
00743         if p != -1:
00744             exe = exe[:p]
00745 
00746         exes_to_install.append (exe)
00747 
00748     # Find a 'FoomaticRIPCommandLine' attribute.
00749     exe = exepath = None
00750     attr = ppd.findAttr ('FoomaticRIPCommandLine')
00751     if attr:
00752         # Foomatic RIP command line to check.
00753         cmdline = attr.value.replace ('&&\n', '')
00754         cmdline = cmdline.replace ('"', '"')
00755         cmdline = cmdline.replace ('&lt;', '<')
00756         cmdline = cmdline.replace ('&gt;', '>')
00757         if (cmdline.find ("(") != -1 or
00758             cmdline.find ("&") != -1):
00759             # Don't try to handle sub-shells or unreplaced HTML entities.
00760             cmdline = ""
00761 
00762         # Strip out foomatic '%'-style place-holders
00763         pipes = cmdline.split (';')
00764         for pipe in pipes:
00765             cmds = pipe.strip ().split ('|')
00766             for cmd in cmds:
00767                 args = cmd.strip ().split (' ')
00768                 exe = args[0]
00769                 exepath = pathcheck (exe)
00770                 if not exepath:
00771                     add_missing (exe)
00772                     continue
00773 
00774                 # Main executable found.  But if it's 'gs',
00775                 # perhaps there is an IJS server we also need
00776                 # to check.
00777                 if os.path.basename (exepath) == 'gs':
00778                     argn = len (args)
00779                     argi = 1
00780                     search = "-sIjsServer="
00781                     while argi < argn:
00782                         arg = args[argi]
00783                         if arg.startswith (search):
00784                             exe = arg[len (search):]
00785                             exepath = pathcheck (exe)
00786                             if not exepath:
00787                                 add_missing (exe)
00788 
00789                             break
00790 
00791                         argi += 1
00792 
00793             if not exepath:
00794                 # Next pipe.
00795                 break
00796 
00797     if exepath or not exe:
00798         # Look for '*cupsFilter' lines in the PPD and check that
00799         # the filters are installed.
00800         (tmpfd, tmpfname) = tempfile.mkstemp ()
00801         os.unlink (tmpfname)
00802         ppd.writeFd (tmpfd)
00803         os.lseek (tmpfd, 0, os.SEEK_SET)
00804         f = os.fdopen (tmpfd, "r")
00805         search = "*cupsFilter:"
00806         for line in f.readlines ():
00807             if line.startswith (search):
00808                 line = line[len (search):].strip ().strip ('"')
00809                 try:
00810                     (mimetype, cost, exe) = line.split (' ')
00811                 except:
00812                     continue
00813 
00814                 exepath = pathcheck (exe,
00815                                      "/usr/lib/cups/filter:"
00816                                      "/usr/lib64/cups/filter")
00817                 if not exepath:
00818                     add_missing ("/usr/lib/cups/filter/" + exe)
00819 
00820     return exes_to_install

Here is the caller graph for this function:

Check that all relevant executables for a PPD are installed.

@param ppd: PPD
@type ppd: cups.PPD object
@returns: string list pair, representing missing packages and
missing executables

Definition at line 821 of file cupshelpers.py.

00821 
00822 def missingPackagesAndExecutables(ppd):
00823     """
00824     Check that all relevant executables for a PPD are installed.
00825 
00826     @param ppd: PPD
00827     @type ppd: cups.PPD object
00828     @returns: string list pair, representing missing packages and
00829     missing executables
00830     """
00831     executables = missingExecutables(ppd)
00832     return ([], executables)

Here is the call graph for this function:

Parse an IEEE 1284 Device ID, so that it may be indexed by field name.

@param id: IEEE 1284 Device ID, without the two leading length bytes
@type id: string
@returns: dict indexed by field name

Definition at line 480 of file cupshelpers.py.

00480 
00481 def parseDeviceID (id):
00482     """
00483     Parse an IEEE 1284 Device ID, so that it may be indexed by field name.
00484 
00485     @param id: IEEE 1284 Device ID, without the two leading length bytes
00486     @type id: string
00487     @returns: dict indexed by field name
00488     """
00489     id_dict = {}
00490     pieces = id.split(";")
00491     for piece in pieces:
00492         if piece.find(":") == -1:
00493             continue
00494         name, value = piece.split(":",1)
00495         id_dict[name.strip ()] = value.strip()
00496     if id_dict.has_key ("MANUFACTURER"):
00497         id_dict.setdefault("MFG", id_dict["MANUFACTURER"])
00498     if id_dict.has_key ("MODEL"):
00499         id_dict.setdefault("MDL", id_dict["MODEL"])
00500     if id_dict.has_key ("COMMAND SET"):
00501         id_dict.setdefault("CMD", id_dict["COMMAND SET"])
00502     for name in ["MFG", "MDL", "CMD", "CLS", "DES", "SN", "S", "P", "J"]:
00503         id_dict.setdefault(name, "")
00504     if id_dict["CMD"] == '':
00505         id_dict["CMD"] = []
00506     else:
00507         id_dict["CMD"] = id_dict["CMD"].split(',') 
00508     return id_dict

def cupshelpers.cupshelpers.setPPDPageSize (   ppd,
  language 
)
Set the PPD page size according to locale.

@param ppd: PPD
@type ppd: cups.PPD object
@param language: language, as given by the first element of
locale.setlocale
@type language: string

Definition at line 667 of file cupshelpers.py.

00667 
00668 def setPPDPageSize(ppd, language):
00669     """
00670     Set the PPD page size according to locale.
00671 
00672     @param ppd: PPD
00673     @type ppd: cups.PPD object
00674     @param language: language, as given by the first element of
00675     locale.setlocale
00676     @type language: string
00677     """
00678     # Just set the page size to A4 or Letter, that's all.
00679     # Use the same method CUPS uses.
00680     size = 'A4'
00681     letter = [ 'C', 'POSIX', 'en', 'en_US', 'en_CA', 'fr_CA' ]
00682     for each in letter:
00683         if language == each:
00684             size = 'Letter'
00685     try:
00686         ppd.markOption ('PageSize', size)
00687         _debugprint ("set PageSize = %s" % size)
00688     except:
00689         _debugprint ("Failed to set PageSize (%s not available?)" % size)