diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-03-08 22:30:36 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-03-08 22:30:36 +0100 |
commit | 3a90ab0dbf5826bc7476971cd163c9a080d2fb2f (patch) | |
tree | 6c42ad693ec8cfcec78483c45a7dce4bdc4e4724 /src/ee/_utils.py | |
parent | 5a6ab34565fa3d783337a68e07e3500361c8fcf5 (diff) | |
download | ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.tar.gz ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.tar.bz2 ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.tar.xz ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.zip |
Digikey: extracting http caching into its own class.
Diffstat (limited to 'src/ee/_utils.py')
-rw-r--r-- | src/ee/_utils.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/ee/_utils.py b/src/ee/_utils.py index 29b039a..f917847 100644 --- a/src/ee/_utils.py +++ b/src/ee/_utils.py @@ -1,4 +1,5 @@ -from typing import List +from pathlib import Path +from typing import List, Optional def ensure_has_columns(df: "pandas.DataFrame", columns: List[str]): @@ -40,3 +41,38 @@ def all(filters): return True return f + + +class HttpCache(object): + def __init__(self, path: Path): + self.path = path + + def lookup(self, key): + cache_path = self._make_path(key) + + if cache_path.exists(): + with open(str(cache_path), "r") as f: + return f.read() + + def save(self, key, value): + cache_path = self._make_path(key) + cache_path.parent.mkdir(parents=True, exist_ok=True) + + with cache_path.open("w") as f: + f.write(value) + + def _make_path(self, key) -> Path: + return self.path / "{}.html".format(key) + + +class EmptyHttpCache(object): + @staticmethod + def lookup(key): + return None + + def save(self, key, value): + pass + + +def maybe_cache(path: Optional[Path]) -> HttpCache: + return HttpCache(path) if path is not None else EmptyHttpCache() |