dynaconf icon indicating copy to clipboard operation
dynaconf copied to clipboard

How can I add a export var to override a nested section through @jinja syntax ??

Open msclock opened this issue 4 years ago • 2 comments

How can I add a export var to override a field in nested section through @jinja syntax ??

There is my situation:

default_settings.toml:

[default]
base = "@jinja {{this.current_env | lower}}"
topic = "15021"

[default.mq]
routekey_prefix = "Topic-CAD-Dispatch-Cmd-RouteKey-"
routekey = "@jinja {{this.mq.routekey_prefix}}{{this.base}}.{{this.topic}}"

When I export this topic=11, this lazy loader can not substitute {{this.topic}} in the routekey correctly.I need some help,please.

Thank you.

msclock avatar Dec 09 '21 09:12 msclock

Is this solved? @msclock or can we keep it open to track it?

rochacbruno avatar Dec 10 '21 11:12 rochacbruno

Hi, I just tested the latest release version 3.1.7, and the problem remains unresolved! @rochacbruno

Now,I just add some top vars to overwrite my target vars in nested section.Like this:

default_settings.yaml:

default:
  base: "@jinja {{this.current_env | lower}}"
  topic: "15021"
  db_database: &var_database "@jinja {{this.db.database_prefix}}{{this.base}}"
  minio_origin_file_bucket: &var_origin_file_bucket "@jinja {{this.minio.origin_file_bucket_prefix}}{{this.base}}"
  minio_result_file_bucket: &var_result_file_bucket "@jinja {{this.minio.result_file_bucket_prefix}}{{this.base}}"
  mq_exchange: &var_exchange "@jinja {{this.mq.exchange_prefix}}{{this.base}}"
  mq_routekey: &var_routekey "@jinja {{this.mq.routekey_prefix}}{{this.base}}.{{this.topic}}"
  mq_queue_listen: &var_queue_listen "@jinja {{this.mq.queue_listen_prefix}}{{this.base}}-{{this.topic}}"
  mq_queue_response: &var_queue_response "@jinja {{this.mq.queue_response_prefix}}{{this.base}}"

 db:
    database_prefix: "drawing_analysis_"
    database: *var_database

  minio:
    origin_file_bucket_prefix: "dwg-"
    origin_file_bucket: *var_origin_file_bucket
    result_file_bucket_prefix: "analyser-"
    result_file_bucket: *var_result_file_bucket

  mq:
    exchange_prefix: "Topic-CAD-Dispatch-Cmd-Ex-"
    exchange: *var_exchange
    routekey_prefix: "Topic-CAD-Dispatch-Cmd-RouteKey-"
    routekey: *var_routekey
    queue_listen_prefix: "Topic-CAD-Dispatch-Cmd-"
    queue_listen: *var_queue_listen
    queue_response_prefix: "CAD-Dispatch-R-Cmd-"
    queue_response: *var_queue_response

config.py:


settings = Dynaconf(
    env_switcher="LAUNCH_ENV",
    settings_files=[
        "configs/default_settings.yaml",  
        "configs/settings.yaml",  
        "configs/.secrets.yaml",
    ],
    environments=True,
    envvar_prefix="CONF",
    lowercase_read=True,
)

def popluate_config_sections(settings):
    """overwite fields in nested section"""
    settings.db.database = settings.db_database
    settings.minio.origin_file_bucket = settings.minio_origin_file_bucket
    settings.minio.result_file_bucket = settings.minio_result_file_bucket
    settings.mq.exchange = settings.mq_exchange
    settings.mq.routekey = settings.mq_routekey
    settings.mq.queue_listen = settings.mq_queue_listen
    settings.mq.queue_response = settings.mq_queue_response


popluate_config_sections(settings=settings)

When exporting environment variable CONF_TOPIC = 23000, I can get the output I want.

msclock avatar Feb 18 '22 03:02 msclock