Commit abb1ad98 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common:

- Removed unused ORM
- Removed old tutorial folder
- Commented out unfinished unitary tests
parent 770e7088
Loading
Loading
Loading
Loading

src/common/orm/Database.py

deleted100644 → 0
+0 −52
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from typing import List, Set, Tuple
from .backend._Backend import _Backend

LOGGER = logging.getLogger(__name__)

class Database:
    def __init__(self, backend : _Backend):
        if not isinstance(backend, _Backend):
            str_class_path = '{}.{}'.format(_Backend.__module__, _Backend.__name__)
            raise AttributeError('backend must inherit from {}'.format(str_class_path))
        self._backend = backend

    @property
    def backend(self) -> _Backend: return self._backend

    def clear_all(self, keep_keys : Set[str] = set()) -> None:
        for key in self._backend.keys():
            if key in keep_keys: continue
            self._backend.delete(key)

    def dump(self) -> List[Tuple[str, str, str]]:
        entries = self._backend.dump()
        entries.sort()
        _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

src/common/orm/Exceptions.py

deleted100644 → 0
+0 −19
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class ConstraintException(Exception):
    pass

class MutexException(Exception):
    pass

src/common/orm/Factory.py

deleted100644 → 0
+0 −46
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging, os
from typing import Optional, Union
from .backend._Backend import _Backend
from .backend.BackendEnum import BackendEnum
from .backend.inmemory.InMemoryBackend import InMemoryBackend
from .backend.redis.RedisBackend import RedisBackend

LOGGER = logging.getLogger(__name__)

BACKENDS = {
    BackendEnum.INMEMORY.value: InMemoryBackend,
    BackendEnum.REDIS.value: RedisBackend,
    #BackendEnum.MONGODB.value: MongoDBBackend,
    #BackendEnum.RETHINKDB.value: RethinkDBBackend,
    #BackendEnum.ETCD.value: EtcdBackend,
}

DEFAULT_DB_BACKEND = BackendEnum.INMEMORY

def get_database_backend(backend : Optional[Union[str, BackendEnum]] = None, **settings) -> _Backend:
    # return an instance of Database initialized with selected backend.
    # The backend is selected using following criteria (first that is not None is selected):
    # 1. user selected by parameter (backend=...)
    # 2. environment variable DB_BACKEND
    # 3. default backend: INMEMORY
    if backend is None: backend = os.environ.get('DB_BACKEND', DEFAULT_DB_BACKEND)
    if backend is None: raise Exception('Database Backend not specified')
    if isinstance(backend, BackendEnum): backend = backend.value
    backend_class = BACKENDS.get(backend)
    if backend_class is None: raise Exception('Unsupported DatabaseBackend({:s})'.format(backend))
    LOGGER.info('Selected Database Backend: {:s}'.format(backend))
    return backend_class(**settings)

src/common/orm/HighLevel.py

deleted100644 → 0
+0 −84
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, List, Optional, Set, Tuple
from common.method_wrappers.ServiceExceptions import NotFoundException
from common.orm.Database import Database
from common.orm.backend.Tools import key_to_str
from common.orm.fields.ForeignKeyField import ForeignKeyField
from common.orm.model.Model import Model, MetaModel

def get_all_objects(database : Database, model_class : MetaModel) -> List[Model]:
    db_pks = sorted(list(model_class.get_primary_keys(database)))
    return [model_class(database, db_pk) for db_pk in db_pks]

def get_object(
    database : Database, model_class : Model, key_parts : List[str], raise_if_not_found : bool = True
    ) -> Optional[Model]:

    str_key = key_to_str(key_parts)
    db_object = model_class(database, str_key, auto_load=False)
    found = db_object.load()
    if found: return db_object
    if raise_if_not_found: raise NotFoundException(model_class.__name__.replace('Model', ''), str_key)
    return None

def get_related_objects(
    source_instance : Model, reference_model_class : MetaModel, navigation_field_name : str = None) -> Set[Model]:

    database = source_instance.database
    db_target_instances = set()

    if navigation_field_name is not None:
        navigation_fk_field : Optional[ForeignKeyField] = getattr(reference_model_class, navigation_field_name, None)
        if navigation_fk_field is None or not isinstance(navigation_fk_field, ForeignKeyField):
            msg = 'navigation_field_name({:s}) must be a ForeignKeyField in reference_model_class({:s})'
            raise AttributeError(msg.format(navigation_field_name, reference_model_class.__name__))
        target_model_class = navigation_fk_field.foreign_model

    for db_reference_pk,_ in source_instance.references(reference_model_class):
        db_reference = reference_model_class(database, db_reference_pk)
        if navigation_field_name is not None:
            target_fk_field = getattr(db_reference, navigation_field_name, None)
            if target_fk_field is None: continue
            db_reference = target_model_class(database, target_fk_field)
        db_target_instances.add(db_reference)
    return db_target_instances

def update_or_create_object(
    database : Database, model_class : Model, key_parts : List[str], attributes : Dict[str, Any]
    ) -> Tuple[Model, bool]:

    str_key = key_to_str(key_parts)
    db_object : Model = model_class(database, str_key, auto_load=False)
    found = db_object.load()
    for attr_name, attr_value in attributes.items():
        setattr(db_object, attr_name, attr_value)
    db_object.save()
    updated = found # updated if found, else created
    return db_object, updated

def get_or_create_object(
    database : Database, model_class : Model, key_parts : List[str], defaults : Dict[str, Any] = {}
    ) -> Tuple[Model, bool]:

    str_key = key_to_str(key_parts)
    db_object : Model = model_class(database, str_key, auto_load=False)
    found = db_object.load()
    if not found:
        for attr_name, attr_value in defaults.items():
            setattr(db_object, attr_name, attr_value)
        db_object.save()
    created = not found # created if not found, else loaded
    return db_object, created

src/common/orm/__init__.py

deleted100644 → 0
+0 −14
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading