From 37f656a9d6babc4e82ed4e4399b442b5cdacee0b Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Mon, 14 Oct 2024 13:42:26 +0300 Subject: [PATCH] add: strick check options --- README.md | 25 +++++++++++++++---------- foliant/preprocessors/checksources.py | 14 +++++++++++--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4c9a449..b409dc8 100644 --- a/README.md +++ b/README.md @@ -19,20 +19,25 @@ preprocessors: - checksources ``` -You can add a list of unmentioned files that wouldn't throw warnings by `not_in_chapters` option. -To perform a strict check, use the `strict_check` option: +### Options +- `not_in_chapters` – a list of files not mentioned in the the chapters. + No warnings will be displayed for the specified files. + This option is useful if you don't need to add some files to the table of contents. +- `strict_check` – if a critical error is detected, the build will be failed after applying the preprocessor. + Several checks are supported: + - `not_exist` – checking the existence of the file. + Checks if the file specified in chapters exists; + - `duplicate` – checking for duplicate in the chapters. + + To disable strict check, use `strict_check: false`. And in order to enable all available checks, use `strict_check: true`. + +**Example of options:** ```yaml preprocessors: - checksources: not_in_chapters: - tags.md - strict_check: true + strict_check: + - not_exist ``` - -The `not_in_chapters` option is useful if you don't need to add some files to the table of contents. - -If the `strict_check` option is enabled, then if a critical error is detected, the build will be aborted after applying the preprocessor. - - - diff --git a/foliant/preprocessors/checksources.py b/foliant/preprocessors/checksources.py index c49a082..f07db5a 100644 --- a/foliant/preprocessors/checksources.py +++ b/foliant/preprocessors/checksources.py @@ -12,7 +12,10 @@ class Preprocessor(BasePreprocessorExt): defaults = { 'not_in_chapters': [], - 'strict_check': True, + 'strict_check': [ + 'not_exist', + 'duplicate' + ], } def __init__(self, *args, **kwargs): @@ -23,6 +26,11 @@ def __init__(self, *args, **kwargs): self.logger.debug(f'Preprocessor inited: {self.__dict__}') self.src_dir = self.project_path / self.config['src_dir'] self.critical_error = [] + if isinstance(self.options['strict_check'], bool): + if self.options['strict_check']: + self.options['strict_check'] = self.defaults['strict_check'] + else: + self.options['strict_check'] = [] self.files_list = [] def apply(self): @@ -48,7 +56,7 @@ def _recursive_process_chapters(chapters_subset): else: self.logger.debug('Not exist, throw warning') msg = f'{os.path.relpath(chapter_file_path)} does not exist' - if self.options['strict_check']: + if 'not_exist' in self.options['strict_check']: self.logger.error(msg) self.critical_error.append(msg) output(f'ERROR: {msg}') @@ -56,7 +64,7 @@ def _recursive_process_chapters(chapters_subset): self._warning(msg) if chapters_subset in self.files_list: msg = f'{os.path.relpath(chapter_file_path)} duplicated in chapters' - if self.options['strict_check']: + if 'duplicate' in self.options['strict_check']: self.logger.error(msg) self.critical_error.append(msg) output(f'ERROR: {msg}')