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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
from __future__ import annotations
import json
import os
import re
import warnings
from functools import wraps
from dynaconf.utils import extract_json_objects
from dynaconf.utils import isnamedtupleinstance
from dynaconf.utils import multi_replace
from dynaconf.utils import recursively_evaluate_lazy_format
from dynaconf.utils.boxing import DynaBox
from dynaconf.utils.functional import empty
from dynaconf.vendor import toml
from dynaconf.vendor import tomllib
try:
from jinja2 import Environment
jinja_env = Environment()
for p_method in ("abspath", "realpath", "relpath", "dirname", "basename"):
jinja_env.filters[p_method] = getattr(os.path, p_method)
except ImportError: # pragma: no cover
jinja_env = None
true_values = ("t", "true", "enabled", "1", "on", "yes", "True")
false_values = ("f", "false", "disabled", "0", "off", "no", "False", "")
KV_PATTERN = re.compile(r"([a-zA-Z0-9 ]*=[a-zA-Z0-9\- :]*)")
"""matches `a=b, c=d, e=f` used on `VALUE='@merge foo=bar'` variables."""
class DynaconfFormatError(Exception):
"""Error to raise when formatting a lazy variable fails"""
class DynaconfParseError(Exception):
"""Error to raise when parsing @casts"""
class MetaValue:
"""A Marker to trigger specific actions on `set` and `object_merge`"""
_meta_value = True
def __init__(self, value, box_settings):
self.box_settings = box_settings
self.value = parse_conf_data(
value, tomlfy=True, box_settings=box_settings
)
def __repr__(self):
return f"{self.__class__.__name__}({self.value}) on {id(self)}"
def unwrap(self):
return self.value
class Reset(MetaValue):
"""Triggers an existing key to be reset to its value
NOTE: DEPRECATED on v3.0.0
"""
_dynaconf_reset = True
def __init__(self, value, box_settings):
self.box_settings = box_settings
self.value = parse_conf_data(
value, tomlfy=True, box_settings=self.box_settings
)
warnings.warn(f"{self.value} does not need `@reset` anymore.")
class Del(MetaValue):
"""Triggers an existing key to be deleted"""
_dynaconf_del = True
def unwrap(self):
raise ValueError("Del object has no value")
class Merge(MetaValue):
"""Triggers an existing key to be merged"""
_dynaconf_merge = True
def __init__(self, value, box_settings, unique=False):
if unique:
self._dynaconf_merge_unique = True
self.box_settings = box_settings
self.value = parse_conf_data(
value, tomlfy=True, box_settings=box_settings
)
if isinstance(self.value, (int, float, bool)):
# @merge 1, @merge 1.1, @merge False
self.value = [self.value]
elif isinstance(self.value, str):
# @merge {"valid": "json"}
json_object = list(
extract_json_objects(
multi_replace(
self.value,
{
": True": ": true",
":True": ": true",
": False": ": false",
":False": ": false",
": None": ": null",
":None": ": null",
},
)
)
)
if len(json_object) == 1:
self.value = json_object[0]
else:
matches = KV_PATTERN.findall(self.value)
# a=b, c=d
if matches:
self.value = {
k.strip(): parse_conf_data(
v, tomlfy=True, box_settings=box_settings
)
for k, v in (
match.strip().split("=") for match in matches
)
}
elif "," in self.value:
# @merge foo,bar
self.value = [
parse_conf_data(
v, tomlfy=True, box_settings=box_settings
)
for v in self.value.split(",")
]
else:
# @merge foo
self.value = [self.value]
self.unique = unique
class Insert(MetaValue):
"""Triggers the value to be inserted into a list at specific index"""
_dynaconf_insert = True
def __init__(self, value, box_settings):
"""
normally value will be a string like
`0 foo` or `-1 foo` and needs to get split
but value can also be just a single string with or without space
like `foo` and in this case it will be treated as `0 foo`
but it can also be `foo bar` and in this case it will be treated as `0 foo bar`
we need to check if the first part is a number
if it is not a number then we will treat it as `0 value`
if it is a number then we will split it as `index, value`
this must use a regex to match value, examples:
-1 foo -> index = -1, value = foo
0 foo -> index = 0, value = foo
0 foo bar -> index = 0, value = foo bar
0 42 -> index = 0, value = 42
0 42 foo -> index = 0, value = 42 foo
foo -> index = 0, value = foo
foo bar -> index = 0, value = foo bar
42 -> index = 0, value = 42
42 foo -> index = 42, value = foo
42 foo bar -> index = 42, value = foo bar
"""
self.box_settings = box_settings
try:
if value.lstrip("-+")[0].isdigit():
# `0 foo` or `-1 foo` or `42 foo` or `42`(raise ValueError)
index, value = value.split(" ", 1)
else:
# `foo` or `foo bar`
index, value = 0, value
except ValueError:
# `42` or any other non split able value
index, value = 0, value
self.index = int(index)
self.value = parse_conf_data(
value, tomlfy=True, box_settings=box_settings
)
class BaseFormatter:
def __init__(self, function, token):
self.function = function
self.token = token
def __call__(self, value, **context):
try:
return self.function(value, **context)
except (KeyError, AttributeError) as exc:
# A template like `{this.KEY}` failed with AttributeError
# Or KeyError in the case of `{env[KEY]}`
raise DynaconfFormatError(
f"Dynaconf can't interpolate variable because {exc}"
) from exc
def __str__(self):
return str(self.token)
def _jinja_formatter(value, **context):
if jinja_env is None: # pragma: no cover
raise ImportError(
"jinja2 must be installed to enable '@jinja' settings in dynaconf"
)
return jinja_env.from_string(value).render(**context)
def _get_formatter(value, **context):
"""
Invokes settings.get from the annotation in value.
value can be one of the following:
@get KEY
@get KEY @int
@get KEY default_value
@get KEY @int default_value
@marker KEY_TO_LOOKUP @OPTIONAL_CAST OPTIONAL_DEFAULT_VALUE
key group will match the key
cast group will match anything provided after @
the default group will match anything between single or double quotes
"""
pattern = re.compile(
r"(?P<key>\w+(?:\.\w+)?)\s*"
r"(?:(?P<cast>@\w+)\s*)?"
r'(?P<quote>["\']?)'
r'\s*(?P<default>[^"\']*)\s*(?P=quote)?'
)
if match := pattern.match(value.strip()):
data = match.groupdict()
return context["this"].get(
key=data["key"],
default=data["default"],
cast=data["cast"],
)
else:
raise DynaconfFormatError(f"Error parsing {value} malformed syntax.")
class Formatters:
"""Dynaconf builtin formatters"""
python_formatter = BaseFormatter(str.format, "format")
jinja_formatter = BaseFormatter(_jinja_formatter, "jinja")
get_formatter = BaseFormatter(_get_formatter, "get")
class Lazy:
"""Holds data to format lazily."""
_dynaconf_lazy_format = True
def __init__(
self, value=empty, formatter=Formatters.python_formatter, casting=None
):
self.value = value
self.casting = casting
# Sometimes a simple function is passed to the formatter.
# but on evaluation-time, we may need to access `formatter.token`
# so we are wrapping the fn to comply with this interface.
if isinstance(formatter, BaseFormatter):
self.formatter = formatter
else:
self.formatter = BaseFormatter(formatter, "lambda")
@property
def context(self):
"""Builds a context for formatting."""
return {"env": os.environ, "this": self.settings}
def __call__(self, settings, validator_object=None):
"""LazyValue triggers format lazily."""
self.settings = settings
self.context["_validator_object"] = validator_object
result = self.formatter(self.value, **self.context)
if self.casting is not None:
result = self.casting(result)
return result
def __str__(self):
"""Gives string representation for the object."""
return str(self.value)
def __repr__(self):
"""Give the quoted str representation"""
return f"'@{self.formatter} {self.value}'"
def _dynaconf_encode(self):
"""Encodes this object values to be serializable to json"""
return f"@{self.formatter} {self.value}"
def set_casting(self, casting):
"""Set the casting and return the instance."""
self.casting = casting
return self
def try_to_encode(value, callback=str):
"""Tries to encode a value by verifying existence of `_dynaconf_encode`"""
try:
return value._dynaconf_encode()
except (AttributeError, TypeError):
return callback(value)
def evaluate_lazy_format(f):
"""Marks a method on Settings instance to
lazily evaluate LazyFormat objects upon access."""
@wraps(f)
def evaluate(settings, *args, **kwargs):
value = f(settings, *args, **kwargs)
return recursively_evaluate_lazy_format(value, settings)
return evaluate
converters = {
"@str": lambda value: value.set_casting(str)
if isinstance(value, Lazy)
else str(value),
"@int": lambda value: value.set_casting(int)
if isinstance(value, Lazy)
else int(value),
"@float": lambda value: value.set_casting(float)
if isinstance(value, Lazy)
else float(value),
"@bool": lambda value: value.set_casting(
lambda x: str(x).lower() in true_values
)
if isinstance(value, Lazy)
else str(value).lower() in true_values,
"@json": lambda value: value.set_casting(
lambda x: json.loads(x.replace("'", '"'))
)
if isinstance(value, Lazy)
else json.loads(value),
"@format": lambda value: Lazy(value),
"@jinja": lambda value: Lazy(value, formatter=Formatters.jinja_formatter),
# Meta Values to trigger pre assignment actions
"@reset": Reset, # @reset is DEPRECATED on v3.0.0
"@del": Del,
"@merge": Merge,
"@merge_unique": lambda value, box_settings: Merge(
value, box_settings, unique=True
),
"@insert": Insert,
"@get": lambda value: Lazy(value, formatter=Formatters.get_formatter),
# Special markers to be used as placeholders e.g: in prefilled forms
# will always return None when evaluated
"@note": lambda value: None,
"@comment": lambda value: None,
"@null": lambda value: None,
"@none": lambda value: None,
"@empty": lambda value: empty,
}
def apply_converter(converter_key, value, box_settings):
"""
Get converter and apply it to @value.
Lazy converters will return Lazy objects for later evaluation.
"""
converter = converters[converter_key]
try:
converted_value = converter(value, box_settings=box_settings)
except TypeError:
converted_value = converter(value)
return converted_value
def add_converter(converter_key, func):
"""Adds a new converter to the converters dict"""
if not converter_key.startswith("@"):
converter_key = f"@{converter_key}"
converters[converter_key] = wraps(func)(
lambda value: value.set_casting(func)
if isinstance(value, Lazy)
else Lazy(
value,
casting=func,
formatter=BaseFormatter(lambda x, **_: x, converter_key),
)
)
def parse_with_toml(data):
"""Uses TOML syntax to parse data"""
try: # try tomllib first
try:
return tomllib.loads(f"key={data}")["key"]
except (tomllib.TOMLDecodeError, KeyError):
return data
except UnicodeDecodeError: # pragma: no cover
# fallback to toml (TBR in 4.0.0)
try:
return toml.loads(f"key={data}")["key"]
except (toml.TomlDecodeError, KeyError):
return data
warnings.warn(
"TOML files should have only UTF-8 encoded characters. "
"starting on 4.0.0 dynaconf will stop allowing invalid chars.",
DeprecationWarning,
)
def _parse_conf_data(data, tomlfy=False, box_settings=None):
"""
@int @bool @float @json (for lists and dicts)
strings does not need converters
export DYNACONF_DEFAULT_THEME='material'
export DYNACONF_DEBUG='@bool True'
export DYNACONF_DEBUG_TOOLBAR_ENABLED='@bool False'
export DYNACONF_PAGINATION_PER_PAGE='@int 20'
export DYNACONF_MONGODB_SETTINGS='@json {"DB": "quokka_db"}'
export DYNACONF_ALLOWED_EXTENSIONS='@json ["jpg", "png"]'
"""
# not enforced to not break backwards compatibility with custom loaders
box_settings = box_settings or {}
castenabled = box_settings.get("AUTO_CAST_FOR_DYNACONF", empty)
if castenabled is empty:
castenabled = (
os.environ.get("AUTO_CAST_FOR_DYNACONF", "true").lower()
not in false_values
)
if (
castenabled
and data
and isinstance(data, str)
and data.startswith(tuple(converters.keys()))
):
# Check combination token is used
comb_token = re.match(
f"^({'|'.join(converters.keys())}) @(jinja|format)",
data,
)
if comb_token:
tokens = comb_token.group(0)
converter_key_list = tokens.split(" ")
value = data.replace(tokens, "").strip()
else:
parts = data.partition(" ")
converter_key_list = [parts[0]]
value = parts[-1]
# Parse the converters iteratively
for converter_key in converter_key_list[::-1]:
value = apply_converter(converter_key, value, box_settings)
else:
value = parse_with_toml(data) if tomlfy else data
if isinstance(value, dict):
value = DynaBox(value, box_settings=box_settings)
return value
def parse_conf_data(data, tomlfy=False, box_settings=None):
"""
Apply parsing tokens recursively and return transformed data.
Strings with lazy parser (e.g, @format) will become Lazy objects.
"""
# fix for https://github.com/dynaconf/dynaconf/issues/595
if isnamedtupleinstance(data):
return data
# not enforced to not break backwards compatibility with custom loaders
box_settings = box_settings or {}
if isinstance(data, (tuple, list)):
# recursively parse each sequence item
return [
parse_conf_data(item, tomlfy=tomlfy, box_settings=box_settings)
for item in data
]
if isinstance(data, DynaBox):
# recursively parse inner dict items
# It is important to keep the same object id because
# of mutability
for k, v in data.items(bypass_eval=True):
data[k] = parse_conf_data(
v, tomlfy=tomlfy, box_settings=box_settings
)
return data
if isinstance(data, dict):
# recursively parse inner dict items
for k, v in data.items():
data[k] = parse_conf_data(
v, tomlfy=tomlfy, box_settings=box_settings
)
return data
# return parsed string value
return _parse_conf_data(data, tomlfy=tomlfy, box_settings=box_settings)
def unparse_conf_data(value):
if isinstance(value, bool):
return f"@bool {value}"
if isinstance(value, int):
return f"@int {value}"
if isinstance(value, float):
return f"@float {value}"
if isinstance(value, (list, dict)):
return f"@json {json.dumps(value)}"
if isinstance(value, Lazy):
return try_to_encode(value)
if value is None:
return "@none "
return value
def boolean_fix(value: str | None):
"""Gets a value like `True/False` and turns to `true/false`
This function exists because of issue #976
Toml parser casts booleans from true/false lower case
however envvars are usually exported as True/False capitalized
by mistake, this helper fixes it for envvars only.
Assume envvars are always str.
"""
if value and value.strip() in ("True", "False"):
return value.lower()
return value
|