Skip to content
Permalink
Browse files

Use Printer class

  • Loading branch information
Jacob Marble
Jacob Marble committed Apr 25, 2014
1 parent 06ade05 commit fef6ecf873f97a7eb5d52dab1e58e6b9c80c6241
Showing with 50 additions and 532 deletions.
  1. +1 −0 Makefile.in
  2. +10 −12 backend.py
  3. +5 −188 dynamicppd.py
  4. +2 −2 listcloudprinters.py
  5. +21 −304 printermanager.py
  6. +6 −18 reportissues.py
  7. +5 −8 setupcloudprint.py
@@ -18,6 +18,7 @@ install: all
${INSTALL} -m 644 ${srcdir}/ccputils.py ${prefix}/share/cloudprint-cups/
${INSTALL} -m 644 ${srcdir}/cloudprintrequestor.py ${prefix}/share/cloudprint-cups/
${INSTALL} -m 644 ${srcdir}/printermanager.py ${prefix}/share/cloudprint-cups/
${INSTALL} -m 644 ${srcdir}/printer.py ${prefix}/share/cloudprint-cups/
${INSTALL} -m 644 ${srcdir}/auth.py ${prefix}/share/cloudprint-cups/
${INSTALL} -m 644 ${srcdir}/.coveragerc ${prefix}/share/cloudprint-cups/
unlink ${prefix}/share/cloudprint-cups/pre-commit.py
@@ -61,12 +61,12 @@
printers = printer_manager.getPrinters()

if len(sys.argv) == 1:
print printer_manager.getBackendDescription()
print 'network cloudprint "Unknown" "Google Cloud Print"'

if printers is not None:
try:
for foundprinter in printers:
print printer_manager.getBackendDescriptionForPrinter(foundprinter)
for printer in printers:
print printer.getBackendDescription()
except Exception as error:
sys.stderr.write("ERROR: " + error)
logging.error(error)
@@ -98,17 +98,16 @@
copies = 1

uri = os.getenv('DEVICE_URI')
printername = os.getenv('PRINTER')
cupsprintername = os.getenv('PRINTER')
if uri is None:
message =\
'URI must be "cloudprint://<account name>/<cloud printer id>"!\n'
message = 'URI must be "cloudprint://<account name>/<cloud printer id>"!\n'
sys.stdout.write(message)
sys.exit(255)

logging.info("Printing file " + printFile)
optionsstring = ' '.join(["'%s'" % option for option in sys.argv])
logging.info("Device is %s , printername is %s, params are: %s" %
(uri, printername, optionsstring))
(uri, cupsprintername, optionsstring))

pdfFile = printFile + ".pdf"
if Utils.which("ps2pdf") is None:
@@ -119,6 +118,7 @@

result = 0

logging.info('is this a pdf? ' + printFile)
if not Utils.fileIsPDF(printFile):
sys.stderr.write("INFO: Converting print job to PDF\n")
if subprocess.call(convertToPDFParams) != 0:
@@ -135,13 +135,11 @@
sys.stderr.write("INFO: Sending document to Cloud Print\n")
logging.info("Sending %s to cloud" % pdfFile)

printerid, requestor = printer_manager.getPrinterIDByURI(uri)
printer_manager.requestor = requestor
if printerid is None:
printer = printer_manager.getPrinterByURI(uri)
if printer is None:
print "ERROR: PrinterManager '%s' not found" % uri
result = 1
elif printer_manager.submitJob(
printerid, 'pdf', pdfFile, jobTitle, printername, printOptions):
elif printer.submitJob('pdf', pdfFile, jobTitle, cupsprintername, printOptions):
print "INFO: Successfully printed"
result = 0
else:
@@ -18,196 +18,17 @@
import os
import sys

_list_format = '"cupscloudprint:%s:%s-%s.ppd" en "Google" "%s (%s)" "MFG:GOOGLE;DRV:GCP;CMD:POSTSCRIPT;MDL:%s;"'

