Back to index

system-config-printer  1.3.9+20120706
Public Member Functions | Public Attributes
cupshelpers.openprinting.OpenPrinting Class Reference

List of all members.

Public Member Functions

def __init__
def cancelOperation
def webQuery
def searchPrinters
def listDrivers

Public Attributes

 language
 base_url
 onlyfree
 onlymanufacturer

Detailed Description

Definition at line 88 of file openprinting.py.


Constructor & Destructor Documentation

def cupshelpers.openprinting.OpenPrinting.__init__ (   self,
  language = None 
)
@param language: language, as given by the first element of
locale.setlocale().
@type language: string

Definition at line 89 of file openprinting.py.

00089 
00090     def __init__(self, language=None):
00091         """
00092         @param language: language, as given by the first element of
00093         locale.setlocale().
00094         @type language: string
00095         """
00096         if language == None:
00097             import locale
00098             try:
00099                 language = locale.getlocale(locale.LC_MESSAGES)
00100             except locale.Error, e:
00101                 language = 'C'
00102         self.language = language
00103 
00104         # XXX Read configuration file.
00105         self.base_url = "www.openprinting.org"
00106 
00107         # Restrictions on driver choices XXX Parameters to be taken from
00108         # config file
00109         self.onlyfree = 1
00110         self.onlymanufacturer = 0


Member Function Documentation

Cancel an operation.

@param handle: query/operation handle

Definition at line 111 of file openprinting.py.

00111 
00112     def cancelOperation(self, handle):
00113         """
00114         Cancel an operation.
00115 
00116         @param handle: query/operation handle
00117         """
00118         # Just prevent the callback.
00119         try:
00120             handle.callback = None
00121         except:
00122             pass

def cupshelpers.openprinting.OpenPrinting.listDrivers (   self,
  model,
  callback,
  user_data = None,
  extra_options = None 
)
Obtain a list of printer drivers.

@type model: string or cupshelpers.Device
@param model: foomatic printer model string or a cupshelpers.Device
object
@type callback: function
@param callback: callback function, taking (integer, user_data, string)
parameters with the first parameter being the status code, zero for
success
@type extra_options: string -> string dictionary
@param extra_options: Additional search options, see
http://www.linuxfoundation.org/en/OpenPrinting/Database/Query
@return: query handle

Definition at line 197 of file openprinting.py.

