Skip to content

Commit

Permalink
Simplify custom checkers registring
Browse files Browse the repository at this point in the history
  • Loading branch information
nl78 committed Sep 29, 2024
1 parent f7a152c commit d258982
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
6 changes: 3 additions & 3 deletions doc/checker.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ Every class derived from `OperationAbstract` will be considered as a checker and

### Run `fmutool` with your checker

In order to let `fmutool` know where the file implementing your checker are, you can use `FMUTOOL_CHECKER_DIR`
environment variable before invoking `fmutool`. The pointed directory may contain multiple python files and so
multiple checkers. All of them will be run during the checks runtime.
In order to let `fmutool` know where the file implementing your checker are, you can use `fmutool.checker.add_from_file`
function before invoking `fmutool`. The pointed file may contain multiple classes derivative from `OperationAbstract`.
All of them will be run during the checks runtime.
15 changes: 1 addition & 14 deletions fmutool/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def closure(self):
checker_list = [OperationGenericCheck]


def _add_checkers_from_file(checker_filename: str):
def add_from_file(checker_filename: str):
spec = importlib.util.spec_from_file_location(checker_filename, checker_filename)
if not spec:
print(f"ERROR: Cannot load {checker_filename}. Is this a python file?")
Expand All @@ -60,16 +60,3 @@ def _add_checkers_from_file(checker_filename: str):

except AttributeError:
print(f"ERROR: {checker_filename} should implement class 'OperationCheck'")


def _add_checkers(variable_name: str):
checkers_folder = os.getenv(variable_name)
if checkers_folder:
if os.path.isdir(checkers_folder):
for filename in glob.glob(os.path.join(checkers_folder, "*.py")):
_add_checkers_from_file(filename)
else:
print(f"ERROR: {variable_name} should point to a valid folder.")


_add_checkers("FMUTOOL_CHECKER_DIR")

0 comments on commit d258982

Please sign in to comment.