Skip to content

Commit

Permalink
refactor: split code in files
Browse files Browse the repository at this point in the history
  • Loading branch information
TudorAndrei-Pythia committed Sep 26, 2023
1 parent 9f814fc commit 2755dc0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 72 deletions.
14 changes: 5 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -25,19 +30,10 @@ 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:
- id: commitizen
- id: commitizen-branch
stages:
- push
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.241"
hooks:
- id: ruff
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
3 changes: 2 additions & 1 deletion src/sirqle/__init__.py
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions src/sirqle/config.py
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 9 additions & 62 deletions src/sirqle/query.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 2755dc0

Please sign in to comment.