diff --git a/src/pyaro/timeseries/Reader.py b/src/pyaro/timeseries/Reader.py index da3023f..94c9bee 100644 --- a/src/pyaro/timeseries/Reader.py +++ b/src/pyaro/timeseries/Reader.py @@ -1,8 +1,9 @@ import abc +from contextlib import contextmanager + from .Data import Data from .Station import Station from .Filter import Filter, filters -# from contextlib import contextmanager class Reader(abc.ABC): @@ -21,14 +22,17 @@ def __init__(self, filename_or_obj_or_url, filters=None, **kwargs): pass @abc.abstractmethod - def read(self,): + @contextmanager + def read(self): """define read method. All needed parameters should be put into self by the __init__ method - This function is usually called after the Engine's open function.' + This function is usually called after the Engine's open function. + Should implement context manager """ - pass + yield self + @abc.abstractmethod def metadata(self) -> dict[str, str]: """Metadata set by the datasource. @@ -75,18 +79,3 @@ def close(self) -> None: Implement as dummy (pass) if no cleanup needed. """ pass - - # def __enter__(self): - # """Context managaer function - # - # :return: context-object - # """ - # return self - # - # def __exit__(self, type, value, traceback): - # """Context manager function. - # - # The default implementation calls the close function. - # """ - # self.close() - # return diff --git a/src/pyaro/timeseries/Wrappers.py b/src/pyaro/timeseries/Wrappers.py index 3c8f13a..34c0fe3 100644 --- a/src/pyaro/timeseries/Wrappers.py +++ b/src/pyaro/timeseries/Wrappers.py @@ -46,6 +46,15 @@ def data(self, varname): data._set_variable(varname) return data + def metadata(self): + """Get the metadata from the reader + NOT changing the variable name to the newly given ones for the moment + + :return: metadata from the original reader class + """ + metadata = self._reader.metadata() + return metadata + def stations(self): return self._reader.stations() @@ -59,12 +68,15 @@ def variables(self): def close(self): self._reader.close() + @contextmanager def read(self,): """define read method. All needed parameters should be put into self by the __init__ method This function is usually called after the Engine's open function.' """ - return self._reader.read() + with self._reader.read() as ts: + yield self + # return self._reader.read()