Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/boto/pyami/config.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac |
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:26e78fe6e8c4 |
|---|---|
| 1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ | |
| 2 # Copyright (c) 2011 Chris Moyer http://coredumped.org/ | |
| 3 # | |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | |
| 5 # copy of this software and associated documentation files (the | |
| 6 # "Software"), to deal in the Software without restriction, including | |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit | |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- | |
| 10 # lowing conditions: | |
| 11 # | |
| 12 # The above copyright notice and this permission notice shall be included | |
| 13 # in all copies or substantial portions of the Software. | |
| 14 # | |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| 21 # IN THE SOFTWARE. | |
| 22 # | |
| 23 import os | |
| 24 import re | |
| 25 import warnings | |
| 26 | |
| 27 import boto | |
| 28 | |
| 29 from boto.compat import expanduser, ConfigParser, NoOptionError, NoSectionError, StringIO | |
| 30 | |
| 31 | |
| 32 # By default we use two locations for the boto configurations, | |
| 33 # /etc/boto.cfg and ~/.boto (which works on Windows and Unix). | |
| 34 BotoConfigPath = '/etc/boto.cfg' | |
| 35 BotoConfigLocations = [BotoConfigPath] | |
| 36 UserConfigPath = os.path.join(expanduser('~'), '.boto') | |
| 37 BotoConfigLocations.append(UserConfigPath) | |
| 38 | |
| 39 # If there's a BOTO_CONFIG variable set, we load ONLY | |
| 40 # that variable | |
| 41 if 'BOTO_CONFIG' in os.environ: | |
| 42 BotoConfigLocations = [expanduser(os.environ['BOTO_CONFIG'])] | |
| 43 | |
| 44 # If there's a BOTO_PATH variable set, we use anything there | |
| 45 # as the current configuration locations, split with os.pathsep. | |
| 46 elif 'BOTO_PATH' in os.environ: | |
| 47 BotoConfigLocations = [] | |
| 48 for path in os.environ['BOTO_PATH'].split(os.pathsep): | |
| 49 BotoConfigLocations.append(expanduser(path)) | |
| 50 | |
| 51 | |
| 52 class Config(object): | |
| 53 | |
| 54 def __init__(self, path=None, fp=None, do_load=True): | |
| 55 self._parser = ConfigParser({'working_dir': '/mnt/pyami', | |
| 56 'debug': '0'}) | |
| 57 if do_load: | |
| 58 if path: | |
| 59 self.load_from_path(path) | |
| 60 elif fp: | |
| 61 self.readfp(fp) | |
| 62 else: | |
| 63 self.read(BotoConfigLocations) | |
| 64 if "AWS_CREDENTIAL_FILE" in os.environ: | |
| 65 full_path = expanduser(os.environ['AWS_CREDENTIAL_FILE']) | |
| 66 try: | |
| 67 self.load_credential_file(full_path) | |
| 68 except IOError: | |
| 69 warnings.warn('Unable to load AWS_CREDENTIAL_FILE (%s)' % full_path) | |
| 70 | |
| 71 def __setstate__(self, state): | |
| 72 # There's test that verify that (transitively) a Config | |
| 73 # object can be pickled. Now that we're storing a _parser | |
| 74 # attribute and relying on __getattr__ to proxy requests, | |
| 75 # we need to implement setstate to ensure we don't get | |
| 76 # into recursive loops when looking up _parser when | |
| 77 # this object is unpickled. | |
| 78 self._parser = state['_parser'] | |
| 79 | |
| 80 def __getattr__(self, name): | |
| 81 return getattr(self._parser, name) | |
| 82 | |
| 83 def has_option(self, *args, **kwargs): | |
| 84 return self._parser.has_option(*args, **kwargs) | |
| 85 | |
| 86 def load_credential_file(self, path): | |
| 87 """Load a credential file as is setup like the Java utilities""" | |
| 88 c_data = StringIO() | |
| 89 c_data.write("[Credentials]\n") | |
| 90 for line in open(path, "r").readlines(): | |
| 91 c_data.write(line.replace("AWSAccessKeyId", "aws_access_key_id").replace("AWSSecretKey", "aws_secret_access_key")) | |
| 92 c_data.seek(0) | |
| 93 self.readfp(c_data) | |
| 94 | |
| 95 def load_from_path(self, path): | |
| 96 file = open(path) | |
| 97 for line in file.readlines(): | |
| 98 match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line) | |
| 99 if match: | |
| 100 extended_file = match.group(1) | |
| 101 (dir, file) = os.path.split(path) | |
| 102 self.load_from_path(os.path.join(dir, extended_file)) | |
| 103 self.read(path) | |
| 104 | |
| 105 def save_option(self, path, section, option, value): | |
| 106 """ | |
| 107 Write the specified Section.Option to the config file specified by path. | |
| 108 Replace any previous value. If the path doesn't exist, create it. | |
| 109 Also add the option the the in-memory config. | |
| 110 """ | |
| 111 config = ConfigParser() | |
| 112 config.read(path) | |
| 113 if not config.has_section(section): | |
| 114 config.add_section(section) | |
| 115 config.set(section, option, value) | |
| 116 fp = open(path, 'w') | |
| 117 config.write(fp) | |
| 118 fp.close() | |
| 119 if not self.has_section(section): | |
| 120 self.add_section(section) | |
| 121 self.set(section, option, value) | |
| 122 | |
| 123 def save_user_option(self, section, option, value): | |
| 124 self.save_option(UserConfigPath, section, option, value) | |
| 125 | |
| 126 def save_system_option(self, section, option, value): | |
| 127 self.save_option(BotoConfigPath, section, option, value) | |
| 128 | |
| 129 def get_instance(self, name, default=None): | |
| 130 try: | |
| 131 val = self.get('Instance', name) | |
| 132 except (NoOptionError, NoSectionError): | |
| 133 val = default | |
| 134 return val | |
| 135 | |
| 136 def get_user(self, name, default=None): | |
| 137 try: | |
| 138 val = self.get('User', name) | |
| 139 except (NoOptionError, NoSectionError): | |
| 140 val = default | |
| 141 return val | |
| 142 | |
| 143 def getint_user(self, name, default=0): | |
| 144 try: | |
| 145 val = self.getint('User', name) | |
| 146 except (NoOptionError, NoSectionError): | |
| 147 val = default | |
| 148 return val | |
| 149 | |
| 150 def get_value(self, section, name, default=None): | |
| 151 return self.get(section, name, default) | |
| 152 | |
| 153 def get(self, section, name, default=None): | |
| 154 try: | |
| 155 return self._parser.get(section, name) | |
| 156 except (NoOptionError, NoSectionError): | |
| 157 return default | |
| 158 | |
| 159 def getint(self, section, name, default=0): | |
| 160 try: | |
| 161 return self._parser.getint(section, name) | |
| 162 except (NoOptionError, NoSectionError): | |
| 163 return int(default) | |
| 164 | |
| 165 def getfloat(self, section, name, default=0.0): | |
| 166 try: | |
| 167 return self._parser.getfloat(section, name) | |
| 168 except (NoOptionError, NoSectionError): | |
| 169 return float(default) | |
| 170 | |
| 171 def getbool(self, section, name, default=False): | |
| 172 if self.has_option(section, name): | |
| 173 val = self.get(section, name) | |
| 174 if val.lower() == 'true': | |
| 175 val = True | |
| 176 else: | |
| 177 val = False | |
| 178 else: | |
| 179 val = default | |
| 180 return val | |
| 181 | |
| 182 def setbool(self, section, name, value): | |
| 183 if value: | |
| 184 self.set(section, name, 'true') | |
| 185 else: | |
| 186 self.set(section, name, 'false') | |
| 187 | |
| 188 def dump(self): | |
| 189 s = StringIO() | |
| 190 self.write(s) | |
| 191 print(s.getvalue()) | |
| 192 | |
| 193 def dump_safe(self, fp=None): | |
| 194 if not fp: | |
| 195 fp = StringIO() | |
| 196 for section in self.sections(): | |
| 197 fp.write('[%s]\n' % section) | |
| 198 for option in self.options(section): | |
| 199 if option == 'aws_secret_access_key': | |
| 200 fp.write('%s = xxxxxxxxxxxxxxxxxx\n' % option) | |
| 201 else: | |
| 202 fp.write('%s = %s\n' % (option, self.get(section, option))) | |
| 203 | |
| 204 def dump_to_sdb(self, domain_name, item_name): | |
| 205 from boto.compat import json | |
| 206 sdb = boto.connect_sdb() | |
| 207 domain = sdb.lookup(domain_name) | |
| 208 if not domain: | |
| 209 domain = sdb.create_domain(domain_name) | |
| 210 item = domain.new_item(item_name) | |
| 211 item.active = False | |
| 212 for section in self.sections(): | |
| 213 d = {} | |
| 214 for option in self.options(section): | |
| 215 d[option] = self.get(section, option) | |
| 216 item[section] = json.dumps(d) | |
| 217 item.save() | |
| 218 | |
| 219 def load_from_sdb(self, domain_name, item_name): | |
| 220 from boto.compat import json | |
| 221 sdb = boto.connect_sdb() | |
| 222 domain = sdb.lookup(domain_name) | |
| 223 item = domain.get_item(item_name) | |
| 224 for section in item.keys(): | |
| 225 if not self.has_section(section): | |
| 226 self.add_section(section) | |
| 227 d = json.loads(item[section]) | |
| 228 for attr_name in d.keys(): | |
| 229 attr_value = d[attr_name] | |
| 230 if attr_value is None: | |
| 231 attr_value = 'None' | |
| 232 if isinstance(attr_value, bool): | |
| 233 self.setbool(section, attr_name, attr_value) | |
| 234 else: | |
| 235 self.set(section, attr_name, attr_value) |
