PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/services/servicedef.py

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