Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import sqlalchemy as sa
from sqlalchemy.pool import QueuePool
import clavis
from dynaconf import settings
from awokado.db_helper import Database
database = Database.from_config(settings)
DATABASE_URL = database.db_url
DATABASE_PASSWORD = database.DATABASE_PASSWORD
DATABASE_HOST = database.DATABASE_HOST
DATABASE_USER = database.DATABASE_USER
DATABASE_PORT = database.DATABASE_PORT
DATABASE_DB = database.DATABASE_DB
clavis.configure(DATABASE_URL)
persistent_engine = sa.create_engine(
DATABASE_URL,
encoding="utf-8",
echo=settings.get("DB_ECHO", False),
poolclass=QueuePool,
pool_size=settings.get("DB_CONN_POOL_SIZE", 10),
assert settings.USERNAME == "admin"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("#" * 79)
print("\n* Default and Global settings\n")
print("The env [global] is to override values defined in any other env")
print("It means a value in [global] will always take high precedence")
print("To override any value in [global] just put the value in .toml file")
print("Or via envvars export a value with DYNACONF_ prefix")
print('$ export DYNACONF_USERNAME="NewUsername"')
print('You can also put the value in .env file"')
# this next line is not needed in your program, it is the same as
# `export DYNACONF_USERNAME..`
os.environ["DYNACONF_USERNAME"] = "NewUsername"
settings.reload() # noqa
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
print("settings.EXTRA_VALUE = ", settings.EXTRA_VALUE)
assert settings.SERVER == "customserver.com"
assert settings.PORT == 5555
assert settings.USERNAME == "NewUsername"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
assert settings.EXTRA_VALUE == "Value defined in .env"
print("#" * 79)
print("\n* Using environment variables at program call\n")
print("Another common pattern is defining the value in the program call")
print("$ DYNACONF_USERNAME=YetAnotherUser python program.py")
# this next line is not needed in your program, it is the same as
# `export DYNACONF_USERNAME..`
os.environ["DYNACONF_USERNAME"] = "YetAnotherUser"
settings.reload() # noqa
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
from dynaconf import settings
assert settings.MESSAGE == "Hello from tmp"
print(settings.MESSAGE) # noqa
def upload_profiling_info_to_s3(profile):
stats = pstats.Stats(profile)
marshaled_stats = marshal.dumps(stats.stats)
s3client = boto3.client(
"s3",
aws_access_key_id=settings.AWOKADO_AWS_S3_DEBUG_PROFILING_ACCESS_KEY,
aws_secret_access_key=settings.AWOKADO_AWS_S3_DEBUG_PROFILING_SECRET_KEY,
)
now = datetime.datetime.now()
rand_str = "".join(random.choice(string.ascii_lowercase) for _ in range(10))
key = (
f"profiling"
f"/{now.strftime('%Y-%m-%d')}"
f"/{now.strftime('%Y-%m-%dT%H-%M-%S')}-{rand_str}"
f".prof"
)
kwargs = dict(
Body=marshaled_stats,
Bucket=settings.AWOKADO_AWS_S3_DEBUG_PROFILING_BUCKET_NAME,
Key=key,
)
print("Using password: {}".format(password))
print("-" * 79)
# imagine it connects here...
# The `connect` function needs to take the server, username and value
# and those values must be read from settings.toml config file
# The `password` is sensitive so it comes from .secrets.toml file
# or even better it may come from vaultproject.io
# Dynaconf takes care of it!
from dynaconf import settings # noqa
print(settings.dynaconf_banner)
print("#" * 79)
print("\n* The settings are defined in .toml files\n")
print("$ cat settings.toml")
with io.open(
settings.find_file("settings.toml"),
encoding=os.environ.get("ENCODING_FOR_DYNACONF"),
) as settings_file:
print(settings_file.read())
print("$ cat .secrets.toml")
with io.open(
settings.find_file(".secrets.toml"),
encoding=os.environ.get("ENCODING_FOR_DYNACONF"),
) as secrets_file:
print(secrets_file.read())
print(settings.TEST_LOADERS)
print(settings.MONEY)
print(settings.AGE)
print(settings.ENABLED)
print(settings.CUSTOM)
print("* Switiching to production")
# using [production] env values for context
with settings.using_env("PRODUCTION"):
print(settings.CUSTOM)
print(settings.HOST)
print("* Switiching to development")
# back to default [development] env
print(settings.get("CUSTOM"))
print(settings.HOST)
print("* Switiching to production")
# set env to [production]:
settings.setenv("production")
print(settings.HOST)
print(settings.CUSTOM)
print("* Switiching to development")
# back to [development] env again
settings.setenv()
print(settings.HOST)
print(settings.get("INEXISTENT")) # None
print(settings.WORKS)
print(settings.get("ENVIRONMENT"))
print(settings.HOST)
print(settings.WORKS)
assertions = {
"HOST": "dev_server.com",
"PORT": 5000,
"ENVIRONMENT": "this is development",
"WORKS": "yaml_as_extra_config",
}
for key, value in assertions.items():
found = settings.get(key)
assert found == getattr(settings, key)
assert (
found == value
), "expected: {key}: [{value}] found: [{found}]".format(**locals())
assertions = {
"HOST": "prod_server.com",
"PORT": 5000,
"ENVIRONMENT": "this is production",
}
for key, value in assertions.items():
found = settings.from_env("production").get(key)
assert found == getattr(settings.from_env("production"), key)
assert (
from dynaconf import settings
assert settings.FIRST_VAR == "first_value"
assert settings.SECOND_VAR == "second_value"
assert settings.EXTRA_VAR == "extra_value"