Skip to content

Commit

Permalink
WIP: refactoring so that a context manager implementing read method i…
Browse files Browse the repository at this point in the history
…s required
  • Loading branch information
jgriesfeller committed Oct 29, 2024
1 parent b891183 commit eea525a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
27 changes: 8 additions & 19 deletions src/pyaro/timeseries/Reader.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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
14 changes: 13 additions & 1 deletion src/pyaro/timeseries/Wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()


0 comments on commit eea525a

Please sign in to comment.