Revision 0ea1dcc4 pithos/lib/client.py
b/pithos/lib/client.py | ||
---|---|---|
3 | 3 |
|
4 | 4 |
import json |
5 | 5 |
import types |
6 |
import socket |
|
6 | 7 |
|
7 | 8 |
class Fault(Exception): |
8 | 9 |
def __init__(self, data=''): |
... | ... | |
91 | 92 |
kwargs['body'] = body |
92 | 93 |
kwargs['headers']['Content-Type'] = 'application/octet-stream' |
93 | 94 |
#print '****', method, full_path, kwargs |
95 |
#TODO catch socket.error |
|
94 | 96 |
conn.request(method, full_path, **kwargs) |
95 | 97 |
resp = conn.getresponse() |
96 | 98 |
headers = dict(resp.getheaders()) |
... | ... | |
131 | 133 |
if detail: |
132 | 134 |
data = json.loads(data) |
133 | 135 |
else: |
134 |
data = [data]
|
|
136 |
data = data.split('\n')
|
|
135 | 137 |
return data |
136 | 138 |
|
137 |
def _get_metadata(self, path, prefix): |
|
139 |
def _get_metadata(self, path, prefix=None):
|
|
138 | 140 |
status, headers, data = self.head(path) |
139 | 141 |
if status == '404': |
140 | 142 |
return None |
141 |
prefixlen = len(prefix)
|
|
143 |
prefixlen = prefix and len(prefix) or 0
|
|
142 | 144 |
meta = {} |
143 | 145 |
for key, val in headers.items(): |
144 |
if key.startswith(prefix): |
|
146 |
if prefix and not key.startswith(prefix): |
|
147 |
continue |
|
148 |
elif prefix and key.startswith(prefix): |
|
145 | 149 |
key = key[prefixlen:] |
146 |
meta[key] = val
|
|
150 |
meta[key] = val |
|
147 | 151 |
return meta |
148 | 152 |
|
149 | 153 |
def _set_metadata(self, path, entity, **meta): |
... | ... | |
158 | 162 |
def list_containers(self, detail=False, params=None, headers=None): |
159 | 163 |
return self._list('', detail, params, headers) |
160 | 164 |
|
161 |
def account_metadata(self): |
|
162 |
return self._get_metadata('', 'x-account-meta-') |
|
165 |
def account_metadata(self, restricted=False): |
|
166 |
prefix = restricted and 'x-account-meta-' or None |
|
167 |
return self._get_metadata('', prefix) |
|
163 | 168 |
|
164 | 169 |
def update_account_metadata(self, **meta): |
165 | 170 |
self._set_metadata('', 'account', **meta) |
... | ... | |
180 | 185 |
def delete_container(self, container): |
181 | 186 |
self.delete('/' + container) |
182 | 187 |
|
183 |
def retrieve_container_metadata(self, container): |
|
184 |
return self._get_metadata('/%s' % container, 'x-container-meta-') |
|
188 |
def retrieve_container_metadata(self, container, restricted=False): |
|
189 |
prefix = restricted and 'x-container-meta-' or None |
|
190 |
return self._get_metadata('/%s' % container, prefix) |
|
185 | 191 |
|
186 | 192 |
def update_container_metadata(self, container, **meta): |
187 | 193 |
self._set_metadata('/' + container, 'container', **meta) |
... | ... | |
242 | 248 |
def delete_object(self, container, object): |
243 | 249 |
self.delete('/%s/%s' % (container, object)) |
244 | 250 |
|
245 |
def retrieve_object_metadata(self, container, object): |
|
251 |
def retrieve_object_metadata(self, container, object, restricted=False):
|
|
246 | 252 |
path = '/%s/%s' % (container, object) |
247 |
return self._get_metadata(path, 'x-object-meta-') |
|
253 |
prefix = restricted and 'x-object-meta-' or None |
|
254 |
return self._get_metadata(path, prefix) |
|
248 | 255 |
|
249 | 256 |
def update_object_metadata(self, container, object, **meta): |
250 | 257 |
path = '/%s/%s' % (container, object) |
Also available in: Unified diff