Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/datastore/datastore.py
blob: 38842a04f9c637c18db89cb1796f29daf11b0923 (plain)
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
# Copyright (C) 2007, One Laptop Per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
import logging

import gobject

from sugar.datastore import dbus_helpers

class DSObject(gobject.GObject):
    __gsignals__ = {
        'updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                    ([]))
    }

    def __init__(self, object_id, metadata, file_path):
        gobject.GObject.__init__(self)
        self.object_id = object_id
        self._metadata = metadata
        self._file_path = file_path

    def __getitem__(self, key):
        return self.metadata[key]

    def __setitem__(self, key, value):
        if not self.metadata.has_key(key) or self.metadata[key] != value:
            self.metadata[key] = value
            self.emit('updated')

    def get_metadata(self):
        return self._metadata
    
    def set_metadata(self, metadata):
        if self._metadata != metadata:
            self._metadata = metadata
            self.emit('updated')

    metadata = property(get_metadata, set_metadata)

    def get_file_path(self):
        return self._file_path
    
    def set_file_path(self, file_path):
        if self._file_path != file_path:
            self._file_path = file_path
            self.emit('updated')

    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, metadata, file_path)
    # TODO: register the object for updates
    return ds_object

def create():
    return DSObject(object_id=None, metadata={}, file_path=None)

def write(ds_object):
    logging.debug('datastore.write')
    if ds_object.object_id:
        dbus_helpers.update(ds_object.object_id,
                            ds_object.metadata,
                            ds_object.file_path)
    else:
        ds_object.object_id = dbus_helpers.create(ds_object.metadata,
                                                  ds_object.file_path)
        # TODO: register the object for updates
    logging.debug('Written object %s to the datastore.' % ds_object.object_id)

def find(query):
    object_ids = dbus_helpers.find({})
    objects = []
    for object_id in object_ids:
        objects.append(get(object_id))
    return objects