Commit 10e25f75 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

First functional version of backend-agnostic ORM including test units.

parent 384e8dd2
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
import logging
from typing import List, Set
from typing import List, Set, Tuple
from .backend._Backend import _Backend

LOGGER = logging.getLogger(__name__)
@@ -19,7 +19,20 @@ class Database:
            if key in keep_keys: continue
            self._backend.delete(key)

    def dump(self) -> List[str]:
    def dump(self) -> List[Tuple[str, str, str]]:
        entries = self._backend.dump()
        entries.sort()
        return ['[{:>4s}] {:100s} :: {}'.format(k_type, k_name, k_value) for k_name,k_type,k_value in entries]
        _entries = []
        for str_key, str_type, value in entries:
            if isinstance(value, list):
                str_value = ', '.join(map("'{:s}'".format, sorted(list(value))))
                str_value = '[' + str_value + ']'
            elif isinstance(value, set):
                str_value = ', '.join(map("'{:s}'".format, sorted(list(value))))
                str_value = '{' + str_value + '}'
            elif isinstance(value, dict):
                sorted_keys = sorted(value.keys())
                str_value = ', '.join(["'{}': '{}'".format(key, value[key]) for key in sorted_keys])
                str_value = '{' + str_value + '}'
            _entries.append((str_type, str_key, str_value))
        return _entries
+2 −2
Original line number Diff line number Diff line
from typing import Dict, List, Optional, Set, Tuple
from typing import Any, Dict, List, Optional, Set, Tuple

class _Backend:
    def __init__(self, **settings) -> None:
@@ -49,5 +49,5 @@ class _Backend:
    def set_remove(self, key : List[str], item : str) -> None:
        raise NotImplementedError()

    def dump(self) -> List[Tuple[str, str, str]]:
    def dump(self) -> List[Tuple[str, str, Any]]:
        raise NotImplementedError()
+3 −3
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
#            USE ANOTHER BACKEND IN PRODUCTION ENVIRONMENTS.

import copy, logging, threading, uuid
from typing import Dict, List, Optional, Set, Tuple, Union
from typing import Any, Dict, List, Optional, Set, Tuple, Union
from .._Backend import _Backend
from ..Tools import key_to_str
from .Tools import get_dict, get_list, get_or_create_dict, get_or_create_list, get_or_create_set, get_set
@@ -139,9 +139,9 @@ class InMemoryBackend(_Backend):
            container.discard(item)
            if len(container) == 0: self._keys.pop(str_key)

    def dump(self) -> List[Tuple[str, str, str]]:
    def dump(self) -> List[Tuple[str, str, Any]]:
        with self._lock:
            entries = []
            for str_key,key_value in self._keys.items():
                entries.append((str_key, type(key_value).__name__, str(key_value)))
                entries.append((str_key, type(key_value).__name__, key_value))
        return entries
+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ class RedisBackend(_Backend):
        str_key = key_to_str(key)
        self._client.srem(str_key, item)

    def dump(self) -> List[Tuple[str, str, str]]:
    def dump(self) -> List[Tuple[str, str, Any]]:
        entries = []
        for str_key in self._client.keys():
            str_key = str_key.decode('UTF-8')
+7 −17
Original line number Diff line number Diff line
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import Union
from common.type_checkers.Checkers import chk_boolean
from .Field import Field

if TYPE_CHECKING:
    from ..model.Model import Model

BOOL_TRUE_VALUES = {'TRUE', 'T', '1'}

class BooleanField(Field):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, type_=bool, **kwargs)

    def __set__(self, instance : 'Model', value : bool) -> None:
        super().__set__(instance, self.validate(value))

    def validate(self, value):
        value = super().validate(value)
        return None if value is None else chk_boolean(self.name, value)

    def serialize(self, value : bool) -> str:
        value = self.validate(value)
        return None if value is None else str(value)

    def deserialize(self, value : str) -> bool:
        return (value.upper() in BOOL_TRUE_VALUES)
    def validate(self, value : Union[bool, str], try_convert_type=False) -> bool:
        value = self.is_required(value)
        if value is None: return None
        if try_convert_type and isinstance(value, str):
            return value.upper() in BOOL_TRUE_VALUES
        return chk_boolean(self.name, value)
Loading