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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
|
from __future__ import annotations
import importlib
import inspect as python_inspect
import json
import os
import pprint
import sys
import warnings
import webbrowser
from contextlib import redirect_stdout
from contextlib import suppress
from pathlib import Path
from dynaconf import constants
from dynaconf import default_settings
from dynaconf import LazySettings
from dynaconf import loaders
from dynaconf import settings as legacy_settings
from dynaconf.base import Settings
from dynaconf.loaders.py_loader import get_module
from dynaconf.utils import prepare_json
from dynaconf.utils import upperfy
from dynaconf.utils.files import read_file
from dynaconf.utils.functional import empty
from dynaconf.utils.inspect import builtin_dumpers
from dynaconf.utils.inspect import EnvNotFoundError
from dynaconf.utils.inspect import inspect_settings
from dynaconf.utils.inspect import KeyNotFoundError
from dynaconf.utils.inspect import OutputFormatError
from dynaconf.utils.inspect import print_debug_info
from dynaconf.utils.parse_conf import parse_conf_data
from dynaconf.utils.parse_conf import unparse_conf_data
from dynaconf.validator import ValidationError
from dynaconf.validator import Validator
from dynaconf.vendor import click
from dynaconf.vendor import toml
from dynaconf.vendor import tomllib
os.environ["PYTHONIOENCODING"] = "utf-8"
CWD = None
with suppress(FileNotFoundError):
CWD = Path.cwd()
EXTS = ["ini", "toml", "yaml", "json", "py", "env"]
WRITERS = ["ini", "toml", "yaml", "json", "py", "redis", "vault", "env"]
ENC = default_settings.ENCODING_FOR_DYNACONF
def set_settings(ctx, instance=None):
"""Pick correct settings instance and set it to a global variable."""
global settings
settings = None
_echo_enabled = ctx.invoked_subcommand not in [
"get",
"inspect",
"debug-info",
None,
]
if "--json" in sys.argv:
_echo_enabled = False
if instance is not None:
if ctx.invoked_subcommand in ["init"]:
raise click.UsageError(
"-i/--instance option is not allowed for `init` command"
)
sys.path.insert(0, ".")
settings = import_settings(instance)
elif "FLASK_APP" in os.environ: # pragma: no cover
with suppress(ImportError, click.UsageError):
from flask.cli import ScriptInfo # noqa
from dynaconf import FlaskDynaconf
app_import_path = os.environ["FLASK_APP"]
flask_app = ScriptInfo(app_import_path).load_app()
settings = FlaskDynaconf(flask_app, **flask_app.config).settings
if _echo_enabled:
click.echo(
click.style(
"Flask app detected", fg="white", bg="bright_black"
)
)
elif "DJANGO_SETTINGS_MODULE" in os.environ: # pragma: no cover
sys.path.insert(0, os.path.abspath(os.getcwd()))
import django # noqa
django.setup() # ensure django is setup to avoid AppRegistryNotReady
settings_module = import__django_settings(
os.environ["DJANGO_SETTINGS_MODULE"]
)
found_settings = python_inspect.getmembers(
settings_module,
lambda item: isinstance(item, (LazySettings, Settings)),
)
if found_settings:
settings = found_settings[0][1]
if settings is not None and _echo_enabled:
click.echo(
click.style(
"Django app detected", fg="white", bg="bright_black"
)
)
if settings is None:
if instance is None and "--help" not in sys.argv:
if ctx.invoked_subcommand and ctx.invoked_subcommand not in [
"init",
]:
warnings.warn(
"Starting on 3.x the param --instance/-i is now required. "
"try passing it `dynaconf -i path.to.settings <cmd>` "
"Example `dynaconf -i config.settings list` "
)
settings = legacy_settings
else:
settings = LazySettings(create_new_settings=True)
else:
settings = LazySettings()
def import__django_settings(django_settings_module):
"""Import the Django settings module from the string importable path."""
try:
with redirect_stdout(None):
module = importlib.import_module(django_settings_module)
except ImportError as e:
raise click.UsageError(e)
except FileNotFoundError:
return
return module
def import_settings(dotted_path):
"""Import settings instance from python dotted path.
Last item in dotted path must be settings instance.
Example: import_settings('path.to.settings')
"""
if "." in dotted_path:
module, name = dotted_path.rsplit(".", 1)
else:
raise click.UsageError(
f"invalid path to settings instance: {dotted_path}"
)
try:
with redirect_stdout(None):
module = importlib.import_module(module)
except ImportError as e:
raise click.UsageError(e)
except FileNotFoundError:
return
try:
return getattr(module, name)
except AttributeError as e:
raise click.UsageError(e)
def split_vars(_vars):
"""Splits values like foo=bar=zaz in {'foo': 'bar=zaz'}"""
return (
{
upperfy(k.strip()): parse_conf_data(
v.strip(), tomlfy=True, box_settings=settings
)
for k, _, v in [item.partition("=") for item in _vars]
}
if _vars
else {}
)
def read_file_in_root_directory(*names, **kwargs):
"""Read a file on root dir."""
return read_file(
os.path.join(os.path.dirname(__file__), *names),
encoding=kwargs.get("encoding", "utf-8"),
)
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo(read_file_in_root_directory("VERSION"))
ctx.exit()
def open_docs(ctx, param, value): # pragma: no cover
if not value or ctx.resilient_parsing:
return
url = "https://dynaconf.com/"
webbrowser.open(url, new=2)
click.echo(f"{url} opened in browser")
ctx.exit()
def show_banner(ctx, param, value):
"""Shows dynaconf awesome banner"""
if not value or ctx.resilient_parsing:
return
set_settings(ctx)
click.echo(settings.dynaconf_banner)
click.echo("Learn more at: http://github.com/dynaconf/dynaconf")
ctx.exit()
@click.group()
@click.option(
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Show dynaconf version",
)
@click.option(
"--docs",
is_flag=True,
callback=open_docs,
expose_value=False,
is_eager=True,
help="Open documentation in browser",
)
@click.option(
"--banner",
is_flag=True,
callback=show_banner,
expose_value=False,
is_eager=True,
help="Show awesome banner",
)
@click.option(
"--instance",
"-i",
default=None,
envvar="INSTANCE_FOR_DYNACONF",
help="Custom instance of LazySettings",
)
@click.pass_context
def main(ctx, instance):
"""Dynaconf - Command Line Interface\n
Documentation: https://dynaconf.com/
"""
set_settings(ctx, instance)
@main.command()
@click.option(
"--format", "fileformat", "-f", default="toml", type=click.Choice(EXTS)
)
@click.option(
"--path", "-p", default=CWD, help="defaults to current directory"
)
@click.option(
"--env",
"-e",
default=None,
help="deprecated command (kept for compatibility but unused)",
)
@click.option(
"--vars",
"_vars",
"-v",
multiple=True,
default=None,
help=(
"extra values to write to settings file "
"e.g: `dynaconf init -v NAME=foo -v X=2`"
),
)
@click.option(
"--secrets",
"_secrets",
"-s",
multiple=True,
default=None,
help=(
"secret key values to be written in .secrets "
"e.g: `dynaconf init -s TOKEN=kdslmflds"
),
)
@click.option("--wg/--no-wg", default=True)
@click.option("-y", default=False, is_flag=True)
@click.option("--django", default=os.environ.get("DJANGO_SETTINGS_MODULE"))
@click.pass_context
def init(ctx, fileformat, path, env, _vars, _secrets, wg, y, django):
"""
Inits a dynaconf project.
By default it creates a settings.toml and a .secrets.toml
for [default|development|staging|testing|production|global] envs.
The format of the files can be changed passing
--format=yaml|json|ini|py.
This command must run on the project's root folder or you must pass
--path=/myproject/root/folder.
The --env/-e is deprecated (kept for compatibility but unused)
"""
click.echo("⚙️ Configuring your Dynaconf environment")
click.echo("-" * 42)
if "FLASK_APP" in os.environ: # pragma: no cover
click.echo(
"⚠️ Flask detected, you can't use `dynaconf init` "
"on a flask project, instead go to dynaconf.com/flask/ "
"for more information.\n"
"Or add the following to your app.py\n"
"\n"
"from dynaconf import FlaskDynaconf\n"
"app = Flask(__name__)\n"
"FlaskDynaconf(app)\n"
)
sys.exit(1)
path = Path(path)
if env is not None:
click.secho(
"⚠️ The --env/-e option is deprecated (kept for\n"
" compatibility but unused)\n",
fg="red",
bold=True,
# stderr=True,
)
if settings.get("create_new_settings") is True:
filename = Path("config.py")
if not filename.exists():
with open(filename, "w") as new_settings:
new_settings.write(
constants.INSTANCE_TEMPLATE.format(
settings_files=[
f"settings.{fileformat}",
f".secrets.{fileformat}",
]
)
)
click.echo(
"🐍 The file `config.py` was generated.\n"
" on your code now use `from config import settings`.\n"
" (you must have `config` importable in your PYTHONPATH).\n"
)
else:
click.echo(
f"⁉️ You already have a {filename} so it is not going to be\n"
" generated for you, you will need to create your own \n"
" settings instance e.g: config.py \n"
" from dynaconf import Dynaconf \n"
" settings = Dynaconf(**options)\n"
)
sys.path.append(str(path))
set_settings(ctx, "config.settings")
env = settings.current_env.lower()
loader = importlib.import_module(f"dynaconf.loaders.{fileformat}_loader")
# Turn foo=bar=zaz in {'foo': 'bar=zaz'}
env_data = split_vars(_vars)
_secrets = split_vars(_secrets)
# create placeholder data for every env
settings_data = {}
secrets_data = {}
if env_data:
settings_data[env] = env_data
settings_data["default"] = {k: "a default value" for k in env_data}
if _secrets:
secrets_data[env] = _secrets
secrets_data["default"] = {k: "a default value" for k in _secrets}
if str(path).endswith(
constants.ALL_EXTENSIONS + ("py",)
): # pragma: no cover # noqa
settings_path = path
secrets_path = path.parent / f".secrets.{fileformat}"
gitignore_path = path.parent / ".gitignore"
else:
if fileformat == "env":
if str(path) in (".env", "./.env"): # pragma: no cover
settings_path = path
elif str(path).endswith("/.env"): # pragma: no cover
settings_path = path
elif str(path).endswith(".env"): # pragma: no cover
settings_path = path.parent / ".env"
else:
settings_path = path / ".env"
Path.touch(settings_path)
secrets_path = None
else:
settings_path = path / f"settings.{fileformat}"
secrets_path = path / f".secrets.{fileformat}"
gitignore_path = path / ".gitignore"
if fileformat in ["py", "env"] or env == "main":
# for Main env, Python and .env formats writes a single env
settings_data = settings_data.get(env, {})
secrets_data = secrets_data.get(env, {})
if not y and settings_path and settings_path.exists(): # pragma: no cover
click.confirm(
f"⁉ {settings_path} exists do you want to overwrite it?",
abort=True,
)
if not y and secrets_path and secrets_path.exists(): # pragma: no cover
click.confirm(
f"⁉ {secrets_path} exists do you want to overwrite it?",
abort=True,
)
if settings_path:
loader.write(settings_path, settings_data, merge=True)
click.echo(f"🎛️ {settings_path.name} created to hold your settings.\n")
if secrets_path:
loader.write(secrets_path, secrets_data, merge=True)
click.echo(f"🔑 {secrets_path.name} created to hold your secrets.\n")
ignore_line = ".secrets.*"
comment = "\n# Ignore dynaconf secret files\n"
if not gitignore_path.exists():
with open(str(gitignore_path), "w", encoding=ENC) as f:
f.writelines([comment, ignore_line, "\n"])
else:
existing = (
ignore_line in open(str(gitignore_path), encoding=ENC).read()
)
if not existing: # pragma: no cover
with open(str(gitignore_path), "a+", encoding=ENC) as f:
f.writelines([comment, ignore_line, "\n"])
click.echo(
f"🙈 the {secrets_path.name} is also included in `.gitignore` \n"
" beware to not push your secrets to a public repo \n"
" or use dynaconf builtin support for Vault Servers.\n"
)
if django: # pragma: no cover
dj_module, _ = get_module({}, django)
dj_filename = dj_module.__file__
if Path(dj_filename).exists():
click.confirm(
f"⁉ {dj_filename} is found do you want to add dynaconf?",
abort=True,
)
with open(dj_filename, "a") as dj_file:
dj_file.write(constants.DJANGO_PATCH)
click.echo("🎠 Now your Django settings are managed by Dynaconf")
else:
click.echo("❌ Django settings file not written.")
else:
click.echo(
"🎉 Dynaconf is configured! read more on https://dynaconf.com\n"
" Use `dynaconf -i config.settings list` to see your settings\n"
)
@main.command(name="get")
@click.argument("key", required=True)
@click.option(
"--default",
"-d",
default=empty,
help="Default value if settings doesn't exist",
)
@click.option(
"--env", "-e", default=None, help="Filters the env to get the values"
)
@click.option(
"--unparse",
"-u",
default=False,
help="Unparse data by adding markers such as @none, @int etc..",
is_flag=True,
)
def get(key, default, env, unparse):
"""Returns the raw value for a settings key.
If result is a dict, list or tuple it is printed as a valid json string.
"""
if env:
env = env.strip()
if key:
key = key.strip()
if env:
settings.setenv(env)
if default is not empty:
result = settings.get(key, default)
else:
try:
result = settings[key]
except KeyError:
click.echo("Key not found", nl=False, err=True)
sys.exit(1)
if unparse:
result = unparse_conf_data(result)
if isinstance(result, (dict, list, tuple)):
result = json.dumps(prepare_json(result), sort_keys=True, default=repr)
click.echo(result, nl=False)
@main.command(name="list")
@click.option(
"--env", "-e", default=None, help="Filters the env to get the values"
)
@click.option("--key", "-k", default=None, help="Filters a single key")
@click.option(
"--more",
"-m",
default=None,
help="Pagination more|less style",
is_flag=True,
)
@click.option(
"--loader",
"-l",
default=None,
help="a loader identifier to filter e.g: toml|yaml",
)
@click.option(
"--all",
"_all",
"-a",
default=False,
is_flag=True,
help="show dynaconf internal settings?",
)
@click.option(
"--output",
"-o",
type=click.Path(writable=True, dir_okay=False),
default=None,
help="Filepath to write the listed values as json",
)
@click.option(
"--output-flat",
"flat",
is_flag=True,
default=False,
help="Output file is flat (do not include [env] name)",
)
@click.option(
"--json",
"_json",
"-j",
is_flag=True,
default=False,
help="Prints out data serialized as JSON",
)
def _list(
env,
key,
more,
loader,
_all=False,
output=None,
flat=False,
_json=False,
):
"""
Lists user defined settings or all (including internal configs).
By default, shows only user defined. If `--all` is passed it also shows
dynaconf internal variables aswell.
"""
if env:
env = env.strip()
if key:
key = key.strip()
if loader:
loader = loader.strip()
if env:
settings.setenv(env)
cur_env = settings.current_env.lower()
if cur_env == "main":
flat = True
if not _json:
click.echo(
click.style(
f"Working in {cur_env} environment ",
bold=True,
bg="bright_blue",
fg="bright_white",
)
)
if not loader:
data = settings.as_dict(env=env, internal=_all)
else:
identifier = f"{loader}_{cur_env}"
data = settings._loaded_by_loaders.get(identifier, {})
data = data or settings._loaded_by_loaders.get(loader, {})
# remove to avoid displaying twice
data.pop("SETTINGS_MODULE", None)
def color(_k):
if _k in dir(default_settings):
return "blue"
return "magenta"
def format_setting(_k, _v):
key = click.style(_k, bg=color(_k), fg="bright_white")
data_type = click.style(
f"<{type(_v).__name__}>", bg="bright_black", fg="bright_white"
)
value = pprint.pformat(_v)
return f"{key}{data_type} {value}"
if not key:
if not _json:
datalines = "\n".join(
format_setting(k, v)
for k, v in data.items()
if k not in data.get("RENAMED_VARS", [])
)
(click.echo_via_pager if more else click.echo)(datalines)
if output:
loaders.write(output, prepare_json(data), env=not flat and cur_env)
if _json:
json_data = json.dumps(
prepare_json(data), sort_keys=True, default=repr
)
click.echo(json_data, nl=False)
else:
key = upperfy(key)
try:
value = settings.get(key, empty)
except AttributeError:
value = empty
if value is empty:
click.secho("Key not found", bg="red", fg="white", err=True)
sys.exit(1)
if not _json:
click.echo(format_setting(key, value))
if output:
loaders.write(
output, prepare_json({key: value}), env=not flat and cur_env
)
if _json:
click.echo(
json.dumps(prepare_json({key: value}), default=repr), nl=True
)
if env:
settings.setenv()
@main.command()
@click.argument("to", required=True, type=click.Choice(WRITERS))
@click.option(
"--vars",
"_vars",
"-v",
multiple=True,
default=None,
help=(
"key values to be written "
"e.g: `dynaconf write toml -e NAME=foo -e X=2`"
),
)
@click.option(
"--secrets",
"_secrets",
"-s",
multiple=True,
default=None,
help=(
"secret key values to be written in .secrets "
"e.g: `dynaconf write toml -s TOKEN=kdslmflds -s X=2`"
),
)
@click.option(
"--path",
"-p",
default=CWD,
help="defaults to current directory/settings.{ext}",
)
@click.option(
"--env",
"-e",
default="default",
help=(
"env to write to defaults to DEVELOPMENT for files "
"for external sources like Redis and Vault "
"it will be DYNACONF or the value set in "
"$ENVVAR_PREFIX_FOR_DYNACONF"
),
)
@click.option("-y", default=False, is_flag=True)
def write(to, _vars, _secrets, path, env, y):
"""Writes data to specific source."""
_vars = split_vars(_vars)
_secrets = split_vars(_secrets)
loader = importlib.import_module(f"dynaconf.loaders.{to}_loader")
if to in EXTS:
# Lets write to a file
path = Path(path)
if str(path).endswith(constants.ALL_EXTENSIONS + ("py",)):
settings_path = path
secrets_path = path.parent / f".secrets.{to}"
else:
if to == "env":
if str(path) in (".env", "./.env"): # pragma: no cover
settings_path = path
elif str(path).endswith("/.env"):
settings_path = path
elif str(path).endswith(".env"):
settings_path = path.parent / ".env"
else:
settings_path = path / ".env"
Path.touch(settings_path)
secrets_path = None
_vars.update(_secrets)
else:
settings_path = path / f"settings.{to}"
secrets_path = path / f".secrets.{to}"
if (
_vars and not y and settings_path and settings_path.exists()
): # pragma: no cover # noqa
click.confirm(
f"{settings_path} exists do you want to overwrite it?",
abort=True,
)
if (
_secrets and not y and secrets_path and secrets_path.exists()
): # pragma: no cover # noqa
click.confirm(
f"{secrets_path} exists do you want to overwrite it?",
abort=True,
)
if to not in ["py", "env"]:
if _vars:
_vars = {env: _vars}
if _secrets:
_secrets = {env: _secrets}
if _vars and settings_path:
loader.write(settings_path, _vars, merge=True)
click.echo(f"Data successful written to {settings_path}")
if _secrets and secrets_path:
loader.write(secrets_path, _secrets, merge=True)
click.echo(f"Data successful written to {secrets_path}")
else: # pragma: no cover
# lets write to external source
with settings.using_env(env):
# make sure we're in the correct environment
loader.write(settings, _vars, **_secrets)
click.echo(f"Data successful written to {to}")
@main.command()
@click.option(
"--path", "-p", default=CWD, help="defaults to current directory"
)
def validate(path): # pragma: no cover
"""
Validates Dynaconf settings based on provided rules.
Rules should be defined in dynaconf_validators.toml
"""
# reads the 'dynaconf_validators.toml' from path
# for each section register the validator for specific env
# call validate
path = Path(path)
if not str(path).endswith(".toml"):
path = path / "dynaconf_validators.toml"
if not path.exists(): # pragma: no cover # noqa
click.echo(click.style(f"{path} not found", fg="white", bg="red"))
sys.exit(1)
# parse validator file
try: # try tomlib first
validation_data = tomllib.load(open(str(path), "rb"))
except UnicodeDecodeError: # fallback to legacy toml (TBR in 4.0.0)
warnings.warn(
"TOML files should have only UTF-8 encoded characters. "
"starting on 4.0.0 dynaconf will stop allowing invalid chars.",
)
validation_data = toml.load(
open(str(path), encoding=default_settings.ENCODING_FOR_DYNACONF),
)
except tomllib.TOMLDecodeError as e:
click.echo(
click.style(
f"Error parsing TOML: {e}. Maybe it should be quoted.",
fg="white",
bg="red",
)
)
sys.exit(1)
# guarantee there is an environment
validation_data = {k.lower(): v for k, v in validation_data.items()}
if not validation_data.get("default"):
validation_data = {"default": validation_data}
success = True
for env, name_data in validation_data.items():
for name, data in name_data.items():
if not isinstance(data, dict): # pragma: no cover
click.echo(
click.style(
f"Invalid rule for parameter '{name}'"
"(this will be skipped)",
fg="white",
bg="yellow",
)
)
else:
data.setdefault("env", env)
click.echo(
click.style(
f"Validating '{name}' with '{data}'",
fg="white",
bg="blue",
)
)
try:
Validator(name, **data).validate(settings)
except ValidationError as e:
click.echo(
click.style(f"Error: {e}", fg="white", bg="red")
)
success = False
if success:
click.echo(click.style("Validation success!", fg="white", bg="green"))
else:
click.echo(click.style("Validation error!", fg="white", bg="red"))
sys.exit(1)
INSPECT_FORMATS = list(builtin_dumpers.keys())
@main.command()
@click.option("--key", "-k", help="Filters result by key.")
@click.option(
"--env",
"-e",
help="Filters result by environment on --report-mode=inspect.",
default=None,
)
@click.option(
"--format",
"-f",
help="The output format.",
default="json",
type=click.Choice(INSPECT_FORMATS),
)
@click.option(
"--old-first",
"new_first",
"-s",
help="Invert history sorting to 'old-first' on --report-mode=inspect.",
default=True,
is_flag=True,
)
@click.option(
"--limit",
"history_limit",
"-n",
default=None,
type=int,
help="Limits how many history entries are shown on --report-mode=inspect.",
)
@click.option(
"--all",
"_all",
"-a",
default=False,
is_flag=True,
help="Show dynaconf internal settings?",
)
@click.option(
"--report-mode",
"report_mode",
"-m",
default="inspect",
type=click.Choice(["inspect", "debug"]),
)
@click.option(
"-v",
"--verbose",
count=True,
help="Increase verbosity of the output on --report-mode=debug.",
)
def inspect(
key, env, format, new_first, history_limit, _all, report_mode, verbose
): # pragma: no cover
"""
Inspect the loading history of the given settings instance.
Filters by key and environment, otherwise shows all.
"""
if report_mode == "debug":
print_debug_info(
settings,
dumper=format,
verbosity=verbose,
key=key,
)
click.echo()
elif report_mode == "inspect":
try:
inspect_settings(
settings,
key=key,
env=env or None,
dumper=format,
new_first=new_first,
include_internal=_all,
history_limit=history_limit,
print_report=True,
)
click.echo()
except (KeyNotFoundError, EnvNotFoundError, OutputFormatError) as err:
click.echo(err)
sys.exit(1)
else:
click.echo("Invalid report mode")
sys.exit(1)
if __name__ == "__main__": # pragma: no cover
main()
|