Factor out 'as CFG'

pull/4/head
Darksider3 5 years ago
parent 670aa3d9c3
commit 934b6bf75a

@ -3,10 +3,7 @@
import ListUsers import ListUsers
import csv import csv
import io import io
import lib.CFG as CFG import lib.CFG
import lib.validator
import lib.UserExceptions
import os
class Backup: class Backup:
@ -15,7 +12,7 @@ class Backup:
dialect: str dialect: str
field_names: tuple 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.setFilename(fname)
self.setQuoting(quoting) self.setQuoting(quoting)
self.setDialect(dialect) self.setDialect(dialect)
@ -52,7 +49,7 @@ if __name__ == "__main__":
L = ListUsers.ListUsers() L = ListUsers.ListUsers()
fetch = L.getFetch() fetch = L.getFetch()
B = Backup() B = Backup()
if CFG.args.Import: if lib.CFG.args.Import:
print("For importing please call the ./Import.py file with the --Import flag") print("For importing please call the ./Import.py file with the --Import flag")
else: else:
B.BackupToFile(fetch) B.BackupToFile(fetch)

@ -1,10 +1,10 @@
import lib.CFG as CFG import lib.CFG
import csv import csv
import os import os
import lib.UserExceptions 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([])): userids: tuple = tuple([])):
if not os.path.isfile(fname): if not os.path.isfile(fname):
print(f"File {fname} don't exist") 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.sqlitedb
import lib.System import lib.System
sysctl = lib.System.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 reader = csv.DictReader(f) # @TODO csv.Sniffer to compare? When yes, give force-accept option
for row in reader: for row in reader:
if row["status"] == "1": if row["status"] == "1":
@ -64,11 +64,11 @@ def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.config['DEFAULT']['
if __name__ == "__main__": if __name__ == "__main__":
try: try:
if not CFG.args.Import: if not lib.CFG.args.Import:
print("Error, need the import flag") print("Error, need the import flag")
if not CFG.args.file: if not lib.CFG.args.file:
print("Error, need the import 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") print("You MUST set a CSV-file with the -f/--file flag that already exist")
exit(1) exit(1)
ImportFromFile() ImportFromFile()

@ -1,15 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from lib.sqlitedb import SQLitedb from lib.sqlitedb import SQLitedb
import lib.CFG as CFG import lib.CFG
class ListUsers: class ListUsers:
db = None db = None
usersFetch = None usersFetch = None
def __init__(self, uap: bool = CFG.args.unapproved, app: bool = CFG.args.approved): def __init__(self, uap: bool = lib.CFG.args.unapproved, app: bool = lib.CFG.args.approved):
self.db = SQLitedb(CFG.config['DEFAULT']['applications_db']) self.db = SQLitedb(lib.CFG.config['DEFAULT']['applications_db'])
if uap: # only unapproved users if uap: # only unapproved users
query = "SELECT * FROM `applications` WHERE status = '0'" query = "SELECT * FROM `applications` WHERE status = '0'"
elif app: # Approved users elif app: # Approved users
@ -64,8 +64,8 @@ print(t.draw())
ret += "%-4i| %-14s| %-25s| %-22s| %-8s | %-5i |\n" % ( ret += "%-4i| %-14s| %-25s| %-22s| %-8s | %-5i |\n" % (
user["id"], user["username"], user["email"], user["name"], user["timestamp"], user["status"] user["id"], user["username"], user["email"], user["name"], user["timestamp"], user["status"]
) )
if CFG.args.file != "stdout": if lib.CFG.args.file != "stdout":
with open(CFG.args.file, 'w') as f: with open(lib.CFG.args.file, 'w') as f:
print(ret, file=f) print(ret, file=f)
else: else:
print(ret) print(ret)

@ -1,7 +1,7 @@
import re import re
import pwd import pwd
import lib.sqlitedb import lib.sqlitedb
import lib.CFG as CFG import lib.CFG
def checkUsernameCharacters(username: str): def checkUsernameCharacters(username: str):
@ -34,8 +34,8 @@ def checkUserExists(username: str):
def checkUserInDB(username: str): def checkUserInDB(username: str):
try: try:
L = lib.sqlitedb.SQLitedb(CFG.config['DEFAULT']['applications_db']) ldb = lib.sqlitedb.SQLitedb(lib.CFG.config['DEFAULT']['applications_db'])
fetched = L.safequery("SELECT * FROM 'applications' WHERE username = ?", tuple([username])) fetched = ldb.safequery("SELECT * FROM 'applications' WHERE username = ?", tuple([username]))
if fetched: if fetched:
return True return True
except lib.sqlitedb.sqlite3.Error as e: except lib.sqlitedb.sqlite3.Error as e:

Loading…
Cancel
Save