Permalink
Browse files

Add SQSTileStore

  • Loading branch information...
twpayne committed Apr 5, 2012
1 parent 930c2d1 commit 976b85ddbfedb6bd27a9cfadfd7c4fcfc1cca052
Showing with 66 additions and 1 deletion.
  1. +1 −1 README.md
  2. +10 −0 tilecloud/__init__.py
  3. +55 −0 tilecloud/store/sqs.py
View
@@ -14,7 +14,7 @@ TileCloud is a powerful utility for generating, managing, transforming, visualis
* WMTS
-* Amazon S3
+* Amazon [S3](http://aws.amazon.com/s3/) and [SQS](http://aws.amazon.com/sqs/)
* [MBTiles](https://github.com/mapbox/mbtiles-spec)
View
@@ -387,6 +387,16 @@ def load(cls, name):
from tilecloud.store.s3 import S3TileStore
bucket, template = name[5:].split('/', 1)
return S3TileStore(bucket, TemplateTileLayout(template))
+ if name.startswith('sqs://'):
+ from tilecloud.store.sqs import SQSTileStore
+ import boto
+ import boto.sqs
+ from boto.sqs.jsonmessage import JSONMessage
+ region_name, queue_name = name[6:].split('/', 1)
+ connection = boto.sqs.connect_to_region(region_name)
+ queue = connection.create_queue(queue_name)
+ queue.set_message_class(JSONMessage)
+ return SQSTileStore(queue)
root, ext = os.path.splitext(name)
if ext == '.bsddb':
import bsddb
View
@@ -0,0 +1,55 @@
+import logging
+
+from boto.exception import SQSDecodeError, SQSError
+
+from tilecloud import Tile, TileCoord, TileStore
+
+
+logger = logging.getLogger(__name__)
+
+
+class SQSTileStore(TileStore):
+
+ def __init__(self, queue, **kwargs):
+ TileStore.__init__(self, **kwargs)
+ self.queue = queue
+
+ def __contains__(self, tile):
+ return False
+
+ def get_one(self, tile):
+ return tile
+
+ def list(self):
+ while True:
+ try:
+ sqs_message = self.queue.read()
+ if sqs_message is None:
+ break # FIXME or maybe retry?
+ z = sqs_message.get('z')
+ x = sqs_message.get('x')
+ y = sqs_message.get('y')
+ # FIXME deserialize other attributes
+ tile = Tile(TileCoord(z, x, y), sqs_message=sqs_message)
+ yield tile
+ except SQSDecodeError as e:
+ logger.warning(str(e))
+ sqs_message.delete()
+
+ def delete_one(self, tile):
+ assert hasattr(tile, 'sqs_message')
+ tile.sqs_message.delete()
+ delattr(tile, 'sqs_message')
+ return tile
+
+ def put_one(self, tile):
+ sqs_message = self.queue.new_message()
+ sqs_message['z'] = tile.tilecoord.z
+ sqs_message['x'] = tile.tilecoord.x
+ sqs_message['y'] = tile.tilecoord.y
+ # FIXME serialize other attributes
+ try:
+ self.queue.write(sqs_message)
+ tile.sqs_message = sqs_message
+ except SQSError as e:
+ tile.error = e

0 comments on commit 976b85d

Please sign in to comment.