diff options
author | Frederick Muriuki Muriithi | 2023-08-08 04:47:02 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-08-08 04:47:02 +0300 |
commit | b2c23d6ddcb581a1dfd44f3a695aab15b35c9b63 (patch) | |
tree | b7c049fb75afda32589847d8517c6218c7f29014 /gn_auth/auth/db/mariadb.py | |
parent | d88ef631a78c32baed34a126d79f4679a2f09d10 (diff) | |
download | gn-auth-b2c23d6ddcb581a1dfd44f3a695aab15b35c9b63.tar.gz |
Fix issues caught by linter
* Add a .pylintrc to silence annoying messages
* Fix imports
* Add missing `parse_db_url` function
* Add a new `gn_auth.auth.db.redis` module
Diffstat (limited to 'gn_auth/auth/db/mariadb.py')
-rw-r--r-- | gn_auth/auth/db/mariadb.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/gn_auth/auth/db/mariadb.py b/gn_auth/auth/db/mariadb.py index a934fd9..69c1d8e 100644 --- a/gn_auth/auth/db/mariadb.py +++ b/gn_auth/auth/db/mariadb.py @@ -1,12 +1,22 @@ """Connections to MariaDB""" +import logging import traceback import contextlib -from typing import Iterator +from urllib.parse import urlparse +from typing import Tuple, Iterator import MySQLdb as mdb from .protocols import DbConnection +def parse_db_url(sql_uri: str) -> Tuple: + """Parse SQL_URI env variable note:there is a default value for SQL_URI so a + tuple result is always expected""" + parsed_db = urlparse(sql_uri) + return ( + parsed_db.hostname, parsed_db.username, parsed_db.password, + parsed_db.path[1:], parsed_db.port) + @contextlib.contextmanager def database_connection(sql_uri) -> Iterator[DbConnection]: """Connect to MySQL database.""" @@ -18,7 +28,7 @@ def database_connection(sql_uri) -> Iterator[DbConnection]: port=port or 3306) try: yield connection - except Exception as _exc: # TODO: Make the Exception class less general + except mdb.Error as _mdb_err: logging.debug(traceback.format_exc()) connection.rollback() finally: |