diff options
author | Matthias Baumgartner <dev@igsor.net> | 2022-12-08 16:33:36 +0100 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2022-12-08 16:33:36 +0100 |
commit | 729f025f392d45b621941da9d052834e0d81506e (patch) | |
tree | d8ff3b58267da4735dbce67fc168ab31e687f58f | |
parent | bbfcee4fffc553b5dd08f37a79dd6ccddbf340f8 (diff) | |
download | bsfs-729f025f392d45b621941da9d052834e0d81506e.tar.gz bsfs-729f025f392d45b621941da9d052834e0d81506e.tar.bz2 bsfs-729f025f392d45b621941da9d052834e0d81506e.zip |
namespaces
-rw-r--r-- | bsfs/namespace/__init__.py | 21 | ||||
-rw-r--r-- | bsfs/namespace/namespace.py | 80 | ||||
-rw-r--r-- | bsfs/namespace/predefined.py | 39 | ||||
-rw-r--r-- | test/namespace/__init__.py | 0 | ||||
-rw-r--r-- | test/namespace/test_namespace.py | 97 |
5 files changed, 237 insertions, 0 deletions
diff --git a/bsfs/namespace/__init__.py b/bsfs/namespace/__init__.py new file mode 100644 index 0000000..98d472f --- /dev/null +++ b/bsfs/namespace/__init__.py @@ -0,0 +1,21 @@ +""" + +Part of the BlackStar filesystem (bsfs) module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import typing + +# inner-module imports +from . import predefined as ns +from .namespace import ClosedNamespace, Namespace + +# exports +__all__: typing.Sequence[str] = ( + 'ClosedNamespace', + 'Namespace', + 'ns', + ) + +## EOF ## diff --git a/bsfs/namespace/namespace.py b/bsfs/namespace/namespace.py new file mode 100644 index 0000000..8080f5d --- /dev/null +++ b/bsfs/namespace/namespace.py @@ -0,0 +1,80 @@ +""" + +Part of the BlackStar filesystem (bsfs) module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import typing + +# bsfs imports +from bsfs.utils import URI, typename + +# exports +__all__: typing.Sequence[str] = ( + 'ClosedNamespace', + 'Namespace', + ) + + +## code ## + +class Namespace(): + """A namespace consists of a common prefix that is used in a set of URIs. + + Note that the prefix must include the separator between + path and fragment (typically a '#' or a '/'). + """ + + # namespace prefix. + prefix: URI + + def __init__(self, prefix: URI): + self.prefix = URI(prefix) + + def __eq__(self, other: typing.Any) -> bool: + return isinstance(other, type(self)) and self.prefix == other.prefix + + def __hash__(self) -> int: + return hash((type(self), self.prefix)) + + def __str__(self) -> str: + return f'{typename(self)}({self.prefix})' + + def __repr__(self) -> str: + return f'{typename(self)}({self.prefix})' + + def __getattr__(self, fragment: str) -> URI: + """Return prefix + fragment.""" + return URI(self.prefix + fragment) + + def __getitem__(self, fragment: str) -> URI: + """Alias for getattr(self, fragment).""" + return self.__getattr__(fragment) + + +class ClosedNamespace(Namespace): + """Namespace that covers a restricted set of URIs.""" + + # set of permissible fragments. + fragments: typing.Set[str] + + def __init__(self, prefix: URI, *args: str): + super().__init__(prefix) + self.fragments = set(args) + + def __eq__(self, other: typing.Any) -> bool: + return super().__eq__(other) and self.fragments == other.fragments + + def __hash__(self) -> int: + 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. + """ + if fragment not in self.fragments: + raise KeyError('fragment') + return super().__getattr__(fragment) + +## EOF ## diff --git a/bsfs/namespace/predefined.py b/bsfs/namespace/predefined.py new file mode 100644 index 0000000..21ca560 --- /dev/null +++ b/bsfs/namespace/predefined.py @@ -0,0 +1,39 @@ +""" + +Part of the BlackStar filesystem (bsfs) module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import typing + +# bsfs imports +from bsfs.utils import URI + +# inner-module imports +from . import namespace + +# essential bsfs namespaces +bsfs: namespace.Namespace = namespace.Namespace(URI('http://bsfs.ai/schema/')) + +# additional bsfs namespaces +bse: namespace.Namespace = namespace.Namespace(URI('http://bsfs.ai/schema/Entity#')) +bsm: namespace.Namespace = namespace.Namespace(URI('http://bsfs.ai/schema/Meta#')) + +# generic namespaces +rdf: namespace.Namespace = namespace.Namespace(URI('http://www.w3.org/1999/02/22-rdf-syntax-ns#')) +rdfs: namespace.Namespace = namespace.Namespace(URI('http://www.w3.org/2000/01/rdf-schema#')) +schema: namespace.Namespace = namespace.Namespace(URI('http://schema.org/')) +xsd: namespace.Namespace = namespace.Namespace(URI('http://www.w3.org/2001/XMLSchema#')) + +__all__: typing.Sequence[str] = ( + 'bse', + 'bsfs', + 'bsm', + 'rdf', + 'rdfs', + 'schema', + 'xsd', + ) + +## EOF ## diff --git a/test/namespace/__init__.py b/test/namespace/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/namespace/__init__.py diff --git a/test/namespace/test_namespace.py b/test/namespace/test_namespace.py new file mode 100644 index 0000000..1ad53e3 --- /dev/null +++ b/test/namespace/test_namespace.py @@ -0,0 +1,97 @@ +""" + +Part of the tagit test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import unittest + +# objects to test +from bsfs.namespace.namespace import Namespace, ClosedNamespace + + +## code ## + +class TestNamespace(unittest.TestCase): + def test_essentials(self): + # string conversion + self.assertEqual(str(Namespace('http://example.org/')), 'Namespace(http://example.org/)') + self.assertEqual(str(Namespace('http://example.org#')), 'Namespace(http://example.org#)') + self.assertEqual(repr(Namespace('http://example.org/')), 'Namespace(http://example.org/)') + self.assertEqual(repr(Namespace('http://example.org#')), 'Namespace(http://example.org#)') + # comparison + class Foo(Namespace): pass + self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org/')) + self.assertNotEqual(Namespace('http://example.org/'), Foo('http://example.org/')) + self.assertNotEqual(Foo('http://example.org/'), Namespace('http://example.org/')) + self.assertNotEqual(Namespace('http://example.org/'), Namespace('http://example.org#')) + self.assertNotEqual(Namespace('http://example.org#'), Namespace('http://example.org/')) + # hashing + self.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org/'))) + self.assertNotEqual(hash(Namespace('http://example.org/')), hash(Foo('http://example.org/'))) + self.assertNotEqual(hash(Foo('http://example.org/')), hash(Namespace('http://example.org/'))) + self.assertNotEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org#'))) + self.assertNotEqual(hash(Namespace('http://example.org#')), hash(Namespace('http://example.org/'))) + + def test_getattr(self): + self.assertEqual(Namespace('http://example.org/').foo, 'http://example.org/foo') + self.assertEqual(Namespace('http://example.org/').bar, 'http://example.org/bar') + self.assertEqual(Namespace('http://example.org/me#').foo, 'http://example.org/me#foo') + self.assertEqual(Namespace('http://example.org/me#').bar, 'http://example.org/me#bar') + + def test_getitem(self): + self.assertEqual(Namespace('http://example.org/')['foo'], 'http://example.org/foo') + self.assertEqual(Namespace('http://example.org/')['bar'], 'http://example.org/bar') + self.assertEqual(Namespace('http://example.org/me#')['foo'], 'http://example.org/me#foo') + self.assertEqual(Namespace('http://example.org/me#')['bar'], 'http://example.org/me#bar') + + +class TestClosedNamespace(unittest.TestCase): + def test_essentials(self): + # string conversion + self.assertEqual(str(ClosedNamespace('http://example.org/')), 'ClosedNamespace(http://example.org/)') + self.assertEqual(str(ClosedNamespace('http://example.org#')), 'ClosedNamespace(http://example.org#)') + self.assertEqual(repr(ClosedNamespace('http://example.org/')), 'ClosedNamespace(http://example.org/)') + self.assertEqual(repr(ClosedNamespace('http://example.org#')), 'ClosedNamespace(http://example.org#)') + # comparison + class Foo(ClosedNamespace): pass + self.assertEqual(ClosedNamespace('http://example.org/'), ClosedNamespace('http://example.org/')) + self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar'), ClosedNamespace('http://example.org/', 'foo', 'bar')) + self.assertNotEqual(ClosedNamespace('http://example.org/', 'foo'), ClosedNamespace('http://example.org/', 'bar')) + self.assertNotEqual(ClosedNamespace('http://example.org/'), Foo('http://example.org/')) + self.assertNotEqual(Foo('http://example.org/'), ClosedNamespace('http://example.org/')) + self.assertNotEqual(ClosedNamespace('http://example.org/'), ClosedNamespace('http://example.org#')) + self.assertNotEqual(ClosedNamespace('http://example.org#'), ClosedNamespace('http://example.org/')) + # hashing + self.assertEqual(hash(ClosedNamespace('http://example.org/')), hash(ClosedNamespace('http://example.org/'))) + self.assertEqual(hash(ClosedNamespace('http://example.org/', 'foo', 'bar')), hash(ClosedNamespace('http://example.org/', 'foo', 'bar'))) + self.assertNotEqual(hash(ClosedNamespace('http://example.org/', 'foo')), hash(ClosedNamespace('http://example.org/', 'bar'))) + self.assertNotEqual(hash(ClosedNamespace('http://example.org/')), hash(Foo('http://example.org/'))) + self.assertNotEqual(hash(Foo('http://example.org/')), hash(ClosedNamespace('http://example.org/'))) + self.assertNotEqual(hash(ClosedNamespace('http://example.org/')), hash(ClosedNamespace('http://example.org#'))) + self.assertNotEqual(hash(ClosedNamespace('http://example.org#')), hash(ClosedNamespace('http://example.org/'))) + + def test_getattr(self): + self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar').foo, 'http://example.org/foo') + self.assertEqual(ClosedNamespace('http://example.org/', 'bar', 'bar').bar, 'http://example.org/bar') + self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar').foo, 'http://example.org/me#foo') + self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar').bar, 'http://example.org/me#bar') + self.assertRaises(KeyError, getattr, ClosedNamespace('http://example.org/', 'bar', 'bar'), 'foobar') + self.assertRaises(KeyError, getattr, ClosedNamespace('http://example.org#', 'bar', 'bar'), 'foobar') + + def test_getitem(self): + self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar')['foo'], 'http://example.org/foo') + self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar')['bar'], 'http://example.org/bar') + self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar')['foo'], 'http://example.org/me#foo') + self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar')['bar'], 'http://example.org/me#bar') + self.assertRaises(KeyError, ClosedNamespace('http://example.org/', 'bar', 'bar').__getitem__, 'foobar') + self.assertRaises(KeyError, ClosedNamespace('http://example.org#', 'bar', 'bar').__getitem__, 'foobar') + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## |