forked from simoncadman/CUPS-Cloud-Print
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cupshelper.py
115 lines (96 loc) · 4.03 KB
/
cupshelper.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
# CUPS Cloudprint - Print via Google Cloud Print
# Copyright (C) 2011 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/>.
from ccputils import Utils
import cups
class CUPSHelper(object):
def __init__(self, connection=None):
if connection is None:
self._connection = cups.Connection()
else:
self._connection = connection
def _getCUPSQueueName(self, uri):
for cups_queue_name, printer in self.getPrinters().items():
if printer['device-uri'] == uri:
return cups_queue_name
return None
def _getCUPSQueueNameAndPrinter(self, uri):
for cups_queue_name, printer in self.getPrinters().items():
if printer['device-uri'] == uri:
return cups_queue_name, printer
return None, None
def getPPDs(self):
return self._connection.getPPDs()
def getPPDAttributes(self, name):
return cups.PPD(self._connection.getPPD(name)).attributes
def getPrinter(self, uri):
for cups_queue_name, printer in self.getPrinters().items():
if printer['device-uri'] == uri:
return printer
return None
def getPrinters(self):
printers = self._connection.getPrinters()
return dict((name, printer) for (name, printer) in printers.iteritems()
if printer['device-uri'].startswith(Utils.PROTOCOL))
def deletePrinter(self, uri):
return self._connection.deletePrinter(uri)
def renamePrinter(self, uri, name, location):
cups_queue_name, printer = self._getCUPSQueueNameAndPrinter(uri)
if cups_queue_name is not None:
if printer['printer-info'] != name:
self._connection.setPrinterInfo(cups_queue_name, name)
if printer['printer-location'] != location:
self._connection.setPrinterLocation(cups_queue_name, location)
def addPrinter(self, ccp_printer, name, location=None, ppd=None):
error = None
try:
if ppd:
ppd_name = ppd
else:
ppd_name = ccp_printer.getPPDName()
if location is None:
location = 'Google Cloud Print'
if location.strip() == '':
location = ccp_printer.getLocation()
error = self._connection.addPrinter(
name=name,
ppdname=ppd_name,
info=name,
location=location,
device=ccp_printer.getURI())
self._connection.enablePrinter(name)
self._connection.acceptJobs(name)
self._connection.setPrinterShared(name, False)
except Exception as e:
error = e
if error is not None:
print 'Error adding: %s' % name
print error
return False
return True
def getServerSetting(self, name):
settings = self._connection.adminGetServerSettings()
if name in settings:
return settings[name]
return None
@staticmethod
def generateCUPSQueueName(ccp_printer):
"""Generates a queue name that complies to validate_name() in lpadmin.c"""
name = ''
for letter in '%s-%s' % (ccp_printer.getDisplayName(), ccp_printer.getAccount()):
if letter in ('@', '/', '#') or ord(letter) <= ord(' ') or ord(letter) >= 127:
continue
name += letter
return name[:127]