diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index b11922f..0e95ae7 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -21,11 +21,14 @@ __all__ = _ERRORS+[] def init_logging(filename=None, loglevel="ERROR", filemode='w', handler=None): """ Initializes logging - :param filename: The name of the log file - :param loglevel: The level of logging - :type loglevel: str - :param filemode: The mode of the log file + :param filename: The path for the log file + :param loglevel: The level of logging: "ERROR", "INFO", "WARNING", "DEBUG", "CRITICAL" + :param filemode: The mode of the log file: "w", "a" :param handler: The handler to use + :type filename: str + :type loglevel: str + :type filemode: str + :type handler: logging.Handler :return: None """ loglevel = loglevel.upper() @@ -50,6 +53,7 @@ def log_message(message, level="ERROR"): :param message: The message to log :param level: The level of the message :type level: str + :type message: str :return: None """ level = level.lower() @@ -83,25 +87,66 @@ def log_exception(exception, level="ERROR"): return +def dict_check(dic, *args): + """ + Helper function to test existence of a list of succesive keys within a nested dictionary. + :param dic: The dictionary of dictionaries to check + :param args: Any number of keys to check the dictionaries for + :type dic: dict | config.ConfigLoader + :type args: str + :return: A table of Booleans for every key checked. + """ + output = [False for _ in args] + for i in range(len(args)): + if args[i] in dic: + output[i] = True + dic = dic[args[i]] + else: + break + return output + + +def str_dict_check(dic, *args): + """ + Helper function to test existence within a dictionary and return a string of the values of the keys checked + and wether they exist. + 'arg[0]' : True or False + :param dic: The nested dictionary to check + :param args: Any number of keys to check successively + :type dic: dict + :type args: str + :return: A string of the values of the keys checked and wether they exist. + :rtype: str + """ + out = dict_check(dic, *args) + return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))]) + + class BootstraparseError(Exception): + """ + Base class for all Bootstraparse errors + """ pass class ParsingError(Exception): """ Exception class for parsing errors - :param message: The message to display - :type message: str - :param line: The line number of the error - :param column: The column number of the error - :param file: The name of the file - :return: None """ def __init__(self, message=None, line=None, column=None, file=None): """ Initializes the ParsingError class with the filename and a line and column number based on the current position in the file (if available) + :param message: The message to display + :type message: str + :param line: The line number of the error + :type line: int + :param column: The column number of the error + :type column: int + :param file: The name of the file + :type file: str + :return: None """ super().__init__(message) self.line = line @@ -127,6 +172,11 @@ class MismatchedContainerError(BootstraparseError): The token is not final and cannot be contained. Indicative of a mismatched token. """ def __init__(self, token): + """ + Initializes the MismatchedContainerError class with the token that was not final + :param token: The token that is not final + :type token: syntax.SemanticType + """ self.token = token if token: self.line = token.line_number @@ -144,6 +194,14 @@ class LonelyOptionalError(BootstraparseError): Optional token could not be matched with last element in pile (not a container). """ def __init__(self, token, last_in_pile): + """ + Initializes the LonelyOptionalError class with the token found and the last element in the pile + Used when a token is found that is optional and cannot be matched with the last element in the pile (not a container) + :param token: The token that is not final + :type token: syntax.SemanticType + :param last_in_pile: The last element in the pile + :type last_in_pile: syntax.SemanticType + """ self.token = token if token: self.line = token.line_number