private/lib/Validator.py: Further code quality enhancements

Namely:
* f => file_handle
* errstr => err_str
* intendantion adjustements.
* factor out uneccessary if-return-else-return-clause.

Also a new functionality to NOT check for users existence but everything
else. Needed to sanitize in case it's a backup of the current running
instance already. This further adds to commit
b4bb45ef85.
dev
Darksider3 4 years ago
parent d2f99d95d0
commit 108492db3f

@ -156,7 +156,7 @@ def checkName(name: str) -> bool:
return True return True
def checkImportFile(path: str, db: str): def checkImportFile(path: str, db: str, test_existence: bool = True):
""" Checks an CSV file against most of the validators and prints an Error message with the line number corresponding """ Checks an CSV file against most of the validators and prints an Error message with the line number corresponding
to said failure.. Those includes: checkName, checkUsernameCharacters, to said failure.. Those includes: checkName, checkUsernameCharacters,
ckeckUsernameLength, duplicate usernames(in the CSV), checkSSHKey, checkEmail, checkUserExists, checkUserInDB, ckeckUsernameLength, duplicate usernames(in the CSV), checkSSHKey, checkEmail, checkUserExists, checkUserInDB,
@ -166,54 +166,64 @@ def checkImportFile(path: str, db: str):
:type path: str :type path: str
:param db: Path to database file(SQLite) :param db: Path to database file(SQLite)
:type db: str :type db: str
:param test_existence: Flag, checking users existence while true, won't when set to false. Default's to true.
:type test_existence: bool
:return: Str when Failure, True when success(All tests passed) :return: Str when Failure, True when success(All tests passed)
:rtype: Str or None :rtype: Str or None
""" """
errstr = "" err_str = ""
valid = True valid = True
ln = 1 # line number line = 1 # line number
valid_names_list = [] valid_names_list = []
with open(path, 'r', newline='') as f: with open(path, 'r', newline='') as file_handle:
reader = csv.DictReader(f) reader = csv.DictReader(file_handle)
for row in reader: for row in reader:
# if any of this fails move on to the next user, just print a relatively helpful message lel # if any of this fails move on to the next user, just print a relatively helpful message lel
if not lib.Validator.checkName(row["name"]): if not lib.Validator.checkName(row["name"]):
errstr += f"Line {ln}: Name: '{row['name']}' seems not legit. Character followed by character should" \ err_str += f"Line {line}: Name: '{row['name']}' seems not legit. " \
f" be correct.\n" f"Character followed by character should be correct.\n"
valid = False valid = False
if not lib.Validator.checkUsernameCharacters(row["username"]): if not lib.Validator.checkUsernameCharacters(row["username"]):
errstr += (f"Line {ln}: Username contains unsupported characters or starts with a number: '" err_str += (f"Line {line}: "
f"{row['username']}'.\n") f"Username contains unsupported characters or starts with a number: '"
f"{row['username']}'.\n")
valid = False valid = False
if not lib.Validator.checkUsernameLength(row["username"]): if not lib.Validator.checkUsernameLength(row["username"]):
errstr += f"Line {ln}: Username '{row['username']}' is either too long(>16) or short(<3)\n" err_str += f"Line {line}: " \
f"Username '{row['username']}' is either too long(>16) or short(<3)\n"
valid = False valid = False
# dup checking # dup checking
if row["username"] in valid_names_list: if row["username"] in valid_names_list:
errstr += f"Line {ln}: Duplicate Username {row['username']}!\n" err_str += f"Line {line}: Duplicate Username {row['username']}!\n"
valid = False valid = False
else: else:
valid_names_list.append(row["username"]) valid_names_list.append(row["username"])
# dup end # dup end
if not lib.Validator.checkSSHKey(row["pubkey"]): if not lib.Validator.checkSSHKey(row["pubkey"]):
errstr += f"Line {ln}: Following SSH-Key of user '{row['username']}' isn't valid: " \ err_str += f"Line {line}: " \
f"'{row['pubkey']}'.\n" f"Following SSH-Key of user '{row['username']}' isn't valid: " \
f"'{row['pubkey']}'.\n"
valid = False valid = False
if not lib.Validator.checkEmail(row["email"]): if not lib.Validator.checkEmail(row["email"]):
errstr += f"Line {ln}: E-Mail address of user '{row['username']}' '{row['email']}' is not valid.\n" err_str += \
f"Line {line}: " \
f"E-Mail address of user '{row['username']}' '{row['email']}' is not valid.\n"
valid = False valid = False
if lib.Validator.checkUserExists(row["username"]) or checkUserInDB(row["username"], db): if lib.Validator.checkUserExists(row["username"]) or checkUserInDB(row["username"], db):
errstr += f"Line {ln}: User '{row['username']}' already exists.\n" if test_existence:
valid = False err_str += f"Line {line}: User '{row['username']}' already exists.\n"
valid = False
else:
pass
if not lib.Validator.checkDatetimeFormat(row["timestamp"]): if not lib.Validator.checkDatetimeFormat(row["timestamp"]):
errstr += f"Line {ln}: Timestamp '{row['timestamp']}' from user '{row['username']}' is invalid.\n" err_str += f"Line {line}: Timestamp '{row['timestamp']}' " \
f"from user '{row['username']}' is invalid.\n"
valid = False valid = False
if int(row["status"]) > 1 or int(row["status"]) < 0: if int(row["status"]) > 1 or int(row["status"]) < 0:
errstr += f"Line {ln}: Status '{row['status']}' MUST be either 0 or 1.\n" err_str += f"Line {line}: Status '{row['status']}' MUST be either 0 or 1.\n"
valid = False valid = False
ln += 1 line += 1
if valid: if valid:
return True err_str = True
else: return err_str
return errstr

Loading…
Cancel
Save