From 709991993e3aa9e9f8fbdd8453660b7a43547486 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 9 Dec 2024 10:36:21 -0600 Subject: Parse a larger pool of possible boolean values When a key is supposed to have a boolean value, parse the value from a larger pool of both true and false values. --- gn_libs/mysqldb.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'gn_libs/mysqldb.py') diff --git a/gn_libs/mysqldb.py b/gn_libs/mysqldb.py index 6c05a6b..c738d96 100644 --- a/gn_libs/mysqldb.py +++ b/gn_libs/mysqldb.py @@ -15,9 +15,13 @@ class InvalidOptionValue(Exception): """Raised whenever a parsed value is invalid for the specific option.""" -def __check_true__(val: str) -> bool: +def __parse_boolean__(val: str) -> bool: """Check whether the variable 'val' has the string value `true`.""" - return val.strip().lower() == "true" + true_vals = ("t", "T", "true", "TRUE", "True") + false_vals = ("f", "F", "false", "FALSE", "False") + if (val.strip() not in (true_vals + false_vals)): + raise InvalidOptionValue(f"Invalid value: {val}") + return val.strip().lower() in true_vals def __parse_db_opts__(opts: str) -> dict: @@ -33,15 +37,15 @@ def __parse_db_opts__(opts: str) -> dict: conversion_fns: dict[str, Callable] = { **{opt: str for opt in allowed_opts}, "connect_timeout": int, - "compress": __check_true__, - "use_unicode": __check_true__, + "compress": __parse_boolean__, + "use_unicode": __parse_boolean__, # "cursorclass": __load_cursor_class__ "client_flag": int, - "multi_statements": __check_true__, # "ssl": __parse_ssl_options__, - "local_infile": __check_true__, - "autocommit": __check_true__, - "binary_prefix": __check_true__ + "multi_statements": __parse_boolean__, + "local_infile": __parse_boolean__, + "autocommit": __parse_boolean__, + "binary_prefix": __parse_boolean__ } queries = tuple(filter(bool, opts.split("&"))) if len(queries) > 0: -- cgit v1.2.3