| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Copyright (C) 2007, 2008, 2009, 2010, 2012, 2013 Red Hat, Inc. |
| 4 |
## Copyright (C) 2008 Novell, Inc. |
| 5 |
## Authors: Tim Waugh <[email protected]>, Vincent Untz |
| 6 |
|
| 7 |
## This program is free software; you can redistribute it and/or modify |
| 8 |
## it under the terms of the GNU General Public License as published by |
| 9 |
## the Free Software Foundation; either version 2 of the License, or |
| 10 |
## (at your option) any later version. |
| 11 |
|
| 12 |
## This program is distributed in the hope that it will be useful, |
| 13 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
## GNU General Public License for more details. |
| 16 |
|
| 17 |
## You should have received a copy of the GNU General Public License |
| 18 |
## along with this program; if not, write to the Free Software |
| 19 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
import cups |
| 22 |
import os |
| 23 |
from debug import * |
| 24 |
import debug |
| 25 |
|
| 26 |
###### |
| 27 |
###### A class to keep track of what we're trying to achieve in order |
| 28 |
###### to display that to the user if authentication is required. |
| 29 |
###### |
| 30 |
class SemanticOperations(object): |
| 31 |
def __init__ (self): |
| 32 |
self._operation_stack = [] |
| 33 |
|
| 34 |
def _begin_operation (self, operation): |
| 35 |
self._operation_stack.append (operation) |
| 36 |
|
| 37 |
def _end_operation (self): |
| 38 |
self._operation_stack.pop () |
| 39 |
|
| 40 |
def current_operation (self): |
| 41 |
try: |
| 42 |
return self._operation_stack[0] |
| 43 |
except IndexError: |
| 44 |
return None |
| 45 |
|
| 46 |
###### |
| 47 |
###### Destructible method call. Required so that all references can |
| 48 |
###### be dropped when an asynchronous method call is destroyed. |
| 49 |
###### |
| 50 |
class _AsyncMethodCall: |
| 51 |
def __init__ (self, fn, reply_handler, error_handler, auth_handler): |
| 52 |
self._fn = fn |
| 53 |
self._reply_handler = reply_handler |
| 54 |
self._error_handler = error_handler |
| 55 |
self._auth_handler = auth_handler |
| 56 |
self._destroyed = False |
| 57 |
debugprint ("+%s" % self) |
| 58 |
|
| 59 |
def __del__ (self): |
| 60 |
debugprint ("-%s" % self) |
| 61 |
|
| 62 |
def destroy (self): |
| 63 |
if self._destroyed: |
| 64 |
return |
| 65 |
|
| 66 |
debugprint ("DESTROY: %s" % self) |
| 67 |
self._destroyed = True |
| 68 |
self._reply_handler = None |
| 69 |
self._error_handler = None |
| 70 |
self._auth_handler = None |
| 71 |
self._reply_data = None |
| 72 |
self._error_data = None |
| 73 |
self._auth_data = None |
| 74 |
|
| 75 |
def run (self, *args, **kwds): |
| 76 |
self._reply_data = kwds.get ('reply_handler') |
| 77 |
self._error_data = kwds.get ('error_handler') |
| 78 |
self._auth_data = kwds.get ('auth_handler') |
| 79 |
kwds['reply_handler'] = self.reply_handler |
| 80 |
kwds['error_handler'] = self.error_handler |
| 81 |
kwds['auth_handler'] = self.auth_handler |
| 82 |
debugprint ("%s: calling %s" % (self, self._fn)) |
| 83 |
self._fn (*args, **kwds) |
| 84 |
|
| 85 |
def reply_handler (self, *args): |
| 86 |
if not self._destroyed: |
| 87 |
debugprint ("%s: to reply_handler at %s" % (self, |
| 88 |
self._reply_handler)) |
| 89 |
self._reply_handler (self, self._reply_data, *args) |
| 90 |
|
| 91 |
def error_handler (self, *args): |
| 92 |
if not self._destroyed: |
| 93 |
debugprint ("%s: to error_handler at %s" % (self, |
| 94 |
self._error_handler)) |
| 95 |
self._error_handler (self, self._error_data, *args) |
| 96 |
|
| 97 |
def auth_handler (self, *args): |
| 98 |
if not self._destroyed: |
| 99 |
debugprint ("%s: to auth_handler at %s" % (self, |
| 100 |
self._auth_handler)) |
| 101 |
self._auth_handler (self, self.auth_data, *args) |
| 102 |
|
| 103 |
###### |
| 104 |
###### An asynchronous libcups API using IPP or PolicyKit as |
| 105 |
###### appropriate. |
| 106 |
###### |
| 107 |
|
| 108 |
class Connection(SemanticOperations): |
| 109 |
def __init__ (self, reply_handler=None, error_handler=None, |
| 110 |
auth_handler=None, host=None, port=None, encryption=None, |
| 111 |
parent=None, try_as_root=True, prompt_allowed=True): |
| 112 |
super (Connection, self).__init__ () |
| 113 |
|
| 114 |
self._destroyed = False |
| 115 |
|
| 116 |
# Decide whether to use direct IPP or PolicyKit. |
| 117 |
if host is None: |
| 118 |
host = cups.getServer() |
| 119 |
use_pk = ((host.startswith ('/') or host == 'localhost') and |
| 120 |
os.getuid () != 0) |
| 121 |
|
| 122 |
def subst_reply_handler (conn, reply): |
| 123 |
self._subst_reply_handler (None, reply_handler, reply) |
| 124 |
|
| 125 |
def subst_error_handler (conn, exc): |
| 126 |
self._subst_error_handler (None, error_handler, exc) |
| 127 |
|
| 128 |
def subst_auth_handler (prompt, conn, method, resource): |
| 129 |
self._subst_auth_handler (None, auth_handler, prompt, method, resource) |
| 130 |
|
| 131 |
if use_pk and try_as_root: |
| 132 |
debugprint ("Using polkit-1 connection class") |
| 133 |
import asyncpk1 |
| 134 |
c = asyncpk1.PK1Connection (reply_handler=subst_reply_handler, |
| 135 |
error_handler=subst_error_handler, |
| 136 |
host=host, port=port, |
| 137 |
encryption=encryption, |
| 138 |
parent=parent) |
| 139 |
self._conn = c |
| 140 |
else: |
| 141 |
debugprint ("Using IPP connection class") |
| 142 |
import asyncipp |
| 143 |
c = asyncipp.IPPAuthConnection (reply_handler=subst_reply_handler, |
| 144 |
error_handler=subst_error_handler, |
| 145 |
auth_handler=subst_auth_handler, |
| 146 |
host=host, port=port, |
| 147 |
encryption=encryption, |
| 148 |
parent=parent, |
| 149 |
try_as_root=try_as_root, |
| 150 |
prompt_allowed=prompt_allowed, |
| 151 |
semantic=self) |
| 152 |
self._conn = c |
| 153 |
|
| 154 |
methodtype = type (self._conn.getPrinters) |
| 155 |
instancemethodtype = type (self._conn.getDevices) |
| 156 |
bindings = [] |
| 157 |
for fname in dir (self._conn): |
| 158 |
if fname.startswith ('_'): |
| 159 |
continue |
| 160 |
fn = getattr (self._conn, fname) |
| 161 |
if type (fn) != methodtype and type (fn) != instancemethodtype: |
| 162 |
continue |
| 163 |
if not hasattr (self, fname): |
| 164 |
setattr (self, fname, self._make_binding (fn)) |
| 165 |
bindings.append (fname) |
| 166 |
|
| 167 |
self._bindings = bindings |
| 168 |
self._methodcalls = [] |
| 169 |
debugprint ("+%s" % self) |
| 170 |
|
| 171 |
def __del__ (self): |
| 172 |
debug.debugprint ("-%s" % self) |
| 173 |
|
| 174 |
def destroy (self): |
| 175 |
debugprint ("DESTROY: %s" % self) |
| 176 |
self._destroyed = True |
| 177 |
try: |
| 178 |
self._conn.destroy () |
| 179 |
except AttributeError: |
| 180 |
pass |
| 181 |
|
| 182 |
for methodcall in self._methodcalls: |
| 183 |
methodcall.destroy () |
| 184 |
|
| 185 |
for binding in self._bindings: |
| 186 |
delattr (self, binding) |
| 187 |
|
| 188 |
def _make_binding (self, fn): |
| 189 |
return lambda *args, **kwds: self._call_function (fn, *args, **kwds) |
| 190 |
|
| 191 |
def _call_function (self, fn, *args, **kwds): |
| 192 |
methodcall = _AsyncMethodCall (fn, |
| 193 |
self._subst_reply_handler, |
| 194 |
self._subst_error_handler, |
| 195 |
self._subst_auth_handler) |
| 196 |
self._methodcalls.append (methodcall) |
| 197 |
methodcall.run (*args, **kwds) |
| 198 |
|
| 199 |
def _subst_reply_handler (self, methodcall, reply_handler, *args): |
| 200 |
if methodcall: |
| 201 |
methodcall.destroy () |
| 202 |
i = self._methodcalls.index (methodcall) |
| 203 |
del self._methodcalls[i] |
| 204 |
args = args[1:] |
| 205 |
if reply_handler and not self._destroyed: |
| 206 |
debugprint ("%s: chaining up to %s" % (self, reply_handler)) |
| 207 |
reply_handler (self, *args) |
| 208 |
|
| 209 |
def _subst_error_handler (self, methodcall, error_handler, *args): |
| 210 |
if methodcall: |
| 211 |
methodcall.destroy () |
| 212 |
i = self._methodcalls.index (methodcall) |
| 213 |
del self._methodcalls[i] |
| 214 |
args = args[1:] |
| 215 |
if error_handler and not self._destroyed: |
| 216 |
debugprint ("%s: chaining up to %s" % (self, error_handler)) |
| 217 |
error_handler (self, *args) |
| 218 |
|
| 219 |
def _subst_auth_handler (self, methodcall, auth_handler, prompt, method, resource): |
| 220 |
if methodcall: |
| 221 |
methodcall.destroy () |
| 222 |
i = self._methodcalls.index (methodcall) |
| 223 |
del self._methodcalls[i] |
| 224 |
if auth_handler and not self._destroyed: |
| 225 |
debugprint ("%s: chaining up to %s" % (self, auth_handler)) |
| 226 |
auth_handler (prompt, self, method, resource) |
| 227 |
|
| 228 |
def set_auth_info (self, password): |
| 229 |
"""Call this from your auth_handler function.""" |
| 230 |
self.thread.set_auth_info (password) |
| 231 |
|
| 232 |
if __name__ == "__main__": |
| 233 |
# Demo |
| 234 |
from gi.repository import GObject |
| 235 |
set_debugging (True) |
| 236 |
|
| 237 |
class Test: |
| 238 |
def __init__ (self, quit): |
| 239 |
self._conn = Connection () |
| 240 |
self._quit = quit |
| 241 |
debugprint ("+%s" % self) |
| 242 |
|
| 243 |
def __del__ (self): |
| 244 |
debug.debugprint ("-%s" % self) |
| 245 |
|
| 246 |
def destroy (self): |
| 247 |
debugprint ("DESTROY: %s" % self) |
| 248 |
self._conn.destroy () |
| 249 |
if self._quit: |
| 250 |
loop.quit () |
| 251 |
|
| 252 |
def getDevices (self): |
| 253 |
self._conn.getDevices (reply_handler=self.getDevices_reply, |
| 254 |
error_handler=self.getDevices_error) |
| 255 |
|
| 256 |
def getDevices_reply (self, conn, result): |
| 257 |
print (conn, result) |
| 258 |
self.destroy () |
| 259 |
|
| 260 |
def getDevices_error (self, conn, exc): |
| 261 |
print (repr (exc)) |
| 262 |
self.destroy () |
| 263 |
|
| 264 |
t = Test (False) |
| 265 |
loop = GObject.MainLoop () |
| 266 |
t.getDevices () |
| 267 |
t.destroy () |
| 268 |
|
| 269 |
t = Test (True) |
| 270 |
t.getDevices () |
| 271 |
loop.run () |