From 3000aeee0918a26b5b65ca6fe68200eed0e8c04f 2020-04-21 17:35:56 From: Brett Smith Date: 2020-04-21 17:35:56 Subject: [PATCH] config: Add Config.books_loader() method. --- diff --git a/conservancy_beancount/config.py b/conservancy_beancount/config.py index 4064981645732999173cbd4a31568384693c9208..643a1852136e711d4e90b6c42428d755a446fc55 100644 --- a/conservancy_beancount/config.py +++ b/conservancy_beancount/config.py @@ -111,6 +111,13 @@ class Config: retval = default or (Path.home() / self._ENVIRON_DEFAULT_PATHS[key]) return retval + def books_loader(self) -> Optional[books.Loader]: + books_path = self.books_path() + if books_path is None: + return None + else: + return books.Loader(books_path, self.fiscal_year_begin()) + def books_path(self) -> Optional[Path]: try: retval = Path(self.file_config['Beancount'].get('books dir')) diff --git a/tests/test_config.py b/tests/test_config.py index 284bb984855ad53ac493916adfc0bd2b538e988e..e18a20eabac7071e05794bc68a93a9f704c15b77 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -381,3 +381,14 @@ def test_default_fiscal_year_begin(): actual = config.fiscal_year_begin() assert actual.month == 3 assert actual.day == 1 + +def test_books_loader(): + books_path = testutil.test_path('bookstest') + config = config_mod.Config() + config.load_string(f'[Beancount]\nbooks dir = {books_path}\n') + loader = config.books_loader() + expected = 'include "{}"'.format(books_path / 'books/2020.beancount') + assert loader.fy_range_string(0, 2020, {}) == expected + +def test_books_loader_without_books(): + assert config_mod.Config().books_loader() is None