Skip to content
Permalink
Browse files

Prevent duplicate options and capabilities being generated

  • Loading branch information
simoncadman committed Sep 8, 2013
1 parent a2b5836 commit c0449b4d864e0f2616ef6c6a05e547e9fe1bd95c
Showing with 36 additions and 11 deletions.
  1. +8 −2 dynamicppd.py
  2. +28 −9 printer.py
@@ -126,9 +126,12 @@ def showUsage():
foundprinter['fulldetails'] = ast.literal_eval(f.read())

if 'capabilities' in foundprinter['fulldetails']:
addedCapabilities = []

for capability in foundprinter['fulldetails']['capabilities']:
originCapabilityName = None
internalcapabilityName = printer.getInternalName(capability, 'capability')
internalcapabilityName = printer.getInternalName(capability, 'capability', addedCapabilities)
addedCapabilities.append(internalcapabilityName)

if 'displayName' in capability and len(capability['displayName']) > 0:
originCapabilityName = printer.sanitizeText(capability['displayName'])
@@ -144,6 +147,8 @@ def showUsage():
# translation of capability, allows use of 8 bit chars
ppddetails += '*' + language + '.Translation' + ' ' + internalcapabilityName + '/' + originCapabilityName + ": \"\"\n"

addedOptions = []

for option in capability['options']:
originOptionName = None
if 'displayName' in option and len(option['displayName']) > 0:
@@ -153,7 +158,8 @@ def showUsage():
else:
originOptionName = printer.sanitizeText(option['name'])
engOptionName = printer.sanitizeText(option['name'])
internalOptionName = printer.getInternalName(option, 'option', capability['name'])
internalOptionName = printer.getInternalName(option, 'option', capability['name'], addedOptions)
addedOptions.append(internalOptionName)
if 'default' in option and option['default'] == True:
ppddetails += '*Default' + internalcapabilityName + ': ' + internalOptionName + "\n"
ppddetails += '*' + internalcapabilityName + ' ' + internalOptionName + ':' + internalOptionName + "\n"
@@ -300,19 +300,24 @@ def getCapabilities ( self, gcpid, cupsprintername, overrideoptionsstring ) :
details = self.getPrinterDetails( gcpid )
fulldetails = details['printers'][0]
gcpoption = None
addedCapabilities = []
for capability in fulldetails['capabilities']:
if hashname == self.getInternalName(capability, 'capability'):
gcpname = capability['name']
for option in capability['options']:
if attr.value == self.getInternalName(option, 'option', gcpname):
internalCapability = self.getInternalName(option, 'option', gcpname, addedCapabilities)
addedCapabilities.append(internalCapability)
if attr.value == internalCapability:
gcpoption = option['name']
break

addedOptions = []
for overridecapability in overridecapabilities:
if 'Default' + overridecapability == attr.name:
selectedoption = overridecapabilities[overridecapability]
for option in capability['options']:
if selectedoption == self.getInternalName(option, 'option', gcpname):
internalOption = self.getInternalName(option, 'option', gcpname, addedOptions)
addedOptions.append(internalOption)
if selectedoption == internalOption:
gcpoption = option['name']
break
break
@@ -389,8 +394,8 @@ def submitJob(self, printerid, jobtype, jobfile, jobname, printername, options="
print('ERROR: Print job %s failed with %s' % ( jobtype, error_msg ))
return False

def getInternalName ( self, details, internalType, capabilityName = None ) :

def getInternalName ( self, details, internalType, capabilityName = None, existingList = [] ) :
returnValue = None
fixedNameMap = {}
reservedWords = [ 'Duplex', 'Resolution' ]

@@ -408,7 +413,8 @@ def getInternalName ( self, details, internalType, capabilityName = None ) :

for itemName in fixedNameMap:
if details['name'] == itemName:
return fixedNameMap[itemName]
returnValue = fixedNameMap[itemName]
break

if 'displayName' in details and len(details['displayName']) > 0:
name = details['displayName']
@@ -423,7 +429,20 @@ def getInternalName ( self, details, internalType, capabilityName = None ) :
sanitisedName = 'GCP_' + sanitisedName

# only sanitise, no hash
if len(sanitisedName) <= 30 and sanitisedName.decode("utf-8", 'ignore').encode("ascii","ignore") == sanitisedName:
return sanitisedName
if returnValue == None and len(sanitisedName) <= 30 and sanitisedName.decode("utf-8", 'ignore').encode("ascii","ignore") == sanitisedName:
returnValue = sanitisedName

if returnValue == None:
returnValue = hashlib.sha256(sanitisedName).hexdigest()[:7]

if returnValue not in existingList:
return returnValue

# max 100 rotations, prevent infinite loop
for i in range(1,100):
if returnValue in existingList:
returnValue += '_' + str(i)

# TODO: need to error if limit hit, or run out of chars allowed etc

return hashlib.sha256(sanitisedName).hexdigest()[:7]
return returnValue

0 comments on commit c0449b4

Please sign in to comment.
You can’t perform that action at this time.