From 2755dc073328ace04203b835328a124f2a812c2b Mon Sep 17 00:00:00 2001 From: TudorAndrei-Pythia Date: Tue, 26 Sep 2023 17:51:17 +0300 Subject: [PATCH] refactor: split code in files --- .pre-commit-config.yaml | 14 +++----- pyproject.toml | 3 ++ src/sirqle/__init__.py | 3 +- src/sirqle/config.py | 65 +++++++++++++++++++++++++++++++++++++ src/sirqle/query.py | 71 ++++++----------------------------------- 5 files changed, 84 insertions(+), 72 deletions(-) create mode 100644 src/sirqle/config.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 27e7b4c..7419bb5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,9 @@ repos: + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: "v0.0.241" + hooks: + - id: ruff + args: [--ignore, F401] - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: @@ -25,11 +30,6 @@ repos: rev: 23.1.0 hooks: - id: black - - repo: https://github.com/pycqa/isort - rev: 5.12.0 - hooks: - - id: isort - args: ["--profile", "black"] - repo: https://github.com/commitizen-tools/commitizen rev: v2.42.0 hooks: @@ -37,7 +37,3 @@ repos: - id: commitizen-branch stages: - push - - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: "v0.0.241" - hooks: - - id: ruff diff --git a/pyproject.toml b/pyproject.toml index d2b58f2..b5a0859 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,3 +65,6 @@ bump_message = "bump: $current_version → $new_version [skip ci]" [tool.pytest.ini_options] testpaths = 'tests' asyncio_mode = 'strict' + +[tool.ruff] +ignore = ['F401'] diff --git a/src/sirqle/__init__.py b/src/sirqle/__init__.py index c8057a2..bd7a65a 100644 --- a/src/sirqle/__init__.py +++ b/src/sirqle/__init__.py @@ -1,4 +1,5 @@ import importlib.metadata __version__ = importlib.metadata.version(__package__ or __name__) -from .query import Config, Query +from src.sirqle.config import Config +from src.sirqle.query import Query diff --git a/src/sirqle/config.py b/src/sirqle/config.py new file mode 100644 index 0000000..cbe6eb2 --- /dev/null +++ b/src/sirqle/config.py @@ -0,0 +1,65 @@ +import os +from typing import Optional +from urllib.parse import urlparse + +from dotenv import dotenv_values +from surrealdb.http import SurrealHTTP +from surrealdb.ws import Surreal + +PARAMS = ["URL", "NAMESPACE", "USERNAME", "PASSWORD", "DATABASE"] + + +CLIENT = { + "ws": Surreal, + "wss": Surreal, + "http": SurrealHTTP, + "https": SurrealHTTP, +} + + +class Config: + def __init__( + self, + env_file: str = ".db_conf", + client: Optional[SurrealHTTP | Surreal] = None, + url: Optional[str] = None, + namespace: Optional[str] = None, + database: Optional[str] = None, + username: Optional[str] = None, + password: Optional[str] = None, + ) -> None: + """Generate a Config class is used to configure the connection the database. + + It uses the `SurrealHTTP` client from the `surrealdb` library + + Args: + env_file: the name of an env file + client: a predefined Surreal client + url: the URL to the database + namespace: the namespace in the database + database: the name of the database + username: the username used for authentication + password: the password used for authentication + """ + if client: + self.client = client + elif os.path.isfile(env_file): + conf = dotenv_values(env_file) + if set(PARAMS).issubset(set(conf.keys())): + scheme = str(urlparse(conf["URL"]).scheme) + self.client = CLIENT[scheme]( + **{param.lower(): conf[param] for param in PARAMS} + ) + elif username and database and password and namespace and url: + scheme = str(urlparse(url).scheme) + self.client = CLIENT[scheme]( + url, + namespace=namespace, + database=database, + username=username, + password=password, + ) + elif url: + scheme = str(urlparse(url).scheme) + if scheme in ["wss", "ws"]: + self.client = CLIENT[scheme](url=url) diff --git a/src/sirqle/query.py b/src/sirqle/query.py index 1dd9b51..73ad9d1 100644 --- a/src/sirqle/query.py +++ b/src/sirqle/query.py @@ -1,71 +1,12 @@ from __future__ import annotations -import os -from typing import List, Optional, Tuple -from urllib.parse import urlparse +from typing import List, Tuple from warnings import warn -from dotenv import dotenv_values from surrealdb.http import SurrealHTTP from surrealdb.ws import ConnectionState, Surreal -PARAMS = ["URL", "NAMESPACE", "USERNAME", "PASSWORD", "DATABASE"] - - -CLIENT = { - "ws": Surreal, - "wss": Surreal, - "http": SurrealHTTP, - "https": SurrealHTTP, -} - - -class Config: - def __init__( - self, - env_file: str = ".db_conf", - client: Optional[SurrealHTTP | Surreal] = None, - url: Optional[str] = None, - namespace: Optional[str] = None, - database: Optional[str] = None, - username: Optional[str] = None, - password: Optional[str] = None, - ) -> None: - """Generate a Config class is used to configure the connection the database. - - It uses the `SurrealHTTP` client from the `surrealdb` library - - Args: - env_file: the name of an env file - client: a predefined Surreal client - url: the URL to the database - namespace: the namespace in the database - database: the name of the database - username: the username used for authentication - password: the password used for authentication - """ - if client: - self.client = client - elif os.path.isfile(env_file): - conf = dotenv_values(env_file) - if set(PARAMS).issubset(set(conf.keys())): - scheme = str(urlparse(conf["URL"]).scheme) - self.client = CLIENT[scheme]( - **{param.lower(): conf[param] for param in PARAMS} - ) - elif username and database and password and namespace and url: - scheme = str(urlparse(url).scheme) - self.client = CLIENT[scheme]( - url, - namespace=namespace, - database=database, - username=username, - password=password, - ) - elif url: - scheme = str(urlparse(url).scheme) - if scheme in ["wss", "ws"]: - self.client = CLIENT[scheme](url=url) +from sirqle.config import Config class Query: @@ -331,7 +272,13 @@ async def execute(self): if self.client is None: raise Exception("No client provided!") res = await self._execute_query() - return res + res = res[0] + if res["status"] == "OK": + return res["result"] + elif res["status"] == "ERR": + raise Exception(f"SurrealDB query not OK {res['detail']}") + else: + raise Exception(f"SurrealDB query not OK {res}") async def use(self, ns: str, db: str) -> None: if isinstance(self.client, Surreal):