Beispiel #1
0
def _list(env, key, more, loader, _all=False, output=None, flat=False):
    """Lists all user defined config values
    and if `--all` is passed it also shows dynaconf internal variables.
    """
    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()

    click.echo(
        click.style(
            "Working in %s environment " % cur_env,
            bold=True,
            bg="bright_blue",
            fg="bright_white",
        ))

    if not loader:
        data = settings.as_dict(env=env, internal=_all)
    else:
        identifier = "{}_{}".format(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 "green"

    def format_setting(_k, _v):
        return "{key}{data_type} {value}".format(
            key=click.style(_k, bg=color(_k), fg="white"),
            data_type=click.style("<{}>".format(type(_v).__name__),
                                  bg="bright_black",
                                  fg="white"),
            value=pprint.pformat(_v),
        )

    if not key:
        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, data, env=not flat and cur_env)
    else:
        key = upperfy(key)
        value = data.get(key)
        if not value:
            click.echo(click.style("Key not found", bg="red", fg="white"))
            return
        click.echo(format_setting(key, value))
        if output:
            loaders.write(output, {key: value}, env=not flat and cur_env)

    if env:
        settings.setenv()
Beispiel #2
0
def _list(env, key, more, loader, _all=False, output=None):
    """Lists all user defined config values
    and if `--all` is passed it also shows dynaconf internal variables.
    """
    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()

    click.echo(
        click.style(
            "Working in %s environment " % cur_env,
            bold=True,
            bg="blue",
            fg="bright_black",
        ))

    if not loader:
        data = settings.as_dict(env=env, internal=_all)
    else:
        identifier = "{}_{}".format(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 "green"

    if not key:
        datalines = "\n".join(
            "%s: %s" %
            (click.style(k, bg=color(k), fg="white"), pprint.pformat(v))
            for k, v in data.items())
        (click.echo_via_pager if more else click.echo)(datalines)
        if output:
            with open(output, "w") as output_file:
                json.dump({cur_env: data}, output_file)
    else:
        key = key.upper()
        value = data.get(key)
        if not value:
            click.echo(click.style("Key not found", bg="red", fg="white"))
            return
        click.echo("%s: %s" % (
            click.style(key.upper(), bg=color(key), fg="white"),
            pprint.pformat(value),
        ))
        if output:
            with open(output, "w") as output_file:
                json.dump({cur_env: {key.upper(): value}}, output_file)

    if env:
        settings.setenv()