WIP on cache

This commit is contained in:
Jules Lasne (jlasne - seluj78) 2020-09-15 13:49:52 +02:00
parent dbdececbbf
commit efe7c9ad7b
4 changed files with 52 additions and 5 deletions

3
.gitignore vendored
View File

@ -104,4 +104,5 @@ venv.bak/
.mypy_cache/
venv
.idea
.idea
.potodo

22
potodo/_cache.py Normal file
View File

@ -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)

View File

@ -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__
)

View File

@ -18,6 +18,7 @@ config = {
"exclude_fuzzy": False,
"only_reserved": False,
"show_reservation_dates": False,
"no_cache": True,
}