Back to index

system-config-printer  1.3.9+20120706
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes
cupshelpers.cupshelpers.Printer Class Reference

List of all members.

Public Member Functions

def __init__
def __del__
def __repr__
def update
def getAttributes
def getServer
def getPPD
def setOption
def unsetOption
def setEnabled
def setAccepting
def setShared
def setErrorPolicy
def setOperationPolicy
def setJobSheets
def setAccess
def jobsQueued
def jobsPreserved
def testsQueued
def setAsDefault

Public Attributes

 name
 connection
 class_members
 state
 enabled
 device_uri
 info
 is_shared
 location
 make_and_model
 type
 uri_supported
 other_attributes
 attributes
 possible_attributes
 job_sheet_end
 job_sheets_supported
 error_policy
 error_policy_supported
 op_policy
 op_policy_supported
 default_allow
 except_users
 except_users_string

Private Member Functions

def _expand_flags

Private Attributes

 _ppd

Static Private Attributes

list _flags_blacklist = ["options", "local"]

Detailed Description

Definition at line 26 of file cupshelpers.py.


Constructor & Destructor Documentation

def cupshelpers.cupshelpers.Printer.__init__ (   self,
  name,
  connection,
  kw 
)
@param name: printer name
@type name: string
@param connection: CUPS connection
@type connection: CUPS.Connection object
@param kw: printer attributes
@type kw: dict indexed by string

Definition at line 29 of file cupshelpers.py.

00029 
00030     def __init__(self, name, connection, **kw):
00031         """
00032         @param name: printer name
00033         @type name: string
00034         @param connection: CUPS connection
00035         @type connection: CUPS.Connection object
00036         @param kw: printer attributes
00037         @type kw: dict indexed by string
00038         """
00039         self.name = name
00040         self.connection = connection
00041         self.class_members = []
00042         have_kw = len (kw) > 0
00043         fetch_attrs = True
00044         if have_kw:
00045             self.update (**kw)
00046             if self.is_class:
00047                 fetch_attrs = True
00048             else:
00049                 fetch_attrs = False
00050 
00051         if fetch_attrs:
00052             self.getAttributes ()
00053 
00054         self._ppd = None # load on demand

Definition at line 55 of file cupshelpers.py.

00055 
00056     def __del__ (self):
00057         if self._ppd != None:
00058             os.unlink(self._ppd)


Member Function Documentation

Definition at line 59 of file cupshelpers.py.

00059 
00060     def __repr__ (self):
00061         return "<cupshelpers.Printer \"%s\">" % self.name

Here is the caller graph for this function:

Definition at line 62 of file cupshelpers.py.

00062 
00063     def _expand_flags(self):
00064 
00065         def _ascii_lower(str):
00066             return str.translate(string.maketrans(string.ascii_uppercase,
00067                                                   string.ascii_lowercase));
00068 
00069         prefix = "CUPS_PRINTER_"
00070         prefix_length = len(prefix)
00071 
00072         # loop over cups constants
00073         for name in cups.__dict__:
00074             if name.startswith(prefix):
00075                 attr_name = \
00076                     _ascii_lower(name[prefix_length:])
00077                 if attr_name in self._flags_blacklist: continue
00078                 if attr_name == "class": attr_name = "is_class"
00079                 # set as attribute
00080                 setattr(self, attr_name,
00081                         bool(self.type & getattr(cups, name)))

Fetch further attributes for the printer.

Normally only a small set of attributes is fetched.  This
method is for fetching more.

Definition at line 110 of file cupshelpers.py.

