home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / frontend.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.8 KB  |  94 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import pwd
  21.  
  22. DBUS_INTERFACE_NAME = "com.ubuntu.checkbox"
  23.  
  24. DBUS_BUS_NAME = "com.ubuntu.checkbox"
  25.  
  26.  
  27. class Frontend(object):
  28.  
  29.     globals = {}
  30.  
  31.     def __init__(self, function, method):
  32.         self._function = function
  33.         self._method = method
  34.  
  35.     def __get__(self, instance, cls=None):
  36.         self._instance = instance
  37.         return self
  38.  
  39.     def __call__(self, *args, **kwargs):
  40.         if self.user == "root":
  41.             return self._function(self._instance, *args, **kwargs)
  42.         else:
  43.             return getattr(self, self._method)(*args, **kwargs)
  44.  
  45.     @property
  46.     def user(self):
  47.         uid = os.getuid()
  48.         user = pwd.getpwuid(uid)[0]
  49.         return user
  50.  
  51.     @property
  52.     def client(self):
  53.         import dbus
  54.         import dbus.mainloop.glib
  55.  
  56.         if "client" in self.globals:
  57.             return self.globals["client"]
  58.         else:
  59.             dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  60.             bus = dbus.SystemBus()
  61.             obj = bus.get_object(DBUS_BUS_NAME, '/checkbox')
  62.             client = dbus.Interface(obj, DBUS_INTERFACE_NAME)
  63.  
  64.             return self.globals.setdefault("client", client)
  65.  
  66.     def get_test_result(self, *args, **kwargs):
  67.         from checkbox.test import TestResult
  68.  
  69.         test = self._instance.test
  70.         if test.user:
  71.             (status, data, duration) = self.client.get_test_result(test.suite, test.name)
  72.             return TestResult(self.test, status, data, float(duration))
  73.         else:
  74.             return self._function(self._instance, *args, **kwargs)
  75.  
  76.     def get_test_description(self, *args, **kwargs):
  77.         test = self._instance.test
  78.         if test.user:
  79.             return self.client.get_test_description(test.suite, test.name)
  80.         else:
  81.             return self._function(self._instance, *args, **kwargs)
  82.  
  83.     def get_registry(self, *args, **kwargs):
  84.         if self._instance.user:
  85.             return self.client.get_registry(self._instance.__module__)
  86.         else:
  87.             return self._function(self._instance, *args, **kwargs)
  88.  
  89.  
  90. def frontend(method):
  91.     def wrapper(func):
  92.         return Frontend(func, method)
  93.     return wrapper
  94.