# Countries where letter sized paper is used, according to:
# http://en.wikipedia.org/wiki/Letter_(paper_size)
_letter_countries = set(('US', 'CA', 'MX', 'BO', 'CO', 'VE', 'PH', 'CL'))

_ppd_template_head = """*PPD-Adobe: "4.3"
*%%%%%%%% PPD file for Cloud Print with CUPS.
*FormatVersion: "4.3"
*FileVersion: "1.0"
*LanguageVersion: English
*LanguageEncoding: ISOLatin1
*cupsLanguages: \"%(language)s\"
*cupsFilter: "application/vnd.cups-postscript 100 -"
*cupsFilter: "application/vnd.cups-pdf 0 -"
*PCFileName: "ccp.ppd"
*Product: "(Google Cloud Print)"
*Manufacturer: "Google"
*ModelName: "Google Cloud Print"
*ShortNickName: "Google Cloud Print"
*NickName: "Google Cloud Print, 1.0"
*PSVersion: "(3010.000) 550"
*LanguageLevel: "3"
*ColorDevice: True
*DefaultColorSpace: RGB
*FileSystem: False
*Throughput: "1"
*LandscapeOrientation: Minus90
*TTRasterizer: Type42
*%% Driver-defined attributes...
*1284DeviceID: "MFG:GOOGLE;DRV:GCP;CMD:POSTSCRIPT;MDL:%(uri)s;"
*OpenUI *PageSize/Media Size: PickOne
*%(language)s.Translation PageSize/Media Size: ""
*OrderDependency: 10 AnySetup *PageSize
*DefaultPageSize: %(defaultpapertype)s.Fullbleed
*PageSize Letter.Fullbleed/US Letter: "<</PageSize[612 792]/ImagingBBox null>>setpagedevice"
*%(language)s.PageSize Letter.Fullbleed/US Letter: ""
*PageSize Legal.Fullbleed/US Legal: "<</PageSize[612 1008]/ImagingBBox null>>setpagedevice"
*%(language)s.PageSize Legal.Fullbleed/US Legal: ""
*PageSize A4.Fullbleed/A4: "<</PageSize[595 842]/ImagingBBox null>>setpagedevice"
*%(language)s.PageSize A4.Fullbleed/A4: ""
*CloseUI: *PageSize
*OpenUI *PageRegion/Page Region: PickOne
*%(language)s.Translation PageRegion/Page Region: ""
*OrderDependency: 10 AnySetup *PageRegion
*DefaultPageRegion: %(defaultpapertype)s.Fullbleed
*PageRegion Letter.Fullbleed/US Letter: "<</PageSize[612 792]/ImagingBBox null>>setpagedevice"
*%(language)s.PageRegion Letter.Fullbleed/US Letter: ""
*PageRegion Legal.Fullbleed/US Legal: "<</PageSize[612 1008]/ImagingBBox null>>setpagedevice"
*%(language)s.PageRegion Legal.Fullbleed/US Legal: ""
*PageRegion A4.Fullbleed/A4: "<</PageSize[595 842]/ImagingBBox null>>setpagedevice"
*%(language)s.PageRegion A4.Fullbleed/A4: ""
*CloseUI: *PageRegion
*DefaultImageableArea: %(defaultpapertype)s.Fullbleed
*ImageableArea Letter.Fullbleed/US Letter: "0 0 612 792"
*ImageableArea Legal.Fullbleed/US Legal: "0 0 612 1008"
*ImageableArea A4.Fullbleed/A4: "0 0 595 842"
*DefaultPaperDimension: %(defaultpapertype)s.Fullbleed
*PaperDimension Letter.Fullbleed/US Letter: "612 792"
*PaperDimension Legal.Fullbleed/US Legal: "612 1008"
*PaperDimension A4.Fullbleed/A4: "595 842"
"""

