Skip to content

Commit

Permalink
Add @path cast prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-dv committed Dec 19, 2020
1 parent c2d8b71 commit 83d9c9d
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -25,7 +25,7 @@
### Install

```bash
$ pip install dynaconf
$ pip install git+https://github.com/mk-dv/dynaconf.git#egg=dynaconf
```

#### Initialize Dynaconf on project root directory
Expand Down
10 changes: 7 additions & 3 deletions dynaconf/base.py
Expand Up @@ -394,7 +394,7 @@ def get(
:param key: The name of the setting value, will always be upper case
:param default: In case of not found it will be returned
:param cast: Should cast in to @int, @float, @bool or @json ?
:param cast: Should cast in to @int, @float, @bool, @json or @path ?
:param fresh: Should reload from loaders store before access?
:param dotted_lookup: Should perform dotted-path lookup?
:param parent: Is there a pre-loaded parent in a nested data?
Expand Down Expand Up @@ -449,7 +449,7 @@ def get_fresh(self, key, default=None, cast=None):
:param key: The name of the setting value, will always be upper case
:param default: In case of not found it will be returned
:param cast: Should cast in to @int, @float, @bool or @json ?
:param cast: Should cast in to @int, @float, @bool, @json or @path ?
:return: The value if found, default or None
"""
return self.get(key, default=default, cast=cast, fresh=True)
Expand All @@ -459,7 +459,7 @@ def get_environ(self, key, default=None, cast=None):
:param key: The name of the setting value, will always be upper case
:param default: In case of not found it will be returned
:param cast: Should cast in to @int, @float, @bool or @json ?
:param cast: Should cast in to @int, @float, @bool, @json or @path ?
or cast must be true to use cast inference
:return: The value if found, default or None
"""
Expand Down Expand Up @@ -492,6 +492,10 @@ def as_json(self, key):
"""Partial method for get with json cast"""
return self.get(key, cast="@json")

def as_path(self, key):
"""Partial method for get with path cast"""
return self.get(key, cast="@path")

@property
def loaded_envs(self):
"""Get or create internal loaded envs list"""
Expand Down
4 changes: 2 additions & 2 deletions dynaconf/contrib/flask_dynaconf.py
Expand Up @@ -32,8 +32,8 @@ class FlaskDynaconf:
# get is case insensitive
app.config['SQL_PORT']
Dynaconf uses `@int, @bool, @float, @json` to cast
env vars
Dynaconf uses `@int, @bool, @float, @json, @path`
to cast env vars
SETTINGS_FILE_FOR_DYNACONF = The name of the module or file to use as
default to load settings. If nothing is
Expand Down
11 changes: 10 additions & 1 deletion dynaconf/utils/parse_conf.py
Expand Up @@ -3,6 +3,7 @@
import re
import warnings
from functools import wraps
from pathlib import Path

from dynaconf.utils import extract_json_objects
from dynaconf.utils import multi_replace
Expand Down Expand Up @@ -141,6 +142,11 @@ def __str__(self):
return str(self.token)


class PathFormatter(BaseFormatter):
def __call__(self, value, **context):
return Path(self.function(value, **context))


def _jinja_formatter(value, **context):
if jinja_env is None: # pragma: no cover
raise ImportError(
Expand All @@ -154,6 +160,7 @@ class Formatters:

python_formatter = BaseFormatter(str.format, "format")
jinja_formatter = BaseFormatter(_jinja_formatter, "jinja")
path_formatter = PathFormatter(str.format, "format")


class Lazy:
Expand Down Expand Up @@ -215,6 +222,7 @@ def evaluate(settings, *args, **kwargs):
"@json": json.loads,
"@format": lambda value: Lazy(value),
"@jinja": lambda value: Lazy(value, formatter=Formatters.jinja_formatter),
"@path": lambda value: Lazy(value, formatter=Formatters.path_formatter),
# Meta Values to trigger pre assignment actions
"@reset": Reset, # @reset is DEPRECATED on v3.0.0
"@del": Del,
Expand Down Expand Up @@ -250,7 +258,7 @@ def parse_with_toml(data):

def _parse_conf_data(data, tomlfy=False, box_settings=None):
"""
@int @bool @float @json (for lists and dicts)
@int @bool @float @json @path (for lists and dicts)
strings does not need converters
export DYNACONF_DEFAULT_THEME='material'
Expand All @@ -259,6 +267,7 @@ def _parse_conf_data(data, tomlfy=False, box_settings=None):
export DYNACONF_PAGINATION_PER_PAGE='@int 20'
export DYNACONF_MONGODB_SETTINGS='@json {"DB": "quokka_db"}'
export DYNACONF_ALLOWED_EXTENSIONS='@json ["jpg", "png"]'
export DYNACONF_BASE_DIR='@path "/tmp/bla.py"
"""
# not enforced to not break backwards compatibility with custom loaders
box_settings = box_settings or {}
Expand Down
1 change: 1 addition & 0 deletions example/app_with_dotenv/.env
Expand Up @@ -4,6 +4,7 @@ DYNACONF_EXAMPLE="Hello World"
DYNACONF_MONEY_VALUE=@float 345.6
DYNACONF_BOOL_VALUE=@bool off
DYNACONF_JSON_VALUE='@json ["a", "b"]'
DYNACONF_PATH_VALUE='@path /tmp/bla.py'
DYNACONF_WORKS=app_with_dotenv
SETTINGS_FILE_FOR_DYNACONF='/tmp/bla.py'

Expand Down
3 changes: 2 additions & 1 deletion example/common-encoding/program.py
Expand Up @@ -155,7 +155,8 @@ def connect(server, port, username, password):
print("and perform the explicit cast when reading")
print('>>> connect(..., settings.as_int("PORT"), ...)')
print(
"types: @int|.as_int, @float|.as_float, @bool|.as_bool and @json|.as_json"
"types: @int|.as_int, @float|.as_float, @bool|.as_bool, @path|.as_path and"
"@json|.as_json"
)

print("#" * 79)
Expand Down
1 change: 1 addition & 0 deletions example/flask_with_ini/.env
Expand Up @@ -16,3 +16,4 @@ FLASK_INTVAR=@int 42
FLASK_FLOATVAR=@float 4.2
FLASK_BOOLVAR=@bool true
FLASK_JSONVAR=@json ["flask", "rocks"]
FLASK_PATHVAR=@path /flask/path
1 change: 1 addition & 0 deletions example/flask_with_toml/.env
Expand Up @@ -14,6 +14,7 @@ FLASK_INTVAR=@int 42
FLASK_FLOATVAR=@float 4.2
FLASK_BOOLVAR=@bool true
FLASK_JSONVAR=@json ["flask", "rocks"]
FLASK_PATHVAR=@path /flask/path

FLASK_OTHER_JSONVAR=['a', 'b']

Expand Down
1 change: 1 addition & 0 deletions example/flask_with_yaml/.env
Expand Up @@ -16,3 +16,4 @@ FLASK_INTVAR=@int 42
FLASK_FLOATVAR=@float 4.2
FLASK_BOOLVAR=@bool true
FLASK_JSONVAR=@json ["flask", "rocks"]
FLASK_PATHVAR=@path /flask/path
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -22,7 +22,7 @@ def read(*names, **kwargs):
setup(
name="dynaconf",
version=read("dynaconf", "VERSION"),
url="https://github.com/rochacbruno/dynaconf",
url="https://github.com/mk-dv/dynaconf",
license="MIT",
author="Bruno Rocha",
author_email="rochacbruno@gmail.com",
Expand Down

0 comments on commit 83d9c9d

Please sign in to comment.