1# Copyright (c) 2006,2007 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 22from boto.pyami.config import Config 23from boto.services.message import ServiceMessage 24import boto 25 26class ServiceDef(Config): 27 28 def __init__(self, config_file, aws_access_key_id=None, aws_secret_access_key=None): 29 super(ServiceDef, self).__init__(config_file) 30 self.aws_access_key_id = aws_access_key_id 31 self.aws_secret_access_key = aws_secret_access_key 32 script = Config.get(self, 'Pyami', 'scripts') 33 if script: 34 self.name = script.split('.')[-1] 35 else: 36 self.name = None 37 38 39 def get(self, name, default=None): 40 return super(ServiceDef, self).get(self.name, name, default) 41 42 def has_option(self, option): 43 return super(ServiceDef, self).has_option(self.name, option) 44 45 def getint(self, option, default=0): 46 try: 47 val = super(ServiceDef, self).get(self.name, option) 48 val = int(val) 49 except: 50 val = int(default) 51 return val 52 53 def getbool(self, option, default=False): 54 try: 55 val = super(ServiceDef, self).get(self.name, option) 56 if val.lower() == 'true': 57 val = True 58 else: 59 val = False 60 except: 61 val = default 62 return val 63 64 def get_obj(self, name): 65 """ 66 Returns the AWS object associated with a given option. 67 68 The heuristics used are a bit lame. If the option name contains 69 the word 'bucket' it is assumed to be an S3 bucket, if the name 70 contains the word 'queue' it is assumed to be an SQS queue and 71 if it contains the word 'domain' it is assumed to be a SimpleDB 72 domain. If the option name specified does not exist in the 73 config file or if the AWS object cannot be retrieved this 74 returns None. 75 """ 76 val = self.get(name) 77 if not val: 78 return None 79 if name.find('queue') >= 0: 80 obj = boto.lookup('sqs', val) 81 if obj: 82 obj.set_message_class(ServiceMessage) 83 elif name.find('bucket') >= 0: 84 obj = boto.lookup('s3', val) 85 elif name.find('domain') >= 0: 86 obj = boto.lookup('sdb', val) 87 else: 88 obj = None 89 return obj 90 91 92