From b4bb45ef851edf7e942a4d63f615fce8f12c4265 Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Sat, 30 May 2020 12:11:26 +0200 Subject: [PATCH] Import.py: Sanitize given csv in case --Import isnt given and... ... fixup some variable names, also a `pass` that i forget to delete beforehand. Also, as you can see in the title, i added the ability to sanitize a given csv file in case the user didnt supply the --Import flag. Thus, it isnt required anymore. --- private/{Import.py => import.py} | 52 ++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 22 deletions(-) rename private/{Import.py => import.py} (65%) diff --git a/private/Import.py b/private/import.py similarity index 65% rename from private/Import.py rename to private/import.py index 0d57471..4493078 100755 --- a/private/Import.py +++ b/private/import.py @@ -1,14 +1,17 @@ #!/usr/bin/env python3 - +""" +Import users or sanitize backup files +""" import configparser import csv import os import lib.UserExceptions import lib.uis.config_ui # dont go to default, just following -c flag +import lib.Validator -def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bool: +def import_from_file(file_path: str, database_file: str, user_ids: tuple = tuple([])) -> bool: """ Imports Users from a given CSV-file to the system and DB :param file_path: @@ -23,22 +26,21 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo if not os.path.isfile(file_path): print(f"File {file_path} don't exist") return False - if not os.path.isfile(db): - print(f"The database file {db} don't exist") + if not os.path.isfile(database_file): + print(f"The database file {database_file} don't exist") return False if user_ids: pass # empty tuple means everything # noinspection PyBroadException try: with open(file_path, 'r', newline='') as f: - import lib.Validator - sql = lib.sqlitedb.SQLiteDB(db) - err = lib.Validator.checkImportFile(file_path, db) + import lib.sqlitedb + import lib.System + sql = lib.sqlitedb.SQLiteDB(database_file) + err = lib.Validator.checkImportFile(file_path, database_file) if err is not True: print(err) exit(0) - import lib.sqlitedb - import lib.System sys_ctl = lib.System.System("root") reader = csv.DictReader(f) # @TODO csv.Sniffer to compare? When yes, give force-accept option for row in reader: @@ -47,8 +49,8 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo sys_ctl.setUser(row["username"]) sys_ctl.aio_approve(row["pubkey"]) print(row['username'], "====> Registered.") - except lib.UserExceptions.General as GeneralExcept: - print(f"Something didnt work out! {GeneralExcept}") + except lib.UserExceptions.General as general_except: + print(f"Something didnt work out! {general_except}") elif row["status"] == "0": print(row['username'] + " not approved, therefore not registered.") try: @@ -56,11 +58,10 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo "INSERT INTO `applications` (username, name, timestamp, email, pubkey, status) " "VALUES (?,?,?,?,?,?)", tuple([row["username"], row["name"], row["timestamp"], row["email"], row["pubkey"], row["status"]])) - except OSError as E: - pass - print(f"UUFFF, something went WRONG with the file {file_path}: {E}") - except Exception as didntCatch: - print(f"Exception! UNCATCHED! {type(didntCatch)}: {didntCatch}") + except OSError as os_exception: + print(f"UUFFF, something went WRONG with the file {file_path}: {os_exception}") + except Exception as didnt_catch: + print(f"Exception! UNCATCHED! {type(didnt_catch)}: {didnt_catch}") return True @@ -71,17 +72,24 @@ if __name__ == "__main__": ArgParser.add_argument('-f', '--file', default="stdout", type=str, help='Import from CSV file', required=True) ArgParser.add_argument('--Import', default=False, action="store_true", - help="Import Users.", required=True) + help="Import Users. If not set, just sanitize the supplied csv", required=False) args = ArgParser.parse_args() config = configparser.ConfigParser() config.read(args.config) - if not args.Import: - print("Error, need the import flag") if not args.file: print("Error, need the import file") - if not args.file: - print("You MUST set a CSV-file with the -f/--file flag that already exist") + if not args.Import: + # we assume that you just want to sanitize the backup + if not os.path.isfile(args.file): + print(f"File {args.file} doesnt exist") exit(1) - import_from_file(args.file, config['DEFAULT']['applications_db']) + sanitized = lib.Validator.checkImportFile(args.file, config['DEFAULT']['applications_db']) + if sanitized is not True: + print(sanitized) + else: + print(f"{args.file} is valid!") + exit(0) + elif args.Import: + import_from_file(args.file, config['DEFAULT']['applications_db']) exit(0)