aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs/namespace/namespace.py
blob: b388f537cd61ec14fa72b88670a1bd5dae49d8bc (plain)
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

# imports
import typing

# bsfs imports
from bsfs.utils import URI

# exports
__all__: typing.Sequence[str] = (
    'Namespace',
    'FinalNamespace',
    )


## code ##


class Namespace(URI):
    """The Namespace allows you to incrementally append path segments to an URI.

    Segments are separated by `Namespace.sep` ('/').
    The `__call__` method signals that the URI is complete until the query part.

    """

    # path separator
    sep: str = '/'

    def __getattr__(self, query: str) -> 'Namespace':
        """Append the *query* to the current value and return as Namespace."""
        return Namespace(self + self.sep + query)

    def __call__(self, sep: str = '#') -> 'FinalNamespace':
        """Finalize the namespace."""
        return FinalNamespace(self, sep)


# FIXME: Integrate FinalNamespace into Namespace? Do we need to have both?
class FinalNamespace(URI):
    """The FinalNamespace allows you to append a fragment to an URI."""

    # fragment separator
    sep: str

    def __new__(cls, value: str, sep: str = '#'):
        inst = URI.__new__(cls, value)
        inst.sep = sep
        return inst

    def __getattr__(self, fragment: str) -> URI:
        """Append the *fragment* to the current value and return as URI."""
        return URI(self + self.sep + fragment)

## EOF ##