diff --git a/Pipfile b/Pipfile index 97026f3..50f1a9c 100644 --- a/Pipfile +++ b/Pipfile @@ -17,6 +17,7 @@ xdg = "*" pyyaml = "*" colored = "*" colorful = "*" +schwifty = "*" [requires] python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock index cfa770f..5187677 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "816c7c2536e6f09c3088905953eb3d20b067dad888a1626eedb2ddef41c6b045" + "sha256": "c9d6b64c5d520a15a7e020cdcf1479cd32cd46ce1307d9002d22a5a80d5ab669" }, "pipfile-spec": 6, "requires": { @@ -47,6 +47,13 @@ "index": "pypi", "version": "==0.5.4" }, + "iso3166": { + "hashes": [ + "sha256:b07208703bd881a4f974e39fa013c4498dddd64913ada15f24be75d02ae68a44", + "sha256:b1e58dbcf50fbb2c9c418ec7a6057f0cdb30b8f822ac852f72e71ba769dae8c5" + ], + "version": "==1.0.1" + }, "mt-940": { "hashes": [ "sha256:7cbd88fd7252d5a2694593633b31f819eb302423058fecb9f9959e74c01c2b86", @@ -80,6 +87,14 @@ "index": "pypi", "version": "==5.3.1" }, + "schwifty": { + "hashes": [ + "sha256:1774dca3a9224fd98730cb81274d963ed8e545593ffadce56681ebe49cdea7c0", + "sha256:98240f53e01a08a59e130e895235e0b69a3f4caf4bd7e9f461e2e317373b9919" + ], + "index": "pypi", + "version": "==2020.2.2" + }, "sqlalchemy": { "hashes": [ "sha256:c4cca4aed606297afbe90d4306b49ad3a4cd36feb3f87e4bfd655c57fd9ef445" diff --git a/cli b/cli index 2ba2533..0bf4af9 100755 --- a/cli +++ b/cli @@ -7,6 +7,7 @@ import sort import importer import serve import autosort +import validate import info from helper import build_database_filename, create_dirs @@ -41,5 +42,6 @@ if __name__ == '__main__': cli.add_command(importer.command) cli.add_command(serve.command) cli.add_command(autosort.command) + cli.add_command(validate.command) cli.add_command(info.command) cli() diff --git a/info.py b/info.py index bc35421..2695832 100644 --- a/info.py +++ b/info.py @@ -5,10 +5,6 @@ from helper import get_session, list_profiles, build_database_filename, build_ru import sys import click -from prompt_toolkit.completion import FuzzyWordCompleter -from prompt_toolkit.shortcuts import prompt - - @click.command(name="info") def command(): for profile in list_profiles(): diff --git a/validate.py b/validate.py new file mode 100644 index 0000000..1ecb566 --- /dev/null +++ b/validate.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python3 +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from sqlalchemy.ext.declarative import declarative_base +from helper import list_profiles, build_rules_filename, build_database_filename +from schwifty import IBAN +import sys +import click +import os +import yaml +import re + +try: + from yaml import CLoader as Loader, CDumper as Dumper +except ImportError: + from yaml import Loader, Dumper + + + +@click.command(name="validate") +def command(): + for profile in list_profiles(): + print(f"Checking {profile}...") + db_path = build_database_filename(profile) + rules_path = build_rules_filename(profile) + + + # Rules + if os.path.exists(rules_path) and os.path.isfile(rules_path): + with open(rules_path) as fh: + data = yaml.load(fh, Loader=Loader) + + for rule in data: + if rule.get("name"): + try: + re.compile(rule["name"]) + except: + print(f"Invalid name regex: '{rule.get('name')}'") + + if rule.get("description"): + try: + re.compile(rule["description"]) + except: + print(f"Invalid description regex: '{rule.get('description')}'") + + if rule.get("iban"): + try: + IBAN(rule.get("iban")) + except: + print(f"Invalid IBAN: '{rule.get('iban')}'") + + print(f"All rules are valid") + else: + print(f"The rule file doesn't exists") + sys.exit(1) + + # Database + if os.path.exists(db_path) and os.path.isfile(db_path): + print(f"Database exists") + else: + print(f"The database file doesn't exists") + sys.exit(1)