1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# Copyright (C) 2007, One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import logging
import gobject
from sugar.datastore import dbus_helpers
class DSMetadata(gobject.GObject):
__gsignals__ = {
'updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([]))
}
def __init__(self, props=None):
gobject.GObject.__init__(self)
if not props:
self._props = {}
else:
self._props = props
default_keys = ['activity', 'mime_type', 'title_set_by_user']
for key in default_keys:
if not self._props.has_key(key):
self._props[key] = ''
def __getitem__(self, key):
return self._props[key]
def __setitem__(self, key, value):
if not self._props.has_key(key) or self._props[key] != value:
self._props[key] = value
self.emit('updated')
def __delitem__(self, key):
del self._props[key]
def has_key(self, key):
return self._props.has_key(key)
def get_dictionary(self):
return self._props
class DSObject:
def __init__(self, object_id, metadata=None, file_path=None):
self.object_id = object_id
self._metadata = metadata
self._file_path = file_path
def get_metadata(self):
if self._metadata is None and not self.object_id is None:
metadata = DSMetadata(dbus_helpers.get_properties(self.object_id))
self._metadata = metadata
return self._metadata
def set_metadata(self, metadata):
if self._metadata != metadata:
self._metadata = metadata
metadata = property(get_metadata, set_metadata)
def get_file_path(self):
if self._file_path is None and not self.object_id is None:
self.set_file_path(dbus_helpers.get_filename(self.object_id))
return self._file_path
def set_file_path(self, file_path):
if self._file_path != file_path:
self._file_path = file_path
file_path = property(get_file_path, set_file_path)
def get(object_id):
logging.debug('datastore.get')
metadata = dbus_helpers.get_properties(object_id)
file_path = dbus_helpers.get_filename(object_id)
ds_object = DSObject(object_id, DSMetadata(metadata), file_path)
# TODO: register the object for updates
return ds_object
def create():
return DSObject(object_id=None, metadata=DSMetadata(), file_path=None)
def write(ds_object, reply_handler=None, error_handler=None):
logging.debug('datastore.write')
if ds_object.object_id:
dbus_helpers.update(ds_object.object_id,
ds_object.metadata.get_dictionary(),
ds_object.file_path,
reply_handler=reply_handler,
error_handler=error_handler)
else:
ds_object.object_id = dbus_helpers.create(ds_object.metadata.get_dictionary(),
ds_object.file_path)
# TODO: register the object for updates
logging.debug('Written object %s to the datastore.' % ds_object.object_id)
def delete(object_id):
logging.debug('datastore.delete')
dbus_helpers.delete(object_id)
def find(query, sorting=None, limit=None, offset=None, reply_handler=None,
error_handler=None):
if sorting:
query['order_by'] = sorting
if limit:
query['limit'] = limit
if offset:
query['offset'] = offset
props_list, total_count = dbus_helpers.find(query, reply_handler, error_handler)
objects = []
for props in props_list:
if props.has_key('filename') and props['filename']:
file_path = props['filename']
del props['filename']
else:
file_path = None
object_id = props['uid']
del props['uid']
ds_object = DSObject(object_id, DSMetadata(props), file_path)
objects.append(ds_object)
return objects, total_count
def mount(uri, options):
return dbus_helpers.mount(uri, options)
def unmount(mount_point_id):
dbus_helpers.unmount(mount_point_id)
def mounts():
return dbus_helpers.mounts()
def get_unique_values(key):
return dbus_helpers.get_unique_values(key)
|