Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable updating the default config parser even after loading luigi #3135

Merged
merged 2 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions luigi/configuration/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
'toml': LuigiTomlParser,
}

# select parser via env var
DEFAULT_PARSER = 'cfg'
PARSER = os.environ.get('LUIGI_CONFIG_PARSER', DEFAULT_PARSER)
if PARSER not in PARSERS:
warnings.warn("Invalid parser: {parser}".format(parser=PARSER))
PARSER = DEFAULT_PARSER


def _get_default_parser():
parser = os.environ.get('LUIGI_CONFIG_PARSER', DEFAULT_PARSER)
if parser not in PARSERS:
warnings.warn("Invalid parser: {parser}".format(parser=DEFAULT_PARSER))
parser = DEFAULT_PARSER
return parser


def _check_parser(parser_class, parser):
Expand All @@ -50,9 +53,11 @@ def _check_parser(parser_class, parser):
raise ImportError(msg.format(parser=parser))


def get_config(parser=PARSER):
def get_config(parser=None):
"""Get configs singleton for parser
"""
if parser is None:
parser = _get_default_parser()
parser_class = PARSERS[parser]
_check_parser(parser_class, parser)
return parser_class.instance()
Expand All @@ -66,21 +71,22 @@ def add_config_path(path):
return False

# select parser by file extension
default_parser = _get_default_parser()
_base, ext = os.path.splitext(path)
if ext and ext[1:] in PARSERS:
parser = ext[1:]
else:
parser = PARSER
parser = default_parser
parser_class = PARSERS[parser]

_check_parser(parser_class, parser)
if parser != PARSER:
if parser != default_parser:
msg = (
"Config for {added} parser added, but used {used} parser. "
"Set up right parser via env var: "
"export LUIGI_CONFIG_PARSER={added}"
)
warnings.warn(msg.format(added=parser, used=PARSER))
warnings.warn(msg.format(added=parser, used=default_parser))

# add config path to parser
parser_class.add_config_path(path)
Expand Down
11 changes: 10 additions & 1 deletion test/config_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
import os

from luigi.configuration import LuigiConfigParser, get_config
from luigi.configuration import LuigiConfigParser, LuigiTomlParser, get_config
from luigi.configuration.cfg_parser import InterpolationMissingEnvvarError

from helpers import LuigiTestCase, with_config
Expand All @@ -43,6 +43,8 @@ def tearDown(self):
os.environ.pop(key)
for key, value in self.environ_backup:
os.environ[key] = value
if 'LUIGI_CONFIG_PARSER' in os.environ:
del os.environ["LUIGI_CONFIG_PARSER"]

@with_config({"test": {
"a": "testval",
Expand Down Expand Up @@ -95,3 +97,10 @@ def test_underscore_vs_dash_style_priority(self):
config = get_config()
self.assertEqual(config.get("test", "foo-bar"), "bax")
self.assertEqual(config.get("test", "foo_bar"), "bax")

def test_default_parser(self):
config = get_config()
self.assertIsInstance(config, LuigiConfigParser)
os.environ["LUIGI_CONFIG_PARSER"] = "toml"
config = get_config()
self.assertIsInstance(config, LuigiTomlParser)