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

feat: configuration versioning #3052

Merged
merged 7 commits into from Nov 28, 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
7 changes: 5 additions & 2 deletions src/bentoml/__init__.py
Expand Up @@ -17,11 +17,12 @@

from typing import TYPE_CHECKING

from ._internal.configuration import load_config
from ._internal.configuration import save_config
from ._internal.configuration import BENTOML_VERSION as __version__
from ._internal.configuration import load_global_config

# Inject dependencies and configurations
load_global_config()
load_config()

# Bento management APIs
from .bentos import get
Expand Down Expand Up @@ -175,4 +176,6 @@
"transformers",
"xgboost",
"monitor",
"load_config",
"save_config",
]
30 changes: 15 additions & 15 deletions src/bentoml/_internal/configuration/__init__.py
@@ -1,11 +1,13 @@
from __future__ import annotations

import os
import re
import typing as t
import logging
from functools import lru_cache

from bentoml.exceptions import BentoMLException
from bentoml.exceptions import BentoMLConfigException
from ...exceptions import BentoMLException
from ...exceptions import BentoMLConfigException

try:
from ..._version import __version__
Expand Down Expand Up @@ -87,14 +89,14 @@ def is_pypi_installed_bentoml() -> bool:
return is_tagged and is_clean and not_been_modified


def get_bentoml_config_file_from_env() -> t.Optional[str]:
def get_bentoml_config_file_from_env() -> str | None:
if CONFIG_ENV_VAR in os.environ:
# User local config file for customizing bentoml
return expand_env_var(os.environ.get(CONFIG_ENV_VAR, ""))
return None


def get_bentoml_override_config_from_env() -> t.Optional[str]:
def get_bentoml_override_config_from_env() -> str | None:
if CONFIG_OVERRIDE_ENV_VAR in os.environ:
# User local config options for customizing bentoml
return os.environ.get(CONFIG_OVERRIDE_ENV_VAR, None)
Expand Down Expand Up @@ -129,7 +131,7 @@ def get_quiet_mode() -> bool:
return False


def load_global_config(bentoml_config_file: t.Optional[str] = None):
def load_config(bentoml_config_file: str | None = None):
"""Load global configuration of BentoML"""

from .containers import BentoMLContainer
Expand All @@ -141,24 +143,22 @@ def load_global_config(bentoml_config_file: t.Optional[str] = None):
if bentoml_config_file:
if not bentoml_config_file.endswith((".yml", ".yaml")):
raise BentoMLConfigException(
"BentoML config file specified in ENV VAR does not end with `.yaml`: "
f"`BENTOML_CONFIG={bentoml_config_file}`"
f"BentoML config file specified in ENV VAR does not end with either '.yaml' or '.yml': 'BENTOML_CONFIG={bentoml_config_file}'"
) from None
if not os.path.isfile(bentoml_config_file):
raise FileNotFoundError(
"BentoML config file specified in ENV VAR not found: "
f"`BENTOML_CONFIG={bentoml_config_file}`"
f"BentoML config file specified in ENV VAR not found: 'BENTOML_CONFIG={bentoml_config_file}'"
) from None

bentoml_configuration = BentoMLConfiguration(
override_config_file=bentoml_config_file,
override_config_values=get_bentoml_override_config_from_env(),
BentoMLContainer.config.set(
BentoMLConfiguration(
override_config_file=bentoml_config_file,
override_config_values=get_bentoml_override_config_from_env(),
).to_dict()
)

BentoMLContainer.config.set(bentoml_configuration.as_dict())


def save_global_config(config_file_handle: t.IO[t.Any]):
def save_config(config_file_handle: t.IO[t.Any]):
import yaml

from ..configuration.containers import BentoMLContainer
Expand Down