00110 
00111     def getAttributes(self):
00112         """
00113         Fetch further attributes for the printer.
00114 
00115         Normally only a small set of attributes is fetched.  This
00116         method is for fetching more.
00117         """
00118         attrs = self.connection.getPrinterAttributes(self.name)
00119         self.attributes = {}
00120         self.other_attributes = {}
00121         self.possible_attributes = {
00122             'landscape' : ('False', ['True', 'False']),
00123             'page-border' : ('none', ['none', 'single', 'single-thick',
00124                                      'double', 'double-thick']),
00125             }
00126 
00127         for key, value in attrs.iteritems():
00128             if key.endswith("-default"):
00129                 name = key[:-len("-default")]
00130                 if name in ["job-sheets", "printer-error-policy",
00131                             "printer-op-policy", # handled below
00132                             "notify-events", # cannot be set
00133                             "document-format", # cannot be set
00134                             "notify-lease-duration"]: # cannot be set
00135                     continue 
00136 
00137                 supported = attrs.get(name + "-supported", None) or \
00138                             self.possible_attributes.get(name, None) or \
00139                             ""
00140 
00141                 # Convert a list into a comma-separated string, since
00142                 # it can only really have been misinterpreted as a list
00143                 # by CUPS.
00144                 if isinstance (value, list):
00145                     value = reduce (lambda x, y: x+','+y, value)
00146 
00147                 self.attributes[name] = value
00148                     
00149                 if attrs.has_key(name+"-supported"):
00150                     supported = attrs[name+"-supported"]
00151                     self.possible_attributes[name] = (value, supported)
00152             elif (not key.endswith ("-supported") and
00153                   key != 'job-sheets-default' and
00154                   key != 'printer-error-policy' and
00155                   key != 'printer-op-policy' and
00156                   not key.startswith ('requesting-user-name-')):
00157                 self.other_attributes[key] = value
00158         
00159         self.job_sheet_start, self.job_sheet_end = attrs.get(
00160             'job-sheets-default', ('none', 'none'))
00161         self.job_sheets_supported = attrs.get('job-sheets-supported', ['none'])
00162         self.error_policy = attrs.get('printer-error-policy', 'none')
00163         self.error_policy_supported = attrs.get(
00164             'printer-error-policy-supported', ['none'])
00165         self.op_policy = attrs.get('printer-op-policy', "") or "default"
00166         self.op_policy_supported = attrs.get(
00167             'printer-op-policy-supported', ["default"])
00168 
00169         self.default_allow = True
00170         self.except_users = []
00171         if attrs.has_key('requesting-user-name-allowed'):
00172             self.except_users = attrs['requesting-user-name-allowed']
00173             self.default_allow = False
00174         elif attrs.has_key('requesting-user-name-denied'):
00175             self.except_users = attrs['requesting-user-name-denied']
00176         self.except_users_string = ', '.join(self.except_users)
00177         self.update (**attrs)

Obtain the printer's PPD.

@returns: cups.PPD object, or False for raw queues
@raise cups.IPPError: IPP error

Definition at line 193 of file cupshelpers.py.

00193 
00194     def getPPD(self):
00195         """
00196         Obtain the printer's PPD.
00197 
00198         @returns: cups.PPD object, or False for raw queues
00199         @raise cups.IPPError: IPP error
00200         """
00201         result = None
00202         if self._ppd is None:
00203             try:
00204                 self._ppd = self.connection.getPPD(self.name)
00205                 result = cups.PPD (self._ppd)
00206             except cups.IPPError, (e, m):
00207                 if e == cups.IPP_NOT_FOUND:
00208                     result = False
00209                 else:
00210                     raise
00211 
00212         if result == None and self._ppd != None:
00213             result = cups.PPD (self._ppd)
00214 
00215         return result

Find out which server defines this printer.

@returns: server URI or None

Definition at line 178 of file cupshelpers.py.

00178 
00179     def getServer(self):
00180         """
00181         Find out which server defines this printer.
00182 
00183         @returns: server URI or None
00184         """
00185         if not self.uri_supported[0].startswith('ipp://'):
00186             return None
00187         uri = self.uri_supported[0][6:]
00188         uri = uri.split('/')[0]
00189         uri = uri.split(':')[0]
00190         if uri == "localhost.localdomain":
00191             uri = "localhost"
00192         return uri

def cupshelpers.cupshelpers.Printer.jobsPreserved (   self,
  limit = None 
)
Find out whether there are preserved jobs for this printer.

@return: list of job IDs

Definition at line 373 of file cupshelpers.py.

