home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / asyncconn.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  6.4 KB  |  191 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2007, 2008, 2009, 2010 Red Hat, Inc.
  4. ## Copyright (C) 2008 Novell, Inc.
  5. ## Authors: Tim Waugh <twaugh@redhat.com>, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import cups
  22. import gobject
  23. import gtk
  24. import os
  25.  
  26. import asyncipp
  27. import asyncpk1
  28. import authconn
  29. import config
  30. from debug import *
  31. import debug
  32.  
  33. def set_gettext_function (x):
  34.     asyncipp.set_gettext_function (x)
  35.  
  36. ######
  37. ###### A class to keep track of what we're trying to achieve in order
  38. ###### to display that to the user if authentication is required.
  39. ######
  40. class SemanticOperations(object):
  41.     def __init__ (self):
  42.         self._operation_stack = []
  43.  
  44.     def _begin_operation (self, operation):
  45.         self._operation_stack.append (operation)
  46.  
  47.     def _end_operation (self):
  48.         self._operation_stack.pop ()
  49.  
  50.     def current_operation (self):
  51.         try:
  52.             return self._operation_stack[0]
  53.         except IndexError:
  54.             return None
  55.  
  56. ######
  57. ###### An asynchronous libcups API using IPP or PolicyKit as
  58. ###### appropriate.
  59. ######
  60.  
  61. class Connection(SemanticOperations):
  62.     def __init__ (self, reply_handler=None, error_handler=None,
  63.                   auth_handler=None, host=None, port=None, encryption=None,
  64.                   parent=None, try_as_root=True, prompt_allowed=True):
  65.         super (Connection, self).__init__ ()
  66.  
  67.         # Decide whether to use direct IPP or PolicyKit.
  68.         if host == None:
  69.             host = cups.getServer()
  70.         use_pk = ((host.startswith ('/') or host == 'localhost') and
  71.                   os.getuid () != 0)
  72.  
  73.         def subst_reply_handler (conn, reply):
  74.             if reply_handler:
  75.                 reply_handler (self, reply)
  76.  
  77.         def subst_error_handler (conn, exc):
  78.             if error_handler:
  79.                 error_handler (self, exc)
  80.  
  81.         def subst_auth_handler (prompt, conn, method, resource):
  82.             if auth_handler:
  83.                 auth_handler (prompt, self, method, resource)
  84.  
  85.         if use_pk and try_as_root:
  86.             debugprint ("Using polkit-1 connection class")
  87.             c = asyncpk1.PK1Connection (reply_handler=subst_reply_handler,
  88.                                         error_handler=subst_error_handler,
  89.                                         host=host, port=port,
  90.                                         encryption=encryption,
  91.                                         parent=parent)
  92.             self._conn = c
  93.         else:
  94.             debugprint ("Using IPP connection class")
  95.             c = asyncipp.IPPAuthConnection (reply_handler=subst_reply_handler,
  96.                                             error_handler=subst_error_handler,
  97.                                             auth_handler=subst_auth_handler,
  98.                                             host=host, port=port,
  99.                                             encryption=encryption,
  100.                                             parent=parent,
  101.                                             try_as_root=try_as_root,
  102.                                             prompt_allowed=prompt_allowed,
  103.                                             semantic=self)
  104.             self._conn = c
  105.  
  106.         methodtype = type (self._conn.getPrinters)
  107.         instancemethodtype = type (self._conn.getDevices)
  108.         bindings = []
  109.         for fname in dir (self._conn):
  110.             if fname.startswith ('_'):
  111.                 continue
  112.             fn = getattr (self._conn, fname)
  113.             if type (fn) != methodtype and type (fn) != instancemethodtype:
  114.                 continue
  115.             if not hasattr (self, fname):
  116.                 setattr (self, fname, self._make_binding (fn))
  117.                 bindings.append (fname)
  118.  
  119.         self._bindings = bindings
  120.         debugprint ("+%s" % self)
  121.  
  122.     def __del__ (self):
  123.         debug.debugprint ("-%s" % self)
  124.  
  125.     def destroy (self):
  126.         debugprint ("DESTROY: %s" % self)
  127.         try:
  128.             self._conn.destroy ()
  129.         except AttributeError:
  130.             pass
  131.  
  132.         for binding in self._bindings:
  133.             delattr (self, binding)
  134.  
  135.     def _make_binding (self, fn):
  136.         return lambda *args, **kwds: self._call_function (fn, *args, **kwds)
  137.  
  138.     def _call_function (self, fn, *args, **kwds):
  139.         reply_handler = error_handler = auth_handler = False
  140.         if kwds.has_key ("reply_handler"):
  141.             reply_handler = kwds["reply_handler"]
  142.             kwds["reply_handler"] = lambda c, r: reply_handler (self, r)
  143.         if kwds.has_key ("error_handler"):
  144.             error_handler = kwds["error_handler"]
  145.             kwds["error_handler"] = lambda c, e: error_handler (self, e)
  146.         if kwds.has_key ("auth_handler"):
  147.             auth_handler = kwds["auth_handler"]
  148.             kwds["auth_handler"] = lambda p, c, m, r: auth_handler (p, self,
  149.                                                                     m, r)
  150.  
  151.         fn (*args, **kwds)
  152.  
  153.     def set_auth_info (self, password):
  154.         """Call this from your auth_handler function."""
  155.         self.thread.set_auth_info (password)
  156.  
  157. if __name__ == "__main__":
  158.     # Demo
  159.     set_debugging (True)
  160.     gobject.threads_init ()
  161.  
  162.     class Test:
  163.         def __init__ (self):
  164.             self._conn = Connection ()
  165.             debugprint ("+%s" % self)
  166.  
  167.         def __del__ (self):
  168.             debug.debugprint ("-%s" % self)
  169.  
  170.         def destroy (self):
  171.             debugprint ("DESTROY: %s" % self)
  172.             self._conn.destroy ()
  173.             loop.quit ()
  174.  
  175.         def getDevices (self):
  176.             self._conn.getDevices (reply_handler=self.getDevices_reply,
  177.                                    error_handler=self.getDevices_error)
  178.  
  179.         def getDevices_reply (self, conn, result):
  180.             print result
  181.             self.destroy ()
  182.  
  183.         def getDevices_error (self, conn, exc):
  184.             print repr (exc)
  185.             self.destroy ()
  186.  
  187.     t = Test ()
  188.     loop = gobject.MainLoop ()
  189.     t.getDevices ()
  190.     loop.run ()
  191.