aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs/namespace/namespace.py
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2022-12-18 13:42:34 +0100
committerMatthias Baumgartner <dev@igsor.net>2022-12-18 13:42:34 +0100
commit0e52514639b043454425a9cc2317d27e628a1027 (patch)
treee9630257a423521ec3aa6c83f6b56a0b2a581bc2 /bsfs/namespace/namespace.py
parent21a02197d73f263ae222f2ccc49248d8617e2d7d (diff)
downloadbsfs-0e52514639b043454425a9cc2317d27e628a1027.tar.gz
bsfs-0e52514639b043454425a9cc2317d27e628a1027.tar.bz2
bsfs-0e52514639b043454425a9cc2317d27e628a1027.zip
namespace and uri extensions
Diffstat (limited to 'bsfs/namespace/namespace.py')
-rw-r--r--bsfs/namespace/namespace.py48
1 files changed, 36 insertions, 12 deletions
diff --git a/bsfs/namespace/namespace.py b/bsfs/namespace/namespace.py
index 8080f5d..f652dcd 100644
--- a/bsfs/namespace/namespace.py
+++ b/bsfs/namespace/namespace.py
@@ -29,29 +29,55 @@ class Namespace():
# namespace prefix.
prefix: URI
- def __init__(self, prefix: URI):
- self.prefix = URI(prefix)
+ # fragment separator.
+ fsep: str
+
+ # path separator.
+ psep: str
+
+ def __init__(self, prefix: URI, fsep: str = '#', psep: str = '/'):
+ # ensure prefix type
+ prefix = URI(prefix)
+ # truncate fragment separator
+ while prefix.endswith(fsep):
+ prefix = URI(prefix[:-1])
+ # truncate path separator
+ while prefix.endswith(psep):
+ prefix = URI(prefix[:-1])
+ # store members
+ self.prefix = prefix
+ self.fsep = fsep
+ self.psep = psep
def __eq__(self, other: typing.Any) -> bool:
- return isinstance(other, type(self)) and self.prefix == other.prefix
+ return isinstance(other, type(self)) \
+ and self.prefix == other.prefix \
+ and self.fsep == other.fsep \
+ and self.psep == other.psep
def __hash__(self) -> int:
- return hash((type(self), self.prefix))
+ return hash((type(self), self.prefix, self.fsep, self.psep))
def __str__(self) -> str:
return f'{typename(self)}({self.prefix})'
def __repr__(self) -> str:
- return f'{typename(self)}({self.prefix})'
+ return f'{typename(self)}({self.prefix}, {self.fsep}, {self.psep})'
def __getattr__(self, fragment: str) -> URI:
"""Return prefix + fragment."""
- return URI(self.prefix + fragment)
+ return URI(self.prefix + self.fsep + fragment)
def __getitem__(self, fragment: str) -> URI:
"""Alias for getattr(self, fragment)."""
return self.__getattr__(fragment)
+ def __add__(self, value: typing.Any) -> 'Namespace':
+ """Concatenate another namespace to this one."""
+ if not isinstance(value, str):
+ return NotImplemented
+ return Namespace(self.prefix + self.psep + value, self.fsep, self.psep)
+
class ClosedNamespace(Namespace):
"""Namespace that covers a restricted set of URIs."""
@@ -59,8 +85,8 @@ class ClosedNamespace(Namespace):
# set of permissible fragments.
fragments: typing.Set[str]
- def __init__(self, prefix: URI, *args: str):
- super().__init__(prefix)
+ def __init__(self, prefix: URI, *args: str, fsep: str = '#', psep: str = '/'):
+ super().__init__(prefix, fsep, psep)
self.fragments = set(args)
def __eq__(self, other: typing.Any) -> bool:
@@ -70,11 +96,9 @@ class ClosedNamespace(Namespace):
return hash((type(self), self.prefix, tuple(sorted(self.fragments))))
def __getattr__(self, fragment: str) -> URI:
- """Return prefix + fragment.
- Raises a KeyError if the fragment is not allowed in this namespace.
- """
+ """Return prefix + fragment or raise a KeyError if the fragment is not part of this namespace."""
if fragment not in self.fragments:
- raise KeyError('fragment')
+ raise KeyError(f'{fragment} is not a valid fragment of namespace {self.prefix}')
return super().__getattr__(fragment)
## EOF ##