From 3a90ab0dbf5826bc7476971cd163c9a080d2fb2f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 8 Mar 2019 22:30:36 +0100 Subject: Digikey: extracting http caching into its own class. --- src/ee/_utils.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/ee/_utils.py') 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() -- cgit v1.2.3