# Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import boto
from boto.utils import find_class

__sdb = None
__domain = None
__s3_conn = None
        
def set_domain(domain_name, aws_access_key_id=None, aws_secret_access_key=None):
    """
    Set the domain in which persisted objects will be stored
    """
    global __sdb, __domain
    __sdb = boto.connect_sdb(aws_access_key_id=aws_access_key_id,
                             aws_secret_access_key=aws_secret_access_key)
    __domain = __sdb.lookup(domain_name)
    if not __domain:
        __domain = __sdb.create_domain(domain_name)

def get_domain():
    return __domain

def get_s3_connection():
    global __s3_conn
    if __s3_conn == None:
        __s3_conn = boto.connect_s3()
    return __s3_conn

def revive_object_from_id(id):
    domain = get_domain()
    attrs = domain.get_attributes(id, ['__module__', '__type__'])
    cls = find_class(attrs['__module__'], attrs['__type__'])
    return cls(id)

def object_lister(cls, query_lister):
    for item in query_lister:
        if cls:
            yield cls(item.name)
        else:
            yield revive_object_from_id(item.name)


@@ -1148,16 +1148,63 @@
 nd_class
+%0Afrom boto.exception import SDBPersistanceError
 %0A%0A__sdb 
@@ -1706,24 +1706,126 @@
 t_domain():%0A
+    if __domain == None:%0A        raise SDBPersistanceError('SimpleDB Domain for Persistence Not Set')%0A
     return _

Raise an exception if the domain has not been set.