1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
22
import StringIO, os, re
26
BotoConfigPath = '/etc/boto.cfg'
27
BotoConfigLocations = [BotoConfigPath]
28
if 'HOME' in os.environ:
29
UserConfigPath = os.path.expanduser('~/.boto')
30
BotoConfigLocations.append(UserConfigPath)
33
if 'BOTO_CONFIG' in os.environ:
34
BotoConfigLocations.append(os.path.expanduser(os.environ['BOTO_CONFIG']))
36
class Config(ConfigParser.SafeConfigParser):
38
def __init__(self, path=None, fp=None, do_load=True):
39
ConfigParser.SafeConfigParser.__init__(self, {'working_dir' : '/mnt/pyami',
43
self.load_from_path(path)
47
self.read(BotoConfigLocations)
48
if "AWS_CREDENTIAL_FILE" in os.environ:
49
self.load_credential_file(os.path.expanduser(os.environ['AWS_CREDENTIAL_FILE']))
51
def load_credential_file(self, path):
52
"""Load a credential file as is setup like the Java utilities"""
53
c_data = StringIO.StringIO()
54
c_data.write("[Credentials]\n")
55
for line in open(path, "r").readlines():
56
c_data.write(line.replace("AWSAccessKeyId", "aws_access_key_id").replace("AWSSecretKey", "aws_secret_access_key"))
60
def load_from_path(self, path):
62
for line in file.readlines():
63
match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line)
65
extended_file = match.group(1)
66
(dir, file) = os.path.split(path)
67
self.load_from_path(os.path.join(dir, extended_file))
70
def save_option(self, path, section, option, value):
72
Write the specified Section.Option to the config file specified by path.
73
Replace any previous value. If the path doesn't exist, create it.
74
Also add the option the the in-memory config.
76
config = ConfigParser.SafeConfigParser()
78
if not config.has_section(section):
79
config.add_section(section)
80
config.set(section, option, value)
84
if not self.has_section(section):
85
self.add_section(section)
86
self.set(section, option, value)
88
def save_user_option(self, section, option, value):
89
self.save_option(UserConfigPath, section, option, value)
91
def save_system_option(self, section, option, value):
92
self.save_option(BotoConfigPath, section, option, value)
94
def get_instance(self, name, default=None):
96
val = self.get('Instance', name)
101
def get_user(self, name, default=None):
103
val = self.get('User', name)
108
def getint_user(self, name, default=0):
110
val = self.getint('User', name)
115
def get_value(self, section, name, default=None):
116
return self.get(section, name, default)
118
def get(self, section, name, default=None):
120
val = ConfigParser.SafeConfigParser.get(self, section, name)
125
def getint(self, section, name, default=0):
127
val = ConfigParser.SafeConfigParser.getint(self, section, name)
132
def getfloat(self, section, name, default=0.0):
134
val = ConfigParser.SafeConfigParser.getfloat(self, section, name)
139
def getbool(self, section, name, default=False):
140
if self.has_option(section, name):
141
val = self.get(section, name)
142
if val.lower() == 'true':
150
def setbool(self, section, name, value):
152
self.set(section, name, 'true')
154
self.set(section, name, 'false')
157
s = StringIO.StringIO()
161
def dump_safe(self, fp=None):
163
fp = StringIO.StringIO()
164
for section in self.sections():
165
fp.write('[%s]\n' % section)
166
for option in self.options(section):
167
if option == 'aws_secret_access_key':
168
fp.write('%s = xxxxxxxxxxxxxxxxxx\n' % option)
170
fp.write('%s = %s\n' % (option, self.get(section, option)))
172
def dump_to_sdb(self, domain_name, item_name):
174
sdb = boto.connect_sdb()
175
domain = sdb.lookup(domain_name)
177
domain = sdb.create_domain(domain_name)
178
item = domain.new_item(item_name)
180
for section in self.sections():
182
for option in self.options(section):
183
d[option] = self.get(section, option)
184
item[section] = simplejson.dumps(d)
187
def load_from_sdb(self, domain_name, item_name):
189
sdb = boto.connect_sdb()
190
domain = sdb.lookup(domain_name)
191
item = domain.get_item(item_name)
192
for section in item.keys():
193
if not self.has_section(section):
194
self.add_section(section)
195
d = simplejson.loads(item[section])
196
for attr_name in d.keys():
197
attr_value = d[attr_name]
198
if attr_value == None:
200
if isinstance(attr_value, bool):
201
self.setbool(section, attr_name, attr_value)
203
self.set(section, attr_name, attr_value)