Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
Prevent duplicate options and capabilities being generated
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncadman committed Sep 8, 2013
1 parent a2b5836 commit c0449b4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
10 changes: 8 additions & 2 deletions dynamicppd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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:
Expand All @@ -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"
Expand Down
37 changes: 28 additions & 9 deletions printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' ]

Expand All @@ -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']
Expand All @@ -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.