-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Dont write defaults to config #1188
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
Changes from all commits
39cac68
01def7f
e8d8800
6aa943f
caf1f6d
ca3ce66
2684151
9aaf593
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| from feast.constants import ConfigOptions as opt | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
| _UNSET = object() | ||
|
|
||
|
|
||
| def _init_config(path: str): | ||
|
|
@@ -50,17 +51,14 @@ def _init_config(path: str): | |
| os.makedirs(os.path.dirname(config_dir), exist_ok=True) | ||
|
|
||
| # Create the configuration file itself | ||
| config = ConfigParser(defaults=opt().defaults()) | ||
| config = ConfigParser(defaults=opt().defaults(), allow_no_value=True) | ||
| if os.path.exists(path): | ||
| config.read(path) | ||
|
|
||
| # Store all configuration in a single section | ||
| if not config.has_section(CONFIG_FILE_SECTION): | ||
| config.add_section(CONFIG_FILE_SECTION) | ||
|
|
||
| # Save the current configuration | ||
| config.write(open(path, "w")) | ||
|
|
||
| return config | ||
|
|
||
|
|
||
|
|
@@ -117,69 +115,66 @@ def __init__( | |
| self._config = config # type: ConfigParser | ||
| self._path = path # type: str | ||
|
|
||
| def get(self, option): | ||
| def _get(self, option, default, get_method): | ||
| fallback = {} if default is _UNSET else {"fallback": default} | ||
| return get_method( | ||
| CONFIG_FILE_SECTION, | ||
| option, | ||
| vars={**_get_feast_env_vars(), **self._options}, | ||
| **fallback, | ||
| ) | ||
|
|
||
| def get(self, option, default=_UNSET): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mike0sv How are you planning for people to use this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, his option can be used if there is no value even in |
||
| """ | ||
| Returns a single configuration option as a string | ||
|
|
||
| Args: | ||
| option: Name of the option | ||
| default: Default value to return if option is not found | ||
|
|
||
| Returns: String option that is returned | ||
|
|
||
| """ | ||
| return self._config.get( | ||
| CONFIG_FILE_SECTION, | ||
| option, | ||
| vars={**_get_feast_env_vars(), **self._options}, | ||
| ) | ||
| return self._get(option, default, self._config.get) | ||
|
|
||
| def getboolean(self, option): | ||
| def getboolean(self, option, default=_UNSET): | ||
| """ | ||
| Returns a single configuration option as a boolean | ||
|
|
||
| Args: | ||
| option: Name of the option | ||
| default: Default value to return if option is not found | ||
|
|
||
| Returns: Boolean option value that is returned | ||
|
|
||
| """ | ||
| return self._config.getboolean( | ||
| CONFIG_FILE_SECTION, | ||
| option, | ||
| vars={**_get_feast_env_vars(), **self._options}, | ||
| ) | ||
| return self._get(option, default, self._config.getboolean) | ||
|
|
||
| def getint(self, option): | ||
| def getint(self, option, default=_UNSET): | ||
| """ | ||
| Returns a single configuration option as an integer | ||
|
|
||
| Args: | ||
| option: Name of the option | ||
| default: Default value to return if option is not found | ||
|
|
||
| Returns: Integer option value that is returned | ||
|
|
||
| """ | ||
| return self._config.getint( | ||
| CONFIG_FILE_SECTION, | ||
| option, | ||
| vars={**_get_feast_env_vars(), **self._options}, | ||
| ) | ||
| return self._get(option, default, self._config.getint) | ||
|
|
||
| def getfloat(self, option): | ||
| def getfloat(self, option, default=_UNSET): | ||
| """ | ||
| Returns a single configuration option as an integer | ||
|
|
||
| Args: | ||
| option: Name of the option | ||
| default: Default value to return if option is not found | ||
|
|
||
| Returns: Float option value that is returned | ||
|
|
||
| """ | ||
| return self._config.getfloat( | ||
| CONFIG_FILE_SECTION, | ||
| option, | ||
| vars={**_get_feast_env_vars(), **self._options}, | ||
| ) | ||
| return self._get(option, default, self._config.getfloat) | ||
|
|
||
| def set(self, option, value): | ||
| """ | ||
|
|
@@ -211,7 +206,12 @@ def save(self): | |
| Save the current configuration to disk. This does not include | ||
| environmental variables or initialized options | ||
| """ | ||
| self._config.write(open(self._path, "w")) | ||
| defaults = self._config.defaults() | ||
| try: | ||
| self._config._defaults = {} | ||
| self._config.write(open(self._path, "w")) | ||
| finally: | ||
| self._config._defaults = defaults | ||
|
|
||
| def __str__(self): | ||
| result = "" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is the
object()type used here? Is there any reason why we can't usedefault=None?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a signleton, just like
configparser._UNSET. If you useNone, either you can't useNoneas default value, or you'll getNoneeven if you don't want default value and do expect an exception