| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/ | |
| 2 # | |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a | |
| 4 # copy of this software and associated documentation files (the | |
| 5 # "Software"), to deal in the Software without restriction, including | |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- | |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit | |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- | |
| 9 # lowing conditions: | |
| 10 # | |
| 11 # The above copyright notice and this permission notice shall be included | |
| 12 # in all copies or substantial portions of the Software. | |
| 13 # | |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| 20 # IN THE SOFTWARE. | |
| 21 | |
| 22 import boto | |
| 23 from boto.utils import find_class | |
| 24 | |
| 25 class Manager(object): | |
| 26 | |
| 27 DefaultDomainName = boto.config.get('Persist', 'default_domain', None) | |
| 28 | |
| 29 def __init__(self, domain_name=None, aws_access_key_id=None, aws_secret_acce
ss_key=None, debug=0): | |
| 30 self.domain_name = domain_name | |
| 31 self.aws_access_key_id = aws_access_key_id | |
| 32 self.aws_secret_access_key = aws_secret_access_key | |
| 33 self.domain = None | |
| 34 self.sdb = None | |
| 35 self.s3 = None | |
| 36 if not self.domain_name: | |
| 37 self.domain_name = self.DefaultDomainName | |
| 38 if self.domain_name: | |
| 39 boto.log.info('No SimpleDB domain set, using default_domain: %s'
% self.domain_name) | |
| 40 else: | |
| 41 boto.log.warning('No SimpleDB domain set, persistance is disable
d') | |
| 42 if self.domain_name: | |
| 43 self.sdb = boto.connect_sdb(aws_access_key_id=self.aws_access_key_id
, | |
| 44 aws_secret_access_key=self.aws_secret_ac
cess_key, | |
| 45 debug=debug) | |
| 46 self.domain = self.sdb.lookup(self.domain_name) | |
| 47 if not self.domain: | |
| 48 self.domain = self.sdb.create_domain(self.domain_name) | |
| 49 | |
| 50 def get_s3_connection(self): | |
| 51 if not self.s3: | |
| 52 self.s3 = boto.connect_s3(self.aws_access_key_id, self.aws_secret_ac
cess_key) | |
| 53 return self.s3 | |
| 54 | |
| 55 def get_manager(domain_name=None, aws_access_key_id=None, aws_secret_access_key=
None, debug=0): | |
| 56 return Manager(domain_name, aws_access_key_id, aws_secret_access_key, debug=
debug) | |
| 57 | |
| 58 def set_domain(domain_name): | |
| 59 Manager.DefaultDomainName = domain_name | |
| 60 | |
| 61 def get_domain(): | |
| 62 return Manager.DefaultDomainName | |
| 63 | |
| 64 def revive_object_from_id(id, manager): | |
| 65 if not manager.domain: | |
| 66 return None | |
| 67 attrs = manager.domain.get_attributes(id, ['__module__', '__type__', '__line
age__']) | |
| 68 try: | |
| 69 cls = find_class(attrs['__module__'], attrs['__type__']) | |
| 70 return cls(id, manager=manager) | |
| 71 except ImportError: | |
| 72 return None | |
| 73 | |
| 74 def object_lister(cls, query_lister, manager): | |
| 75 for item in query_lister: | |
| 76 if cls: | |
| 77 yield cls(item.name) | |
| 78 else: | |
| 79 o = revive_object_from_id(item.name, manager) | |
| 80 if o: | |
| 81 yield o | |
| 82 | |
| 83 | |
| OLD | NEW |