Update data store to new dbus-python bindings API and sqlite3

This commit is contained in:
Dan Williams 2007-03-01 11:53:43 -05:00
parent 1c69095310
commit 7ab0b60b10
3 changed files with 77 additions and 67 deletions

View File

@ -18,7 +18,7 @@
import os import os
import dbus, dbus.glib, gobject import dbus, dbus.glib, gobject
import logging import logging
import sqlite import sqlite3
import dbus_helpers import dbus_helpers
import string import string
@ -131,36 +131,36 @@ class ObjectDBusHelper(dbus_helpers.FallbackObject):
dbus_helpers.FallbackObject.__init__(self, bus_name, _DS_OBJECT_OBJECT_PATH) dbus_helpers.FallbackObject.__init__(self, bus_name, _DS_OBJECT_OBJECT_PATH)
@dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE, @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
in_signature="", out_signature="ay", dbus_message_keyword="dbus_message") in_signature="", out_signature="ay", object_path_keyword="dbus_object_path")
def get_data(self, dbus_message=None): def get_data(self, dbus_object_path=None):
if not dbus_message: if not dbus_object_path:
raise RuntimeError("Need the dbus message.") raise RuntimeError("Need the dbus object path.")
uid = _get_uid_from_op(dbus_message.get_path()) uid = _get_uid_from_op(dbus_object_path)
return self._parent.get_data(uid) return self._parent.get_data(uid)
@dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE, @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
in_signature="ay", out_signature="i", dbus_message_keyword="dbus_message") in_signature="ay", out_signature="i", object_path_keyword="dbus_object_path")
def set_data(self, data, dbus_message=None): def set_data(self, data, dbus_object_path=None):
if not dbus_message: if not dbus_object_path:
raise RuntimeError("Need the dbus message.") raise RuntimeError("Need the dbus object path.")
uid = _get_uid_from_op(dbus_message.get_path()) uid = _get_uid_from_op(dbus_object_path)
self._parent.set_data(uid, data) self._parent.set_data(uid, data)
return 0 return 0
@dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE, @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
in_signature="as", out_signature="a{sv}", dbus_message_keyword="dbus_message") in_signature="as", out_signature="a{sv}", object_path_keyword="dbus_object_path")
def get_properties(self, keys, dbus_message=None): def get_properties(self, keys, dbus_object_path=None):
if not dbus_message: if not dbus_object_path:
raise RuntimeError("Need the dbus message.") raise RuntimeError("Need the dbus object path.")
uid = _get_uid_from_op(dbus_message.get_path()) uid = _get_uid_from_op(dbus_object_path)
return self._parent.get_properties(uid, keys) return self._parent.get_properties(uid, keys)
@dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE, @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
in_signature="a{sv}", out_signature="i", dbus_message_keyword="dbus_message") in_signature="a{sv}", out_signature="i", object_path_keyword="dbus_object_path")
def set_properties(self, prop_dict, dbus_message=None): def set_properties(self, prop_dict, dbus_object_path=None):
if not dbus_message: if not dbus_object_path:
raise RuntimeError("Need the dbus message.") raise RuntimeError("Need the dbus object path.")
uid = _get_uid_from_op(dbus_message.get_path()) uid = _get_uid_from_op(dbus_object_path)
self._parent.set_properties(uid, prop_dict) self._parent.set_properties(uid, prop_dict)
return 0 return 0
@ -188,7 +188,7 @@ class DataStore(object):
if not os.path.exists(os.path.dirname(self._dbfile)): if not os.path.exists(os.path.dirname(self._dbfile)):
os.makedirs(os.path.dirname(self._dbfile), 0755) os.makedirs(os.path.dirname(self._dbfile), 0755)
self._dbcx = sqlite.connect(self._dbfile, encoding="utf-8", timeout=3) self._dbcx = sqlite3.connect(self._dbfile, timeout=3)
try: try:
self._ensure_table() self._ensure_table()
except StandardError, e: except StandardError, e:
@ -244,7 +244,7 @@ class DataStore(object):
def create(self, data, prop_dict=None, activity_id=None): def create(self, data, prop_dict=None, activity_id=None):
curs = self._dbcx.cursor() curs = self._dbcx.cursor()
data = sqlite.encode(_get_data_as_string(data)) data = sqlite3.encode(_get_data_as_string(data))
if not activity_id: if not activity_id:
curs.execute("INSERT INTO objects (uid, data) VALUES (NULL, '%s');" % data) curs.execute("INSERT INTO objects (uid, data) VALUES (NULL, '%s');" % data)
else: else:
@ -256,7 +256,7 @@ class DataStore(object):
uid = last_row[0] uid = last_row[0]
for (key, value) in prop_dict.items(): for (key, value) in prop_dict.items():
safe_key = key.replace("'", "''") safe_key = key.replace("'", "''")
value = sqlite.encode(_get_data_as_string(value)) value = sqlite3.encode(_get_data_as_string(value))
curs.execute("INSERT INTO properties (objid, key, value) VALUES (%d, '%s', '%s');" % (uid, safe_key, value)) curs.execute("INSERT INTO properties (objid, key, value) VALUES (%d, '%s', '%s');" % (uid, safe_key, value))
self._dbcx.commit() self._dbcx.commit()
del curs del curs
@ -279,7 +279,7 @@ class DataStore(object):
value = _get_data_as_string(value) value = _get_data_as_string(value)
if not len(value): if not len(value):
raise ValueError("Property values must not be blank.") raise ValueError("Property values must not be blank.")
substr = "(key='%s' AND value='%s')" % (safe_key, sqlite.encode(value)) substr = "(key='%s' AND value='%s')" % (safe_key, sqlite3.encode(value))
if len(subquery) > 0: if len(subquery) > 0:
subquery += " OR " subquery += " OR "
subquery += substr subquery += substr
@ -305,7 +305,7 @@ class DataStore(object):
if len(res) <= 0: if len(res) <= 0:
del curs del curs
raise NotFoundError("Object %d was not found." % uid) raise NotFoundError("Object %d was not found." % uid)
data = sqlite.encode(_get_data_as_string(data)) data = sqlite3.encode(_get_data_as_string(data))
curs.execute("UPDATE objects SET data='%s' WHERE uid=%d;" % (data, uid)) curs.execute("UPDATE objects SET data='%s' WHERE uid=%d;" % (data, uid))
self._dbcx.commit() self._dbcx.commit()
del curs del curs
@ -332,7 +332,7 @@ class DataStore(object):
# delete the property # delete the property
curs.execute("DELETE FROM properties WHERE (objid=%d AND key='%s');" % (uid, safe_key)) curs.execute("DELETE FROM properties WHERE (objid=%d AND key='%s');" % (uid, safe_key))
else: else:
enc_value = sqlite.encode(value) enc_value = sqlite3.encode(value)
curs.execute("SELECT objid FROM properties WHERE (objid=%d AND key='%s');" % (uid, safe_key)) curs.execute("SELECT objid FROM properties WHERE (objid=%d AND key='%s');" % (uid, safe_key))
if len(curs.fetchall()) > 0: if len(curs.fetchall()) > 0:
curs.execute("UPDATE properties SET value='%s' WHERE (objid=%d AND key='%s');" % (enc_value, uid, safe_key)) curs.execute("UPDATE properties SET value='%s' WHERE (objid=%d AND key='%s');" % (enc_value, uid, safe_key))
@ -371,7 +371,7 @@ class DataStore(object):
prop_dict = {} prop_dict = {}
for row in rows: for row in rows:
conv_key = row['key'].replace("''", "'") conv_key = row['key'].replace("''", "'")
prop_dict[conv_key] = sqlite.decode(row['value']) prop_dict[conv_key] = sqlite3.decode(row['value'])
prop_dict['uid'] = str(uid) prop_dict['uid'] = str(uid)
del curs del curs
return prop_dict return prop_dict

