1 2
import io
2 2
from pathlib import Path
3 2
from warnings import warn
4

5 2
from dynaconf import default_settings
6 2
from dynaconf.constants import YAML_EXTENSIONS
7 2
from dynaconf.loaders.base import BaseLoader
8 2
from dynaconf.utils import object_merge
9 2
from dynaconf.utils.parse_conf import try_to_encode
10 2
from dynaconf.vendor.ruamel import yaml
11

12
# Add support for Dynaconf Lazy values to YAML dumper
13 2
yaml.SafeDumper.yaml_representers[
14
    None
15
] = lambda self, data: yaml.representer.SafeRepresenter.represent_str(
16
    self, try_to_encode(data)
17
)
18

19

20 2
def load(obj, env=None, silent=True, key=None, filename=None):
21
    """
22
    Reads and loads in to "obj" a single key or all keys from source file.
23

24
    :param obj: the settings instance
25
    :param env: settings current env default='development'
26
    :param silent: if errors should raise
27
    :param key: if defined load a single key, else load all in env
28
    :param filename: Optional custom filename to load
29
    :return: None
30
    """
31
    if yaml is None:  # pragma: no cover
32
        BaseLoader.warn_not_installed(obj, "yaml")
33
        return
34

35
    # Resolve the loaders
36
    # https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation
37
    # Possible values are `safe_load, full_load, unsafe_load, load`
38 2
    yaml_reader = getattr(
39
        yaml, obj.get("YAML_LOADER_FOR_DYNACONF"), yaml.safe_load
40
    )
41
    if yaml_reader.__name__ == "unsafe_load":  # pragma: no cover
42
        warn(
43
            "yaml.unsafe_load is deprecated."
44
            " Please read https://msg.pyyaml.org/load for full details."
45
            " Try to use full_load or safe_load."
46
        )
47

48 2
    loader = BaseLoader(
49
        obj=obj,
50
        env=env,
51
        identifier="yaml",
52
        extensions=YAML_EXTENSIONS,
53
        file_reader=yaml_reader,
54
        string_reader=yaml_reader,
55
    )
56 2
    loader.load(filename=filename, key=key, silent=silent)
57

58

59 2
def write(settings_path, settings_data, merge=True):
60
    """Write data to a settings file.
61

62
    :param settings_path: the filepath
63
    :param settings_data: a dictionary with data
64
    :param merge: boolean if existing file should be merged with new data
65
    """
66 2
    settings_path = Path(settings_path)
67
    if settings_path.exists() and merge:  # pragma: no cover
68
        with io.open(
69
            str(settings_path), encoding=default_settings.ENCODING_FOR_DYNACONF
70
        ) as open_file:
71
            object_merge(yaml.safe_load(open_file), settings_data)
72

73 2
    with io.open(
74
        str(settings_path),
75
        "w",
76
        encoding=default_settings.ENCODING_FOR_DYNACONF,
77
    ) as open_file:
78 2
        yaml.dump(
79
            settings_data,
80
            open_file,
81
            Dumper=yaml.dumper.SafeDumper,
82
            explicit_start=True,
83
            indent=2,
84
            default_flow_style=False,
85
        )

Read our documentation on viewing source code .

Loading