This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
/
dynamicppd.py
executable file
·206 lines (188 loc) · 10.8 KB
/
dynamicppd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#! /usr/bin/env python2
# CUPS Cloudprint - Print via Google Cloud Print
# Copyright (C) 2013 Simon Cadman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os, hashlib, locale
libpath = "/usr/local/lib/cloudprint-cups/"
if not os.path.exists( libpath ):
libpath = "/usr/lib/cloudprint-cups"
sys.path.insert(0, libpath)
from auth import Auth
from printer import Printer
def showUsage():
sys.stderr.write("ERROR: Usage: " + sys.argv[0] + " [list|cat drivername]\n")
sys.exit(1)
requestors, storage = Auth.SetupAuth(False)
printer = Printer(requestors)
printers = printer.getPrinters(True)
if printers == None:
print("ERROR: No Printers Found")
sys.exit(1)
if ( len(sys.argv) < 2 ):
showUsage()
if sys.argv[1] == 'list':
for foundprinter in printers:
print('"cupscloudprint:' + foundprinter['account'].encode('ascii', 'replace').replace(' ', '-') +':' + foundprinter['name'].encode('ascii', 'replace').replace(' ', '-') + '.ppd" en "Google" "' + foundprinter['name'].encode('ascii', 'replace') + ' (' + foundprinter['account'] + ')" "MFG:GOOGLE;DRV:GCP;CMD:POSTSCRIPT;MDL:' + printer.printerNameToUri( foundprinter['account'], foundprinter['name'] ) +';"')
elif sys.argv[1] == 'cat':
if len(sys.argv) == 2 or sys.argv[2] == "":
showUsage()
else:
ppdname = sys.argv[2]
# find printer
for foundprinter in printers:
if ppdname == 'cupscloudprint:' + foundprinter['account'].encode('ascii', 'replace').replace(' ', '-') +':' + foundprinter['name'].encode('ascii', 'replace').replace(' ', '-') + '.ppd':
capabilities = []
# generate and output ppd
language = "en"
defaultpapertype = "Letter"
defaultlocal = locale.getdefaultlocale()[0]
if defaultlocal != None:
language = defaultlocal
# taken from wikipedia
lettercountries = [ 'US', 'CA', 'MX', 'BO', 'CO', 'VE', 'PH', 'CL' ]
if len(language.split('_')) > 1:
if language.split('_')[1] not in lettercountries:
defaultpapertype = "A4"
ppddetails = """*PPD-Adobe: "4.3"
*%%%% PPD file for Cloud Print with CUPS.
*FormatVersion: "4.3"
*FileVersion: "1.0"
*LanguageVersion: English
*LanguageEncoding: ISOLatin1
*cupsLanguages: \"""" + language +"""\"
*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: "(1) 0"
*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:""" + printer.printerNameToUri( foundprinter['account'], foundprinter['name'] ) + """;"
*OpenUI *PageSize/Media Size: PickOne
*""" + language + """.Translation PageSize/Media Size: ""
*OrderDependency: 10 AnySetup *PageSize
*DefaultPageSize: """ + defaultpapertype + """.Fullbleed
*PageSize Letter.Fullbleed/US Letter: "<</PageSize[612 792]/ImagingBBox null>>setpagedevice"
*""" + language + """.PageSize Letter.Fullbleed/US Letter: ""
*PageSize Legal.Fullbleed/US Legal: "<</PageSize[612 1008]/ImagingBBox null>>setpagedevice"
*""" + language + """.PageSize Legal.Fullbleed/US Legal: ""
*PageSize A4.Fullbleed/A4: "<</PageSize[595 842]/ImagingBBox null>>setpagedevice"
*""" + language + """.PageSize A4.Fullbleed/A4: ""
*CloseUI: *PageSize
*OpenUI *PageRegion/Media Size: PickOne
*OrderDependency: 10 AnySetup *PageRegion
*DefaultPageRegion: """ + defaultpapertype + """.Fullbleed
*PageRegion Letter.Fullbleed/US Letter: "<</PageSize[612 792]/ImagingBBox null>>setpagedevice"
*PageRegion Legal.Fullbleed/US Legal: "<</PageSize[612 1008]/ImagingBBox null>>setpagedevice"
*PageRegion A4.Fullbleed/A4: "<</PageSize[595 842]/ImagingBBox null>>setpagedevice"
*CloseUI: *PageRegion
*DefaultImageableArea: """ + defaultpapertype + """.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 + """.Fullbleed
*PaperDimension Letter.Fullbleed/US Letter: "612 792"
*PaperDimension Legal.Fullbleed/US Legal: "612 1008"
*PaperDimension A4.Fullbleed/A4: "595 842"
"""
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())
if 'capabilities' in foundprinter['fulldetails']:
for capability in foundprinter['fulldetails']['capabilities']:
originCapabilityName = None
internalcapabilityName = hashlib.sha256(printer.sanitizeText(capability['name'])).hexdigest()[:7]
if 'displayName' in capability and len(capability['displayName']) > 0:
originCapabilityName = printer.sanitizeText(capability['displayName'])
elif 'psk:DisplayName' in capability and len(capability['psk:DisplayName']) > 0:
originCapabilityName = printer.sanitizeText(capability['psk:DisplayName'])
else:
originCapabilityName = printer.sanitizeText(capability['name'])
engCapabilityName = printer.sanitizeText(capability['name'])
if capability['type'] == 'Feature':
ppddetails += '*OpenUI *GCP_' + internalcapabilityName + '/' + internalcapabilityName +': PickOne' + "\n"
# translation of capability, allows use of 8 bit chars
ppddetails += '*' + language + '.Translation' + ' GCP_' + internalcapabilityName + '/' + originCapabilityName + ": \"\"\n"
for option in capability['options']:
originOptionName = None
if 'displayName' in option and len(option['displayName']) > 0:
originOptionName = printer.sanitizeText(option['displayName'])
elif 'psk:DisplayName' in option and len(option['psk:DisplayName']) > 0:
originOptionName = printer.sanitizeText(option['psk:DisplayName'])
else:
originOptionName = printer.sanitizeText(option['name'])
engOptionName = printer.sanitizeText(option['name'])
internalOptionName = hashlib.sha256(printer.sanitizeText(option['name'])).hexdigest()[:7]
if 'default' in option and option['default'] == True:
ppddetails += '*DefaultGCP_' + internalcapabilityName + ': ' + internalOptionName + "\n"
ppddetails += '*GCP_' + internalcapabilityName + ' ' + internalOptionName + ':' + internalOptionName + "\n"
# translation of option, allows use of 8 bit chars
ppddetails += '*' + language + '.GCP_' + internalcapabilityName + ' ' + internalOptionName + "/" + originOptionName + ": \"\"\n"
ppddetails += '*CloseUI: *GCP_' + internalcapabilityName + "\n"
elif capability['type'] == 'ParameterDef':
pass
ppddetails += """*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."""
print ppddetails
sys.exit(0)
# no printers found
print("ERROR: PPD " + ppdname +" Not Found")
sys.exit(1)