Permalink
Browse files

Allow overriding print params per print job

  • Loading branch information...
simoncadman committed Aug 22, 2013
1 parent 37c14bf commit 37488f9dc4263cb4dad9ef118c499f6ddee29de6
Showing with 31 additions and 11 deletions.
  1. +1 −1 backend.py
  2. +27 −7 printer.py
  3. +3 −3 submitjob.py
View
@@ -159,7 +159,7 @@ def getBackendDescription ( ) :
sys.stderr.write( "INFO: Sending document to Cloud Print\n")
logfile.write("Sending "+ pdfFile + " to cloud\n")
result = 0
- p = subprocess.Popen([submitjobpath, pdfFile, jobTitle, uri, printername], stdout=subprocess.PIPE)
+ p = subprocess.Popen([submitjobpath, pdfFile, jobTitle, uri, printername, printOptions], stdout=subprocess.PIPE)
output = p.communicate()[0]
result = p.returncode
sys.stderr.write(output)
View
@@ -266,45 +266,64 @@ def encodeMultiPart(self, fields, file_type='application/xml'):
lines.append('') # blank line
return self.CRLF.join(lines)
- def getCapabilities ( self, gcpid, cupsprintername ) :
+ def getCapabilities ( self, gcpid, cupsprintername, overrideoptionsstring ) :
"""Gets capabilities of printer and maps them against list
Args:
gcpid: printer id from google
cupsprintername: name of the printer in cups
+ overrideoptionsstring: override for print job
Returns:
List of capabilities
"""
+ overrideoptions = overrideoptionsstring.split(' ')
import cups
connection = cups.Connection()
cupsprinters = connection.getPrinters()
capabilities = { "capabilities" : [] }
-
+ overridecapabilities = {}
+ for optiontext in overrideoptions:
+ if '=' in optiontext :
+ optionparts = optiontext.split('=')
+ option = optionparts[0]
+ value = optionparts[1]
+ overridecapabilities[option] = value
+
attrs = cups.PPD(connection.getPPD(cupsprintername)).attributes
for attr in attrs:
if attr.name.startswith('DefaultGCP_'):
# gcp setting, reverse back to GCP capability
- gcpname = ""
+ gcpname = None
hashname = attr.name.replace('DefaultGCP_', '')
# find item name from hashes
details = self.getPrinterDetails( gcpid )
fulldetails = details['printers'][0]
- gcpoption = ""
+ gcpoption = None
for capability in fulldetails['capabilities']:
if hashname == hashlib.sha256(self.sanitizeText(capability['name'])).hexdigest()[:7]:
gcpname = capability['name']
for option in capability['options']:
if attr.value == hashlib.sha256(self.sanitizeText(option['name'])).hexdigest()[:7]:
gcpoption = option['name']
break
+
+ for overridecapability in overridecapabilities:
+ if 'Default' + overridecapability == attr.name:
+ selectedoption = overridecapabilities[overridecapability]
+ for option in capability['options']:
+ if selectedoption == hashlib.sha256(self.sanitizeText(option['name'])).hexdigest()[:7]:
+ gcpoption = option['name']
+ break
+ break
break
# hardcoded to feature type temporarily
- capabilities['capabilities'].append( { 'type' : 'Feature', 'name' : gcpname, 'options' : [ { 'name' : gcpoption } ] } )
+ if gcpname != None and gcpoption != None:
+ capabilities['capabilities'].append( { 'type' : 'Feature', 'name' : gcpname, 'options' : [ { 'name' : gcpoption } ] } )
return capabilities
- def submitJob(self, printerid, jobtype, jobfile, jobname, printername ):
+ def submitJob(self, printerid, jobtype, jobfile, jobname, printername, options ):
"""Submit a job to printerid with content of dataUrl.
Args:
@@ -313,6 +332,7 @@ def submitJob(self, printerid, jobtype, jobfile, jobname, printername ):
jobfile: string, points to source for job. Could be a pathname or id string.
jobname: string, name of the print job ( usually page name ).
printername: string, Google Cloud Print printer name.
+ options: string, key-value pair of options from print job.
Returns:
boolean: True = submitted, False = errors.
"""
@@ -350,7 +370,7 @@ def submitJob(self, printerid, jobtype, jobfile, jobname, printername ):
('title', title),
('content', content[jobtype]),
('contentType', content_type[jobtype]),
- ('capabilities', json.dumps( self.getCapabilities(printerid, printername) ) )
+ ('capabilities', json.dumps( self.getCapabilities(printerid, printername, options ) ) )
]
edata = ""
if jobtype in ['pdf', 'jpeg', 'png']:
View
@@ -19,8 +19,8 @@
from auth import Auth
from printer import Printer
-if ( len(sys.argv) < 5 ):
- sys.stderr.write("ERROR: Usage: " + sys.argv[0] + " pdf-file page-title printer-uri cups-printer-name\n")
+if ( len(sys.argv) < 6 ):
+ sys.stderr.write("ERROR: Usage: " + sys.argv[0] + " pdf-file page-title printer-uri cups-printer-name options\n")
sys.exit(1)
requestors, storage = Auth.SetupAuth(False)
@@ -32,7 +32,7 @@
print("ERROR: Printer '" + sys.argv[3] + "' not found")
sys.exit(1)
-if printer.submitJob(printerid, 'pdf', sys.argv[1], sys.argv[2], sys.argv[4] ):
+if printer.submitJob(printerid, 'pdf', sys.argv[1], sys.argv[2], sys.argv[4], sys.argv[5] ):
print("INFO: Successfully printed")
sys.exit(0)
else:

0 comments on commit 37488f9

Please sign in to comment.