winazurestorageのフォーク
リビジョン | 132c7dcdcc51549576cc6f2a1dfbc35a75339b9e (tree) |
---|---|
日時 | 2009-03-19 16:18:42 |
作者 | Steve Marx <Steve.Marx@micr...> |
コミッター | Steve Marx |
better table support, minimal queue support
@@ -3,6 +3,7 @@ | ||
3 | 3 | """ |
4 | 4 | Python wrapper around Windows Azure storage |
5 | 5 | Sriram Krishnan <sriramk@microsoft.com> |
6 | +Steve Marx <steve.marx@microsoft.com> | |
6 | 7 | """ |
7 | 8 | |
8 | 9 | import base64 |
@@ -38,13 +39,16 @@ TIME_FORMAT ="%a, %d %b %Y %H:%M:%S %Z" | ||
38 | 39 | |
39 | 40 | def parse_edm_datetime(input): |
40 | 41 | d = datetime.strptime(input[:input.find('.')], "%Y-%m-%dT%H:%M:%S") |
41 | - if input[:input.find('.')] != -1: | |
42 | + if input.find('.') != -1: | |
42 | 43 | d += timedelta(0, 0, int(round(float(input[input.index('.'):-1])*1000000))) |
43 | 44 | return d |
44 | 45 | |
45 | 46 | def parse_edm_int32(input): |
46 | 47 | return int(input) |
47 | 48 | |
49 | +def parse_edm_double(input): | |
50 | + return float(input) | |
51 | + | |
48 | 52 | def parse_edm_boolean(input): |
49 | 53 | return input.lower() == "true" |
50 | 54 |
@@ -122,12 +126,69 @@ class Storage(object): | ||
122 | 126 | |
123 | 127 | class TableEntity(object): pass |
124 | 128 | |
129 | +class QueueStorage(Storage): | |
130 | + def __init__(self, host, account_name, secret_key, use_path_style_uris = None): | |
131 | + super(QueueStorage, self).__init__(host, account_name, secret_key, use_path_style_uris) | |
132 | + | |
133 | + def create_queue(self, name): | |
134 | + req = RequestWithMethod("PUT", "%s/%s" % (self.get_base_url(), name)) | |
135 | + req.add_header("Content-Length", "0") | |
136 | + self._credentials.sign_request(req) | |
137 | + try: | |
138 | + response = urlopen(req) | |
139 | + return response.code | |
140 | + except URLError, e: | |
141 | + return e.code | |
142 | + | |
143 | + def delete_queue(self, name): | |
144 | + req = RequestWithMethod("DELETE", "%s/%s" % (self.get_base_url(), name)) | |
145 | + self._credentials.sign_request(req) | |
146 | + try: | |
147 | + response = urlopen(req) | |
148 | + return response.code | |
149 | + except URLError, e: | |
150 | + return e.code | |
151 | + | |
125 | 152 | class TableStorage(Storage): |
126 | - '''Due to local development storage not supporting SharedKeyLite authentication, this class | |
153 | + '''Due to local development storage not supporting SharedKey authentication, this class | |
127 | 154 | will only work against cloud storage.''' |
128 | 155 | def __init__(self, host, account_name, secret_key, use_path_style_uris = None): |
129 | 156 | super(TableStorage, self).__init__(host, account_name, secret_key, use_path_style_uris) |
130 | 157 | |
158 | + def create_table(self, name): | |
159 | + data = """<?xml version="1.0" encoding="utf-8" standalone="yes"?> | |
160 | +<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> | |
161 | + <title /> | |
162 | + <updated>%s</updated> | |
163 | + <author> | |
164 | + <name /> | |
165 | + </author> | |
166 | + <id /> | |
167 | + <content type="application/xml"> | |
168 | + <m:properties> | |
169 | + <d:TableName>%s</d:TableName> | |
170 | + </m:properties> | |
171 | + </content> | |
172 | +</entry>""" % (time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime()), name) | |
173 | + req = RequestWithMethod("POST", "%s/Tables" % self.get_base_url(), data=data) | |
174 | + req.add_header("Content-Length", "%d" % len(data)) | |
175 | + req.add_header("Content-Type", "application/atom+xml") | |
176 | + self._credentials.sign_table_request(req) | |
177 | + try: | |
178 | + response = urlopen(req) | |
179 | + return response.code | |
180 | + except URLError, e: | |
181 | + return e.code | |
182 | + | |
183 | + def delete_table(self, name): | |
184 | + req = RequestWithMethod("DELETE", "%s/Tables('%s')" % (self.get_base_url(), name)) | |
185 | + self._credentials.sign_table_request(req) | |
186 | + try: | |
187 | + response = urlopen(req) | |
188 | + return response.code | |
189 | + except URLError, e: | |
190 | + return e.code | |
191 | + | |
131 | 192 | def list_tables(self): |
132 | 193 | req = Request("%s/Tables" % self.get_base_url()) |
133 | 194 | self._credentials.sign_table_request(req) |
@@ -157,8 +218,9 @@ class TableStorage(Storage): | ||
157 | 218 | if t.lower() == 'edm.datetime': value = parse_edm_datetime(property.firstChild.data) |
158 | 219 | elif t.lower() == 'edm.int32': value = parse_edm_int32(property.firstChild.data) |
159 | 220 | elif t.lower() == 'edm.boolean': value = parse_edm_boolean(property.firstChild.data) |
221 | + elif t.lower() == 'edm.double': value = parse_edm_double(property.firstChild.data) | |
160 | 222 | else: raise Exception(t.lower()) |
161 | - else: value = property.firstChild.data | |
223 | + else: value = property.firstChild is not None and property.firstChild.data or None | |
162 | 224 | setattr(entity, key, value) |
163 | 225 | return entity |
164 | 226 |