Back to index

system-config-printer  1.3.9+20120706
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
probe_printer.PrinterFinder Class Reference

List of all members.

Public Member Functions

def __init__
def find
def cancel

Public Attributes

 quit
 hostname
 callback_fn
 op

Private Member Functions

def _do_find
def _new_device
def _probe_snmp
def _probe_lpd
def _probe_hplip
def _probe_smb
def _probe_jetdirect
def _probe_ipp

Private Attributes

 _cached_attributes

Detailed Description

Definition at line 228 of file probe_printer.py.


Constructor & Destructor Documentation

Definition at line 229 of file probe_printer.py.

00229 
00230     def __init__ (self):
00231         self.quit = False


Member Function Documentation

def probe_printer.PrinterFinder._do_find (   self) [private]

Definition at line 241 of file probe_printer.py.

00241 
00242     def _do_find (self):
00243         self._cached_attributes = dict()
00244         for fn in [self._probe_jetdirect,
00245                    self._probe_ipp,
00246                    self._probe_snmp,
00247                    self._probe_lpd,
00248                    self._probe_hplip,
00249                    self._probe_smb]:
00250             if self.quit:
00251                 return
00252 
00253             try:
00254                 fn ()
00255             except Exception, e:
00256                 nonfatalException ()
00257 
00258         # Signal that we've finished.
00259         if not self.quit:
00260             self.callback_fn (None)

def probe_printer.PrinterFinder._new_device (   self,
  uri,
  info,
  location = None 
) [private]

Definition at line 261 of file probe_printer.py.

00261 
00262     def _new_device (self, uri, info, location = None):
00263         device_dict = { 'device-class': 'network',
00264                         'device-info': "%s" % info }
00265         if location:
00266             device_dict['device-location']=location
00267         device_dict.update (self._cached_attributes)
00268         new_device = cupshelpers.Device (uri, **device_dict)
00269         debugprint ("Device found: %s" % uri)
00270         self.callback_fn (new_device)

Here is the caller graph for this function:

def probe_printer.PrinterFinder._probe_hplip (   self) [private]

Definition at line 357 of file probe_printer.py.

00357 
00358     def _probe_hplip (self):
00359         null = file ("/dev/null", "r+")
00360         try:
00361             debugprint ("hplip: trying")
00362             p = subprocess.Popen (args=["hp-makeuri", "-c", self.hostname],
00363                                   close_fds=True,
00364                                   stdin=null,
00365                                   stdout=subprocess.PIPE,
00366                                   stderr=null)
00367         except OSError, e:
00368             if e == errno.ENOENT:
00369                 return
00370 
00371             raise
00372 
00373         (stdout, stderr) = p.communicate ()
00374         if p.returncode != 0:
00375             debugprint ("hplip: no good (return code %d)" % p.returncode)
00376             return
00377 
00378         if self.quit:
00379             debugprint ("hplip: no good")
00380             return
00381 
00382         uri = stdout.strip ()
00383         debugprint ("hplip: uri is %s" % uri)
00384         if uri.find (":") != -1:
00385             self._new_device(uri, uri)
00386 
00387         debugprint ("hplip: done")

Here is the call graph for this function:

def probe_printer.PrinterFinder._probe_ipp (   self) [private]

Definition at line 447 of file probe_printer.py.

00447 
00448     def _probe_ipp (self):
00449         debugprint ("ipp: trying")
00450         try:
00451             ai = socket.getaddrinfo(self.hostname, 631, socket.AF_UNSPEC,
00452                                     socket.SOCK_STREAM)
00453         except socket.gaierror:
00454             debugprint ("ipp: can't resolve %s" % self.hostname)
00455             debugprint ("ipp: no good")
00456             return
00457         for res in ai:
00458             af, socktype, proto, canonname, sa = res
00459             if (af == socket.AF_INET and sa[0] == '127.0.0.1' or
00460                 af == socket.AF_INET6 and sa[0] == '::1'):
00461                 debugprint ("ipp: do not probe local cups server")
00462                 debugprint ("ipp: no good")
00463                 return
00464 
00465         try:
00466             c = cups.Connection (host = self.hostname)
00467         except RuntimeError:
00468             debugprint ("ipp: can't connect to server/printer")
00469             debugprint ("ipp: no good")
00470             return
00471 
00472         try:
00473             printers = c.getPrinters ()
00474         except cups.IPPError:
00475             debugprint ("%s is probably not a cups server but IPP printer" %
00476                         self.hostname)
00477             uri = "ipp://%s:631/ipp" % (self.hostname)
00478             info = "IPP (%s)" % self.hostname
00479             self._new_device(uri, info)
00480             debugprint ("ipp: done")
00481             return
00482 
00483         for name, queue in printers.iteritems ():
00484             uri = queue['printer-uri-supported']
00485             info = queue['printer-info']
00486             location = queue['printer-location']
00487             self._new_device(uri, info, location)
00488 
00489         debugprint ("ipp: done")

Here is the call graph for this function:

Definition at line 430 of file probe_printer.py.

00430 
00431     def _probe_jetdirect (self):
00432         port = 9100    #jetdirect
00433         sock_address = (self.hostname, port)
00434         debugprint ("jetdirect: trying")
00435         s = open_socket(self.hostname, port)
00436         if not s:
00437             debugprint ("jetdirect: %s:%d CLOSED" % sock_address)
00438         else:
00439             # port is open so assume its a JetDirect device
00440             debugprint ("jetdirect %s:%d OPEN" % sock_address)
00441             uri = "socket://%s:%d" % sock_address
00442             info = "JetDirect (%s)" % self.hostname
00443             self._new_device(uri, info)
00444             s.close ()
00445 
00446         debugprint ("jetdirect: done")

Here is the call graph for this function:

