/usr/lib/python2.7/dist-packages/twistedcaldav/config.py is in calendarserver 5.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | ##
# Copyright (c) 2005-2014 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
__all__ = [
"Config",
"ConfigDict",
"ConfigProvider",
"ConfigurationError",
"config",
]
import os
import copy
class ConfigurationError(RuntimeError):
"""
Invalid server configuration.
"""
class ConfigDict(dict):
"""
Dictionary which can be accessed using attribute syntax, because
that reads and writes nicer in code. For example:
C{config.Thingo.Tiny.Tweak}
instead of:
C{config["Thingo"]["Tiny"]["Tweak"]}
"""
def __init__(self, mapping=None):
if mapping is not None:
for key, value in mapping.iteritems():
self[key] = value
def __repr__(self):
return "*" + dict.__repr__(self)
def __setitem__(self, key, value):
if key.startswith("_"):
# Names beginning with "_" are reserved for real attributes
raise KeyError("Keys may not begin with '_': %s" % (key,))
if isinstance(value, dict) and not isinstance(value, self.__class__):
dict.__setitem__(self, key, self.__class__(value))
else:
dict.__setitem__(self, key, value)
def __setattr__(self, attr, value):
if attr.startswith("_"):
dict.__setattr__(self, attr, value)
else:
self[attr] = value
def __getattr__(self, attr):
if not attr.startswith("_") and attr in self:
return self[attr]
else:
return dict.__getattribute__(self, attr)
def __delattr__(self, attr):
if not attr.startswith("_") and attr in self:
del self[attr]
else:
dict.__delattr__(self, attr)
class ConfigProvider(object):
"""
Configuration provider, abstraction for config storage/format/defaults.
"""
def __init__(self, defaults=None):
"""
Create configuration provider with given defaults.
"""
self._configFileName = None
if defaults is None:
self._defaults = ConfigDict()
else:
self._defaults = ConfigDict(copy.deepcopy(defaults))
def getDefaults(self):
"""
Return defaults.
"""
return self._defaults
def setDefaults(self, defaults):
"""
Change defaults.
"""
self._defaults = ConfigDict(copy.deepcopy(defaults))
def getConfigFileName(self):
"""
Return current configuration file path and name.
"""
return self._configFileName
def setConfigFileName(self, configFileName):
"""
Change configuration file path and name for next load operations.
"""
self._configFileName = configFileName
if self._configFileName:
self._configFileName = os.path.abspath(configFileName)
def hasErrors(self):
"""
Return true if last load operation encountered any errors.
"""
return False
def loadConfig(self):
"""
Load the configuration, return a dictionary of settings.
"""
return self._defaults
class Config(object):
def __init__(self, provider=None):
if not provider:
self._provider = ConfigProvider()
else:
self._provider = provider
self._updating = False
self._beforeResetHook = None
self._afterResetHook = None
self._preUpdateHooks = []
self._postUpdateHooks = []
self.reset()
def __setattr__(self, attr, value):
if "_data" in self.__dict__ and attr in self.__dict__["_data"]:
self._data[attr] = value
else:
self.__dict__[attr] = value
# So as not to cause a flurry of updates, don't mark ourselves
# dirty when the attribute begins with an underscore
if not attr.startswith("_"):
self.__dict__["_dirty"] = True
_dirty = False
_data = ()
def __getattr__(self, attr):
if self._dirty:
self.update()
if attr in self._data:
return self._data[attr]
raise AttributeError(attr)
def __hasattr__(self, attr):
return attr in self._data
def __str__(self):
return str(self._data)
def get(self, attr, defaultValue):
parts = attr.split(".")
lastDict = self._data
for part in parts[:-1]:
if not part in lastDict:
lastDict[attr] = ConfigDict()
lastDict = lastDict.__getattr__(part)
configItem = parts[-1]
if configItem in lastDict:
return lastDict[configItem]
else:
lastDict[configItem] = defaultValue
return defaultValue
def addResetHooks(self, before, after):
"""
Hooks for preserving config across reload( ) + reset( )
Each hook will be passed the config data; whatever the before hook
returns will be passed as the second arg to the after hook.
"""
self._beforeResetHook = before
self._afterResetHook = after
def addPreUpdateHooks(self, hooks):
self._preUpdateHooks.extend(hooks)
def addPostUpdateHooks(self, hooks):
self._postUpdateHooks.extend(hooks)
def getProvider(self):
return self._provider
def setProvider(self, provider):
self._provider = provider
self.reset()
def setDefaults(self, defaults):
self._provider.setDefaults(defaults)
self.reset()
def updateDefaults(self, items):
mergeData(self._provider.getDefaults(), items)
self.update(items)
def update(self, items=None, reloading=False):
if self._updating:
return
self._updating = True
if not isinstance(items, ConfigDict):
items = ConfigDict(items)
# Call hooks
for hook in self._preUpdateHooks:
hook(self._data, items, reloading=reloading)
mergeData(self._data, items)
for hook in self._postUpdateHooks:
hook(self._data, reloading=reloading)
self._updating = False
self._dirty = False
def load(self, configFile):
self._provider.setConfigFileName(configFile)
configDict = self._provider.loadConfig()
if not self._provider.hasErrors():
self.update(configDict)
else:
raise ConfigurationError("Invalid configuration in %s"
% (self._provider.getConfigFileName(),))
def reload(self):
configDict = self._provider.loadConfig()
if not self._provider.hasErrors():
if self._beforeResetHook:
# Give the beforeResetHook a chance to stash away values we want
# to preserve across the reload( )
preserved = self._beforeResetHook(self._data)
else:
preserved = None
self.reset()
if preserved and self._afterResetHook:
# Pass the preserved data back to the afterResetHook
self._afterResetHook(self._data, preserved)
self.update(configDict, reloading=True)
else:
raise ConfigurationError("Invalid configuration in %s"
% (self._provider.getConfigFileName(), ))
def reset(self):
self._data = ConfigDict(copy.deepcopy(self._provider.getDefaults()))
self._dirty = True
def mergeData(oldData, newData):
"""
Merge two ConfigDict objects; oldData will be updated with all the keys
and values from newData
@param oldData: the object to modify
@type oldData: ConfigDict
@param newData: the object to copy data from
@type newData: ConfigDict
"""
for key, value in newData.iteritems():
if isinstance(value, (dict,)):
if key in oldData:
assert isinstance(oldData[key], ConfigDict), \
"%r in %r is not a ConfigDict" % (oldData[key], oldData)
else:
oldData[key] = {}
mergeData(oldData[key], value)
else:
oldData[key] = value
def fullServerPath(base, path):
if type(path) is str:
return os.path.join(base, path) if path and path[0] not in ('/', '.',) else path
else:
return path
config = Config()
|