|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (C) 2006-2009, TUBITAK/UEKAE |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify it |
| 7 | +# under the terms of the GNU General Public License as published by the |
| 8 | +# Free Software Foundation; either version 2 of the License, or (at your |
| 9 | +# option) any later version. Please read the COPYING file. |
| 10 | +# |
| 11 | + |
| 12 | +__version__ = '2.4.9' |
| 13 | + |
| 14 | +import dbus |
| 15 | +import locale |
| 16 | +import os |
| 17 | +import subprocess |
| 18 | + |
| 19 | +class Call: |
| 20 | + def __init__(self, link, group, class_=None, package=None, method=None): |
| 21 | + self.link = link |
| 22 | + self.group = group |
| 23 | + self.class_ = class_ |
| 24 | + self.package = package |
| 25 | + self.method = method |
| 26 | + self.async = None |
| 27 | + self.quiet = False |
| 28 | + |
| 29 | + if self.package: |
| 30 | + self.package = self.package.replace("-", "_") |
| 31 | + |
| 32 | + def __getitem__(self, key): |
| 33 | + if not self.class_: |
| 34 | + raise KeyError, "Package should be selected after class" |
| 35 | + if not isinstance(key, basestring): |
| 36 | + raise KeyError |
| 37 | + return Call(self.link, self.group, self.class_, key) |
| 38 | + |
| 39 | + def __getattr__(self, name): |
| 40 | + if self.class_: |
| 41 | + c = Call(self.link, self.group, self.class_, self.package, name) |
| 42 | + return c.call |
| 43 | + else: |
| 44 | + if name[0] < 'A' or name[0] > 'Z': |
| 45 | + raise AttributeError |
| 46 | + |
| 47 | + return Call(self.link, self.group, name) |
| 48 | + |
| 49 | + def __iter__(self): |
| 50 | + if self.class_: |
| 51 | + obj = self.link.bus.get_object(self.link.address, "/", introspect=False) |
| 52 | + packages = obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface) |
| 53 | + for package in packages: |
| 54 | + yield unicode(package) |
| 55 | + |
| 56 | + def call(self, *args, **kwargs): |
| 57 | + self.async = kwargs.get("async", None) |
| 58 | + self.quiet = kwargs.get("quiet", False) |
| 59 | + self.timeout = kwargs.get("timeout", 120) |
| 60 | + if self.async and self.quiet: |
| 61 | + raise Exception, "async and quiet arguments can't be used together" |
| 62 | + if self.async or self.quiet: |
| 63 | + if self.package: |
| 64 | + obj = self.link.bus.get_object(self.link.address, "/package/%s" % self.package) |
| 65 | + met = getattr(obj, self.method) |
| 66 | + |
| 67 | + def handleResult(*result): |
| 68 | + self.async(self.package, None, result) |
| 69 | + def handleError(exception): |
| 70 | + if "policy.auth" in exception._dbus_error_name or "Comar.PolicyKit" in exception._dbus_error_name: |
| 71 | + action = exception.get_dbus_message() |
| 72 | + if self.queryPolicyKit(action): |
| 73 | + return self.call(*args, **kwargs) |
| 74 | + self.async(self.package, exception, None) |
| 75 | + |
| 76 | + if self.quiet: |
| 77 | + met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), *args) |
| 78 | + else: |
| 79 | + met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), reply_handler=handleResult, error_handler=handleError, timeout=self.timeout, *args) |
| 80 | + else: |
| 81 | + def handlePackages(packages): |
| 82 | + if self.quiet: |
| 83 | + for package in packages: |
| 84 | + obj = self.link.bus.get_object(self.link.address, "/package/%s" % package) |
| 85 | + met = getattr(obj, self.method) |
| 86 | + met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), *args) |
| 87 | + else: |
| 88 | + def handleResult(package): |
| 89 | + def handler(*result): |
| 90 | + return self.async(package, None, result) |
| 91 | + return handler |
| 92 | + def handleError(package): |
| 93 | + def handler(exception): |
| 94 | + return self.async(package, exception, None) |
| 95 | + return handler |
| 96 | + |
| 97 | + for package in packages: |
| 98 | + obj = self.link.bus.get_object(self.link.address, "/package/%s" % package) |
| 99 | + met = getattr(obj, self.method) |
| 100 | + |
| 101 | + met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), reply_handler=handleResult(package), error_handler=handleError(package), timeout=self.timeout, *args) |
| 102 | + |
| 103 | + def handlePackError(exception): |
| 104 | + if self.quiet: |
| 105 | + pass |
| 106 | + else: |
| 107 | + raise exception |
| 108 | + |
| 109 | + if self.quiet: |
| 110 | + obj = self.link.bus.get_object(self.link.address, "/", introspect=False) |
| 111 | + packages = obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface) |
| 112 | + handlePackages(packages) |
| 113 | + else: |
| 114 | + obj = self.link.bus.get_object(self.link.address, "/", introspect=False) |
| 115 | + obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface, reply_handler=handlePackages, error_handler=handlePackError, timeout=self.timeout) |
| 116 | + else: |
| 117 | + if self.package: |
| 118 | + obj = self.link.bus.get_object(self.link.address, "/package/%s" % self.package) |
| 119 | + met = getattr(obj, self.method) |
| 120 | + try: |
| 121 | + return met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), timeout=self.timeout, *args) |
| 122 | + except dbus.DBusException, exception: |
| 123 | + if "policy.auth" in exception._dbus_error_name or "Comar.PolicyKit" in exception._dbus_error_name: |
| 124 | + action = exception.get_dbus_message() |
| 125 | + if self.queryPolicyKit(action): |
| 126 | + return self.call(*args, **kwargs) |
| 127 | + raise dbus.DBusException, exception |
| 128 | + else: |
| 129 | + raise AttributeError, "Package name required for non-async calls." |
| 130 | + |
| 131 | + def queryPolicyKit(self, action): |
| 132 | + return False |
| 133 | + |
| 134 | + |
| 135 | +class Link: |
| 136 | + def __init__(self, version="2", socket=None, alternate=False): |
| 137 | + self.version = str(version) |
| 138 | + self.address = "tr.org.pardus.comar" |
| 139 | + self.interface = "tr.org.pardus.comar" |
| 140 | + self.socket = socket |
| 141 | + |
| 142 | + # Auto-enable agent, if X session present. |
| 143 | + self.use_agent = ("DISPLAY" in os.environ) |
| 144 | + |
| 145 | + if not socket: |
| 146 | + self.bus = dbus.SystemBus() |
| 147 | + else: |
| 148 | + self.bus = dbus.bus.BusConnection(address_or_type="unix:path=%s" % socket) |
| 149 | + |
| 150 | + if alternate: |
| 151 | + self.address += "2" |
| 152 | + |
| 153 | + def useAgent(self, agent=True): |
| 154 | + self.use_agent = agent |
| 155 | + |
| 156 | + def setLocale(self): |
| 157 | + try: |
| 158 | + code, encoding = locale.getdefaultlocale() |
| 159 | + if code: |
| 160 | + if "_" in code: |
| 161 | + code = code.split("_")[0] |
| 162 | + obj = self.bus.get_object(self.address, '/', introspect=False) |
| 163 | + obj.setLocale(code, dbus_interface=self.interface) |
| 164 | + except dbus.DBusException, exception: |
| 165 | + pass |
| 166 | + |
| 167 | + def cancel(self, method="*"): |
| 168 | + try: |
| 169 | + obj = self.bus.get_object(self.address, '/', introspect=False) |
| 170 | + return obj.cancel(method, dbus_interface=self.interface) |
| 171 | + except dbus.DBusException, exception: |
| 172 | + return 0 |
| 173 | + |
| 174 | + def listRunning(self, all=True): |
| 175 | + methods = [] |
| 176 | + try: |
| 177 | + obj = self.bus.get_object(self.address, '/', introspect=False) |
| 178 | + methods = obj.listRunning(all, dbus_interface=self.interface) |
| 179 | + except dbus.DBusException, exception: |
| 180 | + return methods |
| 181 | + for index, method in enumerate(methods): |
| 182 | + if method.startswith("%s." % self.interface): |
| 183 | + methods[index] = method[len("%s." % self.interface):] |
| 184 | + return methods |
| 185 | + |
| 186 | + def listenSignals(self, model, handler): |
| 187 | + def sigHandler(*args, **kwargs): |
| 188 | + if "/package/" not in kwargs["path"]: |
| 189 | + return |
| 190 | + package = kwargs["path"].split("/package/")[1] |
| 191 | + signal = kwargs["signal"] |
| 192 | + handler(package, signal, args) |
| 193 | + self.bus.add_signal_receiver(sigHandler, dbus_interface="%s.%s" % (self.interface, model), member_keyword="signal", path_keyword="path") |
| 194 | + |
| 195 | + def register(self, app, model, script, timeout=120): |
| 196 | + obj = self.bus.get_object(self.address, '/', introspect=False) |
| 197 | + obj.register(app, model, script, dbus_interface=self.interface, timeout=timeout) |
| 198 | + |
| 199 | + def remove(self, app, timeout=120): |
| 200 | + obj = self.bus.get_object(self.address, '/', introspect=False) |
| 201 | + obj.remove(app, dbus_interface=self.interface, timeout=timeout) |
| 202 | + |
| 203 | + def __getattr__(self, name): |
| 204 | + if name[0] < 'A' or name[0] > 'Z': |
| 205 | + raise AttributeError |
| 206 | + return Call(self, name) |
0 commit comments