00197 
00198     def listDrivers(self, model, callback, user_data=None, extra_options=None):
00199         """
00200         Obtain a list of printer drivers.
00201 
00202         @type model: string or cupshelpers.Device
00203         @param model: foomatic printer model string or a cupshelpers.Device
00204         object
00205         @type callback: function
00206         @param callback: callback function, taking (integer, user_data, string)
00207         parameters with the first parameter being the status code, zero for
00208         success
00209         @type extra_options: string -> string dictionary
00210         @param extra_options: Additional search options, see
00211         http://www.linuxfoundation.org/en/OpenPrinting/Database/Query
00212         @return: query handle
00213         """
00214 
00215         def parse_result (status, data, result):
00216             (callback, user_data) = data
00217             if status != 0:
00218                 callback (status, user_data, result)
00219 
00220             try:
00221                 root = XML (result)
00222                 drivers = {}
00223                 # We store the drivers as a dict of:
00224                 # foomatic_id:
00225                 #   { 'name': name,
00226                 #     'url': url,
00227                 #     'supplier': supplier,
00228                 #     'license': short license string e.g. GPLv2,
00229                 #     'licensetext': license text (Plain text),
00230                 #     'nonfreesoftware': Boolean,
00231                 #     'thirdpartysupplied': Boolean,
00232                 #     'manufacturersupplied': Boolean,
00233                 #     'patents': Boolean,
00234                 #     'supportcontacts' (optional):
00235                 #       list of { 'name',
00236                 #                 'url',
00237                 #                 'level',
00238                 #               }
00239                 #     'shortdescription': short description,
00240                 #     'recommended': Boolean,
00241                 #     'functionality':
00242                 #       { 'text': integer percentage,
00243                 #         'lineart': integer percentage,
00244                 #         'graphics': integer percentage,
00245                 #         'photo': integer percentage,
00246                 #         'speed': integer percentage,
00247                 #       }
00248                 #     'packages' (optional):
00249                 #       { arch:
00250                 #         { file:
00251                 #           { 'url': url,
00252                 #             'realversion': upstream version string,
00253                 #             'version': packaged version string,
00254                 #             'release': package release string
00255                 #           }
00256                 #         }
00257                 #       }
00258                 #     'ppds' (optional):
00259                 #       URL string list
00260                 #   }
00261                 # There is more information in the raw XML, but this
00262                 # can be added to the Python structure as needed.
00263 
00264                 for driver in root.findall ('driver'):
00265                     id = driver.attrib.get ('id')
00266                     if id == None:
00267                         continue
00268 
00269                     dict = {}
00270                     for attribute in ['name', 'url', 'supplier', 'license',
00271                                       'shortdescription' ]:
00272                         element = driver.find (attribute)
00273                         if element != None and element.text != None:
00274                             dict[attribute] = _normalize_space (element.text)
00275 
00276                     element = driver.find ('licensetext')
00277                     if element != None:
00278                         dict['licensetext'] = element.text
00279 
00280                     for boolean in ['nonfreesoftware', 'recommended',
00281                                     'patents', 'thirdpartysupplied',
00282                                     'manufacturersupplied']:
00283                         dict[boolean] = driver.find (boolean) != None
00284 
00285                     # Make a 'freesoftware' tag for compatibility with
00286                     # how the OpenPrinting API used to work (see trac
00287                     # #74).
00288                     dict['freesoftware'] = not dict['nonfreesoftware']
00289 
00290                     supportcontacts = []
00291                     container = driver.find ('supportcontacts')
00292                     if container != None:
00293                         for sc in container.findall ('supportcontact'):
00294                             supportcontact = {}
00295                             if sc.text != None:
00296                                 supportcontact['name'] = \
00297                                     _normalize_space (sc.text)
00298                             else:
00299                                 supportcontact['name'] = ""
00300                             supportcontact['url'] = sc.attrib.get ('url')
00301                             supportcontact['level'] = sc.attrib.get ('level')
00302                             supportcontacts.append (supportcontact)
00303 
00304                     if supportcontacts:
00305                         dict['supportcontacts'] = supportcontacts
00306 
00307                     if not dict.has_key ('name') or not dict.has_key ('url'):
00308                         continue
00309 
00310                     container = driver.find ('functionality')
00311                     if container != None:
00312                         functionality = {}
00313                         for attribute in ['text', 'lineart', 'graphics',
00314                                           'photo', 'speed']:
00315                             element = container.find (attribute)
00316                             if element != None:
00317                                 functionality[attribute] = element.text
00318                         if functionality:
00319                             dict[container.tag] = functionality
00320 
00321                     packages = {}
00322                     container = driver.find ('packages')
00323                     if container != None:
00324                         for arch in container.getchildren ():
00325                             rpms = {}
00326                             for package in arch.findall ('package'):
00327                                 rpm = {}
00328                                 for attribute in ['realversion','version',
00329                                                   'release', 'url', 'pkgsys']:
00330                                     element = package.find (attribute)
00331                                     if element != None:
00332                                         rpm[attribute] = element.text
00333 
00334                                 repositories = package.find ('repositories')
00335                                 if repositories != None:
00336                                     for pkgsys in repositories.getchildren ():
00337                                         rpm.setdefault('repositories', {})[pkgsys.tag] = pkgsys.text
00338 
00339                                 rpms[package.attrib['file']] = rpm
00340                             packages[arch.tag] = rpms
00341 
00342                     if packages:
00343                         dict['packages'] = packages
00344 
00345                     ppds = []
00346                     container = driver.find ('ppds')
00347                     if container != None:
00348                         for each in container.getchildren ():
00349                             ppds.append (each.text)
00350 
00351                     if ppds:
00352                         dict['ppds'] = ppds
00353 
00354                     drivers[id] = dict
00355                 callback (0, user_data, drivers)
00356             except:
00357                 callback (1, user_data, sys.exc_info ())
00358 
00359         if isinstance(model, Device):
00360             model = model.id
00361 
00362         params = { 'type': 'drivers',
00363                    'moreinfo': '1',
00364                    'showprinterid': '1',
00365                    'onlynewestdriverpackages': '1',
00366                    'architectures': platform.machine(),
00367                    'noobsoletes': '1',
00368                    'onlyfree': str (self.onlyfree),
00369                    'onlymanufacturer': str (self.onlymanufacturer),
00370                    'printer': model,
00371                    'format': 'xml'}
00372         if extra_options:
00373             params.update(extra_options)
00374         return self.webQuery(params, parse_result, (callback, user_data))

