b1d80f
#!/usr/bin/python3
b1d80f
import os
b1d80f
import re
b1d80f
import sys
b1d80f
if len (sys.argv) < 3:
b1d80f
    print ("Specify hpcups.drv and hpijs.drv pathnames")
b1d80f
    sys.exit (1)
b1d80f
b1d80f
hpcups_drv = sys.argv[1]
b1d80f
hpijs_drv = sys.argv[2]
b1d80f
b1d80f
# Match e.g.      Model "ModelName"
b1d80f
# and catch 'ModelName' in group 0
b1d80f
mn_re = re.compile ('^\s*ModelName\s+"(.*)"\s*$')
b1d80f
b1d80f
# Match e.g.      Attribute "1284DeviceID" "" "blah"
b1d80f
# and catch everything before 'blah' in group 0, 'blah' in group 1,
b1d80f
# trailing characters in group 2
b1d80f
devid_re = re.compile ('^(\s*Attribute\s+"1284DeviceID"\s+""\s+")(.*)("\s*)$')
b1d80f
b1d80f
# Match e.g.   }
b1d80f
end_re = re.compile ('^\s*}')
b1d80f
b1d80f
devid_by_mn = dict()
b1d80f
b1d80f
hpcups_lines = open (hpcups_drv, "r").readlines ()
b1d80f
current_mn = None
b1d80f
for line in hpcups_lines:
b1d80f
    if current_mn == None:
b1d80f
        match = mn_re.match (line)
b1d80f
        if match == None:
b1d80f
            continue
b1d80f
b1d80f
        current_mn = match.groups ()[0]
b1d80f
    else:
b1d80f
        match = devid_re.match (line)
b1d80f
        if match:
b1d80f
            devid_by_mn[current_mn] = match.groups ()[1]
b1d80f
            continue
b1d80f
b1d80f
    if end_re.match (line):
b1d80f
        current_mn = None
b1d80f
b1d80f
print("%d IEEE 1284 Device IDs loaded from %s" % (len (devid_by_mn),
b1d80f
                                                os.path.basename (hpcups_drv)),
b1d80f
      file=sys.stderr)
b1d80f
b1d80f
replaced = 0
b1d80f
hpijs_lines = open (hpijs_drv, "r").readlines ()
b1d80f
current_mn = None
b1d80f
for line in hpijs_lines:
b1d80f
    if current_mn == None:
b1d80f
        match = mn_re.match (line)
b1d80f
        if match:
b1d80f
            current_mn = match.groups ()[0]
b1d80f
            if current_mn.endswith (" hpijs"):
b1d80f
                current_mn = current_mn[:-6]
b1d80f
    else:
b1d80f
        match = devid_re.match (line)
b1d80f
        if match:
b1d80f
            devid = devid_by_mn.get (current_mn)
b1d80f
            if devid:
b1d80f
                line = (match.groups ()[0] + devid + match.groups ()[2])
b1d80f
                replaced += 1
b1d80f
            else:
b1d80f
                print ("Not matched: %s" % current_mn, file=sys.stderr)
b1d80f
b1d80f
    if end_re.match (line):
b1d80f
        current_mn = None
b1d80f
b1d80f
    print (line.rstrip ("\n"))
b1d80f
b1d80f
print("%d IEEE 1284 Device IDs loaded in %s" % (replaced,
b1d80f
                                                os.path.basename (hpijs_drv)),
b1d80f
      file=sys.stderr)