def start(self): """Start the notification server. """ if self._thread: raise EnvironmentError("Server is already running") LOGGER.info("Starting RSS feed listener at %s", self._rss_uri) self._thread = threading.Thread(target=self.serve_forever) self._thread.daemon = True self._thread.start() self.cancel_subscriptions() # Renew notifications subscription if osp.exists(cups.getServer()): hostname = 'localhost' else: hostname = cups.getServer() cups_uri = "ipp://{}:{}".format(hostname, cups.getPort()) try: self._conn.createSubscription(cups_uri, recipient_uri=self._rss_uri, events=self._filters, job_id=self._job_id) except cups.IPPError as e: status = e.args[0] if status == 1030: LOGGER.warn("The job {} not exists".format(self._job_id)) raise JobNotExists() raise e
def run (self): if self.host == None: self.host = cups.getServer () if self.port == None: self.port = cups.getPort () if self._encryption == None: self._encryption = cups.getEncryption () if self.user: cups.setUser (self.user) else: self.user = cups.getUser () try: cups.setPasswordCB2 (self._auth) except AttributeError: # Requires pycups >= 1.9.47. Fall back to rubbish API. cups.setPasswordCB (self._auth) try: conn = cups.Connection (host=self.host, port=self.port, encryption=self._encryption) self._reply (None) except RuntimeError, e: conn = None self._error (e)
def action_ok(self, cr, uid, ids, context=None): # Update CUPS URL url = 'https://%s:%s' % ( cups.getServer(), cups.getPort() ) id = self.pool.get('ir.model.data')._get_id(cr, uid, 'printjob', 'action_open_cups') id = self.pool.get('ir.model.data').browse(cr, uid, id, context).res_id self.pool.get('ir.actions.url').write(cr, uid, [id], { 'url': url, }, context) # Update Printers try: connection = cups.Connection() printers = connection.getPrinters() except: return {} ids = self.pool.get('printjob.printer').search(cr, uid, [('system_name','in',printers.keys())], context=context) for printer in self.pool.get('printjob.printer').browse(cr, uid, ids, context): del printers[printer.system_name] for name in printers: printer = printers[name] self.pool.get('printjob.printer').create(cr, uid, { 'name': printer['printer-info'], 'system_name': name, 'model': printer.get('printer-make-and-model', False), 'location': printer.get('printer-location', False), 'uri': printer.get('device-uri', False), }, context) return {}
def __init__(self, parent=None, try_as_root=True, lock=False, host=None, port=None, encryption=None): if host != None: cups.setServer(host) if port != None: cups.setPort(port) if encryption != None: cups.setEncryption(encryption) self._use_password = '' self._parent = parent self._try_as_root = try_as_root self._use_user = cups.getUser() self._server = cups.getServer() self._port = cups.getPort() self._encryption = cups.getEncryption() self._prompt_allowed = True self._operation_stack = [] self._lock = lock self._gui_event = threading.Event() self._connect()
def __init__ (self, parent=None, try_as_root=True, lock=False, host=None, port=None, encryption=None): if host != None: cups.setServer (host) if port != None: cups.setPort (port) if encryption != None: cups.setEncryption (encryption) self._use_password = '' self._parent = parent self._try_as_root = try_as_root self._use_user = cups.getUser () self._server = cups.getServer () self._port = cups.getPort() self._encryption = cups.getEncryption () self._prompt_allowed = True self._operation_stack = [] self._lock = lock self._gui_event = threading.Event () creds = global_authinfocache.lookup_auth_info (host=host, port=port) if creds != None: if (creds[0] != 'root' or try_as_root): (self._use_user, self._use_password) = creds del creds self._connect ()
def __init__ (self, reply_handler=None, error_handler=None, auth_handler=None, host=None, port=None, encryption=None, parent=None, try_as_root=True, prompt_allowed=True): super (Connection, self).__init__ () self._destroyed = False # Decide whether to use direct IPP or PolicyKit. if host == None: host = cups.getServer() use_pk = ((host.startswith ('/') or host == 'localhost') and os.getuid () != 0) use_pk = False def subst_reply_handler (conn, reply): self._subst_reply_handler (None, reply_handler, reply) def subst_error_handler (conn, exc): self._subst_error_handler (None, error_handler, exc) def subst_auth_handler (prompt, conn, method, resource): self._subst_auth_handler (None, auth_handler, prompt, method, resource) if use_pk and try_as_root: debugprint ("Using polkit-1 connection class") c = asyncpk1.PK1Connection (reply_handler=subst_reply_handler, error_handler=subst_error_handler, host=host, port=port, encryption=encryption, parent=parent) self._conn = c else: debugprint ("Using IPP connection class") c = asyncipp.IPPAuthConnection (reply_handler=subst_reply_handler, error_handler=subst_error_handler, auth_handler=subst_auth_handler, host=host, port=port, encryption=encryption, parent=parent, try_as_root=try_as_root, prompt_allowed=prompt_allowed, semantic=self) self._conn = c methodtype = type (self._conn.getPrinters) instancemethodtype = type (self._conn.getDevices) bindings = [] for fname in dir (self._conn): if fname.startswith ('_'): continue fn = getattr (self._conn, fname) if type (fn) != methodtype and type (fn) != instancemethodtype: continue if not hasattr (self, fname): setattr (self, fname, self._make_binding (fn)) bindings.append (fname) self._bindings = bindings self._methodcalls = [] debugprint ("+%s" % self)
def __init__ (self, parent=None, try_as_root=True): self._use_password = '' self._parent = parent self._try_as_root = try_as_root self._use_user = cups.getUser () self._server = cups.getServer () self._port = cups.getPort() self._connect () self._prompt_allowed = True
def __init__(self, bus=None, my_jobs=True, specific_dests=None, monitor_jobs=True, host=None, port=None, encryption=None): GObject.GObject.__init__(self) self.my_jobs = my_jobs self.specific_dests = specific_dests self.monitor_jobs = monitor_jobs self.jobs = {} self.printer_state_reasons = {} self.printers = set() self.process_pending_events = True self.fetch_jobs_timer = None self.cups_connection_in_error = False if host: cups.setServer(host) if port: cups.setPort(port) if encryption: cups.setEncryption(encryption) self.user = cups.getUser() self.host = cups.getServer() self.port = cups.getPort() self.encryption = cups.getEncryption() self.ppdcache = ppdcache.PPDCache(host=self.host, port=self.port, encryption=self.encryption) self.which_jobs = "not-completed" self.reasons_seen = {} self.connecting_timers = {} self.still_connecting = set() self.connecting_to_device = {} self.received_any_dbus_signals = False self.update_timer = None if bus is None: try: bus = dbus.SystemBus() except dbus.exceptions.DBusException: # System bus not running. pass self.bus = bus if bus is not None: bus.add_signal_receiver(self.handle_dbus_signal, path=self.DBUS_PATH, dbus_interface=self.DBUS_IFACE) self.sub_id = -1
def start(self): """Start the notification server. """ if self._thread: raise EnvironmentError("Server is already running") LOGGER.info("Starting RSS feed listener at %s", self._rss_uri) self._thread = threading.Thread(target=self.serve_forever) self._thread.daemon = True self._thread.start() self.cancel_subscriptions() # Renew notifications subscription if osp.exists(cups.getServer()): hostname = 'localhost' else: hostname = cups.getServer() cups_uri = "ipp://{}:{}".format(hostname, cups.getPort()) self._conn.createSubscription(cups_uri, recipient_uri=self._rss_uri, events=self._filters)
def __init__(self, bus=None, my_jobs=True, specific_dests=None, monitor_jobs=True, host=None, port=None, encryption=None): GObject.GObject.__init__ (self) self.my_jobs = my_jobs self.specific_dests = specific_dests self.monitor_jobs = monitor_jobs self.jobs = {} self.printer_state_reasons = {} self.printers = set() self.process_pending_events = True self.fetch_jobs_timer = None self.cups_connection_in_error = False if host: cups.setServer (host) if port: cups.setPort (port) if encryption: cups.setEncryption (encryption) self.user = cups.getUser () self.host = cups.getServer () self.port = cups.getPort () self.encryption = cups.getEncryption () self.ppdcache = ppdcache.PPDCache (host=self.host, port=self.port, encryption=self.encryption) self.which_jobs = "not-completed" self.reasons_seen = {} self.connecting_timers = {} self.still_connecting = set() self.connecting_to_device = {} self.received_any_dbus_signals = False self.update_timer = None if bus is None: try: bus = dbus.SystemBus () except dbus.exceptions.DBusException: # System bus not running. pass self.bus = bus if bus is not None: bus.add_signal_receiver (self.handle_dbus_signal, path=self.DBUS_PATH, dbus_interface=self.DBUS_IFACE) self.sub_id = -1
def __init__(self, parent=None, try_as_root=True, lock=False, host=None, port=None, encryption=None): if host != None: cups.setServer(host) if port != None: cups.setPort(port) if encryption != None: cups.setEncryption(encryption) self._use_password = "" self._parent = parent self._try_as_root = try_as_root self._use_user = cups.getUser() self._server = cups.getServer() self._port = cups.getPort() self._encryption = cups.getEncryption() self._prompt_allowed = True self._operation_stack = [] self._lock = lock self._gui_event = threading.Event() self._connect()
def run (self): if self.host == None: self.host = cups.getServer () if self.port == None: self.port = cups.getPort () if self._encryption == None: self._encryption = cups.getEncryption () if self.user: cups.setUser (self.user) else: self.user = cups.getUser () cups.setPasswordCB2 (self._auth) try: conn = cups.Connection (host=self.host, port=self.port, encryption=self._encryption) self._reply (None) except RuntimeError, e: conn = None self._error (e)
def run (self): if self.host == None: self.host = cups.getServer () if self.port == None: self.port = cups.getPort () if self._encryption == None: self._encryption = cups.getEncryption () if self.user: cups.setUser (self.user) else: self.user = cups.getUser () cups.setPasswordCB2 (self._auth) try: conn = cups.Connection (host=self.host, port=self.port, encryption=self._encryption) self._reply (None) except RuntimeError as e: conn = None self._error (e) while True: # Wait to find out what operation to try. debugprint ("Awaiting further instructions") self.idle = self._queue.empty () item = self._queue.get () debugprint ("Next task: %s" % repr (item)) if item == None: # Our signal to quit. self._queue.task_done () break self.idle = False (fn, args, kwds, rh, eh, ah) = item if rh != False: self._reply_handler = rh if eh != False: self._error_handler = eh if ah != False: self._auth_handler = ah if fn == True: # Our signal to change user and reconnect. self.user = args[0] cups.setUser (self.user) debugprint ("Set user=%s; reconnecting..." % self.user) cups.setPasswordCB2 (self._auth) try: conn = cups.Connection (host=self.host, port=self.port, encryption=self._encryption) debugprint ("...reconnected") self._queue.task_done () self._reply (None) except RuntimeError as e: debugprint ("...failed") self._queue.task_done () self._error (e) continue # Normal IPP operation. Try to perform it. try: debugprint ("Call %s" % fn) result = fn (conn, *args, **kwds) if fn == cups.Connection.adminGetServerSettings.__call__: # Special case for a rubbish bit of API. if result == {}: # Authentication failed, but we aren't told that. raise cups.IPPError (cups.IPP_NOT_AUTHORIZED, '') debugprint ("...success") self._reply (result) except Exception as e: debugprint ("...failure (%s)" % repr (e)) self._error (e) self._queue.task_done () debugprint ("Thread exiting") self._destroyed = True del self._conn # already destroyed del self._reply_handler del self._error_handler del self._auth_handler del self._queue del self._auth_queue del conn cups.setPasswordCB2 (None)
def run(self): if self.host == None: self.host = cups.getServer() if self.port == None: self.port = cups.getPort() if self._encryption == None: self._encryption = cups.getEncryption() if self.user: cups.setUser(self.user) else: self.user = cups.getUser() cups.setPasswordCB2(self._auth) try: conn = cups.Connection(host=self.host, port=self.port, encryption=self._encryption) self._reply(None) except RuntimeError as e: conn = None self._error(e) while True: # Wait to find out what operation to try. debugprint("Awaiting further instructions") self.idle = self._queue.empty() item = self._queue.get() debugprint("Next task: %s" % repr(item)) if item == None: # Our signal to quit. self._queue.task_done() break elif self._destroyed: # Just mark all tasks done self._queue.task_done() continue self.idle = False (fn, args, kwds, rh, eh, ah) = item if rh != False: self._reply_handler = rh if eh != False: self._error_handler = eh if ah != False: self._auth_handler = ah if fn == True: # Our signal to change user and reconnect. self.user = args[0] cups.setUser(self.user) debugprint("Set user=%s; reconnecting..." % self.user) cups.setPasswordCB2(self._auth) try: conn = cups.Connection(host=self.host, port=self.port, encryption=self._encryption) debugprint("...reconnected") self._queue.task_done() self._reply(None) except RuntimeError as e: debugprint("...failed") self._queue.task_done() self._error(e) continue # Normal IPP operation. Try to perform it. try: debugprint("Call %s" % fn) result = fn(conn, *args, **kwds) if fn == cups.Connection.adminGetServerSettings.__call__: # Special case for a rubbish bit of API. if result == {}: # Authentication failed, but we aren't told that. raise cups.IPPError(cups.IPP_NOT_AUTHORIZED, "") debugprint("...success") self._reply(result) except Exception as e: debugprint("...failure (%s)" % repr(e)) self._error(e) self._queue.task_done() debugprint("Thread exiting") del self._conn # already destroyed del self._reply_handler del self._error_handler del self._auth_handler del self._queue del self._auth_queue del conn cups.setPasswordCB2(None)
def __init__(self, reply_handler=None, error_handler=None, auth_handler=None, host=None, port=None, encryption=None, parent=None, try_as_root=True, prompt_allowed=True): super(Connection, self).__init__() self._destroyed = False # Decide whether to use direct IPP or PolicyKit. if host is None: host = cups.getServer() use_pk = ((host.startswith('/') or host == 'localhost') and os.getuid() != 0) def subst_reply_handler(conn, reply): self._subst_reply_handler(None, reply_handler, reply) def subst_error_handler(conn, exc): self._subst_error_handler(None, error_handler, exc) def subst_auth_handler(prompt, conn, method, resource): self._subst_auth_handler(None, auth_handler, prompt, method, resource) if use_pk and try_as_root: debugprint("Using polkit-1 connection class") import asyncpk1 c = asyncpk1.PK1Connection(reply_handler=subst_reply_handler, error_handler=subst_error_handler, host=host, port=port, encryption=encryption, parent=parent) self._conn = c else: debugprint("Using IPP connection class") import asyncipp c = asyncipp.IPPAuthConnection(reply_handler=subst_reply_handler, error_handler=subst_error_handler, auth_handler=subst_auth_handler, host=host, port=port, encryption=encryption, parent=parent, try_as_root=try_as_root, prompt_allowed=prompt_allowed, semantic=self) self._conn = c methodtype = type(self._conn.getPrinters) instancemethodtype = type(self._conn.getDevices) bindings = [] for fname in dir(self._conn): if fname.startswith('_'): continue fn = getattr(self._conn, fname) if type(fn) != methodtype and type(fn) != instancemethodtype: continue if not hasattr(self, fname): setattr(self, fname, self._make_binding(fn)) bindings.append(fname) self._bindings = bindings self._methodcalls = [] debugprint("+%s" % self)