Skip to content

Commit

Permalink
Merge pull request #14 from valentinarho/fix/generate-var-name-when-e…
Browse files Browse the repository at this point in the history
…mpty-prefix

Fix help generation when prefix is empty
  • Loading branch information
hynek committed Mar 28, 2020
2 parents 4b76ba6 + f9b1720 commit 95ab519
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Deprecations:
Changes:
^^^^^^^^

*none*
- Fixed environment variables' names when prefix is empty.
`#14 <https://github.com/hynek/environ-config/pull/14>`_



----
Expand Down
47 changes: 45 additions & 2 deletions src/environ/_environ_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,49 @@ def _format_help_dicts(help_dicts, display_defaults=False):
return "\n".join(help_strs)


def _generate_var_name(prefix, field_name):
"""
Generate the environment variable name, given a prefix
and the configuration field name.
Examples:
>>> _generate_var_name("", "some_var")
"SOME_VAR"
>>> _generate_var_name("my_app", "some_var")
"MY_APP_SOME_VAR"
:param prefix: the prefix to be used, can be empty
:param field_name: the name of the field from which the variable is derived
"""
return (
"_".join((prefix, field_name)).upper()
if prefix
else field_name.upper()
)


def _generate_new_prefix(current_prefix, class_name):
"""
Generate the new prefix to be used when handling nested configurations.
Examples:
>>> _generate_new_prefix("", "config_group_1")
"CONFIG_GROUP_1"
>>> _generate_new_prefix("my_app", "another_config_group")
"MY_APP_ANOTHER_CONFIG_GROUP"
:param prefix: the prefix to be used, can be empty
:param field_name: the name of the field from which the variable is derived
"""
return (
"_".join((current_prefix, class_name)).upper()
if current_prefix
else class_name.upper()
)


def _generate_help_dicts(config_cls, _prefix=None):
"""
Generate dictionaries for use in building help strings.
Expand Down Expand Up @@ -289,7 +332,7 @@ def _generate_help_dicts(config_cls, _prefix=None):
continue
if ce.sub_cls is None: # Base case for "leaves".
if ce.name is None:
var_name = "_".join((_prefix, a.name)).upper()
var_name = _generate_var_name(_prefix, a.name)
else:
var_name = ce.name
req = ce.default == RAISE
Expand All @@ -301,7 +344,7 @@ def _generate_help_dicts(config_cls, _prefix=None):
help_dicts.append(help_dict)
else: # Construct the new prefix and recurse.
help_dicts += _generate_help_dicts(
ce.sub_cls, _prefix="_".join((_prefix, a.name)).upper()
ce.sub_cls, _prefix=_generate_new_prefix(_prefix, a.name)
)
return help_dicts

Expand Down
20 changes: 20 additions & 0 deletions tests/test_environ_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class Child(object):
child = environ.group(Child)


@environ.config(prefix="")
class NoPrefix(object):
a_var = environ.var(help="a_var, no default")
another_var = environ.var("bar", help="another_var, has default")
_start_with_underscore = environ.var(help="this starts with an underscore")


class TestEnvironConfig(object):
def test_empty(self):
"""
Expand Down Expand Up @@ -279,6 +286,19 @@ def test_generate_help_str_with_defaults(self):
FOO_CHILD_VAR14 (Required)"""
)

def test_generate_help_str_when_prefix_is_empty(self):
"""
Environment variables' names don't start with an underscore
"""
help_str = environ.generate_help(NoPrefix)

assert (
help_str
== """A_VAR (Required): a_var, no default
ANOTHER_VAR (Optional): another_var, has default
_START_WITH_UNDERSCORE (Required): this starts with an underscore"""
)

def test_custom_formatter(self):
"""
Custom formatters can be passed and are used.
Expand Down

0 comments on commit 95ab519

Please sign in to comment.