00373 
00374     def jobsPreserved(self, limit=None):
00375         """
00376         Find out whether there are preserved jobs for this printer.
00377 
00378         @return: list of job IDs
00379         """
00380         ret = []
00381         try:
00382             try:
00383                 r = ['job-id', 'job-printer-uri', 'job-state']
00384                 jobs = self.connection.getJobs (which_jobs='completed',
00385                                                 requested_attributes=r)
00386             except TypeError:
00387                 # requested_attributes requires pycups 1.9.50
00388                 jobs = self.connection.getJobs (which_jobs='completed')
00389         except cups.IPPError:
00390             return ret
00391 
00392         for id, attrs in jobs.iteritems():
00393             try:
00394                 uri = attrs['job-printer-uri']
00395                 uri = uri[uri.rindex ('/') + 1:]
00396             except:
00397                 continue
00398             if uri != self.name:
00399                 continue
00400             if (attrs.get ('job-state',
00401                            cups.IPP_JOB_PENDING) < cups.IPP_JOB_COMPLETED):
00402                 continue
00403             ret.append (id)
00404             if limit != None and len (ret) == limit:
00405                 break
00406 
00407         return ret

def cupshelpers.cupshelpers.Printer.jobsQueued (   self,
  only_tests = False,
  limit = None 
)
Find out whether jobs are queued for this printer.

@param only_tests: whether to restrict search to test pages
@type only_tests: bool
@returns: list of job IDs

Definition at line 336 of file cupshelpers.py.

00336 
00337     def jobsQueued(self, only_tests=False, limit=None):
00338         """
00339         Find out whether jobs are queued for this printer.
00340 
00341         @param only_tests: whether to restrict search to test pages
00342         @type only_tests: bool
00343         @returns: list of job IDs
00344         """
00345         ret = []
00346         try:
00347             try:
00348                 r = ['job-id', 'job-printer-uri', 'job-name']
00349                 jobs = self.connection.getJobs (requested_attributes=r)
00350             except TypeError:
00351                 # requested_attributes requires pycups 1.9.50
00352                 jobs = self.connection.getJobs ()
00353         except cups.IPPError:
00354             return ret
00355 
00356         for id, attrs in jobs.iteritems():
00357             try:
00358                 uri = attrs['job-printer-uri']
00359                 uri = uri[uri.rindex ('/') + 1:]
00360             except:
00361                 continue
00362             if uri != self.name:
00363                 continue
00364 
00365             if (not only_tests or
00366                 (attrs.has_key ('job-name') and
00367                  attrs['job-name'] == 'Test Page')):
00368                 ret.append (id)
00369 
00370                 if limit != None and len (ret) == limit:
00371                     break
00372         return ret

Here is the caller graph for this function:

def cupshelpers.cupshelpers.Printer.setAccepting (   self,
  on,
  reason = None 
)
Set the printer's accepting state.

@param on: whether it will be accepting
@type on: bool
@param reason: reason for this state
@type reason: string

Definition at line 258 of file cupshelpers.py.

00258 
00259     def setAccepting(self, on, reason=None):
00260         """
00261         Set the printer's accepting state.
00262 
00263         @param on: whether it will be accepting
00264         @type on: bool
00265         @param reason: reason for this state
00266         @type reason: string
00267         """
00268         if on:
00269             self.connection.acceptJobs(self.name)
00270         else:
00271             if reason:
00272                 self.connection.rejectJobs(self.name, reason=reason)
00273             else:
00274                 self.connection.rejectJobs(self.name)

def cupshelpers.cupshelpers.Printer.setAccess (   self,
  allow,
  except_users 
)
Set access control list.

@param allow: whether to allow by default, otherwise deny
@type allow: bool
@param except_users: exception list
@type except_users: string list

Definition at line 313 of file cupshelpers.py.

00313 
00314     def setAccess(self, allow, except_users):
00315         """
00316         Set access control list.
00317 
00318         @param allow: whether to allow by default, otherwise deny
00319         @type allow: bool
00320         @param except_users: exception list
00321         @type except_users: string list
00322         """
00323         if isinstance(except_users, str):
00324             users = except_users.split()
00325             users = [u.split(",") for u in users]
00326             except_users = []
00327             for u in users:
00328                 except_users.extend(u)
00329             except_users = [u.strip() for u in except_users]
00330             except_users = filter(None, except_users)
00331             
00332         if allow:
00333             self.connection.setPrinterUsersDenied(self.name, except_users)
00334         else:
00335             self.connection.setPrinterUsersAllowed(self.name, except_users)

Set this printer as the system default.

Definition at line 416 of file cupshelpers.py.

