/python-slip-0.2.20/doc/dbus/example/example-conf-mechanism.py

#
Python | 88 lines | 58 code | 23 blank | 7 comment | 8 complexity | 7f325e65d8a1eb59b8a09bdcb413d6b1 MD5 | raw file
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import gobject
  4. import dbus
  5. import dbus.service
  6. import dbus.mainloop.glib
  7. # FIND THE ACTUAL EXAMPLE CODE BELOW...
  8. # try to find the module in the unpacked source tree
  9. import sys
  10. import os.path
  11. import import_marker
  12. # try to find the slip.dbus module
  13. import imp
  14. modfile = import_marker.__file__
  15. path = os.path.dirname(modfile)
  16. found = False
  17. oldsyspath = sys.path
  18. while not found and path and path != "/":
  19. path = os.path.abspath(os.path.join(path, os.path.pardir))
  20. try:
  21. slipmod = imp.find_module("slip", [path] + sys.path)
  22. if slipmod[1].startswith(path + "/"):
  23. found = True
  24. sys.path.insert(0, path)
  25. import slip.dbus.service
  26. except ImportError:
  27. pass
  28. if not found:
  29. # fall back to system paths
  30. sys.path = oldsyspath
  31. import slip.dbus.service
  32. # ...BELOW HERE:
  33. class ExampleObject(slip.dbus.service.Object):
  34. def __init__(self, *p, **k):
  35. super(ExampleObject, self).__init__(*p, **k)
  36. self.config_data = """These are the contents of a configuration file.
  37. They extend over some lines.
  38. And one more."""
  39. print "service object constructed"
  40. def __del__(self):
  41. print "service object deleted"
  42. @slip.dbus.polkit.require_auth("org.fedoraproject.slip.example.read")
  43. @dbus.service.method("org.fedoraproject.slip.example.mechanism",
  44. in_signature="", out_signature="s")
  45. def read(self):
  46. print "%s.read () -> '%s'" % (self, self.config_data)
  47. return self.config_data
  48. @slip.dbus.polkit.require_auth("org.fedoraproject.slip.example.write")
  49. @dbus.service.method("org.fedoraproject.slip.example.mechanism",
  50. in_signature="s", out_signature="")
  51. def write(self, config_data):
  52. print "%s.write ('%s')" % (self, config_data)
  53. self.config_data = config_data
  54. if __name__ == "__main__":
  55. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  56. bus = dbus.SystemBus()
  57. name = dbus.service.BusName("org.fedoraproject.slip.example.mechanism",
  58. bus)
  59. object = ExampleObject(name, "/org/fedoraproject/slip/example/object")
  60. mainloop = gobject.MainLoop()
  61. slip.dbus.service.set_mainloop(mainloop)
  62. print "Running example service."
  63. mainloop.run()