__init__.py

Last source

View documentation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Package defining the repository for the :obj:`.Namespace` entity."""

import abc
from typing import Iterable, Literal, Union  # type: ignore

from .....utils.repository import AbstractInMemoryRepository, AbstractRepository
from ...entities import Namespace


class AbstractNamespaceRepository(
    AbstractRepository[Namespace], entity_class=Namespace
):
    """Base repository for the :obj:`.Namespace` entity."""

    @abc.abstractmethod
    def for_namespace(
        self, namespace: Union[Namespace, Literal[None]]
    ) -> Iterable[Namespace]:
        """Iterate on namespaces found in the given `namespace`, or with no namespace if ``None``.

        Parameters
        ----------
        namespace : Union[Namespace, None]
            The namespace for which we want to find the namespaces.
            If ``None``, will look for namespaces having no parent namespace.

        Returns
        -------
        Iterable[Namespace]
            An iterable of the namespaces found in the `namespace` (or that have no namespace if
            `namespace` is ``None``)

        """


class InMemoryNamespaceRepository(
    AbstractInMemoryRepository, AbstractNamespaceRepository
):
    """Repository to handle :obj:`.Namespace` entities in memory."""

    def add(self, entity: Namespace) -> Namespace:
        """Add the given Namespace `entity` in the repository.

        For the parameters, see :obj:`AbstractRepository.add`.

        Returns
        -------
        Namespace
            The added Namespace

        Raises
        ------
        self.UniquenessError
            - If a namespace with the same identifier as the given one already exists.
            - If a namespace with the same name and parent namespace (including no namespace) as
              the given one already exists.

        """
        if any(
            namespace
            for namespace in self.for_namespace(entity.namespace)
            if namespace.name == entity.name
        ):
            raise self.UniquenessError(
                f"One already exists with name={entity.name} and namespace={entity.namespace}"
            )
        return super().add(entity)

    def for_namespace(
        self, namespace: Union[Namespace, Literal[None]]
    ) -> Iterable[Namespace]:
        """Iterate on namespaces found in the given `namespace`, or with no namespace if ``None``.

        For the parameters, see :obj:`AbstractNamespaceRepository.for_namespace`

        Returns
        -------
        Iterable[Namespace]
            An iterable of the namespaces found in the `namespace`

        """
        return (entity for entity in self._collection if entity.namespace == namespace)

Changes

docs(domain): Add diagrams for repositories

Commit
Hash

34f4694cc9f5649a16e1952276eb5e26d4f84d00

Date

2020-10-07 12:10:48 +0200

Type

Modified

Stats

+7 -3

@@ -1,7 +1,7 @@
 """Package defining the repository for the :obj:`.Namespace` entity."""

 import abc
-from typing import Iterable, Union
+from typing import Iterable, Literal, Union  # type: ignore

 from .....utils.repository import AbstractInMemoryRepository, AbstractRepository
 from ...entities import Namespace
@@ -13,7 +13,9 @@ class AbstractNamespaceRepository(
     """Base repository for the :obj:`.Namespace` entity."""

     @abc.abstractmethod
-    def for_namespace(self, namespace: Union[Namespace, None]) -> Iterable[Namespace]:
+    def for_namespace(
+        self, namespace: Union[Namespace, Literal[None]]
+    ) -> Iterable[Namespace]:
         """Iterate on namespaces found in the given `namespace`, or with no namespace if ``None``.

         Parameters
@@ -64,7 +66,9 @@ class InMemoryNamespaceRepository(
             )
         return super().add(entity)

-    def for_namespace(self, namespace: Union[Namespace, None]) -> Iterable[Namespace]:
+    def for_namespace(
+        self, namespace: Union[Namespace, Literal[None]]
+    ) -> Iterable[Namespace]:
         """Iterate on namespaces found in the given `namespace`, or with no namespace if ``None``.

         For the parameters, see :obj:`AbstractNamespaceRepository.for_namespace`

feat(repository): Add domain repositories

Commit
Hash

27f013e2a3722926a9bbe300a77a493604f0993c

Date

2020-10-06 17:30:45 +0200

Type

Added

Stats

+78 -0

@@ -0,0 +1,78 @@
+"""Package defining the repository for the :obj:`.Namespace` entity."""
+
+import abc
+from typing import Iterable, Union
+
+from .....utils.repository import AbstractInMemoryRepository, AbstractRepository
+from ...entities import Namespace
+
+
+class AbstractNamespaceRepository(
+    AbstractRepository[Namespace], entity_class=Namespace
+):
+    """Base repository for the :obj:`.Namespace` entity."""
+
+    @abc.abstractmethod
+    def for_namespace(self, namespace: Union[Namespace, None]) -> Iterable[Namespace]:
+        """Iterate on namespaces found in the given `namespace`, or with no namespace if ``None``.
+
+        Parameters
+        ----------
+        namespace : Union[Namespace, None]
+            The namespace for which we want to find the namespaces.
+            If ``None``, will look for namespaces having no parent namespace.
+
+        Returns
+        -------
+        Iterable[Namespace]
+            An iterable of the namespaces found in the `namespace` (or that have no namespace if
+            `namespace` is ``None``)
+
+        """
+
+
+class InMemoryNamespaceRepository(
+    AbstractInMemoryRepository, AbstractNamespaceRepository
+):
+    """Repository to handle :obj:`.Namespace` entities in memory."""
+
+    def add(self, entity: Namespace) -> Namespace:
+        """Add the given Namespace `entity` in the repository.
+
+        For the parameters, see :obj:`AbstractRepository.add`.
+
+        Returns
+        -------
+        Namespace
+            The added Namespace
+
+        Raises
+        ------
+        self.UniquenessError
+            - If a namespace with the same identifier as the given one already exists.
+            - If a namespace with the same name and parent namespace (including no namespace) as
+              the given one already exists.
+
+        """
+        if any(
+            namespace
+            for namespace in self.for_namespace(entity.namespace)
+            if namespace.name == entity.name
+        ):
+            raise self.UniquenessError(
+                f"One already exists with name={entity.name} and namespace={entity.namespace}"
+            )
+        return super().add(entity)
+
+    def for_namespace(self, namespace: Union[Namespace, None]) -> Iterable[Namespace]:
+        """Iterate on namespaces found in the given `namespace`, or with no namespace if ``None``.
+
+        For the parameters, see :obj:`AbstractNamespaceRepository.for_namespace`
+
+        Returns
+        -------
+        Iterable[Namespace]
+            An iterable of the namespaces found in the `namespace`
+
+        """
+        return (entity for entity in self._collection if entity.namespace == namespace)