00416 
00417     def setAsDefault(self):
00418         """
00419         Set this printer as the system default.
00420         """
00421         self.connection.setDefault(self.name)
00422 
00423         # Also need to check system-wide lpoptions because that's how
00424         # previous Fedora versions set the default (bug #217395).
00425         (tmpfd, tmpfname) = tempfile.mkstemp ()
00426         os.remove (tmpfname)
00427         try:
00428             resource = "/admin/conf/lpoptions"
00429             self.connection.getFile(resource, fd=tmpfd)
00430         except cups.HTTPError, (s,):
00431             if s == cups.HTTP_NOT_FOUND:
00432                 return False
00433 
00434             raise cups.HTTPError (s)
00435 
00436         f = os.fdopen (tmpfd, 'r+')
00437         f.seek (0)
00438         lines = f.readlines ()
00439         changed = False
00440         i = 0
00441         for line in lines:
00442             if line.startswith ("Default "):
00443                 # This is the system-wide default.
00444                 name = line.split (' ')[1]
00445                 if name != self.name:
00446                     # Stop it from over-riding the server default.
00447                     lines[i] = "Dest " + line[8:]
00448                     changed = True
00449                 i += 1
00450 
00451         if changed:
00452             f.seek (0)
00453             f.writelines (lines)
00454             f.truncate ()
00455             os.lseek (tmpfd, 0, os.SEEK_SET)
00456             try:
00457                 self.connection.putFile (resource, fd=tmpfd)
00458             except cups.HTTPError, (s,):
00459                 return False
00460 
00461         return changed

def cupshelpers.cupshelpers.Printer.setEnabled (   self,
  on,
  reason = None 
)
Set the printer's enabled state.

@param on: whether it will be enabled
@type on: bool
@param reason: reason for this state
@type reason: string

Definition at line 241 of file cupshelpers.py.

00241 
00242     def setEnabled(self, on, reason=None):
00243         """
00244         Set the printer's enabled state.
00245 
00246         @param on: whether it will be enabled
00247         @type on: bool
00248         @param reason: reason for this state
00249         @type reason: string
00250         """
00251         if on:
00252             self.connection.enablePrinter(self.name)
00253         else:
00254             if reason:
00255                 self.connection.disablePrinter(self.name, reason=reason)
00256             else:
00257                 self.connection.disablePrinter(self.name)

def cupshelpers.cupshelpers.Printer.setErrorPolicy (   self,
  policy 
)
Set the printer's error policy.

@param policy: error policy
@type policy: string

Definition at line 284 of file cupshelpers.py.

00284 
00285     def setErrorPolicy (self, policy):
00286         """
00287         Set the printer's error policy.
00288 
00289         @param policy: error policy
00290         @type policy: string
00291         """
00292         self.connection.setPrinterErrorPolicy(self.name, policy)

def cupshelpers.cupshelpers.Printer.setJobSheets (   self,
  start,
  end 
)
Set the printer's job sheets.

@param start: start sheet
@type start: string
@param end: end sheet
@type end: string

Definition at line 302 of file cupshelpers.py.

00302 
00303     def setJobSheets(self, start, end):
00304         """
00305         Set the printer's job sheets.
00306 
00307         @param start: start sheet
00308         @type start: string
00309         @param end: end sheet
00310         @type end: string
00311         """
00312         self.connection.setPrinterJobSheets(self.name, start, end)

Set the printer's operation policy.

@param policy: operation policy
@type policy: string

Definition at line 293 of file cupshelpers.py.

00293 
00294     def setOperationPolicy(self, policy):
00295         """
00296         Set the printer's operation policy.
00297 
00298         @param policy: operation policy
00299         @type policy: string
00300         """
00301         self.connection.setPrinterOpPolicy(self.name, policy)    

def cupshelpers.cupshelpers.Printer.setOption (   self,
  name,
  value 
)
Set a printer's option.

@param name: option name
@type name: string
@param value: option value
@type value: option-specific

Definition at line 216 of file cupshelpers.py.

00216 
00217     def setOption(self, name, value):
00218         """
00219         Set a printer's option.
00220 
00221         @param name: option name
00222         @type name: string
00223         @param value: option value
00224         @type value: option-specific
00225         """
00226         if isinstance (value, float):
00227             radixchar = locale.nl_langinfo (locale.RADIXCHAR)
00228             if radixchar != '.':
00229                 # Convert floats to strings, being careful with decimal points.
00230                 value = str (value).replace (radixchar, '.')
00231         self.connection.addPrinterOptionDefault(self.name, name, value)