def probe_printer.PrinterFinder._probe_lpd (   self) [private]

Definition at line 332 of file probe_printer.py.

00332 
00333     def _probe_lpd (self):
00334         debugprint ("lpd: trying")
00335         lpd = LpdServer (self.hostname)
00336         for name in lpd.get_possible_queue_names ():
00337             if self.quit:
00338                 debugprint ("lpd: no good")
00339                 return
00340 
00341             found = lpd.probe_queue (name, [])
00342             if found == None:
00343                 # Couldn't even connect.
00344                 debugprint ("lpd: couldn't connect")
00345                 break
00346 
00347             if found:
00348                 uri = "lpd://%s/%s" % (self.hostname, name)
00349                 self._new_device(uri, self.hostname)
00350 
00351             if not found and name.startswith ("pr"):
00352                 break
00353 
00354             time.sleep(0.1) # avoid DOS and following counter measures 
00355 
00356         debugprint ("lpd: done")

Here is the call graph for this function:

def probe_printer.PrinterFinder._probe_smb (   self) [private]

Definition at line 388 of file probe_printer.py.

00388 
00389     def _probe_smb (self):
00390         if not PYSMB_AVAILABLE:
00391             return
00392 
00393         smbc_auth = BackgroundSmbAuthContext ()
00394         debug = 0
00395         if get_debugging ():
00396             debug = 10
00397         ctx = pysmb.smbc.Context (debug=debug,
00398                                   auth_fn=smbc_auth.callback)
00399         entries = []
00400         uri = "smb://%s/" % self.hostname
00401         debugprint ("smb: trying")
00402         try:
00403             while smbc_auth.perform_authentication () > 0:
00404                 if self.quit:
00405                     debugprint ("smb: no good")
00406                     return
00407 
00408                 try:
00409                     entries = ctx.opendir (uri).getdents ()
00410                 except Exception, e:
00411                     smbc_auth.failed (e)
00412         except RuntimeError, (e, s):
00413             if e not in [errno.ENOENT, errno.EACCES, errno.EPERM]:
00414                 debugprint ("Runtime error: %s" % repr ((e, s)))
00415         except:
00416             nonfatalException ()
00417 
00418         if self.quit:
00419             debugprint ("smb: no good")
00420             return
00421 
00422         for entry in entries:
00423             if entry.smbc_type == pysmb.smbc.PRINTER_SHARE:
00424                 uri = "smb://%s/%s" % (smburi.urlquote (self.hostname),
00425                                        smburi.urlquote (entry.name))
00426                 info = "SMB (%s)" % self.hostname
00427                 self._new_device(uri, info)
00428 
00429         debugprint ("smb: done")

Here is the call graph for this function:

def probe_printer.PrinterFinder._probe_snmp (   self) [private]

Definition at line 271 of file probe_printer.py.

00271 
00272     def _probe_snmp (self):
00273         # Run the CUPS SNMP backend, pointing it at the host.
00274         null = file ("/dev/null", "r+")
00275         try:
00276             debugprint ("snmp: trying")
00277             p = subprocess.Popen (args=["/usr/lib/cups/backend/snmp",
00278                                         self.hostname],
00279                                   close_fds=True,
00280                                   stdin=null,
00281                                   stdout=subprocess.PIPE,
00282                                   stderr=null)
00283         except OSError, e:
00284             debugprint ("snmp: no good")
00285             if e == errno.ENOENT:
00286                 return
00287 
00288             raise
00289 
00290         (stdout, stderr) = p.communicate ()
00291         if p.returncode != 0:
00292             debugprint ("snmp: no good (return code %d)" % p.returncode)
00293             return
00294 
00295         if self.quit:
00296             debugprint ("snmp: no good")
00297             return
00298 
00299         for line in stdout.split ('\n'):
00300             words = wordsep (line)
00301             n = len (words)
00302             if n == 6:
00303                 (device_class, uri, make_and_model,
00304                  info, device_id, device_location) = words
00305             elif n == 5:
00306                 (device_class, uri, make_and_model, info, device_id) = words
00307             elif n == 4:
00308                 (device_class, uri, make_and_model, info) = words
00309             else:
00310                 continue
00311 
00312             device_dict = { 'device-class': device_class,
00313                             'device-make-and-model': make_and_model,
00314                             'device-info': info }
00315             if n == 5:
00316                 debugprint ("snmp: Device ID found:\n%s" %
00317                             device_id)
00318                 device_dict['device-id'] = device_id
00319             if n == 6:
00320                 device_dict['device-location'] = device_location
00321 
00322             device = cupshelpers.Device (uri, **device_dict)
00323             debugprint ("Device found: %s" % uri)
00324             self.callback_fn (device)
00325 
00326             # Cache the make and model for use by other search methods
00327             # that are not able to determine it.
00328             self._cached_attributes['device-make-and-model'] = make_and_model
00329             self._cached_attributes['device_id'] = device_id
00330 
00331         debugprint ("snmp: done")

Definition at line 237 of file probe_printer.py.

00237 
00238     def cancel (self):
00239         self.op.cancel ()
00240         self.quit = True

Here is the caller graph for this function:

def probe_printer.PrinterFinder.find (   self,
  hostname,
  callback_fn 
)

Definition at line 232 of file probe_printer.py.

00232 
00233     def find (self, hostname, callback_fn):
00234         self.hostname = hostname
00235         self.callback_fn = callback_fn
00236         self.op = TimedOperation (self._do_find, callback=lambda x, y: None)


Member Data Documentation

Definition at line 242 of file probe_printer.py.

Definition at line 234 of file probe_printer.py.

Definition at line 233 of file probe_printer.py.

Definition at line 235 of file probe_printer.py.

Definition at line 230 of file probe_printer.py.


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