utils.py 1.56 KB
Newer Older
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
from os.path import dirname, join, realpath
from typing import List

import yaml
from dotmap import DotMap

CURRENT_DIR = dirname(realpath(__file__))


def get_yml_file(name):
    """
    Helper function to load config files.
    """
    with open(join(CURRENT_DIR, name), "r") as f:
        return yaml.load(f)


def load_viewsets_config() -> List[DotMap]:
    """
    Returns the list of api objects without filtering add default attributes
    added to all objects if they are missing.
    """

    tmp = get_yml_file("viewsets_config.yml")
    api_config = []
    for viewset in tmp.keys():
        obj = tmp[viewset]
        obj["viewset"] = viewset
        api_config.append(obj)

    # clean api_config (add default arguments)
    DEFAULT_SETTINGS = {
        "is_api_view": False,
        "requires_testing": False,
        "viewset_permission": "default",
        "api_attr": None,
        "api_name": None,
    }

    for obj in api_config:
        for key in DEFAULT_SETTINGS:
            if key not in obj:
                obj[key] = DEFAULT_SETTINGS[key]

    return [DotMap(obj) for obj in api_config]


def load_models_config() -> DotMap:
    """
    Load the `models_config.yml` config file and returns it as
    a `DotMap`/dict. No transformation whatsoever is applied.
    """

    data = get_yml_file("models_config.yml")
    return DotMap(data)


def load_defaults() -> DotMap:
    """
    Load the `defaults.yml` config file and returns it as
    a `DotMap`/dict. No transformation whatsoever is applied.
    """

    data = get_yml_file("defaults.yml")
    return DotMap(data)