View File

@ -18,11 +18,12 @@
# Mostly taken from dbus-python's service.py # Mostly taken from dbus-python's service.py
import dbus import dbus
import _dbus_bindings
from dbus import service from dbus import service
import inspect import inspect
def method(dbus_interface, in_signature=None, out_signature=None, async_callbacks=None, sender_keyword=None, dbus_message_keyword=None): def method(dbus_interface, in_signature=None, out_signature=None, async_callbacks=None, sender_keyword=None, utf8_strings=False, byte_arrays=False, object_path_keyword=None):
dbus._util._validate_interface_or_name(dbus_interface) _dbus_bindings.validate_interface_name(dbus_interface)
def decorator(func): def decorator(func):
args = inspect.getargspec(func)[0] args = inspect.getargspec(func)[0]
@ -39,18 +40,17 @@ def method(dbus_interface, in_signature=None, out_signature=None, async_callback
if sender_keyword: if sender_keyword:
args.remove(sender_keyword) args.remove(sender_keyword)
if dbus_message_keyword: if object_path_keyword:
args.remove(dbus_message_keyword) args.remove(object_path_keyword)
if in_signature: if in_signature:
in_sig = tuple(dbus.dbus_bindings.Signature(in_signature)) in_sig = tuple(_dbus_bindings.Signature(in_signature))
if len(in_sig) > len(args): if len(in_sig) > len(args):
raise ValueError, 'input signature is longer than the number of arguments taken' raise ValueError, 'input signature is longer than the number of arguments taken'
elif len(in_sig) < len(args): elif len(in_sig) < len(args):
raise ValueError, 'input signature is shorter than the number of arguments taken' raise ValueError, 'input signature is shorter than the number of arguments taken'
func._dbus_message_keyword = dbus_message_keyword
func._dbus_is_method = True func._dbus_is_method = True
func._dbus_async_callbacks = async_callbacks func._dbus_async_callbacks = async_callbacks
func._dbus_interface = dbus_interface func._dbus_interface = dbus_interface
@ -58,27 +58,28 @@ def method(dbus_interface, in_signature=None, out_signature=None, async_callback
func._dbus_out_signature = out_signature func._dbus_out_signature = out_signature
func._dbus_sender_keyword = sender_keyword func._dbus_sender_keyword = sender_keyword
func._dbus_args = args func._dbus_args = args
func._dbus_get_args_options = {'byte_arrays': byte_arrays,
'utf8_strings': utf8_strings}
func._dbus_object_path_keyword = object_path_keyword
return func return func
return decorator return decorator
def fallback_signal(dbus_interface, signature=None, ignore_args=None): def fallback_signal(dbus_interface, signature=None, ignore_args=None):
dbus._util._validate_interface_or_name(dbus_interface) _dbus_bindings.validate_interface_name(dbus_interface)
def decorator(func): def decorator(func):
def emit_signal(self, *args, **keywords): def emit_signal(self, *args, **keywords):
obj_path = func(self, *args, **keywords) obj_path = func(self, *args, **keywords)
message = dbus.dbus_bindings.Signal(obj_path, dbus_interface, func.__name__) message = _dbus_bindings.SignalMessage(obj_path, dbus_interface, func.__name__)
iter = message.get_iter(True)
if emit_signal._dbus_signature: if emit_signal._dbus_signature:
signature = tuple(dbus.dbus_bindings.Signature(emit_signal._dbus_signature)) message.append(signature=emit_signal._dbus_signature,
for (arg, sig) in zip(args, signature): *args)
iter.append_strict(arg, sig)
else: else:
for arg in args: message.append(*args)
iter.append(arg)
self._connection.send(message) self._connection.send_message(message)
temp_args = inspect.getargspec(func)[0] temp_args = inspect.getargspec(func)[0]
temp_args.pop(0) temp_args.pop(0)
@ -89,7 +90,7 @@ def fallback_signal(dbus_interface, signature=None, ignore_args=None):
args.append(arg) args.append(arg)
if signature: if signature:
sig = tuple(dbus.dbus_bindings.Signature(signature)) sig = tuple(_dbus_bindings.Signature(signature))
if len(sig) > len(args): if len(sig) > len(args):
raise ValueError, 'signal signature is longer than the number of arguments provided' raise ValueError, 'signal signature is longer than the number of arguments provided'
@ -112,14 +113,28 @@ class FallbackObject(dbus.service.Object):
Just inherit from Object and provide a list of methods to share Just inherit from Object and provide a list of methods to share
across the Bus across the Bus
""" """
def __init__(self, bus_name, fallback_object_path):
def __init__(self, conn=None, fallback_object_path=None, bus_name=None):
if fallback_object_path is None:
raise TypeError('The fallback_object_path argument is required')
if isinstance(conn, dbus.service.BusName):
# someone's using the old API; don't gratuitously break them
bus_name = conn
conn = bus_name.get_bus()
elif conn is None:
# someone's using the old API but naming arguments, probably
if bus_name is None:
raise TypeError('Either conn or bus_name is required')
conn = bus_name.get_bus()
self._object_path = fallback_object_path self._object_path = fallback_object_path
self._name = bus_name self._name = bus_name
self._bus = bus_name.get_bus() self._bus = conn
self._connection = self._bus.get_connection() self._connection = self._bus.get_connection()
self._connection.register_fallback(fallback_object_path, self._unregister_cb, self._message_cb) self._connection._register_object_path(fallback_object_path, self._message_cb, self._unregister_cb, fallback=True)
def _message_cb(self, connection, message): def _message_cb(self, connection, message):
try: try:
@ -129,12 +144,11 @@ class FallbackObject(dbus.service.Object):
(candidate_method, parent_method) = dbus.service._method_lookup(self, method_name, interface_name) (candidate_method, parent_method) = dbus.service._method_lookup(self, method_name, interface_name)
# set up method call parameters # set up method call parameters
args = message.get_args_list() args = message.get_args_list(**parent_method._dbus_get_args_options)
keywords = {} keywords = {}
# iterate signature into list of complete types if parent_method._dbus_out_signature is not None:
if parent_method._dbus_out_signature: signature = _dbus_bindings.Signature(parent_method._dbus_out_signature)
signature = tuple(dbus.dbus_bindings.Signature(parent_method._dbus_out_signature))
else: else:
signature = None signature = None
@ -148,8 +162,8 @@ class FallbackObject(dbus.service.Object):
if parent_method._dbus_sender_keyword: if parent_method._dbus_sender_keyword:
keywords[parent_method._dbus_sender_keyword] = message.get_sender() keywords[parent_method._dbus_sender_keyword] = message.get_sender()
if parent_method._dbus_message_keyword: if parent_method._dbus_object_path_keyword:
keywords[parent_method._dbus_message_keyword] = message keywords[parent_method._dbus_object_path_keyword] = message.get_object_path()
# call method # call method
retval = candidate_method(self, *args, **keywords) retval = candidate_method(self, *args, **keywords)
@ -161,17 +175,18 @@ class FallbackObject(dbus.service.Object):
# otherwise we send the return values in a reply. if we have a # otherwise we send the return values in a reply. if we have a
# signature, use it to turn the return value into a tuple as # signature, use it to turn the return value into a tuple as
# appropriate # appropriate
if parent_method._dbus_out_signature: if signature is not None:
signature_tuple = tuple(signature)
# if we have zero or one return values we want make a tuple # if we have zero or one return values we want make a tuple
# for the _method_reply_return function, otherwise we need # for the _method_reply_return function, otherwise we need
# to check we're passing it a sequence # to check we're passing it a sequence
if len(signature) == 0: if len(signature_tuple) == 0:
if retval == None: if retval == None:
retval = () retval = ()
else: else:
raise TypeError('%s has an empty output signature but did not return None' % raise TypeError('%s has an empty output signature but did not return None' %
method_name) method_name)
elif len(signature) == 1: elif len(signature_tuple) == 1:
retval = (retval,) retval = (retval,)
else: else:
if operator.isSequenceType(retval): if operator.isSequenceType(retval):
@ -183,7 +198,6 @@ class FallbackObject(dbus.service.Object):
# no signature, so just turn the return into a tuple and send it as normal # no signature, so just turn the return into a tuple and send it as normal
else: else:
signature = None
if retval == None: if retval == None:
retval = () retval = ()
else: else:
@ -194,11 +208,10 @@ class FallbackObject(dbus.service.Object):
# send error reply # send error reply
dbus.service._method_reply_error(connection, message, exception) dbus.service._method_reply_error(connection, message, exception)
@method('org.freedesktop.DBus.Introspectable', in_signature='', out_signature='s', dbus_message_keyword="dbus_message") @method('org.freedesktop.DBus.Introspectable', in_signature='', out_signature='s', object_path_keyword="dbus_object_path")
def Introspect(self, dbus_message=None): def Introspect(self, dbus_object_path=None):
reflection_data = '<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">\n' reflection_data = '<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">\n'
op = dbus_message.get_path() reflection_data += '<node name="%s">\n' % (dbus_object_path)
reflection_data += '<node name="%s">\n' % (op)
interfaces = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__] interfaces = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__]
for (name, funcs) in interfaces.iteritems(): for (name, funcs) in interfaces.iteritems():
@ -217,6 +230,6 @@ class FallbackObject(dbus.service.Object):
return reflection_data return reflection_data
def __repr__(self): def __repr__(self):
return '<dbus_helpers.FallbackObject %s on %r at %#x>' % (self._fallback_object_path, self._name, id(self)) return '<dbus.service.FallbackObject %s on %r at %#x>' % (self._object_path, self._name, id(self))
__str__ = __repr__ __str__ = __repr__

View File

@ -28,9 +28,6 @@ sys.path.insert(0, env.get_services_dir())
logger.start('data-store') logger.start('data-store')
logging.info('Starting the data store...') logging.info('Starting the data store...')
try:
from datastore import datastore from datastore import datastore
except ImportError:
import datastore
datastore.main() datastore.main()