From 21e82f0cd66767fcaae3206f172049fb459eccd5 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 24 Apr 2020 15:19:31 +0100 Subject: [PATCH] add disable_cachew helper --- my/core/cachew.py | 29 +++++++++++++++++++++++++++++ my/core/time.py | 2 +- my/takeout.py | 17 ++++++----------- 3 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 my/core/cachew.py diff --git a/my/core/cachew.py b/my/core/cachew.py new file mode 100644 index 0000000..551527a --- /dev/null +++ b/my/core/cachew.py @@ -0,0 +1,29 @@ +''' +# TODO this probably belongs to cachew? or cachew.experimental +''' +from contextlib import contextmanager + + +def disable_cachew(): + ''' + NOTE: you need to use it before importing any function using @cachew.cachew + ''' + # TODO not sure... maybe it should instead use some hook.. it's a ibt ugly do + import cachew + + @cachew.doublewrap + def cachew_off(func=None, *args, **kwargs): + return func + old = cachew.cachew + cachew.cachew = cachew_off + return old + + +@contextmanager +def disabled_cachew(): + import cachew + old = disable_cachew() + try: + yield + finally: + cachew.cachew = old diff --git a/my/core/time.py b/my/core/time.py index d34ebf8..2c642d6 100644 --- a/my/core/time.py +++ b/my/core/time.py @@ -11,6 +11,6 @@ tz_lookup = { tz_lookup['UTC'] = pytz.utc # ugh. otherwise it'z Zulu... -@lru_cache(-1) +@lru_cache(None) def abbr_to_timezone(abbr: str): return tz_lookup[abbr] diff --git a/my/takeout.py b/my/takeout.py index 64dbcda..592f439 100644 --- a/my/takeout.py +++ b/my/takeout.py @@ -2,30 +2,25 @@ from pathlib import Path from typing import Optional from .common import get_files +from .kython.kompress import kopen, kexists from my.config import google as config -from .kython.kompress import kopen - def get_last_takeout(*, path: Optional[str]=None) -> Path: """ Ok, sometimes google splits takeout into two zip archives I guess I could detect it (they've got 001/002 etc suffixes), but fornow that works fine.. """ + # TODO FIXME zip is not great.. + # allow a lambda expression? that way the user could restrict it for takeout in reversed(get_files(config.takeout_path, glob='*.zip')): - if path is None: + if path is None or kexists(takeout, path): return takeout else: - try: - kopen(takeout, path) - return takeout - except: - # TODO eh, a bit horrible, but works for now.. - # TODO move ot kompress? 'kexists'? - continue + continue raise RuntimeError(f'Not found: {path}') -# TODO might be a good idea to merge across multiple taekouts... +# TODO might be a good idea to merge across multiple takeouts... # perhaps even a special takeout module that deals with all of this automatically? # e.g. accumulate, filter and maybe report useless takeouts?