goodconf
goodconf copied to clipboard
Allow config saving
Right now we can only load
the settings, but it would be nice to have a save
method to save changed configuration values. It should keep the user comments on the file
I'm not sure if this feature fits in the project, but it could be supported by subclassing the model and adding your own method.
What is the use case for this?
Sometimes it's interesting for a command line tool to be able to change the program's configuration, you can think for example how git
works.
I agree that this can be achieved through subclassing, but then the rest of the goodconf users won't have access to the feature. If you think this is a corner case I'm happy to close the issue.
If I finally implement this in a subclass, would you be interested in a PR?
If the code is straightforward and doesn't create a significant maintenance burden, sure a PR would be great!
Hi @ipmb long time no see. I've started using this snippet in my configs to save the contents:
from ruyaml import YAML
class YamlStorage(GoodConf):
"""Adapter to store and load information from a yaml file."""
@property
def config_file(self) -> str:
"""Return the path to the config file."""
return str(self._config_file)
@property
def store_dir(self) -> Path:
"""Return the path to the store directory."""
return Path(self.config_file).parent
def reload(self) -> None:
"""Reload the contents of the authentication store."""
self.load(self.config_file)
def load(self, filename: Optional[str] = None) -> None:
"""Load a configuration file."""
if not filename:
filename = f"{self.store_dir}/data.yaml"
super().load(self.config_file)
def save(self) -> None:
"""Save the contents of the authentication store."""
with open(self.config_file, "w+", encoding="utf-8") as file_cursor:
yaml = YAML()
yaml.default_flow_style = False
yaml.dump(self.dict(), file_cursor)
I don't know if it's of any help