def get_printers(): '''Return a set of HardwareIDs for connected printers.''' if get_printers.cache is not None: return get_printers.cache get_printers.cache = set() try: import cups import cupshelpers except ImportError: logging.warning('cups and/or cupshelpers Python modules are not present; printer detection is not available') return get_printers.cache try: for dev in cupshelpers.getDevices(cups.Connection()).itervalues(): # openprinting.org database only uses MFG, MDL, DES, and CMD, so don't # send the rest (it might contain personal data such as serial numbers) if dev.id_dict.get('MFG') and dev.id_dict.get('MDL'): id = 'MFG:%s;MDL:%s;' % (dev.id_dict['MFG'], dev.id_dict['MDL']) if dev.id_dict.get('DES'): id += 'DES:' + dev.id_dict['DES'] + ';' if dev.id_dict.get('CMD'): id += 'CMD:' + ','.join(dev.id_dict['CMD']) + ';' _get_printers.cache.add(HardwareID('printer_deviceid', id)) except (RuntimeError, cups.IPPError): logging.warning('cannot connect to cups; printer detection is not available') return set() return get_printers.cache
def get_printers(): '''Return a set of HardwareIDs for connected printers.''' if get_printers.cache is not None: return get_printers.cache get_printers.cache = set() try: import cups import cupshelpers except ImportError: logging.warning( 'cups and/or cupshelpers Python modules are not present; printer detection is not available' ) return get_printers.cache try: for dev in cupshelpers.getDevices(cups.Connection()).itervalues(): # openprinting.org database only uses MFG, MDL, DES, and CMD, so don't # send the rest (it might contain personal data such as serial numbers) if dev.id_dict.get('MFG') and dev.id_dict.get('MDL'): id = 'MFG:%s;MDL:%s;' % (dev.id_dict['MFG'], dev.id_dict['MDL']) if dev.id_dict.get('DES'): id += 'DES:' + dev.id_dict['DES'] + ';' if dev.id_dict.get('CMD'): id += 'CMD:' + ','.join(dev.id_dict['CMD']) + ';' _get_printers.cache.add(HardwareID('printer_deviceid', id)) except (RuntimeError, cups.IPPError): logging.warning( 'cannot connect to cups; printer detection is not available') return set() return get_printers.cache
mfgcmp = cmp(our_mfg.lower(), other_mfg.lower()) if mfgcmp != 0: return mfgcmp mdlcmp = cmp(our_mdl.lower(), other_mdl.lower()) if mdlcmp != 0: return mdlcmp if self.sn == '' or other.sn == '': return 0 return cmp(self.sn, other.sn) if __name__ == '__main__': import authconn c = authconn.Connection() devices = cupshelpers.getDevices(c) physicaldevices = [] for device in devices.values(): physicaldevice = PhysicalDevice(device) try: i = physicaldevices.index(physicaldevice) physicaldevices[i].add_device(device) except ValueError: physicaldevices.append(physicaldevice) physicaldevices.sort() for physicaldevice in physicaldevices: print physicaldevice.get_info() devices = physicaldevice.get_devices() for device in devices:
if our_mfg != other_mfg: return our_mfg < other_mfg if our_mdl != other_mdl: return our_mdl < other_mdl if self.sn == '' or other.sn == '': return False return self.sn < other.sn if __name__ == '__main__': import authconn c = authconn.Connection () devices = cupshelpers.getDevices (c) physicaldevices = [] for device in devices.values (): physicaldevice = PhysicalDevice (device) try: i = physicaldevices.index (physicaldevice) physicaldevices[i].add_device (device) except ValueError: physicaldevices.append (physicaldevice) physicaldevices.sort () for physicaldevice in physicaldevices: print(physicaldevice.get_info ()) devices = physicaldevice.get_devices () for device in devices: