From 62ad85b8f4b39c3ee5913e0348de8490bc6e0d72 Mon Sep 17 00:00:00 2001 From: vicky Date: Thu, 9 Jun 2022 11:25:08 +0200 Subject: [PATCH] moved dict_check and str_dict_check from error_mngr to tools --- src/bootstraparse/modules/error_mngr.py | 27 ----------------------- src/bootstraparse/modules/export.py | 3 ++- src/bootstraparse/modules/tools.py | 29 +++++++++++++++++++++++++ tests/modules/test_error_mngr.py | 21 +++++++++--------- 4 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index 83eed01..b11922f 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -83,33 +83,6 @@ def log_exception(exception, level="ERROR"): return -def dict_check(dic, *args): - """ - Helper function to test existence within a dictionary. - :param dic: The dictionary of dictionaries to check - :param args: Any number of keys to check the dictionaries for - :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. - 'arg[0]' : True or False - - """ - out = dict_check(dic, *args) - return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))]) - - class BootstraparseError(Exception): pass diff --git a/src/bootstraparse/modules/export.py b/src/bootstraparse/modules/export.py index d17de30..a025e1b 100644 --- a/src/bootstraparse/modules/export.py +++ b/src/bootstraparse/modules/export.py @@ -11,6 +11,7 @@ import rich from io import StringIO from bootstraparse.modules import config, pathresolver, error_mngr, context_mngr from collections import namedtuple +from bootstraparse.modules import tools from bootstraparse.modules import syntax # noqa F401 @@ -101,7 +102,7 @@ class ExportManager: start, end = self.templates["bootstrap"][export_request.type][export_request.subtype] except KeyError: log_entries = ["bootstrap", export_request.type, export_request.subtype] - log_ = error_mngr.dict_check(self.templates, *log_entries) + log_ = tools.dict_check(self.templates, *log_entries) print() error_mngr.log_exception( KeyError( diff --git a/src/bootstraparse/modules/tools.py b/src/bootstraparse/modules/tools.py index d54afcf..4dffc12 100644 --- a/src/bootstraparse/modules/tools.py +++ b/src/bootstraparse/modules/tools.py @@ -12,6 +12,35 @@ import os import re +############################################################################### +# Dictionary checks +######################################## +def dict_check(dic, *args): + """ + Helper function to test existence within a dictionary. + :param dic: The dictionary of dictionaries to check + :param args: Any number of keys to check the dictionaries for + :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. + 'arg[0]' : True or False + """ + out = dict_check(dic, *args) + return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))]) + + ############################################################################### # Paths ######################################## diff --git a/tests/modules/test_error_mngr.py b/tests/modules/test_error_mngr.py index f8f25f3..4e3ccda 100644 --- a/tests/modules/test_error_mngr.py +++ b/tests/modules/test_error_mngr.py @@ -4,6 +4,7 @@ from collections import namedtuple from importlib import reload from unittest import TestCase import pytest +import bootstraparse.modules.tools as tools import bootstraparse.modules.error_mngr as error_mngr from bootstraparse.modules.error_mngr import ParsingError, MismatchedContainerError @@ -83,23 +84,23 @@ def test_dict_check(): """ Test the dict_check function """ - assert(error_mngr.dict_check({"test": "test"}, "test") == [True]) - assert(error_mngr.dict_check({"test": "test"}, "test2") == [False]) - assert(error_mngr.dict_check({"test": {"test3": "test5"}}, "test", "test3") == [True, True]) - assert(error_mngr.dict_check({"test": {"test3": "test5"}}, "test4", "test4") == [False, False]) - assert(error_mngr.dict_check({"test": {"test3": "test5"}}, "test", "test34") == [True, False]) + assert(tools.dict_check({"test": "test"}, "test") == [True]) + assert(tools.dict_check({"test": "test"}, "test2") == [False]) + assert(tools.dict_check({"test": {"test3": "test5"}}, "test", "test3") == [True, True]) + assert(tools.dict_check({"test": {"test3": "test5"}}, "test4", "test4") == [False, False]) + assert(tools.dict_check({"test": {"test3": "test5"}}, "test", "test34") == [True, False]) # @pytest.mark.xfail(strict=True, reason="This test is not implemented yet") def test_dict_check_2(): - assert(error_mngr.dict_check({"test": "test"}, "test", "test2") == [True, False]) - assert(error_mngr.dict_check({"test": "test"}, "test", "test2", "test3") == [True, False, False]) + assert(tools.dict_check({"test": "test"}, "test", "test2") == [True, False]) + assert(tools.dict_check({"test": "test"}, "test", "test2", "test3") == [True, False, False]) def test_dict_str(): - assert error_mngr.str_dict_check({"test": "test"}, "test") == "test: True" - assert error_mngr.str_dict_check({"test": "test"}, "test2") == "test2: False" - assert error_mngr.str_dict_check({"test": {"test3": "test5"}}, "test", "test3") == "test: True\ntest3: True" + assert tools.str_dict_check({"test": "test"}, "test") == "test: True" + assert tools.str_dict_check({"test": "test"}, "test2") == "test2: False" + assert tools.str_dict_check({"test": {"test3": "test5"}}, "test", "test3") == "test: True\ntest3: True" def test_weird_exception_cases():