_ppd_template_foot = """*DefaultFont: Courier
*Font AvantGarde-Book: Standard "(1.05)" Standard ROM
*Font AvantGarde-BookOblique: Standard "(1.05)" Standard ROM
*Font AvantGarde-Demi: Standard "(1.05)" Standard ROM
*Font AvantGarde-DemiOblique: Standard "(1.05)" Standard ROM
*Font Bookman-Demi: Standard "(1.05)" Standard ROM
*Font Bookman-DemiItalic: Standard "(1.05)" Standard ROM
*Font Bookman-Light: Standard "(1.05)" Standard ROM
*Font Bookman-LightItalic: Standard "(1.05)" Standard ROM
*Font Courier: Standard "(1.05)" Standard ROM
*Font Courier-Bold: Standard "(1.05)" Standard ROM
*Font Courier-BoldOblique: Standard "(1.05)" Standard ROM
*Font Courier-Oblique: Standard "(1.05)" Standard ROM
*Font Helvetica: Standard "(1.05)" Standard ROM
*Font Helvetica-Bold: Standard "(1.05)" Standard ROM
*Font Helvetica-BoldOblique: Standard "(1.05)" Standard ROM
*Font Helvetica-Narrow: Standard "(1.05)" Standard ROM
*Font Helvetica-Narrow-Bold: Standard "(1.05)" Standard ROM
*Font Helvetica-Narrow-BoldOblique: Standard "(1.05)" Standard ROM
*Font Helvetica-Narrow-Oblique: Standard "(1.05)" Standard ROM
*Font Helvetica-Oblique: Standard "(1.05)" Standard ROM
*Font NewCenturySchlbk-Bold: Standard "(1.05)" Standard ROM
*Font NewCenturySchlbk-BoldItalic: Standard "(1.05)" Standard ROM
*Font NewCenturySchlbk-Italic: Standard "(1.05)" Standard ROM
*Font NewCenturySchlbk-Roman: Standard "(1.05)" Standard ROM
*Font Palatino-Bold: Standard "(1.05)" Standard ROM
*Font Palatino-BoldItalic: Standard "(1.05)" Standard ROM
*Font Palatino-Italic: Standard "(1.05)" Standard ROM
*Font Palatino-Roman: Standard "(1.05)" Standard ROM
*Font Symbol: Special "(001.005)" Special ROM
*Font Times-Bold: Standard "(1.05)" Standard ROM
*Font Times-BoldItalic: Standard "(1.05)" Standard ROM
*Font Times-Italic: Standard "(1.05)" Standard ROM
*Font Times-Roman: Standard "(1.05)" Standard ROM
*Font ZapfChancery-MediumItalic: Standard "(1.05)" Standard ROM
*Font ZapfDingbats: Special "(001.005)" Special ROM
*% End of cloudprint.ppd, 04169 bytes."""


def doList(sys, printer_manager):
"""Lists Google Cloud Print printers."""
printers = printer_manager.getPrinters()
if printers is None:
sys.stderr.write("ERROR: No Printers Found\n")
sys.exit(1)
for foundprinter in printers:
account_no_spaces = foundprinter['account'].encode('ascii', 'replace').replace(' ', '-')
name_no_spaces = foundprinter['name'].encode('ascii', 'replace').replace(' ', '-')
id = foundprinter['id'].encode('ascii', 'replace').replace(' ', '-')
name = foundprinter['name'].encode('ascii', 'replace')
account = foundprinter['account']
uri = printer_manager.printerNameToUri(foundprinter['account'], foundprinter['id'])
print _list_format % (account_no_spaces, name_no_spaces, id, name, account, uri)
for printer in printers:
print printer.getCUPSListDescription()
sys.exit(0)


