home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / PhysicalDevice.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  7.0 KB  |  208 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2008, 2009, 2010 Red Hat, Inc.
  4. ## Authors:
  5. ##  Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. from gettext import gettext as _
  22. import cupshelpers
  23. import urllib
  24.  
  25. import ppdippstr
  26.  
  27. class PhysicalDevice:
  28.     def __init__(self, device):
  29.         self.devices = None
  30.         self._network_host = None
  31.         self.add_device (device)
  32.         self._user_data = {}
  33.         self._ppdippstr = ppdippstr.backends
  34.  
  35.     def _canonical_id (self, device):
  36.         mfg = device.id_dict.get ('MFG', '')
  37.         mdl = device.id_dict.get ('MDL', '')
  38.  
  39.         if mfg == '' or mdl.lower ().startswith (mfg.lower ()):
  40.             make_and_model = mdl
  41.         else:
  42.             make_and_model = "%s %s" % (mfg, mdl)
  43.  
  44.         return cupshelpers.ppds.ppdMakeModelSplit (make_and_model)
  45.  
  46.     def _get_host_from_uri (self, uri):
  47.         (scheme, rest) = urllib.splittype (uri)
  48.         if scheme == 'hp' or scheme == 'hpfax':
  49.             if rest.startswith ("/net/"):
  50.                 (rest, ipparam) = urllib.splitquery (rest[5:])
  51.                 if ipparam != None and ipparam.startswith("ip="):
  52.                     hostport = ipparam[3:]
  53.                 else:
  54.                     return None
  55.             else:
  56.                 return None
  57.         else:
  58.             (hostport, rest) = urllib.splithost (rest)
  59.             if hostport == None:
  60.                 return None
  61.  
  62.         (host, port) = urllib.splitport (hostport)
  63.         return host
  64.  
  65.     def add_device (self, device):
  66.         if self._network_host:
  67.             if hasattr (device, 'address'):
  68.                 host = device.address
  69.             else:
  70.                 host = self._get_host_from_uri (device.uri)
  71.  
  72.             if host != self._network_host:
  73.                 raise ValueError
  74.         else:
  75.             (mfg, mdl) = self._canonical_id (device)
  76.             if self.devices == None:
  77.                 self.mfg = mfg
  78.                 self.mdl = mdl
  79.                 self.mfg_lower = mfg.lower ()
  80.                 self.mdl_lower = mdl.lower ()
  81.                 self.sn = device.id_dict.get ('SN', '')
  82.                 self.devices = []
  83.             else:
  84.                 def nicest (a, b):
  85.                     def count_lower (s):
  86.                         l = s.lower ()
  87.                         n = 0
  88.                         for i in xrange (len (s)):
  89.                             if l[i] != s[i]:
  90.                                 n += 1
  91.                         return n
  92.                     if count_lower (b) < count_lower (a):
  93.                         return b
  94.                     return a
  95.  
  96.                 self.mfg = nicest (self.mfg, mfg)
  97.                 self.mdl = nicest (self.mdl, mdl)
  98.  
  99.                 sn = device.id_dict.get ('SN', '')
  100.                 if sn != '' and self.sn != '' and sn != self.sn:
  101.                     raise ValueError
  102.  
  103.         for d in self.devices:
  104.             if d.uri == device.uri:
  105.                 return
  106.  
  107.         self.devices.append (device)
  108.         self.devices.sort ()
  109.  
  110.         if not self._network_host and device.device_class == "network":
  111.             # We just added a network device.
  112.             self._network_host = self._get_host_from_uri (device.uri)
  113.  
  114.     def get_devices (self):
  115.         return self.devices
  116.  
  117.     def get_info (self):
  118.         # If the manufacturer/model is not known, or useless (in the
  119.         # case of the hpfax backend), show the device-info field
  120.         # instead.
  121.         if self.mfg == '' or (self.mfg == "HP" and self.mdl == "Fax"):
  122.             return self._ppdippstr.get (self.devices[0].info)
  123.  
  124.         info = "%s %s" % (self.mfg, self.mdl)
  125.         if len (self.sn) > 0:
  126.             info += " (%s)" % self.sn
  127.         return info
  128.  
  129.     # User data
  130.     def set_data (self, key, value):
  131.         self._user_data[key] = value
  132.  
  133.     def get_data (self, key):
  134.         return self._user_data.get (key)
  135.  
  136.     def __str__ (self):
  137.         return "(description: %s)" % self.__repr__ ()
  138.  
  139.     def __repr__ (self):
  140.         return "<PhysicalDevice.PhysicalDevice (%s,%s,%s)>" % (self.mfg,
  141.                                                                self.mdl,
  142.                                                                self.sn)
  143.  
  144.     def __cmp__(self, other):
  145.         if other == None or type (other) != type (self):
  146.             return 1
  147.  
  148.         if (self._network_host != None or
  149.             other._network_host != None):
  150.             return cmp (self._network_host, other._network_host)
  151.  
  152.         if (other.mfg == '' and other.mdl == '') or \
  153.            (self.mfg == '' and self.mdl == ''):
  154.             # One or other is just a backend, not a real physical device.
  155.             if other.mfg == '' and other.mdl == '' and \
  156.                self.mfg == '' and self.mdl == '':
  157.                 return cmp (self.devices[0], other.devices[0])
  158.  
  159.             if other.mfg == '' and other.mdl == '':
  160.                 return -1
  161.             return 1
  162.  
  163.         if self.mfg == '' or self.mdl.lower ().startswith (self.mfg.lower ()):
  164.             our_make_and_model = self.mdl
  165.         else:
  166.             our_make_and_model = "%s %s" % (self.mfg, self.mdl)
  167.         (our_mfg, our_mdl) = \
  168.             cupshelpers.ppds.ppdMakeModelSplit (our_make_and_model)
  169.  
  170.         if other.mfg == '' or \
  171.                 other.mdl.lower ().startswith (other.mfg.lower ()):
  172.             other_make_and_model = other.mdl
  173.         else:
  174.             other_make_and_model = "%s %s" % (other.mfg, other.mdl)
  175.         (other_mfg, other_mdl) = \
  176.             cupshelpers.ppds.ppdMakeModelSplit (other_make_and_model)
  177.  
  178.         mfgcmp = cmp (our_mfg.lower (), other_mfg.lower ())
  179.         if mfgcmp != 0:
  180.             return mfgcmp
  181.         mdlcmp = cmp (our_mdl.lower (), other_mdl.lower ())
  182.         if mdlcmp != 0:
  183.             return mdlcmp
  184.         if self.sn == '' or other.sn == '':
  185.             return 0
  186.         return cmp (self.sn, other.sn)
  187.  
  188. if __name__ == '__main__':
  189.     import authconn
  190.     c = authconn.Connection ()
  191.     devices = cupshelpers.getDevices (c)
  192.  
  193.     physicaldevices = []
  194.     for device in devices.values ():
  195.         physicaldevice = PhysicalDevice (device)
  196.         try:
  197.             i = physicaldevices.index (physicaldevice)
  198.             physicaldevices[i].add_device (device)
  199.         except ValueError:
  200.             physicaldevices.append (physicaldevice)
  201.  
  202.     physicaldevices.sort ()
  203.     for physicaldevice in physicaldevices:
  204.         print physicaldevice.get_info ()
  205.         devices = physicaldevice.get_devices ()
  206.         for device in devices:
  207.             print " ", device
  208.