schmeckels/validate.py
Felix Breidenstein 820f2daba4 Formatting
2020-03-30 00:29:00 +02:00

60 lines
1.8 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
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)