Annotation of /linden_release/linden/indra/lib/python/indra/base/config.py
Parent Directory
|
Revision Log
Revision 100 - (view) (download) (as text)
| 1 : | mjm | 57 | """\ |
| 2 : | @file config.py | ||
| 3 : | @brief Utility module for parsing and accessing the indra.xml config file. | ||
| 4 : | |||
| 5 : | $LicenseInfo:firstyear=2006&license=mit$ | ||
| 6 : | |||
| 7 : | mjm | 100 | Copyright (c) 2006-2009, Linden Research, Inc. |
| 8 : | mjm | 57 | |
| 9 : | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 10 : | of this software and associated documentation files (the "Software"), to deal | ||
| 11 : | in the Software without restriction, including without limitation the rights | ||
| 12 : | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 13 : | copies of the Software, and to permit persons to whom the Software is | ||
| 14 : | furnished to do so, subject to the following conditions: | ||
| 15 : | |||
| 16 : | The above copyright notice and this permission notice shall be included in | ||
| 17 : | all copies or substantial portions of the Software. | ||
| 18 : | |||
| 19 : | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 20 : | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 21 : | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 22 : | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 23 : | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 24 : | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 25 : | THE SOFTWARE. | ||
| 26 : | $/LicenseInfo$ | ||
| 27 : | """ | ||
| 28 : | |||
| 29 : | import copy | ||
| 30 : | import os | ||
| 31 : | import traceback | ||
| 32 : | import time | ||
| 33 : | import types | ||
| 34 : | |||
| 35 : | from os.path import dirname, getmtime, join, realpath | ||
| 36 : | from indra.base import llsd | ||
| 37 : | |||
| 38 : | _g_config = None | ||
| 39 : | |||
| 40 : | class IndraConfig(object): | ||
| 41 : | """ | ||
| 42 : | IndraConfig loads a 'indra' xml configuration file | ||
| 43 : | and loads into memory. This representation in memory | ||
| 44 : | can get updated to overwrite values or add new values. | ||
| 45 : | |||
| 46 : | The xml configuration file is considered a live file and changes | ||
| 47 : | to the file are checked and reloaded periodically. If a value had | ||
| 48 : | been overwritten via the update or set method, the loaded values | ||
| 49 : | from the file are ignored (the values from the update/set methods | ||
| 50 : | override) | ||
| 51 : | """ | ||
| 52 : | def __init__(self, indra_config_file): | ||
| 53 : | self._indra_config_file = indra_config_file | ||
| 54 : | self._reload_check_interval = 30 # seconds | ||
| 55 : | self._last_check_time = 0 | ||
| 56 : | self._last_mod_time = 0 | ||
| 57 : | |||
| 58 : | self._config_overrides = {} | ||
| 59 : | self._config_file_dict = {} | ||
| 60 : | self._combined_dict = {} | ||
| 61 : | |||
| 62 : | self._load() | ||
| 63 : | |||
| 64 : | def _load(self): | ||
| 65 : | if self._indra_config_file is None: | ||
| 66 : | return | ||
| 67 : | |||
| 68 : | config_file = open(self._indra_config_file) | ||
| 69 : | self._config_file_dict = llsd.parse(config_file.read()) | ||
| 70 : | self._combine_dictionaries() | ||
| 71 : | config_file.close() | ||
| 72 : | |||
| 73 : | self._last_mod_time = self._get_last_modified_time() | ||
| 74 : | self._last_check_time = time.time() # now | ||
| 75 : | |||
| 76 : | def _get_last_modified_time(self): | ||
| 77 : | """ | ||
| 78 : | Returns the mtime (last modified time) of the config file, | ||
| 79 : | if such exists. | ||
| 80 : | """ | ||
| 81 : | if self._indra_config_file is not None: | ||
| 82 : | return os.path.getmtime(self._indra_config_file) | ||
| 83 : | |||
| 84 : | return 0 | ||
| 85 : | |||
| 86 : | def _combine_dictionaries(self): | ||
| 87 : | self._combined_dict = {} | ||
| 88 : | self._combined_dict.update(self._config_file_dict) | ||
| 89 : | self._combined_dict.update(self._config_overrides) | ||
| 90 : | |||
| 91 : | def _reload_if_necessary(self): | ||
| 92 : | now = time.time() | ||
| 93 : | |||
| 94 : | if (now - self._last_check_time) > self._reload_check_interval: | ||
| 95 : | self._last_check_time = now | ||
| 96 : | try: | ||
| 97 : | modtime = self._get_last_modified_time() | ||
| 98 : | if modtime > self._last_mod_time: | ||
| 99 : | self._load() | ||
| 100 : | except OSError, e: | ||
| 101 : | if e.errno == errno.ENOENT: # file not found | ||
| 102 : | # someone messed with our internal state | ||
| 103 : | # or removed the file | ||
| 104 : | |||
| 105 : | print 'WARNING: Configuration file has been removed ' + (self._indra_config_file) | ||
| 106 : | print 'Disabling reloading of configuration file.' | ||
| 107 : | |||
| 108 : | traceback.print_exc() | ||
| 109 : | |||
| 110 : | self._indra_config_file = None | ||
| 111 : | self._last_check_time = 0 | ||
| 112 : | self._last_mod_time = 0 | ||
| 113 : | else: | ||
| 114 : | raise # pass the exception along to the caller | ||
| 115 : | |||
| 116 : | def __getitem__(self, key): | ||
| 117 : | self._reload_if_necessary() | ||
| 118 : | |||
| 119 : | return self._combined_dict[key] | ||
| 120 : | |||
| 121 : | def get(self, key, default = None): | ||
| 122 : | try: | ||
| 123 : | return self.__getitem__(key) | ||
| 124 : | except KeyError: | ||
| 125 : | return default | ||
| 126 : | |||
| 127 : | def __setitem__(self, key, value): | ||
| 128 : | """ | ||
| 129 : | Sets the value of the config setting of key to be newval | ||
| 130 : | |||
| 131 : | Once any key/value pair is changed via the set method, | ||
| 132 : | that key/value pair will remain set with that value until | ||
| 133 : | change via the update or set method | ||
| 134 : | """ | ||
| 135 : | self._config_overrides[key] = value | ||
| 136 : | self._combine_dictionaries() | ||
| 137 : | |||
| 138 : | def set(self, key, newval): | ||
| 139 : | return self.__setitem__(key, newval) | ||
| 140 : | |||
| 141 : | def update(self, new_conf): | ||
| 142 : | """ | ||
| 143 : | Load an XML file and apply its map as overrides or additions | ||
| 144 : | to the existing config. Update can be a file or a dict. | ||
| 145 : | |||
| 146 : | Once any key/value pair is changed via the update method, | ||
| 147 : | that key/value pair will remain set with that value until | ||
| 148 : | change via the update or set method | ||
| 149 : | """ | ||
| 150 : | if isinstance(new_conf, dict): | ||
| 151 : | overrides = new_conf | ||
| 152 : | else: | ||
| 153 : | # assuming that it is a filename | ||
| 154 : | config_file = open(new_conf) | ||
| 155 : | overrides = llsd.parse(config_file.read()) | ||
| 156 : | config_file.close() | ||
| 157 : | |||
| 158 : | self._config_overrides.update(overrides) | ||
| 159 : | self._combine_dictionaries() | ||
| 160 : | |||
| 161 : | def as_dict(self): | ||
| 162 : | """ | ||
| 163 : | Returns immutable copy of the IndraConfig as a dictionary | ||
| 164 : | """ | ||
| 165 : | return copy.deepcopy(self._combined_dict) | ||
| 166 : | |||
| 167 : | def load(indra_xml_file = None): | ||
| 168 : | global _g_config | ||
| 169 : | |||
| 170 : | if indra_xml_file is None: | ||
| 171 : | ## going from: | ||
| 172 : | ## "/opt/linden/indra/lib/python/indra/base/config.py" | ||
| 173 : | ## to: | ||
| 174 : | ## "/opt/linden/etc/indra.xml" | ||
| 175 : | indra_xml_file = realpath( | ||
| 176 : | dirname(realpath(__file__)) + "../../../../../../etc/indra.xml") | ||
| 177 : | |||
| 178 : | try: | ||
| 179 : | _g_config = IndraConfig(indra_xml_file) | ||
| 180 : | except IOError: | ||
| 181 : | # indra.xml was not openable, so let's initialize with an empty dict | ||
| 182 : | # some code relies on config behaving this way | ||
| 183 : | _g_config = IndraConfig(None) | ||
| 184 : | |||
| 185 : | def dump(indra_xml_file, indra_cfg = None, update_in_mem=False): | ||
| 186 : | ''' | ||
| 187 : | Dump config contents into a file | ||
| 188 : | Kindof reverse of load. | ||
| 189 : | Optionally takes a new config to dump. | ||
| 190 : | Does NOT update global config unless requested. | ||
| 191 : | ''' | ||
| 192 : | global _g_config | ||
| 193 : | |||
| 194 : | if not indra_cfg: | ||
| 195 : | if _g_config is None: | ||
| 196 : | return | ||
| 197 : | |||
| 198 : | indra_cfg = _g_config.as_dict() | ||
| 199 : | |||
| 200 : | if not indra_cfg: | ||
| 201 : | return | ||
| 202 : | |||
| 203 : | config_file = open(indra_xml_file, 'w') | ||
| 204 : | _config_xml = llsd.format_xml(indra_cfg) | ||
| 205 : | config_file.write(_config_xml) | ||
| 206 : | config_file.close() | ||
| 207 : | |||
| 208 : | if update_in_mem: | ||
| 209 : | update(indra_cfg) | ||
| 210 : | |||
| 211 : | def update(new_conf): | ||
| 212 : | global _g_config | ||
| 213 : | |||
| 214 : | if _g_config is None: | ||
| 215 : | # To keep with how this function behaved | ||
| 216 : | # previously, a call to update | ||
| 217 : | # before the global is defined | ||
| 218 : | # make a new global config which does not | ||
| 219 : | # load data from a file. | ||
| 220 : | _g_config = IndraConfig(None) | ||
| 221 : | |||
| 222 : | return _g_config.update(new_conf) | ||
| 223 : | |||
| 224 : | def get(key, default = None): | ||
| 225 : | global _g_config | ||
| 226 : | |||
| 227 : | if _g_config is None: | ||
| 228 : | load() | ||
| 229 : | |||
| 230 : | return _g_config.get(key, default) | ||
| 231 : | |||
| 232 : | def set(key, newval): | ||
| 233 : | """ | ||
| 234 : | Sets the value of the config setting of key to be newval | ||
| 235 : | |||
| 236 : | Once any key/value pair is changed via the set method, | ||
| 237 : | that key/value pair will remain set with that value until | ||
| 238 : | change via the update or set method or program termination | ||
| 239 : | """ | ||
| 240 : | global _g_config | ||
| 241 : | |||
| 242 : | if _g_config is None: | ||
| 243 : | _g_config = IndraConfig(None) | ||
| 244 : | |||
| 245 : | _g_config.set(key, newval) | ||
| 246 : | |||
| 247 : | def get_config(): | ||
| 248 : | global _g_config | ||
| 249 : | return _g_config |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

