feature:inject-config

Introduction

feature:inject-config writes your feature’s config.py from the environment variables its code reads. The framework calls its inject_config(app) when it loads the feature, so every variable lands in app.config with its default. Run it after you add an os.getenv.

splent feature:inject-config splent_feature_followers
  feature:inject-config — splent_feature_followers

  Detected 2 env var(s):
    FOLLOWERS_DIGEST_HOUR               default: "8"
    FOLLOWERS_MAX_PER_DAY               default: "50"

  ✅ config.py created.
"""
followers feature configuration.

Auto-generated by `splent feature:inject-config`.
Injects environment variables into Flask app.config.
"""

import os


def inject_config(app):
    app.config.update({
        "FOLLOWERS_DIGEST_HOUR": int(os.getenv("FOLLOWERS_DIGEST_HOUR", "8")),
        "FOLLOWERS_MAX_PER_DAY": int(os.getenv("FOLLOWERS_MAX_PER_DAY", "50")),
    })

Previewing and updating

splent feature:inject-config splent_feature_followers --dry-run

--dry-run prints the file and writes nothing. Run without it after you read a new variable, and the whole file is generated again (config.py updated (1 new var(s)).). When nothing is missing, it prints config.py is already up to date.


Replacing a config.py you wrote by hand

splent feature:inject-config splent_feature_followers --force

A config.py without the Auto-generated line, and that is not the empty scaffold of feature:create, was written by hand. The command refuses to regenerate it and exits 1, so add the variables yourself or pass --force.


What it detects

It reads every .py file of the package except config.py and migrations/. It finds os.getenv, os.environ.get and os.environ[...] with an uppercase name. Without a default, the default is "".

Default in the code In config.py
True, False, "true", "false" os.getenv("KEY", "True").lower() == "true"
An integer, or a string of digits int(os.getenv("KEY", "3600"))
Another literal, or a call on os or a builtin os.getenv("KEY", <default>)
A name config.py does not import os.getenv("KEY") # default in source: <expr>, with a warning

Reference

Argument or option Description
FEATURE_REF splent_feature_followers or <namespace>/splent_feature_followers. The editable copy at the workspace root
--dry-run Print the generated config.py without writing it
--force Replace a config.py written by hand

It exits 1 when the feature is not at the workspace root, when config.py was written by hand and --force is missing, or when the generated file is not valid Python.


Requirements

  • A selected product (SPLENT_APP).

See also


Back to top

splent. Distributed by an LGPL license v3. Contact us: drorganvidez@us.es