def error(self, **kwargs): check_kwargs(parameter_list=[ "module", "method", "exception", "status", "message", "logger", "verbose" ], caller="ErrorHandler-log", **kwargs) module = kwargs.get("module", None) method = kwargs.get("method", None) status = kwargs.get("status", None) exception = kwargs.get("exception", None) message = kwargs.get("message", None) response_dict = { "status": status or "NA", "module": module or self.defaults["module"], "method": method or self.defaults["method"], "exception": exception, "message": message or "No message was provided!" } self.log(module=response_dict["module"], method=response_dict["method"], exception=response_dict["exception"], status=response_dict["status"], message=response_dict["message"], logger=logging.error, verbose=True) return Response(mimetype="application/json", status=response_dict["status"], response=json.dumps(response_dict))
def log(self, **kwargs): check_kwargs(parameter_list=[ "module", "method", "exception", "status", "message", "logger", "verbose" ], caller="ErrorHandler-log", **kwargs) module = kwargs.get("module", None) method = kwargs.get("method", None) exception = kwargs.get("exception", None) status = kwargs.get("status", None) message = kwargs.get("message", None) logger = kwargs.get("logger", None) verbose = kwargs.get("verbose", None) if logger is None: logger = logging.debug if verbose is None: _verbose = os.getenv("debug_verbose", False) == "true" else: _verbose = verbose if _verbose: _message = "{}: {}: STATUS:{} MESSAGE:{} {}".format( module or self.defaults["module"], method or self.defaults["method"], status, message, "EXCEPTION:{}".format(exception) if exception is not None else "") else: _message = "{}: {}: {}".format(module or self.defaults["module"], method or self.defaults["method"], message) logger(_message)
def test_he_kw_check_kwargs(self): self.assertTrue( check_kwargs(parameter_list=["a", "b", "c"], caller="test_check_kwargs", **{ "a": 1, "b": 2, "c": 3 }))
def __init__(self, **kwargs): """ Constructor to create a new mode. :param mode: <str> A text name for the mode. :param priority: <int> priority of modes (in terms of returning a list) :param digits: <int> number of digits used in this mode. :param digit_type: <int> type of digit, e.g. DigitWord.HEXDIGIT or DigitWord.DIGIT :param guesses_allowed: <int> Number of guesses permitted. :param instruction_text: <str> Instruction text (dependent upon caller to show) :param help_text: <str> Help text (dependent upon caller to show) """ check_kwargs(parameter_list=[ "mode", "priority", "digits", "digit_type", "guesses_allowed", "instruction_text", "help_text" ], caller="GameMode__init__", **kwargs) mode = kwargs.get("mode", None) priority = kwargs.get("priority", None) digits = kwargs.get("digits", None) digit_type = kwargs.get("digit_type", None) guesses_allowed = kwargs.get("guesses_allowed", None) instruction_text = kwargs.get("instruction_text", None) help_text = kwargs.get("help_text", None) # execute_load error handler # self.handler = ErrorHandler(module="GameMode", method="__init__") self.handler = error_handler self.handler.module = "GameMode" self.handler.method = "__init__" # Initialize variables self.handler.log(message="Initializing variables") self._mode = None self._priority = None self._digits = None self._digit_type = None self._guesses_allowed = None self._instruction_text = None self._help_text = None # NOTICE: Properties are used to set 'private' fields (e.g. _mode) to handle # data validation in one place. When adding a new parameter to __init__ ensure # that the property is created (following the existing code) and set the # property not the 'internal' variable. # self.handler.log(message="Creating mode {}".format(mode)) self.mode = mode self.priority = priority self.digits = digits self.digit_type = digit_type self.guesses_allowed = guesses_allowed self.instruction_text = instruction_text self.help_text = help_text self.handler.log( message="Mode {} created: {}".format(mode, self.dump()))
def _set_config(self, **kwargs): check_kwargs(parameter_list=[ "source", "name", "description", "required", "default", "errmsg", "caster", "choices" ], caller="Configurator-_set_config", **kwargs) source = kwargs.get("source", None) name = kwargs.get("name", None) description = kwargs.get("description", None) required = kwargs.get("required", None) default = kwargs.get("default", None) errmsg = kwargs.get("errmsg", None) caster = kwargs.get("caster", None) choices = kwargs.get("choices", None) self.error_handler.log( method="_set_config", message= "In _set_config -- source: {}, name: {}, description: {}, required: {}, default: {}, errmsg: {}, caster: {}, choices: {}" .format(source, name, description, required, default, errmsg, caster, choices), logger=logging.debug) value = source(name.lower(), source(name.upper(), None)) if required and value is None and default is None: raise ValueError( errmsg or "Problem fetching config item: " "{}. It is required and was not found or the value was None.". format(name)) self.error_handler.log(method="_set_config", message="Before casting: {}".format(value), logger=logging.debug) if value is None: value = default if caster: if caster == PersistenceEngine: if not isinstance(value, dict): value = json.loads(str(value).replace("'", "")) value = caster(**value) elif caster == bool and isinstance(value, str): if value.lower() == "false": value = False else: value = True else: value = caster(value) self.error_handler.log(method="_set_config", message="After casting: {}".format(value), logger=logging.debug) if choices and value not in choices: raise ValueError( errmsg or "The configuration value for {}({}) is not in the list of choices: {}" .format(name, value, choices)) self.app.config[name] = value self.error_handler.log( message="In _set_config Set app.config[{}] = {}".format( name, value), logger=logging.debug) return value
def test_he_kw_bad_args(self): with self.assertRaises(TypeError): check_kwargs(parameter_list=["a", "b"], caller="test_kw_bad_args", **{"x": 2})
def test_he_kw_no_params(self): with self.assertRaises(ValueError): check_kwargs(parameter_list=[], caller="test_kw_bad_args", **{"x": 2})
def test_he_kw_empty_kwargs(self): self.assertTrue( check_kwargs(parameter_list=["a", "b", "c"], caller="test_check_kwargs", **{}))