Kaydet (Commit) 8d9306d3 authored tarafından Suleyman Poyraz's avatar Suleyman Poyraz

Basic type python porting

üst d8232222
......@@ -31,8 +31,8 @@ class Call:
def __getitem__(self, key):
if not self.class_:
raise KeyError, "Package should be selected after class"
if not isinstance(key, basestring):
raise KeyError("Package should be selected after class")
if not isinstance(key, str):
raise KeyError
return Call(self.link, self.group, self.class_, key)
......@@ -51,14 +51,14 @@ class Call:
obj = self.link.bus.get_object(self.link.address, "/", introspect=False)
packages = obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface)
for package in packages:
yield unicode(package)
yield str(package)
def call(self, *args, **kwargs):
self.async = kwargs.get("async", None)
self.quiet = kwargs.get("quiet", False)
self.timeout = kwargs.get("timeout", 120)
if self.async and self.quiet:
raise Exception, "async and quiet arguments can't be used together"
raise Exception("async and quiet arguments can't be used together")
if self.async or self.quiet:
if self.package:
obj = self.link.bus.get_object(self.link.address, "/package/%s" % self.package)
......@@ -119,14 +119,14 @@ class Call:
met = getattr(obj, self.method)
try:
return met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), timeout=self.timeout, *args)
except dbus.DBusException, exception:
except dbus.DBusException as exception:
if "policy.auth" in exception._dbus_error_name or "Comar.PolicyKit" in exception._dbus_error_name:
action = exception.get_dbus_message()
if self.queryPolicyKit(action):
return self.call(*args, **kwargs)
raise dbus.DBusException, exception
raise dbus.DBusException(exception)
else:
raise AttributeError, "Package name required for non-async calls."
raise AttributeError("Package name required for non-async calls.")
def queryPolicyKit(self, action):
return False
......@@ -161,14 +161,14 @@ class Link:
code = code.split("_")[0]
obj = self.bus.get_object(self.address, '/', introspect=False)
obj.setLocale(code, dbus_interface=self.interface)
except dbus.DBusException, exception:
except dbus.DBusException as exception:
pass
def cancel(self, method="*"):
try:
obj = self.bus.get_object(self.address, '/', introspect=False)
return obj.cancel(method, dbus_interface=self.interface)
except dbus.DBusException, exception:
except dbus.DBusException as exception:
return 0
def listRunning(self, all=True):
......@@ -176,7 +176,7 @@ class Link:
try:
obj = self.bus.get_object(self.address, '/', introspect=False)
methods = obj.listRunning(all, dbus_interface=self.interface)
except dbus.DBusException, exception:
except dbus.DBusException as exception:
return methods
for index, method in enumerate(methods):
if method.startswith("%s." % self.interface):
......
......@@ -120,14 +120,14 @@ def _getPid(pidfile):
"""Read process ID from a .pid file."""
try:
pid = file(pidfile).read()
except IOError, e:
except IOError as e:
if e.errno != 2:
raise
return None
# Some services put custom data after the first line
pid = pid.split("\n")[0].strip()
# Non-pid data is also seen when stopped state in some services :/
if len(pid) == 0 or len(filter(lambda x: not x in "0123456789", pid)) > 0:
if len(pid) == 0 or len([x for x in pid if not x in "0123456789"]) > 0:
return None
return int(pid)
......@@ -143,7 +143,7 @@ def _checkPid(pid, user_uid=None, command=None, name=None):
if user_uid:
try:
st = os.stat(path)
except OSError, e:
except OSError as e:
if e.errno != 2:
raise
return False
......@@ -153,7 +153,7 @@ def _checkPid(pid, user_uid=None, command=None, name=None):
if command:
try:
cmdline = file("%s/cmdline" % path).read()
except IOError, e:
except IOError as e:
if e.errno != 2:
raise
return False
......@@ -162,7 +162,7 @@ def _checkPid(pid, user_uid=None, command=None, name=None):
elif name:
try:
stats = file("%s/stat" % path).read()
except IOError, e:
except IOError as e:
if e.errno != 2:
raise
return False
......@@ -232,7 +232,7 @@ def startService(command, args=None, pidfile=None, makepid=False, nice=None, det
"""
cmd = [ command ]
if args:
if isinstance(args, basestring):
if isinstance(args, str):
args = shlex.split(args)
cmd.extend(args)
......@@ -254,7 +254,7 @@ def startService(command, args=None, pidfile=None, makepid=False, nice=None, det
if detach:
# Set umask to a sane value
# (other and group has no write permission by default)
os.umask(022)
os.umask(0o22)
# Detach from controlling terminal
try:
tty_fd = os.open("/dev/tty", os.O_RDWR)
......@@ -316,7 +316,7 @@ def stopService(pidfile=None, command=None, args=None, chuid=None, user=None, na
if command and args is not None:
cmd = [ command ]
if args:
if isinstance(args, basestring):
if isinstance(args, str):
args = shlex.split(args)
cmd.extend(args)
......
......@@ -41,7 +41,7 @@ def run(*cmd):
"""Run a command without running a shell"""
command = []
if len(cmd) == 1:
if isinstance(cmd[0], basestring):
if isinstance(cmd[0], str):
command = cmd[0].split()
else:
command = cmd[0]
......@@ -78,9 +78,9 @@ class FileLock:
if timeout != -1:
_type |= fcntl.LOCK_NB
self.fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT, 0600)
self.fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT, 0o600)
if self.fd == -1:
raise IOError, "Cannot create lock file"
raise IOError("Cannot create lock file")
while True:
try:
......
......@@ -5,7 +5,7 @@ PROJECT (comar)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
# Python is required
FIND_PACKAGE(PythonLibs)
FIND_PACKAGE(PythonLibs 3)
FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES (DBUS REQUIRED dbus-1)
......
......@@ -303,7 +303,7 @@ bus_call(const char *path, const char *interface, const char *member, PyObject *
// setLocale first
PyObject *args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyString_FromString(lang));
PyTuple_SetItem(args, 0, PyBytes_FromString(lang));
PyObject *ret = bus_execute(conn, "/", config_interface, "setLocale", args, 25, "s");
if (!ret) {
......
......@@ -190,15 +190,15 @@ db_load_model(iks *xml, PyObject **py_models)
// First argument is type. 0 for methods, 1 for signals
if (iks_strcmp(iks_name(met), "method") == 0) {
PyTuple_SetItem(py_tuple, 0, PyInt_FromLong((long) 0));
PyTuple_SetItem(py_tuple, 0, PyLong_FromLong((long) 0));
}
else {
PyTuple_SetItem(py_tuple, 0, PyInt_FromLong((long) 1));
PyTuple_SetItem(py_tuple, 0, PyLong_FromLong((long) 1));
}
// Second argument is PolicyKit action ID
char *action_id = db_action_id(iface_name, met);
PyTuple_SetItem(py_tuple, 1, PyString_FromString(action_id));
PyTuple_SetItem(py_tuple, 1, PyBytes_FromString(action_id));
// Build argument lists
PyObject *py_args_in = PyList_New(0);
......@@ -215,14 +215,14 @@ db_load_model(iks *xml, PyObject **py_models)
else if (iks_strcmp(iks_name(arg), "arg") == 0) {
if (iks_strcmp(iks_name(met), "method") == 0) {
if (iks_strcmp(iks_find_attrib(arg, "direction"), "out") == 0) {
PyList_Append(py_args_out, PyString_FromString(iks_find_attrib(arg, "type")));
PyList_Append(py_args_out, PyBytes_FromString(iks_find_attrib(arg, "type")));
}
else {
PyList_Append(py_args_in, PyString_FromString(iks_find_attrib(arg, "type")));
PyList_Append(py_args_in, PyBytes_FromString(iks_find_attrib(arg, "type")));
}
}
else if (iks_strcmp(iks_name(met), "signal") == 0) {
PyList_Append(py_args_out, PyString_FromString(iks_find_attrib(arg, "type")));
PyList_Append(py_args_out, PyBytes_FromString(iks_find_attrib(arg, "type")));
}
}
}
......
......@@ -73,7 +73,7 @@ message_execute(DBusMessage *msg, const char *app, const char *model, const char
char *signature = script_signature(model, method, 1);
if (PyDict_GetItemString(model_def, method) != Py_None) {
const char *action_id = PyString_AsString(PyTuple_GetItem(method_def, 1));
const char *action_id = PyBytes_AsString(PyTuple_GetItem(method_def, 1));
const char *sender = dbus_message_get_sender(my_proc.bus_msg);
if (strcmp(action_id, "") != 0) {
......@@ -172,7 +172,7 @@ handle_core_message(DBusMessage *bus_msg, const char *path, const char *iface, c
}
}
log_debug("Killed %d processes.\n", total);
bus_reply_object(bus_msg, PyInt_FromLong((long) total), "i");
bus_reply_object(bus_msg, PyLong_FromLong((long) total), "i");
}
else if (strcmp(method, "listRunning") == 0) {
int i;
......@@ -181,7 +181,7 @@ handle_core_message(DBusMessage *bus_msg, const char *path, const char *iface, c
for (i = 0; i < my_proc.nr_children; i++) {
struct ProcChild *child = &my_proc.children[i];
if (PyTuple_GetItem(py_args, 0) == Py_True || dbus_message_has_sender(child->bus_msg, sender)) {
PyList_Append(py_list, PyString_FromFormat("%s.%s", dbus_message_get_interface(child->bus_msg), dbus_message_get_member(child->bus_msg)));
PyList_Append(py_list, PyBytes_FromFormat("%s.%s", dbus_message_get_interface(child->bus_msg), dbus_message_get_member(child->bus_msg)));
}
}
bus_reply_object(bus_msg, py_list, "as");
......
......@@ -68,19 +68,19 @@ policy_check(const char *sender, const char *action, int *result)
}
PyObject *subject = PyTuple_New(2);
PyTuple_SetItem(subject, 0, PyString_FromString("system-bus-name"));
PyTuple_SetItem(subject, 0, PyBytes_FromString("system-bus-name"));
PyObject *details = PyDict_New();
PyDict_SetItemString(details, "name", PyString_FromString(sender));
PyDict_SetItemString(details, "name", PyBytes_FromString(sender));
PyTuple_SetItem(subject, 1, details);
PyObject *details2 = PyDict_New();
PyObject *obj = PyTuple_New(5);
PyTuple_SetItem(obj, 0, subject);
PyTuple_SetItem(obj, 1, PyString_FromString(action));
PyTuple_SetItem(obj, 1, PyBytes_FromString(action));
PyTuple_SetItem(obj, 2, details2);
PyTuple_SetItem(obj, 3, PyInt_FromLong((long) 1));
PyTuple_SetItem(obj, 4, PyString_FromString("abc"));
PyTuple_SetItem(obj, 3, PyLong_FromLong((long) 1));
PyTuple_SetItem(obj, 4, PyBytes_FromString("abc"));
PyObject *ret = bus_execute2(conn, "org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority", "org.freedesktop.PolicyKit1.Authority", "CheckAuthorization", obj, -1, "(sa{sv})sa{ss}us");
......
......@@ -40,13 +40,13 @@ get_obj_sign(PyObject *obj)
*
*/
if (PyString_Check(obj) || PyUnicode_Check(obj)) {
if (PyBytes_Check(obj) || PyUnicode_Check(obj)) {
return "s";
}
else if (PyBool_Check(obj)) {
return "b";
}
else if (PyInt_Check(obj)) {
else if (PyLong_Check(obj)) {
return "i";
}
else if (PyLong_Check(obj)) {
......@@ -105,7 +105,7 @@ pydbus_export(DBusMessageIter *iter, PyObject *obj, char *signature)
for (i = 0; i < PyTuple_Size(obj); i++) {
PyObject *py_sign = PyList_GetItem(py_list, i);
PyObject *py_item = PyTuple_GetItem(obj, i);
if (pydbus_export_item(iter, py_item, PyString_AsString(py_sign)) != 0) {
if (pydbus_export_item(iter, py_item, PyBytes_AsString(py_sign)) != 0) {
return -1;
}
}
......@@ -226,8 +226,8 @@ pydbus_export_item(DBusMessageIter *iter, PyObject *obj, char *signature)
e = dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &p.b);
break;
case 'n':
if (PyInt_Check(obj)) {
p.i16 = (short) PyInt_AsLong(obj);
if (PyLong_Check(obj)) {
p.i16 = (short) PyLong_AsLong(obj);
}
else {
// TODO: Raise error
......@@ -236,8 +236,8 @@ pydbus_export_item(DBusMessageIter *iter, PyObject *obj, char *signature)
e = dbus_message_iter_append_basic(iter, DBUS_TYPE_INT16, &p.i16);
break;
case 'q':
if (PyInt_Check(obj)) {
p.u16 = (unsigned short) PyInt_AsLong(obj);
if (PyLong_Check(obj)) {
p.u16 = (unsigned short) PyLong_AsLong(obj);
}
else {
// TODO: Raise error
......@@ -246,8 +246,8 @@ pydbus_export_item(DBusMessageIter *iter, PyObject *obj, char *signature)
e = dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, &p.u16);
break;
case 'i':
if (PyInt_Check(obj)) {
p.i32 = (int) PyInt_AsLong(obj);
if (PyLong_Check(obj)) {
p.i32 = (int) PyLong_AsLong(obj);
}
else {
// TODO: Raise error
......@@ -256,8 +256,8 @@ pydbus_export_item(DBusMessageIter *iter, PyObject *obj, char *signature)
e = dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &p.i32);
break;
case 'u':
if (PyInt_Check(obj)) {
p.u32 = (long) PyInt_AsLong(obj);
if (PyLong_Check(obj)) {
p.u32 = (long) PyLong_AsLong(obj);
}
else {
// TODO: Raise error
......@@ -289,8 +289,8 @@ pydbus_export_item(DBusMessageIter *iter, PyObject *obj, char *signature)
e = dbus_message_iter_append_basic(iter, DBUS_TYPE_DOUBLE, &p.d);
break;
case 's':
if (PyString_Check(obj)) {
p.s = PyString_AsString(obj);
if (PyBytes_Check(obj)) {
p.s = PyBytes_AsString(obj);
}
else {
// TODO: Raise error
......
......@@ -106,7 +106,7 @@ script_signature(const char *model, const char *member, int direction)
PyObject *py_models = PyDict_GetItemString(py_core, "models");
PyObject *py_model = PyDict_GetItemString(py_models, model);
PyObject *py_method = PyDict_GetItemString(py_model, member);
PyObject *py_str = PyString_FromString("");
PyObject *py_str = PyBytes_FromString("");
PyObject *py_list;
if (direction == 0) {
......@@ -118,10 +118,10 @@ script_signature(const char *model, const char *member, int direction)
int i;
for (i = 0; i < PyList_Size(py_list); i++) {
PyString_Concat(&py_str, PyList_GetItem(py_list, i));
PyBytes_Concat(&py_str, PyList_GetItem(py_list, i));
}
return PyString_AsString(py_str);
return PyBytes_AsString(py_str);
}
//! Splits signature into atomic DBus object signatures
......@@ -144,7 +144,7 @@ script_signature_each(const char *signature)
return NULL;
}
else {
PyList_Append(py_list, PyString_FromString(sign));
PyList_Append(py_list, PyBytes_FromString(sign));
}
dbus_free(sign);
......@@ -179,10 +179,10 @@ py_catch(char **eStr, char **vStr, int log)
return;
}
*eStr = PyString_AsString(PyObject_GetAttrString(py_type, "__name__"));
*eStr = PyBytes_AsString(PyObject_GetAttrString(py_type, "__name__"));
if (py_value) {
*vStr = PyString_AsString(PyObject_Str(py_value));
*vStr = PyBytes_AsString(PyObject_Str(py_value));
}
if (log == 0) {
......@@ -195,9 +195,9 @@ py_catch(char **eStr, char **vStr, int log)
while (py_trace != NULL && py_trace != Py_None) {
py_frame = PyObject_GetAttrString(py_trace, "tb_frame");
py_code = PyObject_GetAttrString(py_frame, "f_code");
log_error(" File %s, line %d, in %s()\n", PyString_AsString(PyObject_GetAttrString(py_code, "co_filename")),
(int) PyInt_AsLong(PyObject_GetAttrString(py_trace, "tb_lineno")),
PyString_AsString(PyObject_GetAttrString(py_code, "co_name")));
log_error(" File %s, line %d, in %s()\n", PyBytes_AsString(PyObject_GetAttrString(py_code, "co_filename")),
(int) PyLong_AsLong(PyObject_GetAttrString(py_trace, "tb_lineno")),
PyBytes_AsString(PyObject_GetAttrString(py_code, "co_name")));
py_trace = PyObject_GetAttrString(py_trace, "tb_next");
}
}
......@@ -217,8 +217,8 @@ sender_language()
PyObject *py_locales = PyDict_GetItemString(py_core, "locales");
const char *lang;
if (PyDict_Contains(py_locales, PyString_FromString(sender))) {
lang = PyString_AsString(PyDict_GetItemString(py_locales, sender));
if (PyDict_Contains(py_locales, PyBytes_FromString(sender))) {
lang = PyBytes_AsString(PyDict_GetItemString(py_locales, sender));
}
else {
lang = "en";
......@@ -241,11 +241,11 @@ validate_model_member(const char *model, const char *member, int type)
*
*/
if (PyDict_Contains(PyDict_GetItemString(py_core, "models"), PyString_FromString(model))) {
if (PyDict_Contains(PyDict_GetItemString(py_core, "models"), PyBytes_FromString(model))) {
PyObject *py_dict_model = PyDict_GetItemString(PyDict_GetItemString(py_core, "models"), model);
if (PyDict_Contains(py_dict_model, PyString_FromString(member))) {
if (PyDict_Contains(py_dict_model, PyBytes_FromString(member))) {
PyObject *py_tuple = PyDict_GetItemString(py_dict_model, member);
if (PyInt_AsLong(PyTuple_GetItem(py_tuple, 0)) == (long) type) {
if (PyLong_AsLong(PyTuple_GetItem(py_tuple, 0)) == (long) type) {
return 0;
}
}
......@@ -258,7 +258,7 @@ static PyObject *
c_bus_path(PyObject *self, PyObject *args)
{
const char *path = dbus_message_get_path(my_proc.bus_msg);
PyObject *py_str = PyString_FromString(path);
PyObject *py_str = PyBytes_FromString(path);
Py_INCREF(py_str);
return py_str;
}
......@@ -268,7 +268,7 @@ static PyObject *
c_bus_interface(PyObject *self, PyObject *args)
{
const char *iface = dbus_message_get_interface(my_proc.bus_msg);
PyObject *py_str = PyString_FromString(iface);
PyObject *py_str = PyBytes_FromString(iface);
Py_INCREF(py_str);
return py_str;
}
......@@ -278,7 +278,7 @@ static PyObject *
c_bus_member(PyObject *self, PyObject *args)
{
const char *member = dbus_message_get_member(my_proc.bus_msg);
PyObject *py_str = PyString_FromString(member);
PyObject *py_str = PyBytes_FromString(member);
Py_INCREF(py_str);
return py_str;
}
......@@ -288,7 +288,7 @@ static PyObject *
c_bus_sender(PyObject *self, PyObject *args)
{
const char *sender = dbus_message_get_sender(my_proc.bus_msg);
PyObject *py_str = PyString_FromString(sender);
PyObject *py_str = PyBytes_FromString(sender);
Py_INCREF(py_str);
return py_str;
}
......@@ -302,7 +302,7 @@ c_package(PyObject *self, PyObject *args)
if (strncmp(path, "/package/", strlen("/package/")) == 0) {
const char *app = strsub(path, strlen("/package/"), strlen(path));
if (strlen(app) > 0) {
return PyString_FromString(app);
return PyBytes_FromString(app);
}
}
......@@ -319,7 +319,7 @@ c_model(PyObject *self, PyObject *args)
if (strncmp(iface, config_interface, strlen(config_interface)) == 0) {
const char *model = strsub(iface, strlen(config_interface) + 1, strlen(iface));
if (strlen(model) > 0) {
return PyString_FromString(model);
return PyBytes_FromString(model);
}
}
......@@ -338,9 +338,9 @@ c_i18n(PyObject *self, PyObject *args)
return NULL;
}
PyObject *py_lang = PyString_FromString(sender_language());
PyObject *py_lang = PyBytes_FromString(sender_language());
if (!PyDict_Contains(py_dict, py_lang)) {
py_lang = PyString_FromString("en");
py_lang = PyBytes_FromString("en");
}
if (PyDict_Contains(py_dict, py_lang)) {
......@@ -459,7 +459,7 @@ c_fail(PyObject *self, PyObject *args)
static PyObject *
c_cfg_datapath(PyObject *self, PyObject *args)
{
PyObject *py_str = PyString_FromString(config_dir_data);
PyObject *py_str = PyBytes_FromString(config_dir_data);
Py_INCREF(py_str);
return py_str;
}
......@@ -477,7 +477,7 @@ c_cfg_modelbase(PyObject *self, PyObject *args)
static PyObject *
c_cfg_interface(PyObject *self, PyObject *args)
{
PyObject *py_str = PyString_FromString(config_interface);
PyObject *py_str = PyBytes_FromString(config_interface);
Py_INCREF(py_str);
return py_str;
}
......@@ -533,7 +533,7 @@ py_execute(const char *app, const char *model, const char *method, PyObject *py_
py_module = PyImport_ImportModule("sys");
py_dict = PyModule_GetDict(py_module);
py_list = PyDict_GetItemString(py_dict, "path");
PyList_Insert(py_list, 0, PyString_FromString(config_dir_modules));
PyList_Insert(py_list, 0, PyBytes_FromString(config_dir_modules));
// Put CSL methods into __builtin__
py_mod_builtin = PyImport_AddModule("__builtin__");
......@@ -624,7 +624,7 @@ py_execute(const char *app, const char *model, const char *method, PyObject *py_
else {
// Check if PolicyKit action defined at runtime
if (PyObject_HasAttrString(py_func, "policy_action_id")) {
const char *action_id = PyString_AsString(PyObject_GetAttrString(py_func, "policy_action_id"));
const char *action_id = PyBytes_AsString(PyObject_GetAttrString(py_func, "policy_action_id"));
const char *sender = dbus_message_get_sender(my_proc.bus_msg);
int result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment