From 068b3651c16916877eb8d5fdfec52485a507e204 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Mon, 31 Oct 2022 13:05:31 +0100 Subject: path and stat readers --- bsie/reader/__init__.py | 19 +++++++++++++++++++ bsie/reader/path.py | 31 +++++++++++++++++++++++++++++++ bsie/reader/stat.py | 34 ++++++++++++++++++++++++++++++++++ test/reader/test_path.py | 28 ++++++++++++++++++++++++++++ test/reader/test_stat.py | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 bsie/reader/__init__.py create mode 100644 bsie/reader/path.py create mode 100644 bsie/reader/stat.py create mode 100644 test/reader/test_path.py create mode 100644 test/reader/test_stat.py diff --git a/bsie/reader/__init__.py b/bsie/reader/__init__.py new file mode 100644 index 0000000..a45f22b --- /dev/null +++ b/bsie/reader/__init__.py @@ -0,0 +1,19 @@ +"""The Reader classes return high-level content structures from files. + +The Reader fulfills two purposes: + First, it brokers between multiple libraries and file formats. + Second, it separates multiple aspects of a file into distinct content types. + +Often, different libraries focus on reading different types of content from a +file. E.g. one would use different modules to read file system infos than to +read exif or pixel data of an image. Hence, this module is organized by content +type. Each distinct type can be implemented in a file or submodule that +provides a Reader implementation. Through utilization of submodules, different +file formats can be supported. + +Part of the bsie module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" + +## EOF ## diff --git a/bsie/reader/path.py b/bsie/reader/path.py new file mode 100644 index 0000000..d27c664 --- /dev/null +++ b/bsie/reader/path.py @@ -0,0 +1,31 @@ +"""The Path reader produces a file path. + +Part of the bsie module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import os +import typing + +# inner-module imports +from bsie.base import reader + +# exports +__all__: typing.Sequence[str] = ( + 'Path', + ) + + +## code ## + +class Path(reader.Reader): + """Return the path.""" + + CONTENT_TYPE = typing.Union[str] + + def __call__(self, path: str) -> CONTENT_TYPE: + return path + + +## EOF ## diff --git a/bsie/reader/stat.py b/bsie/reader/stat.py new file mode 100644 index 0000000..f0b83fb --- /dev/null +++ b/bsie/reader/stat.py @@ -0,0 +1,34 @@ +"""The Stat reader produces filesystem stat information. + +Part of the bsie module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import os +import typing + +# inner-module imports +from bsie.base import reader, errors + +# exports +__all__: typing.Sequence[str] = ( + 'Stat', + ) + + +## code ## + +class Stat(reader.Reader): + """Read and return the filesystem's stat infos.""" + + CONTENT_TYPE = typing.Union[os.stat_result] + + def __call__(self, path: str) -> CONTENT_TYPE: + try: + return os.stat(path) + except Exception: + raise errors.ReaderError(path) + + +## EOF ## diff --git a/test/reader/test_path.py b/test/reader/test_path.py new file mode 100644 index 0000000..fd7bc5a --- /dev/null +++ b/test/reader/test_path.py @@ -0,0 +1,28 @@ +""" + +Part of the bsie test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import unittest + +# objects to test +from bsie.reader.path import Path + + +## code ## + +class TestPath(unittest.TestCase): + def test_call(self): + self.assertEqual('', Path()('')) + self.assertEqual('/tmp/foo/bar', Path()('/tmp/foo/bar')) + self.assertEqual('/home/myself/some file', Path()('/home/myself/some file')) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/reader/test_stat.py b/test/reader/test_stat.py new file mode 100644 index 0000000..d12ad9c --- /dev/null +++ b/test/reader/test_stat.py @@ -0,0 +1,34 @@ +""" + +Part of the bsie test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import os +import unittest + +# bsie imports +from bsie.base import errors + +# objects to test +from bsie.reader.stat import Stat + + +## code ## + +class TestPath(unittest.TestCase): + def test_call(self): + # test self + self.assertEqual(os.stat(__file__), Stat()(__file__)) + # test invalid file + self.assertRaises(errors.ReaderError, Stat(), '') + self.assertRaises(errors.ReaderError, Stat(), None) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## -- cgit v1.2.3