winazurestorageのフォーク
Revision | 9008da70a682554b06c475c2ac21cfdca9f2ab3f (tree) |
---|---|
Zeit | 2012-04-20 17:09:56 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
fix tablestorage: encoding error when key has unicode value
@@ -15,7 +15,7 @@ import os | ||
15 | 15 | from xml.dom import minidom #TODO: Use a faster way of processing XML |
16 | 16 | import re |
17 | 17 | from urllib2 import Request, urlopen, URLError |
18 | -from urllib import urlencode | |
18 | +from urllib import urlencode, quote | |
19 | 19 | from urlparse import urlsplit, parse_qs |
20 | 20 | from datetime import datetime, timedelta |
21 | 21 |
@@ -158,7 +158,7 @@ class TableEntityException(Exception): | ||
158 | 158 | |
159 | 159 | class TableEntity(object): |
160 | 160 | "Table Entity" |
161 | - def __init__(self, partition_key, row_key, props): | |
161 | + def __init__(self, partition_key="", row_key="", props={}): | |
162 | 162 | self.partition_key = partition_key |
163 | 163 | self.row_key = row_key |
164 | 164 | self.properties = props |
@@ -186,7 +186,7 @@ class TableEntity(object): | ||
186 | 186 | def to_insert_xml(self): |
187 | 187 | contents = [self._make_property_node(propname, self.properties[propname]) for propname in self.properties] |
188 | 188 | contents_str = "\n".join(contents) |
189 | - now_str = datetime.utcnow().isoformat() | |
189 | + now_str = datetime.utcnow().isoformat() + "Z" | |
190 | 190 | xml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?> |
191 | 191 | <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"> |
192 | 192 | <title /> |
@@ -421,7 +421,7 @@ class TableStorage(Storage): | ||
421 | 421 | def insert_entity(self, table_name, entity): |
422 | 422 | data = entity.to_insert_xml() |
423 | 423 | url = "%s/%s" % (self.get_base_url(), table_name) |
424 | - req = winazurestorage.RequestWithMethod("POST", url, data=data) | |
424 | + req = RequestWithMethod("POST", url, data=data) | |
425 | 425 | req.add_header("Content-Length", "%d" % len(data)) |
426 | 426 | req.add_header("Content-Type", "application/atom+xml") |
427 | 427 | self._credentials.sign_table_request(req) |
@@ -434,8 +434,10 @@ class TableStorage(Storage): | ||
434 | 434 | def update_entity(self, table_name, partition_key, row_key, entity): |
435 | 435 | data = entity.to_update_xml() |
436 | 436 | url = """%s/%s(PartitionKey='%s',RowKey='%s')""" % (self.get_base_url(), table_name, partition_key, row_key) |
437 | + if isinstance(url, unicode): | |
438 | + url = url.encode('utf-8') | |
437 | 439 | |
438 | - req = winazurestorage.RequestWithMethod("PUT", url, data=data) | |
440 | + req = RequestWithMethod("PUT", url, data=data) | |
439 | 441 | req.add_header("Content-Length", "%d" % len(data)) |
440 | 442 | req.add_header("Content-Type", "application/atom+xml") |
441 | 443 | self._credentials.sign_table_request(req) |
@@ -443,13 +445,14 @@ class TableStorage(Storage): | ||
443 | 445 | response = urlopen(req) |
444 | 446 | return response.code |
445 | 447 | except URLError, e: |
446 | - print data | |
447 | 448 | return e.code |
448 | 449 | |
449 | 450 | def merge_entity(self, table_name, partition_key, row_key, entity): |
450 | 451 | data = entity.to_update_xml() |
451 | 452 | url = """%s/%s(PartitionKey='%s',RowKey='%s')""" % (self.get_base_url(), table_name, partition_key, row_key) |
452 | - req = winazurestorage.RequestWithMethod("MERGE", url, data=data) | |
453 | + if isinstance(url, unicode): | |
454 | + url = url.encode('utf-8') | |
455 | + req = RequestWithMethod("MERGE", url, data=data) | |
453 | 456 | req.add_header("Content-Length", "%d" % len(data)) |
454 | 457 | req.add_header("Content-Type", "application/atom+xml") |
455 | 458 | self._credentials.sign_table_request(req) |
@@ -462,7 +465,9 @@ class TableStorage(Storage): | ||
462 | 465 | def delete_entity(self, table_name, partition_key, row_key, condition="*"): |
463 | 466 | data = "" |
464 | 467 | url = """%s/%s(PartitionKey='%s',RowKey='%s')""" % (self.get_base_url(), table_name, partition_key, row_key) |
465 | - req = winazurestorage.RequestWithMethod("DELETE", url, data) | |
468 | + if isinstance(url, unicode): | |
469 | + url = url.encode('utf-8') | |
470 | + req = RequestWithMethod("DELETE", url, data) | |
466 | 471 | req.add_header("Content-Length", "%d" % len(data)) |
467 | 472 | req.add_header("Content-Type", "application/atom+xml") |
468 | 473 | req.add_header("If-Match", condition) |
@@ -474,9 +479,11 @@ class TableStorage(Storage): | ||
474 | 479 | return e.code |
475 | 480 | |
476 | 481 | def query_entity(self, table_name, filter): |
477 | - quoted_filter = urllib.quote(filter) | |
482 | + quoted_filter = quote(filter) | |
478 | 483 | url = """%s/%s()?$filter=%s""" % (self.get_base_url(), table_name, quoted_filter) |
479 | - req = winazurestorage.RequestWithMethod("GET", url) | |
484 | + if isinstance(url, unicode): | |
485 | + url = url.encode('utf-8') | |
486 | + req = RequestWithMethod("GET", url) | |
480 | 487 | self._credentials.sign_table_request(req) |
481 | 488 | try: |
482 | 489 | resp = urlopen(req) |
@@ -493,7 +500,9 @@ class TableStorage(Storage): | ||
493 | 500 | |
494 | 501 | def top_entity(self, table_name, size): |
495 | 502 | url = """%s/%s()?$top=%s""" % (self.get_base_url(), table_name, size) |
496 | - req = winazurestorage.RequestWithMethod("GET", url) | |
503 | + if isinstance(url, unicode): | |
504 | + url = url.encode('utf-8') | |
505 | + req = RequestWithMethod("GET", url) | |
497 | 506 | self._credentials.sign_table_request(req) |
498 | 507 | |
499 | 508 | try: |