Set the printer's shared state.

@param on: whether it will be accepting
@type on: bool

Definition at line 275 of file cupshelpers.py.

00275 
00276     def setShared(self,on):
00277         """
00278         Set the printer's shared state.
00279 
00280         @param on: whether it will be accepting
00281         @type on: bool
00282         """
00283         self.connection.setPrinterShared(self.name, on)

def cupshelpers.cupshelpers.Printer.testsQueued (   self,
  limit = None 
)
Find out whether test jobs are queued for this printer.

@returns: list of job IDs

Definition at line 408 of file cupshelpers.py.

00408 
00409     def testsQueued(self, limit=None):
00410         """
00411         Find out whether test jobs are queued for this printer.
00412 
00413         @returns: list of job IDs
00414         """
00415         return self.jobsQueued (only_tests=True, limit=limit)

Here is the call graph for this function:

def cupshelpers.cupshelpers.Printer.unsetOption (   self,
  name 
)
Unset a printer's option.

@param name: option name
@type name: string

Definition at line 232 of file cupshelpers.py.

00232 
00233     def unsetOption(self, name):
00234         """
00235         Unset a printer's option.
00236 
00237         @param name: option name
00238         @type name: string
00239         """
00240         self.connection.deletePrinterOptionDefault(self.name, name)

def cupshelpers.cupshelpers.Printer.update (   self,
  kw 
)
Update object from printer attributes.

@param kw: printer attributes
@type kw: dict indexed by string

Definition at line 82 of file cupshelpers.py.

00082 
00083     def update(self, **kw):
00084         """
00085         Update object from printer attributes.
00086 
00087         @param kw: printer attributes
00088         @type kw: dict indexed by string
00089         """
00090         self.state = kw.get('printer-state', 0)
00091         self.enabled = self.state != cups.IPP_PRINTER_STOPPED
00092         self.device_uri = kw.get('device-uri', "")
00093         self.info = kw.get('printer-info', "")
00094         self.is_shared = kw.get('printer-is-shared', None)
00095         self.location = kw.get('printer-location', "")
00096         self.make_and_model = kw.get('printer-make-and-model', "")
00097         self.type = kw.get('printer-type', 0)
00098         self.uri_supported = kw.get('printer-uri-supported', "")
00099         if type (self.uri_supported) != list:
00100             self.uri_supported = [self.uri_supported]
00101         self._expand_flags()
00102         if self.is_shared is None:
00103             self.is_shared = not self.not_shared
00104         del self.not_shared
00105         self.class_members = kw.get('member-names', [])
00106         if type (self.class_members) != list:
00107             self.class_members = [self.class_members]
00108         self.class_members.sort ()
00109         self.other_attributes = kw

Here is the caller graph for this function:


Member Data Documentation

list cupshelpers.cupshelpers.Printer._flags_blacklist = ["options", "local"] [static, private]

Definition at line 27 of file cupshelpers.py.

Definition at line 53 of file cupshelpers.py.

Definition at line 118 of file cupshelpers.py.

Definition at line 40 of file cupshelpers.py.

Definition at line 39 of file cupshelpers.py.

Definition at line 168 of file cupshelpers.py.

Definition at line 91 of file cupshelpers.py.

Definition at line 90 of file cupshelpers.py.

Definition at line 161 of file cupshelpers.py.

Definition at line 162 of file cupshelpers.py.

Definition at line 169 of file cupshelpers.py.

Definition at line 175 of file cupshelpers.py.

Definition at line 92 of file cupshelpers.py.

Definition at line 93 of file cupshelpers.py.

Definition at line 158 of file cupshelpers.py.

Definition at line 160 of file cupshelpers.py.

Definition at line 94 of file cupshelpers.py.

Definition at line 95 of file cupshelpers.py.

Definition at line 38 of file cupshelpers.py.

Definition at line 164 of file cupshelpers.py.

Definition at line 165 of file cupshelpers.py.

Definition at line 108 of file cupshelpers.py.

Definition at line 120 of file cupshelpers.py.

Definition at line 89 of file cupshelpers.py.

Definition at line 96 of file cupshelpers.py.

Definition at line 97 of file cupshelpers.py.


The documentation for this class was generated from the following file: