From 934b6bf75a337bf8d7ecf9ad7d4cf0a658d7c2dc Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Sat, 19 Oct 2019 14:32:01 +0200 Subject: [PATCH] Factor out 'as CFG' --- private/Backup.py | 9 +++------ private/Import.py | 12 ++++++------ private/ListUsers.py | 10 +++++----- private/lib/validator.py | 6 +++--- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/private/Backup.py b/private/Backup.py index ddb23ea..faa1063 100644 --- a/private/Backup.py +++ b/private/Backup.py @@ -3,10 +3,7 @@ import ListUsers import csv import io -import lib.CFG as CFG -import lib.validator -import lib.UserExceptions -import os +import lib.CFG class Backup: @@ -15,7 +12,7 @@ class Backup: dialect: str field_names: tuple - def __init__(self, fname: str = CFG.args.file, quoting: int = csv.QUOTE_NONNUMERIC, dialect: str = "excel"): + def __init__(self, fname: str = lib.CFG.args.file, quoting: int = csv.QUOTE_NONNUMERIC, dialect: str = "excel"): self.setFilename(fname) self.setQuoting(quoting) self.setDialect(dialect) @@ -52,7 +49,7 @@ if __name__ == "__main__": L = ListUsers.ListUsers() fetch = L.getFetch() B = Backup() - if CFG.args.Import: + if lib.CFG.args.Import: print("For importing please call the ./Import.py file with the --Import flag") else: B.BackupToFile(fetch) diff --git a/private/Import.py b/private/Import.py index dc671ed..0686db2 100644 --- a/private/Import.py +++ b/private/Import.py @@ -1,10 +1,10 @@ -import lib.CFG as CFG +import lib.CFG import csv import os import lib.UserExceptions -def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.config['DEFAULT']['applications_db'], +def ImportFromFile(fname: str = lib.CFG.args.file, db: str = lib.CFG.config['DEFAULT']['applications_db'], userids: tuple = tuple([])): if not os.path.isfile(fname): print(f"File {fname} don't exist") @@ -25,7 +25,7 @@ def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.config['DEFAULT'][' import lib.sqlitedb import lib.System sysctl = lib.System.System() - sql = lib.sqlitedb.SQLitedb(CFG.config['DEFAULT']['applications_db']) + sql = lib.sqlitedb.SQLitedb(lib.CFG.config['DEFAULT']['applications_db']) reader = csv.DictReader(f) # @TODO csv.Sniffer to compare? When yes, give force-accept option for row in reader: if row["status"] == "1": @@ -64,11 +64,11 @@ def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.config['DEFAULT'][' if __name__ == "__main__": try: - if not CFG.args.Import: + if not lib.CFG.args.Import: print("Error, need the import flag") - if not CFG.args.file: + if not lib.CFG.args.file: print("Error, need the import file") - if not CFG.args.file: + if not lib.CFG.args.file: print("You MUST set a CSV-file with the -f/--file flag that already exist") exit(1) ImportFromFile() diff --git a/private/ListUsers.py b/private/ListUsers.py index 1c311d8..2851073 100644 --- a/private/ListUsers.py +++ b/private/ListUsers.py @@ -1,15 +1,15 @@ #!/usr/bin/env python3 from lib.sqlitedb import SQLitedb -import lib.CFG as CFG +import lib.CFG class ListUsers: db = None usersFetch = None - def __init__(self, uap: bool = CFG.args.unapproved, app: bool = CFG.args.approved): - self.db = SQLitedb(CFG.config['DEFAULT']['applications_db']) + def __init__(self, uap: bool = lib.CFG.args.unapproved, app: bool = lib.CFG.args.approved): + self.db = SQLitedb(lib.CFG.config['DEFAULT']['applications_db']) if uap: # only unapproved users query = "SELECT * FROM `applications` WHERE status = '0'" elif app: # Approved users @@ -64,8 +64,8 @@ print(t.draw()) ret += "%-4i| %-14s| %-25s| %-22s| %-8s | %-5i |\n" % ( user["id"], user["username"], user["email"], user["name"], user["timestamp"], user["status"] ) - if CFG.args.file != "stdout": - with open(CFG.args.file, 'w') as f: + if lib.CFG.args.file != "stdout": + with open(lib.CFG.args.file, 'w') as f: print(ret, file=f) else: print(ret) diff --git a/private/lib/validator.py b/private/lib/validator.py index 1431239..6019048 100644 --- a/private/lib/validator.py +++ b/private/lib/validator.py @@ -1,7 +1,7 @@ import re import pwd import lib.sqlitedb -import lib.CFG as CFG +import lib.CFG def checkUsernameCharacters(username: str): @@ -34,8 +34,8 @@ def checkUserExists(username: str): def checkUserInDB(username: str): try: - L = lib.sqlitedb.SQLitedb(CFG.config['DEFAULT']['applications_db']) - fetched = L.safequery("SELECT * FROM 'applications' WHERE username = ?", tuple([username])) + ldb = lib.sqlitedb.SQLitedb(lib.CFG.config['DEFAULT']['applications_db']) + fetched = ldb.safequery("SELECT * FROM 'applications' WHERE username = ?", tuple([username])) if fetched: return True except lib.sqlitedb.sqlite3.Error as e: