schmeckels/validate.py
2020-03-30 21:22:42 +02:00

64 lines
2.1 KiB
Python

#! /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
import colorful as cf
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)
if not data:
print(cf.green(" Ruleset is empty"))
else:
for rule in data:
if rule.get("name"):
try:
re.compile(rule["name"])
except:
print(cf.red(f" Invalid name regex: '{rule.get('name')}'"))
if rule.get("description"):
try:
re.compile(rule["description"])
except:
print(cf.red(f" Invalid description regex: '{rule.get('description')}'"))
if rule.get("iban"):
try:
IBAN(rule.get("iban"))
except:
print(cf.red(f" Invalid IBAN: '{rule.get('iban')}'"))
print(cf.green(f" All rules are valid"))
else:
print(cf.red(f" The rule file doesn't exists"))
sys.exit(1)
# Database
if os.path.exists(db_path) and os.path.isfile(db_path):
print(cf.green(f" Database exists"))
else:
print(cf.red(f" The database file doesn't exists"))
sys.exit(1)