Skip to content

Commit

Permalink
fix the regex
Browse files Browse the repository at this point in the history
  • Loading branch information
bojiang committed Sep 20, 2022
1 parent b908da2 commit 3fbbf51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bentoml/_internal/configuration/__init__.py
Expand Up @@ -30,7 +30,7 @@ class version_mod:
DEBUG_ENV_VAR = "BENTOML_DEBUG"
QUIET_ENV_VAR = "BENTOML_QUIET"
CONFIG_ENV_VAR = "BENTOML_CONFIG"
CONFIG_OVERRIDE_ENV_VAR = "BENTOML_CONFIG_OPTION_OVERRIDE"
CONFIG_OVERRIDE_ENV_VAR = "BENTOML_CONFIG_OPTIONS"
# https://github.com/grpc/grpc/blob/master/doc/environment_variables.md
GRPC_DEBUG_ENV_VAR = "GRPC_VERBOSITY"

Expand Down
19 changes: 16 additions & 3 deletions bentoml/_internal/configuration/containers.py
Expand Up @@ -266,15 +266,28 @@ def __init__(
config_merger.merge(self.config, override_config)

if override_config_values is not None:
logger.info("Applying user config override from ENV VAR")
lines = split_with_quotes(override_config_values, sep="\n", quote='"')
logger.info(
"Applying user config override from ENV VAR: %s"
% override_config_values
)
lines = split_with_quotes(
override_config_values,
sep=r"\s+",
quote='"',
use_regex=True,
)
override_config_map = {
k: v
for k, v in [
split_with_quotes(line, sep="=", quote='"') for line in lines
]
}
override_config = unflatten(override_config_map) # type: ignore
try:
override_config = unflatten(override_config_map) # type: ignore
except ValueError as e:
raise BentoMLConfigException(
f'Failed to parse config options from the env var: {e}. \n *** Note: You can use " to quote the key if it contains special characters. ***'
) from None
config_merger.merge(self.config, override_config)

if override_config_file is not None or override_config_values is not None:
Expand Down
26 changes: 20 additions & 6 deletions bentoml/_internal/utils/__init__.py
Expand Up @@ -172,24 +172,38 @@ def _(*args: P.args, **kwargs: P.kwargs) -> t.Optional[_T_co]:
return _


def split_with_quotes(s: str, sep: str = ",", quote: str = '"') -> list[str]:
def split_with_quotes(
s: str,
sep: str = ",",
quote: str = '"',
use_regex: bool = False,
) -> list[str]:
"""
Split a string with quotes, e.g.:
>>> split_with_quotes('a,b,"c,d",e')
['a', 'b', 'c,d', 'e']
"""
reg = "({quote}[^{quote}]*{quote})|({sep})".format(
quote=re.escape(quote),
sep=re.escape(sep),
)
if use_regex:
assert (
"(" not in sep and ")" not in sep
), "sep cannot contain '(' or ')' when using regex"
reg = "({quote}[^{quote}]*{quote})|({sep})".format(
quote=quote,
sep=sep,
)
else:
reg = "({quote}[^{quote}]*{quote})|({sep})".format(
quote=re.escape(quote),
sep=re.escape(sep),
)
raw_parts = re.split(reg, s)
parts: list[str] = []
part_begin = 0
for i in range(0, len(raw_parts), 3):
if i + 2 > len(raw_parts):
parts.append("".join(filter(None, raw_parts[part_begin : i + 2])))
continue
if raw_parts[i + 2] == sep:
if raw_parts[i + 2] is not None:
parts.append("".join(filter(None, raw_parts[part_begin : i + 2])))
part_begin = i + 3
continue
Expand Down

0 comments on commit 3fbbf51

Please sign in to comment.