From efe7c9ad7baf01a68a84bef7cec5b5b855cbb417 Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne - seluj78)" Date: Tue, 15 Sep 2020 13:49:52 +0200 Subject: [PATCH] WIP on cache --- .gitignore | 3 ++- potodo/_cache.py | 22 ++++++++++++++++++++++ potodo/potodo.py | 31 +++++++++++++++++++++++++++---- tests/test_potodo.py | 1 + 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 potodo/_cache.py diff --git a/.gitignore b/.gitignore index 6f1ca70..2338d48 100644 --- a/.gitignore +++ b/.gitignore @@ -104,4 +104,5 @@ venv.bak/ .mypy_cache/ venv -.idea \ No newline at end of file +.idea +.potodo \ No newline at end of file diff --git a/potodo/_cache.py b/potodo/_cache.py new file mode 100644 index 0000000..7cc711f --- /dev/null +++ b/potodo/_cache.py @@ -0,0 +1,22 @@ +import pickle +import json +from typing import Optional +from datetime import datetime +import os + + +def _get_cache_file_content(path: str = ".potodo/cache.json") -> Optional[str]: + try: + with open(path, "rb") as handle: + data = pickle.load(handle) + except FileNotFoundError: + return None + else: + # data["dt"] = datetime.strptime(data["dt"], "%Y-%m-%d %H:%M:%S.%f") + return data + + +def _set_cache_content(obj, path: str = ".potodo/cache.json"): + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "wb") as handle: + pickle.dump(obj, handle) diff --git a/potodo/potodo.py b/potodo/potodo.py index 0cb566a..9559bc7 100644 --- a/potodo/potodo.py +++ b/potodo/potodo.py @@ -15,7 +15,9 @@ from potodo import __version__ from potodo._github import get_reservation_list from potodo._po_file import get_po_stats_from_repo from potodo._po_file import PoFileStats - +from potodo._po_file import PoFileStats, get_po_stats_from_repo +from potodo._cache import _get_cache_file_content, _set_cache_content +from datetime import datetime, timedelta # TODO: Sort the functions (maybe in different files ? @@ -104,6 +106,7 @@ def exec_potodo( exclude_reserved: bool, only_reserved: bool, show_reservation_dates: bool, + no_cache: bool, ) -> None: """ Will run everything based on the given parameters @@ -121,14 +124,27 @@ def exec_potodo( :param exclude_reserved: Will print out only files that aren't reserved :param only_reserved: Will print only reserved fils :param show_reservation_dates: Will show the reservation dates + :param no_cache: Disables cache """ # Initialize the arguments issue_reservations = get_issue_reservations(offline, hide_reserved, path) - # Dict whose keys are directory names and values are - # stats for each file within the directory. - po_files_and_dirs = get_po_stats_from_repo(path, exclude) + if not no_cache: + content = _get_cache_file_content() + if content: + if False: + pass + po_files_and_dirs = get_po_stats_from_repo(path, exclude) + else: + po_files_and_dirs = content + # load and print cache + else: + # Dict whose keys are directory names and values are + # stats for each file within the directory. + po_files_and_dirs = get_po_stats_from_repo(path, exclude) + + _set_cache_content(po_files_and_dirs) dir_stats: List[Any] = [] for directory_name, po_files in sorted(po_files_and_dirs.items()): @@ -376,6 +392,13 @@ def main() -> None: help="show issue creation dates", ) + parser.add_argument( + "--no-cache", + action="store_true", + dest="no_cache", + help="Disables cache", + ) + parser.add_argument( "--version", action="version", version="%(prog)s " + __version__ ) diff --git a/tests/test_potodo.py b/tests/test_potodo.py index 0e6e9a8..4286aed 100644 --- a/tests/test_potodo.py +++ b/tests/test_potodo.py @@ -18,6 +18,7 @@ config = { "exclude_fuzzy": False, "only_reserved": False, "show_reservation_dates": False, + "no_cache": True, }