diff --git a/conservancy_beancount/config.py b/conservancy_beancount/config.py index d03ea59915da20b518d4bcf4a3e96d3531621a9b..6d1157ad4d2a5e9c6332fd348e4f7f5d32bd369f 100644 --- a/conservancy_beancount/config.py +++ b/conservancy_beancount/config.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import configparser import decimal import functools import os @@ -74,6 +75,15 @@ class Config: 'XDG_CONFIG_HOME': Path('.config'), } + def __init__(self) -> None: + self.file_config = configparser.ConfigParser() + + def load_file(self, config_path: Optional[Path]=None) -> None: + if config_path is None: + config_path = self.config_file_path() + with config_path.open() as config_file: + self.file_config.read_file(config_file) + def _dir_or_none(self, path: Path) -> Optional[Path]: try: path.mkdir(exist_ok=True) @@ -94,6 +104,15 @@ class Config: retval = default or (Path.home() / self._ENVIRON_DEFAULT_PATHS[key]) return retval + def books_path(self) -> Optional[Path]: + try: + retval = Path(self.file_config['Beancount'].get('books dir')) + except (KeyError, ValueError): + ok = False + else: + ok = retval.is_absolute() + return retval if ok else None + def cache_dir_path(self, name: str='conservancy_beancount') -> Optional[Path]: cache_root = self._path_from_environ('XDG_CACHE_HOME') return (