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
|
# Copyright (C) 2012 Aleksey Lim
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import logging
from cStringIO import StringIO
from os.path import isdir, abspath
from sugar_network import client
from sugar_network.toolkit import enforce
from client import Client
_logger = logging.getLogger('sugar_network.objects')
_uid = client.sugar_uid()
class Object(object):
def __init__(self, document, reply, guid=None, props=None,
offset=None, **kwargs):
self.document = document
self._reply = reply or []
self._guid = guid
self._props = props or {}
self._dirty = set()
self.offset = offset
for prop, value in kwargs.items():
self[prop] = value
@property
def guid(self):
return self._guid
@property
def is_author(self):
return _uid in [i.get('guid') for i in self['author']]
def get(self, prop):
if prop == 'guid':
return self._guid
result = self._props.get(prop)
if result is None:
enforce(prop in self._reply,
'Access to not requested %r property in %r',
prop, self.document)
self.fetch()
result = self._props.get(prop)
return result
def fetch(self, props=None):
enforce(self._guid, 'Object needs to be posted first')
to_fetch = []
for prop in (props or self._reply):
if prop not in self._props:
to_fetch.append(prop)
if not to_fetch:
return
response = Client.call('GET',
document=self.document, guid=self._guid, reply=to_fetch)
response.update(self._props)
self._props = response
def post(self):
if not self._dirty:
return
props = {}
for i in self._dirty:
props[i] = self._props.get(i)
if self._guid:
Client.call('PUT',
document=self.document, guid=self._guid, content=props,
content_type='application/json')
else:
self._guid = Client.call('POST',
document=self.document, content=props,
content_type='application/json')
self._dirty.clear()
return self._guid
def get_blob(self, prop):
return Client.call('GET',
document=self.document, guid=self._guid, prop=prop)
def upload_blob(self, prop, content, content_type):
enforce(self._guid, 'Object needs to be posted first')
Client.call('PUT',
document=self.document, guid=self._guid, prop=prop,
content=content, content_type=content_type)
def __getitem__(self, prop):
result = self.get(prop)
enforce(result is not None, KeyError,
'Property %r is absent in %r', prop, self.document)
return result
def __setitem__(self, prop, value):
enforce(prop != 'guid', 'Property "guid" is read-only')
if self._props.get(prop) == value:
return
self._props[prop] = value
self._dirty.add(prop)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.post()
|