diff options
author | Frederick Muriuki Muriithi | 2024-12-09 10:36:21 -0600 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-12-09 11:33:40 -0600 |
commit | 709991993e3aa9e9f8fbdd8453660b7a43547486 (patch) | |
tree | edfa9f90e7cba5cc10c0e91b22ce6396866fd0fb /gn_libs/mysqldb.py | |
parent | fd0b5916ad9d9b231a1322dcc1766fafdd6741a7 (diff) | |
download | gn-libs-709991993e3aa9e9f8fbdd8453660b7a43547486.tar.gz |
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.
Diffstat (limited to 'gn_libs/mysqldb.py')
-rw-r--r-- | gn_libs/mysqldb.py | 20 |
1 files changed, 12 insertions, 8 deletions
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: |