def generatePPD(accountName, foundprinter):
"""Generates a PPD string for foundprinter."""
language = "en"
defaultpapertype = "Letter"
defaultlocale = locale.getdefaultlocale()[0]
if defaultlocale is not None:
language = defaultlocale
if len(language.split('_')) > 1 and language.split('_')[1] not in _letter_countries:
defaultpapertype = "A4"
if '_' in language and language.split("_")[0] != "en":
language = language.split("_")[0]
uri = printer_manager.printerNameToUri(foundprinter['account'], foundprinter['id'])
ppd = _ppd_template_head % \
{'language': language, 'defaultpapertype': defaultpapertype, 'uri': uri}
if len(sys.argv) > 3 and sys.argv[3] == "testmode" and os.path.exists('test-capabilities.serial'):
with file("test-capabilities.serial") as f:
import ast
foundprinter['fulldetails'] = ast.literal_eval(f.read())
else:
printer_manager.requestor = printer_manager.findRequestorForAccount(accountName)
details = printer_manager.getPrinterDetails(foundprinter['id'])
foundprinter['fulldetails'] = details['printers'][0]
if 'capabilities' in foundprinter['fulldetails']:
addedCapabilities = []
for capability in foundprinter['fulldetails']['capabilities']:
originCapabilityName = None
internalCapabilityName = \
printer_manager.getInternalName(capability, 'capability', None, addedCapabilities)
addedCapabilities.append(internalCapabilityName)
if 'displayName' in capability and len(capability['displayName']) > 0:
originCapabilityName = printer_manager.sanitizeText(capability['displayName'])
elif 'psk:DisplayName' in capability and len(capability['psk:DisplayName']) > 0:
originCapabilityName = printer_manager.sanitizeText(capability['psk:DisplayName'])
else:
originCapabilityName = printer_manager.sanitizeText(capability['name'])
if capability['type'] == 'Feature':
ppd += '*OpenUI *%s/%s: PickOne\n' % \
(internalCapabilityName, internalCapabilityName)
# translation of capability, allows use of 8
# bit chars
ppd += '*%s.Translation %s/%s: ""\n' % \
(language, internalCapabilityName, originCapabilityName)
addedOptions = []
for option in capability['options']:
originOptionName = None
if 'displayName' in option and len(option['displayName']) > 0:
originOptionName = printer_manager.sanitizeText(option['displayName'])
elif 'psk:DisplayName' in option and len(option['psk:DisplayName']) > 0:
originOptionName = printer_manager.sanitizeText(option['psk:DisplayName'])
else:
originOptionName = printer_manager.sanitizeText(option['name'])
internalOptionName = printer_manager.getInternalName(
option, 'option', capability['name'], addedOptions)
addedOptions.append(internalOptionName)
if 'default' in option and option['default']:
ppd += '*Default%s: %s\n' % (internalCapabilityName, internalOptionName)
ppd += '*%s %s:%s\n' % \
(internalCapabilityName, internalOptionName, internalOptionName)
# translation of option, allows use of 8
# bit chars
ppd += '*%s.%s %s/%s: ""\n' % \
(language, internalCapabilityName, internalOptionName, originOptionName)

ppd += '*CloseUI: *%s\n' % internalCapabilityName
elif capability['type'] == 'ParameterDef':
pass

ppd += _ppd_template_foot
return ppd


def doCat():
"""Prints a PPD to stdout, per argv arguments."""
ppdname = sys.argv[2]
@@ -228,13 +49,9 @@ def doCat():
sys.exit(1)

# find printer
for foundprinter in printers:
foundppdname = 'cupscloudprint:%s:%s-%s.ppd' % (
foundprinter['account'].encode('ascii', 'replace').replace(' ', '-'),
foundprinter['name'].encode('ascii', 'replace').replace(' ', '-'),
foundprinter['id'].encode('ascii', 'replace').replace(' ', '-'))
if ppdname == foundppdname:
print generatePPD(accountName, foundprinter)
for printer in printers:
if ppdname == printer.getPPDName():
print printer.generatePPD()
sys.exit(0)

# no printers found
@@ -34,5 +34,5 @@
print "No Printers Found"
sys.exit(1)

for foundprinter in printers:
print printer_manager.getListDescription(foundprinter)
for printer in printers:
print printer.getListDescription()

0 comments on commit fef6ecf

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