From 5295aa5901d1eb41260f813d4d7d2cfe368c1cac Mon Sep 17 00:00:00 2001 From: felipez Date: Thu, 21 Jan 2021 16:36:23 +0100 Subject: [PATCH 1/4] add schema validation to user input --- PyVOTCA/schemas.py | 48 +++++++++++++++++++++++++++++++++++++++++ PyVOTCA/xtp_gradient.py | 15 +++++++------ setup.py | 2 +- tests/files/example.yml | 2 ++ 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 PyVOTCA/schemas.py create mode 100644 tests/files/example.yml diff --git a/PyVOTCA/schemas.py b/PyVOTCA/schemas.py new file mode 100644 index 0000000..0398cff --- /dev/null +++ b/PyVOTCA/schemas.py @@ -0,0 +1,48 @@ + +"""Module with the schemas to validate the user input.""" + +from multiprocessing import cpu_count +from numbers import Integral +from typing import Any, Dict + +import yaml +from schema import Optional, Or, Schema, SchemaError + +__all__ = ["validate_input"] + + +input_schema = Schema({ + # Path to the molecule in xyz format + "molecule": str, + + # Number of Threads to run the application + Optional("threads", default=cpu_count()): Integral, + + # Functional + Optional("functional", default="PBE"): str, + + # Basisset + Optional("basis", default=None): Or(str, None), + + # AuxBasisset + Optional("auxbasis", default=None): Or(str, None), + + # GW + Optional("gw", default=None): Or(str, None), + + # BSE + Optional("bse", default=None): Or(str, None), +}) + + +def validate_input(file_input: str) -> Dict[str, Any]: + """Check the input validation against an schema.""" + with open(file_input, 'r') as handler: + dict_input = yaml.load(handler.read(), Loader=yaml.FullLoader) + try: + inp = input_schema.validate(dict_input) + return inp + except SchemaError as err: + msg = f"There was an error in the input yaml provided:\n{err}" + print(msg) + raise diff --git a/PyVOTCA/xtp_gradient.py b/PyVOTCA/xtp_gradient.py index 3cebcbc..22eaca1 100644 --- a/PyVOTCA/xtp_gradient.py +++ b/PyVOTCA/xtp_gradient.py @@ -6,6 +6,7 @@ import sys from .numerical_gradient import NumericalGradient +from .schemas import validate_input def exists(input_file: str) -> Path: @@ -35,22 +36,24 @@ def xtp_gradient(args: argparse.Namespace): def parse_user_arguments() -> argparse.Namespace: """Read the user arguments.""" parser = argparse.ArgumentParser("xtp_gradient") - parser.add_argument("-n", "--name", help="Molecule name") - parser.add_argument("-t", "--threads", help="Number of threads") + parser.add_argument( + "-i", "--input", help="Input file in YAML format", type=exists) # Read the arguments args = parser.parse_args() - if args.name is None: + if args.input is None: parser.print_help() sys.exit() - return args + return args.input def main(): - args = parse_user_arguments() - xtp_gradient(args) + inp = parse_user_arguments() + args = validate_input(inp) + print(args) + # xtp_gradient(args) if __name__ == "__main__": diff --git a/setup.py b/setup.py index 0a2956d..91ac1ca 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ ] }, - install_requires=["h5py", "matplotlib", "numpy"], + install_requires=["h5py", "matplotlib", "numpy", "pyyaml"], extras_require={ 'test': ['coverage', 'mypy', 'pycodestyle', 'pytest>=3.9', 'pytest-asyncio', 'pytest-cov', 'pytest-mock'], diff --git a/tests/files/example.yml b/tests/files/example.yml new file mode 100644 index 0000000..7a7aeab --- /dev/null +++ b/tests/files/example.yml @@ -0,0 +1,2 @@ +molecule: test/files/ethylene.xyz + From e0cdc84f53c68958bd3483a628e1e8851e21b734 Mon Sep 17 00:00:00 2001 From: felipez Date: Thu, 21 Jan 2021 16:37:55 +0100 Subject: [PATCH 2/4] add schema to the dependencies --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 91ac1ca..ecd2aea 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ ] }, - install_requires=["h5py", "matplotlib", "numpy", "pyyaml"], + install_requires=["h5py", "matplotlib", "numpy", "pyyaml", "'schema"], extras_require={ 'test': ['coverage', 'mypy', 'pycodestyle', 'pytest>=3.9', 'pytest-asyncio', 'pytest-cov', 'pytest-mock'], From 9979d16a0496a6591c0d473461065683abd7e3d8 Mon Sep 17 00:00:00 2001 From: felipez Date: Thu, 21 Jan 2021 16:44:40 +0100 Subject: [PATCH 3/4] fix installation --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ecd2aea..60f3d28 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ ] }, - install_requires=["h5py", "matplotlib", "numpy", "pyyaml", "'schema"], + install_requires=["h5py", "matplotlib", "numpy", "pyyaml", "schema"], extras_require={ 'test': ['coverage', 'mypy', 'pycodestyle', 'pytest>=3.9', 'pytest-asyncio', 'pytest-cov', 'pytest-mock'], From 6bd896b3456c48bcfa1af6999ff8d73473b7dd1b Mon Sep 17 00:00:00 2001 From: felipez Date: Tue, 26 Jan 2021 11:11:15 +0100 Subject: [PATCH 4/4] add scipy to the dependencies --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 82627e2..8163790 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ 'xtp_gradient=PyVOTCA.xtp_gradient:main', ] }, - install_requires=["h5py", "matplotlib", "numpy", "pyyaml", "schema"], + install_requires=["h5py", "matplotlib", "numpy", "pyyaml", "schema", "scipy"], extras_require={ 'test': ['coverage', 'mypy', 'pycodestyle', 'pytest>=3.9', 'pytest-asyncio', 'pytest-cov', 'pytest-mock'],