Here is the call graph for this function:

def cupshelpers.openprinting.OpenPrinting.searchPrinters (   self,
  searchterm,
  callback,
  user_data = None 
)
Search for printers using a search term.

@type searchterm: string
@param searchterm: search term
@type callback: function
@param callback: callback function, taking (integer, user_data, string)
parameters with the first parameter being the status code, zero for
success
@return: query handle

Definition at line 139 of file openprinting.py.

00139 
00140     def searchPrinters(self, searchterm, callback, user_data=None):
00141         """
00142         Search for printers using a search term.
00143 
00144         @type searchterm: string
00145         @param searchterm: search term
00146         @type callback: function
00147         @param callback: callback function, taking (integer, user_data, string)
00148         parameters with the first parameter being the status code, zero for
00149         success
00150         @return: query handle
00151         """
00152 
00153         def parse_result (status, data, result):
00154             (callback, user_data) = data
00155             if status != 0:
00156                 callback (status, user_data, result)
00157                 return
00158 
00159             status = 0
00160             printers = {}
00161             try:
00162                 root = XML (result)
00163                 # We store the printers as a dict of:
00164                 # foomatic_id: displayname
00165 
00166                 for printer in root.findall ("printer"):
00167                     id = printer.find ("id")
00168                     make = printer.find ("make")
00169                     model = printer.find ("model")
00170                     if id != None and make != None and model != None:
00171                         idtxt = id.text
00172                         maketxt = make.text
00173                         modeltxt = model.text
00174                         if idtxt and maketxt and modeltxt:
00175                             printers[idtxt] = maketxt + " " + modeltxt
00176             except:
00177                 status = 1
00178                 printers = sys.exc_info ()
00179 
00180             try:
00181                 callback (status, user_data, printers)
00182             except:
00183                 (type, value, tb) = sys.exc_info ()
00184                 tblast = traceback.extract_tb (tb, limit=None)
00185                 if len (tblast):
00186                     tblast = tblast[:len (tblast) - 1]
00187                 extxt = traceback.format_exception_only (type, value)
00188                 for line in traceback.format_tb(tb):
00189                     print (line.strip ())
00190                 print (extxt[0].strip ())
00191 
00192         # Common parameters for the request
00193         params = { 'type': 'printers',
00194                    'printer': searchterm,
00195                    'format': 'xml' }
00196         return self.webQuery(params, parse_result, (callback, user_data))

Here is the call graph for this function:

def cupshelpers.openprinting.OpenPrinting.webQuery (   self,
  parameters,
  callback,
  user_data = None 
)
Run a web query for a driver.

@type parameters: dict
@param parameters: URL parameters
@type callback: function
@param callback: callback function, taking (integer, user_data, string)
parameters with the first parameter being the status code, zero for
success
@return: query handle

Definition at line 123 of file openprinting.py.

00123 
00124     def webQuery(self, parameters, callback, user_data=None):
00125         """
00126         Run a web query for a driver.
00127 
00128         @type parameters: dict
00129         @param parameters: URL parameters
00130         @type callback: function
00131         @param callback: callback function, taking (integer, user_data, string)
00132         parameters with the first parameter being the status code, zero for
00133         success
00134         @return: query handle
00135         """
00136         the_thread = _QueryThread (self, parameters, callback, user_data)
00137         the_thread.start()
00138         return the_thread

Here is the caller graph for this function:


Member Data Documentation

Definition at line 104 of file openprinting.py.

Definition at line 101 of file openprinting.py.

Definition at line 108 of file openprinting.py.

Definition at line 109 of file openprinting.py.


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