[Foomatic] Using usb IDs for autodetection

Tim Waugh twaugh at redhat.com
Thu Apr 8 04:07:49 PDT 2004


On Thu, Apr 08, 2004 at 10:50:30AM +0200, Johannes Meixner wrote:

> 3.
> Even if we had all USB and parallel port IDs we still need the SNMP IDs.
> I think a real solution must work for USB, parallel port and network.

Some large manufacturers provide IEEE 1284 IDs via SNMP.  Below is the
Python code I use to extract it (from the Fedora Core tool).

Tim.
*/

#-----------------------
# SNMP printer detection
#-----------------------
    def detect_snmp_printer (self, agent):
        """Detect an SNMP printer by name."""
        def snmpwalk (agent, oid):
            opts = '-v 1 -c public -Ov'
            c = '/usr/bin/snmpwalk %s "$AGENT" "$OID" 2>/dev/null' % opts
            os.environ['AGENT'] = agent
            os.environ['OID'] = oid
            signal.signal (signal.SIGCHLD, signal.SIG_DFL)
            f = os.popen (c)
            return f.readlines ()

        id = None
        ps_capable = 0

        ls = snmpwalk (agent, 'system.sysObjectID.0')
        oid = None
        lookfor = "OID: "
        for l in ls:
            if l.startswith (lookfor):
                oid = l[len (lookfor):].rstrip ('\n')
                break

        if oid:
            ls = snmpwalk (agent, oid)
            # Look for an IEEE 1284 ID
            lookfor = "STRING: "
            for l in ls:
                if not l.startswith (lookfor):
                    continue

                # IEEE 1284 ID fields are separated by semi-colons
                if not l.find (";") != -1:
                    continue

                l = l[len (lookfor):].rstrip ('\n').strip ('"')
                deviceid = self.parent.conf.parse_ieee1284_deviceid (l)
                try:
                    tuple = (deviceid["manufacturer"].lower().strip (' '),
                             deviceid["model"].lower ().strip (' '))
                    id = self.parent.conf.foomatic.autodetect_dict[tuple].id
                    break
                except:
                    print "No IEEE 1284 match:", deviceid

                try:
                    cmdset = deviceid["cmdset"]
                    for each in cmdset.split (","):
                        if each.lower ().strip () == "postscript":
                            ps_capable = 1
                except:
                    pass

        if not id:
            ls = snmpwalk (agent, 'HOST-RESOURCES-MIB::hrDeviceDescr')
            lookfor = "STRING: "
            for l in ls:
                if not l.startswith (lookfor):
                    continue

                descr = l[len (lookfor):].rstrip ('\n')
                try:
                    id = self.parent.conf.foomatic.snmp_dict[descr].id
                    break
                except:
                    print "No SNMP match: '%s'" % descr

        if not id and ps_capable:
            try:
                make_model = self.parent.conf.foomatic.make_model_dict_dict
                id = make_model["Generic"]["PostScript Printer"].id
            except:
                pass

        return id
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.linux-foundation.org/pipermail/printing-foomatic/attachments/20040408/78350214/attachment.pgp


More information about the Printing-foomatic mailing list