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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
from __future__ import annotations
import os
import warnings
from collections import defaultdict
from json import JSONDecoder
from typing import Any
from typing import Dict
from typing import Iterator
from typing import List
from typing import Optional
from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union
if TYPE_CHECKING: # pragma: no cover
from dynaconf.utils.boxing import DynaBox
from dynaconf.base import LazySettings, Settings
BANNER = """
██████╗ ██╗ ██╗███╗ ██╗ █████╗ ██████╗ ██████╗ ███╗ ██╗███████╗
██╔══██╗╚██╗ ██╔╝████╗ ██║██╔══██╗██╔════╝██╔═══██╗████╗ ██║██╔════╝
██║ ██║ ╚████╔╝ ██╔██╗ ██║███████║██║ ██║ ██║██╔██╗ ██║█████╗
██║ ██║ ╚██╔╝ ██║╚██╗██║██╔══██║██║ ██║ ██║██║╚██╗██║██╔══╝
██████╔╝ ██║ ██║ ╚████║██║ ██║╚██████╗╚██████╔╝██║ ╚████║██║
╚═════╝ ╚═╝ ╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝
"""
if os.name == "nt": # pragma: no cover
# windows can't handle the above charmap
BANNER = "DYNACONF"
def object_merge(
old: Any, new: Any, unique: bool = False, full_path: List[str] = None
) -> Any:
"""
Recursively merge two data structures, new is mutated in-place.
:param old: The existing data.
:param new: The new data to get old values merged in to.
:param unique: When set to True existing list items are not set.
:param full_path: Indicates the elements of a tree.
"""
if full_path is None:
full_path = []
if old == new or old is None or new is None:
# Nothing to merge
return new
if isinstance(old, list) and isinstance(new, list):
for item in old[::-1]:
if unique and item in new:
continue
new.insert(0, item)
if isinstance(old, dict) and isinstance(new, dict):
existing_value = recursive_get(old, full_path) # doesnt handle None
# Need to make every `None` on `_store` to be an wrapped `LazyNone`
for key, value in old.items():
if (
existing_value is not None
and key.lower() == full_path[-1].lower()
and existing_value is value
):
# Here Be The Dragons
# This comparison needs to be smarter
continue
if key not in new:
new[key] = value
else:
object_merge(
value,
new[key],
full_path=full_path[1:] if full_path else None,
)
handle_metavalues(old, new)
return new
def recursive_get(
obj: Union[DynaBox, Dict[str, int], Dict[str, Union[str, int]]],
names: Optional[List[str]],
) -> Any:
"""Given a dot accessible object and a list of names `foo.bar.zaz`
gets recursivelly all names one by one obj.foo.bar.zaz.
"""
if not names:
return
head, tail = names[0], names[1:]
result = getattr(obj, head, None)
if not tail:
return result
return recursive_get(result, tail)
def handle_metavalues(
old: Union[DynaBox, Dict[str, int], Dict[str, Union[str, int]]], new: Any
) -> None:
"""Cleanup of MetaValues on new dict"""
for key in list(new.keys()):
# MetaValue instances
if getattr(new[key], "_dynaconf_reset", False): # pragma: no cover
# a Reset on `new` triggers reasign of existing data
# @reset is deprecated on v3.0.0
new[key] = new[key].unwrap()
elif getattr(new[key], "_dynaconf_del", False):
# a Del on `new` triggers deletion of existing data
new.pop(key, None)
old.pop(key, None)
elif getattr(new[key], "_dynaconf_merge", False):
# a Merge on `new` triggers merge with existing data
new[key] = object_merge(
old.get(key), new[key].unwrap(), unique=new[key].unique
)
# Data structures containing merge tokens
if isinstance(new.get(key), (list, tuple)):
has_merge = "dynaconf_merge" in new[key]
has_merge_unique = "dynaconf_merge_unique" in new[key]
if has_merge or has_merge_unique:
value = list(new[key])
unique = False
try:
value.remove("dynaconf_merge")
except ValueError:
value.remove("dynaconf_merge_unique")
unique = True
for item in old.get(key)[::-1]:
if unique and item in value:
continue
value.insert(0, item)
new[key] = value
elif isinstance(new.get(key), dict):
local_merge = new[key].pop(
"dynaconf_merge", new[key].pop("dynaconf_merge_unique", None)
)
if local_merge not in (True, False, None) and not new[key]:
# In case `dynaconf_merge:` holds value not boolean - ref #241
new[key] = local_merge
if local_merge:
new[key] = object_merge(old.get(key), new[key])
class DynaconfDict(dict):
"""A dict representing en empty Dynaconf object
useful to run loaders in to a dict for testing"""
def __init__(self, *args, **kwargs):
self._fresh = False
self._loaded_envs = []
self._loaded_hooks = defaultdict(dict)
self._loaded_py_modules = []
self._loaded_files = []
self._deleted = set()
self._store = {}
self._env_cache = {}
self._loaded_by_loaders = {}
self._loaders = []
self._defaults = {}
self.environ = os.environ
self.SETTINGS_MODULE = None
self.filter_strategy = kwargs.get("filter_strategy", None)
self._not_installed_warnings = []
self._validate_only = kwargs.pop("validate_only", None)
self._validate_exclude = kwargs.pop("validate_exclude", None)
super(DynaconfDict, self).__init__(*args, **kwargs)
def set(self, key: str, value: str, *args, **kwargs) -> None:
self[key] = value
@staticmethod
def get_environ(key, default=None): # pragma: no cover
return os.environ.get(key, default)
def exists(self, key: str, **kwargs) -> bool:
return self.get(key, missing) is not missing
RENAMED_VARS = {
# old: new
"DYNACONF_NAMESPACE": "ENV_FOR_DYNACONF",
"NAMESPACE_FOR_DYNACONF": "ENV_FOR_DYNACONF",
"DYNACONF_SETTINGS_MODULE": "SETTINGS_FILE_FOR_DYNACONF",
"DYNACONF_SETTINGS": "SETTINGS_FILE_FOR_DYNACONF",
"SETTINGS_MODULE": "SETTINGS_FILE_FOR_DYNACONF",
"SETTINGS_MODULE_FOR_DYNACONF": "SETTINGS_FILE_FOR_DYNACONF",
"PROJECT_ROOT": "ROOT_PATH_FOR_DYNACONF",
"PROJECT_ROOT_FOR_DYNACONF": "ROOT_PATH_FOR_DYNACONF",
"DYNACONF_SILENT_ERRORS": "SILENT_ERRORS_FOR_DYNACONF",
"DYNACONF_ALWAYS_FRESH_VARS": "FRESH_VARS_FOR_DYNACONF",
"BASE_NAMESPACE_FOR_DYNACONF": "DEFAULT_ENV_FOR_DYNACONF",
"GLOBAL_ENV_FOR_DYNACONF": "ENVVAR_PREFIX_FOR_DYNACONF",
}
def compat_kwargs(kwargs: Dict[str, Any]) -> None:
"""To keep backwards compat change the kwargs to new names"""
warn_deprecations(kwargs)
for old, new in RENAMED_VARS.items():
if old in kwargs:
kwargs[new] = kwargs[old]
# update cross references
for c_old, c_new in RENAMED_VARS.items():
if c_new == new:
kwargs[c_old] = kwargs[new]
class Missing:
"""
Sentinel value object/singleton used to differentiate between ambiguous
situations where `None` is a valid value.
"""
def __bool__(self) -> bool:
"""Respond to boolean duck-typing."""
return False
def __eq__(self, other: Union[DynaBox, Missing]) -> bool:
"""Equality check for a singleton."""
return isinstance(other, self.__class__)
# Ensure compatibility with Python 2.x
__nonzero__ = __bool__
def __repr__(self) -> str:
"""
Unambiguously identify this string-based representation of Missing,
used as a singleton.
"""
return "<dynaconf.missing>"
missing = Missing()
def deduplicate(list_object: List[str]) -> List[str]:
"""Rebuild `list_object` removing duplicated and keeping order"""
new = []
for item in list_object:
if item not in new:
new.append(item)
return new
def warn_deprecations(data: Any) -> None:
for old, new in RENAMED_VARS.items():
if old in data:
warnings.warn(
f"You are using {old} which is a deprecated settings "
f"replace it with {new}",
DeprecationWarning,
)
def trimmed_split(
s: str, seps: Union[str, Tuple[str, str]] = (";", ",")
) -> List[str]:
"""Given a string s, split is by one of one of the seps."""
for sep in seps:
if sep not in s:
continue
data = [item.strip() for item in s.strip().split(sep)]
return data
return [s] # raw un-splitted
def ensure_a_list(data: Any) -> Union[List[int], List[str]]:
"""Ensure data is a list or wrap it in a list"""
if not data:
return []
if isinstance(data, (list, tuple, set)):
return list(data)
if isinstance(data, str):
data = trimmed_split(data) # settings.toml,other.yaml
return data
return [data]
def build_env_list(
obj: Union[Settings, LazySettings], env: Optional[str]
) -> List[str]:
"""Build env list for loaders to iterate.
Arguments:
obj {LazySettings} -- A Dynaconf settings instance
env {str} -- The current env to be loaded
Returns:
[str] -- A list of string names of the envs to load.
"""
# add the [default] env
env_list = [obj.get("DEFAULT_ENV_FOR_DYNACONF")]
# compatibility with older versions that still uses [dynaconf] as
# [default] env
global_env = obj.get("ENVVAR_PREFIX_FOR_DYNACONF") or "DYNACONF"
if global_env not in env_list:
env_list.append(global_env)
# add the current env
if obj.current_env and obj.current_env not in env_list:
env_list.append(obj.current_env)
# add a manually set env
if env and env not in env_list:
env_list.append(env)
# add the [global] env
env_list.append("GLOBAL")
# loaders are responsible to change to lower/upper cases
return [env.lower() for env in env_list]
def upperfy(key: str) -> str:
"""Receive a string key and returns its upper version.
Example:
input: foo
output: FOO
input: foo_bar
output: FOO_BAR
input: foo__bar__ZAZ
output: FOO__bar__ZAZ
Arguments:
key {str} -- A string key that may contain dunders `__`
Returns:
The key as upper case but keeping the nested elements.
"""
key = str(key)
if "__" in key:
parts = key.split("__")
return "__".join([parts[0].upper()] + parts[1:])
return key.upper()
def multi_replace(text: str, patterns: Dict[str, str]) -> str:
"""Replaces multiple pairs in a string
Arguments:
text {str} -- A "string text"
patterns {dict} -- A dict of {"old text": "new text"}
Returns:
text -- str
"""
for old, new in patterns.items():
text = text.replace(old, new)
return text
def extract_json_objects(
text: str, decoder: JSONDecoder = JSONDecoder()
) -> Iterator[Dict[str, Union[int, Dict[Any, Any]]]]:
"""Find JSON objects in text, and yield the decoded JSON data
Does not attempt to look for JSON arrays, text, or other JSON types outside
of a parent JSON object.
"""
pos = 0
while True:
match = text.find("{", pos)
if match == -1:
break
try:
result, index = decoder.raw_decode(text[match:])
yield result
pos = match + index
except ValueError:
pos = match + 1
def recursively_evaluate_lazy_format(
value: Any, settings: Union[Settings, LazySettings]
) -> Any:
"""Given a value as a data structure, traverse all its members
to find Lazy values and evaluate it.
For example: Evaluate values inside lists and dicts
"""
if getattr(value, "_dynaconf_lazy_format", None):
value = value(settings)
if isinstance(value, list):
# Keep the original type, can be a BoxList
value = value.__class__(
[
recursively_evaluate_lazy_format(item, settings)
for item in value
]
)
return value
def isnamedtupleinstance(value):
"""Check if value is a namedtuple instance
stackoverflow.com/questions/2166818/
how-to-check-if-an-object-is-an-instance-of-a-namedtuple
"""
t = type(value)
b = t.__bases__
if len(b) != 1 or b[0] != tuple:
return False
f = getattr(t, "_fields", None)
if not isinstance(f, tuple):
return False
return all(type(n) == str for n in f)
|