Skip to content

Commit

Permalink
Some Refactors in code for compliance.
Browse files Browse the repository at this point in the history
  • Loading branch information
phenobarbital committed Feb 24, 2022
1 parent 46812a6 commit ad3d998
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 508 deletions.
16 changes: 12 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

## Preparation

AsyncDB is designed to use last syntax of asyncio-tools, for that reason, you'll need to have at least Python 3.7 available for testing.
AsyncDB is designed to use last syntax of asyncio-tools, for that reason, you'll need to have at least Python 3.8 available for testing.

You can do this with [pyenv][]:

$ pyenv install 3.7.4
$ pyenv local 3.7.4

$ pyenv install 3.8.1
$ pyenv local 3.8.1

Or using virtualenv:

$ python3.8 -m venv .venv

Also, we can use the command "make venv" inside of Makefile.

$ make venv

## Setup

Expand All @@ -23,7 +31,7 @@ install the appropriate tools and dependencies:

## Formatting

asyncDB start using *[black][]* for formatting code.
asyncDB start using *[black][]* for formating code.

$ make format

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ venv:
python3.9 -m venv .venv
echo 'run `source .venv/bin/activate` to start develop asyncDB'

develop:
setup:
pip install wheel==0.37.0
pip install -e .
python -m pip install -Ur docs/requirements.txt
Expand Down
66 changes: 2 additions & 64 deletions asyncdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,17 @@
"""
import asyncio
import uvloop

from .meta import (
asyncORM,
asyncRecord,
)
from .exceptions import (
ProviderError,
asyncDBException,
)
from .utils import module_exists
from .version import (
__title__, __description__, __version__, __author__, __author_email__
)
from .providers import (
InitProvider,
BaseProvider
)
from .connections import AsyncPool, AsyncDB

__all__ = ["InitProvider", "BaseProvider"]
__all__ = ["InitProvider", "BaseProvider", "AsyncPool", "AsyncDB", ]

# install uvloop and set as default loop for asyncio.
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
uvloop.install()


class AsyncPool:
"""
AsyncPool.
Base class for Asyncio-based DB Pools.
Factory interface for Pool-based connectors.
"""

_provider = None
_name = ""

def __new__(cls, provider="dummy", **kwargs):
cls._provider = None
cls._name = provider
classpath = "asyncdb.providers.{provider}".format(provider=cls._name)
poolName = "{}Pool".format(cls._name)
try:
obj = module_exists(poolName, classpath)
if obj:
cls._provider = obj(**kwargs)
return cls._provider
else:
raise asyncDBException(
message="Cannot Load Pool provider {}".format(poolName)
)
except Exception:
raise


class AsyncDB:
"""AsyncDB.
Factory Proxy Interfaces for Database Providers.
"""
_provider = None
_name = ""

def __new__(cls, provider="dummy", **kwargs):
cls._provider = None
cls._name = provider
classpath = "asyncdb.providers.{provider}".format(provider=cls._name)
try:
obj = module_exists(cls._name, classpath)
if obj:
cls._provider = obj(**kwargs)
return cls._provider
else:
raise asyncDBException(
message="Cannot Load provider {}".format(cls._name)
)
except Exception:
raise
59 changes: 59 additions & 0 deletions asyncdb/connections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging
from typing import Callable
from .exceptions import (
AsyncDBException,
)
from .utils import module_exists


class AsyncPool:
"""
AsyncPool.
Base class for Asyncio-based DB Pools.
Factory interface for Pool-based connectors.
"""

_provider: Callable = None
_name: str = ""

def __new__(cls, provider: str = "dummy", **kwargs) -> Callable:
cls._name = provider
classpath = "asyncdb.providers.{provider}".format(provider=cls._name)
pool = "{}Pool".format(cls._name)
try:
obj = module_exists(pool, classpath)
if obj:
cls._provider = obj(**kwargs)
return cls._provider
else:
raise AsyncDBException(
message="Cannot Load Pool provider {}".format(pool)
)
except Exception as err:
logging.exception(err)
raise


class AsyncDB:
"""AsyncDB.
Factory Proxy Interface for Database Providers.
"""
_provider: Callable = None
_name: str = ""

def __new__(cls, provider: str = "dummy", **kwargs) -> Callable:
cls._name = provider
classpath = "asyncdb.providers.{provider}".format(provider=cls._name)
try:
obj = module_exists(cls._name, classpath)
if obj:
cls._provider = obj(**kwargs)
return cls._provider
else:
raise AsyncDBException(
message="Cannot Load provider {}".format(cls._name)
)
except Exception as err:
logging.exception(err)
raise
31 changes: 14 additions & 17 deletions asyncdb/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ def _handle_done_tasks(task: asyncio.Task) -> Any:
logging.exception(f"Exception raised by Task {task}, error: {err}")


class asyncDBException(Exception):
class AsyncDBException(Exception):
"""Base class for other exceptions"""

code: int = 0
message: str = ''

def __init__(self, *args, message: str = '', code: int = None, **kwargs):
super(asyncDBException, self).__init__()
def __init__(self, *args, message: str = '', code: int = None):
self.args = (
message,
code,
*args
)
if message:
self.message = message
if code:
self.code = code
self.message = message
self.code = code
super(AsyncDBException, self).__init__(message)

def __repr__(self):
return f"{__name__}(message={self.message})"
Expand All @@ -98,23 +98,20 @@ def get(self):
return self.message


class ProviderError(asyncDBException):
class ProviderError(AsyncDBException):
"""Database Provider Error"""

def __init__(self, *args, message: str = '', code: int = None, **kwargs):
asyncDBException.__init__(self, message, code, *args, **kwargs)


class DataError(asyncDBException, ValueError):
class DataError(AsyncDBException, ValueError):
"""An error caused by invalid query input."""


class NotSupported(asyncDBException):
class NotSupported(AsyncDBException):
"""Not Supported functionality"""


class UninitializedError(ProviderError):
"""Exception when provider cant be initialized"""
"""Exception when provider cannot be initialized"""


class ConnectionTimeout(ProviderError):
Expand All @@ -131,16 +128,16 @@ class TooManyConnections(ProviderError):
"""Too Many Connections"""


class EmptyStatement(asyncDBException):
class EmptyStatement(AsyncDBException):
"""Raise when no Statement was found"""


class UnknownPropertyError(ProviderError):
"""Raise when invalid property was provide"""
"""Raise when invalid property was provided"""


class StatementError(ProviderError):
"""Raise when an Statement Error"""
"""Raise when statement Error"""


class ConditionsError(ProviderError):
Expand Down
Loading

0 comments on commit ad3d998

Please sign in to comment.