Skip to content
This repository has been archived by the owner on Mar 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #84 from Seluj78/feature/cached_args
Browse files Browse the repository at this point in the history
  • Loading branch information
Seluj78 authored Oct 14, 2020
2 parents 818b0a3 + d83883c commit 4fafbc7
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .potodoignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
venv/*
venv/
2 changes: 1 addition & 1 deletion potodo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__author__ = """Jules Lasne"""
__email__ = "[email protected]"
__version__ = "0.15.0"
__version__ = "0.16.0"
8 changes: 5 additions & 3 deletions potodo/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from pathlib import Path
from typing import cast
from typing import Dict
from typing import Any

from potodo import __version__ as VERSION
from potodo.po_file import PoFileStats


def get_cache_file_content(
cache_args: Any,
path: str = ".potodo/cache.pickle",
) -> Dict[Path, PoFileStats]:
logging.debug("Trying to load cache from %s", path)
Expand All @@ -21,18 +23,18 @@ def get_cache_file_content(
return {}
else:
logging.debug("Found cache")
if data.get("version") != VERSION:
if data.get("version") != VERSION or cache_args != data.get("args"):
logging.info("Found old cache, ignored it.")
return {}
else:
return cast(Dict[Path, PoFileStats], data["data"])


def set_cache_content(
obj: Dict[Path, PoFileStats], path: str = ".potodo/cache.pickle"
obj: Dict[Path, PoFileStats], cache_args: Any, path: str = ".potodo/cache.pickle"
) -> None:
os.makedirs(os.path.dirname(path), exist_ok=True)
data = {"version": VERSION, "data": obj}
data = {"version": VERSION, "args": cache_args, "data": obj}
with open(path, "wb") as handle:
pickle.dump(data, handle)
logging.debug("Set cache to %s", path)
20 changes: 0 additions & 20 deletions potodo/ignore.py

This file was deleted.

6 changes: 5 additions & 1 deletion potodo/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import cast
from typing import Iterable
from typing import List
from typing import Callable

from simple_term_menu import TerminalMenu

Expand Down Expand Up @@ -61,13 +62,16 @@ def _confirmation_menu(choosen_file: str, directory: str) -> int:
return cast(int, choice)


def get_dir_list(repo_path: Path, exclude: Iterable[Path]) -> List[str]:
def get_dir_list(
repo_path: Path, exclude: Iterable[Path], ignore_matches: Callable[[str], bool]
) -> List[str]:
return list(
set(
[
file.parent.name
for file in repo_path.rglob("*.po")
if not any(is_within(file, excluded) for excluded in exclude)
and not ignore_matches(str(file))
]
)
)
Expand Down
16 changes: 13 additions & 3 deletions potodo/po_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from typing import Mapping
from typing import Sequence
from typing import Set
from typing import Callable
from typing import Any

import polib

Expand Down Expand Up @@ -69,7 +71,11 @@ def is_within(file: Path, excluded: Path) -> bool:


def get_po_stats_from_repo_or_cache(
repo_path: Path, exclude: Iterable[Path], no_cache: bool = False
repo_path: Path,
exclude: Iterable[Path],
cache_args: Any,
ignore_matches: Callable[[str], bool],
no_cache: bool = False,
) -> Mapping[str, List[PoFileStats]]:
"""Gets all the po files recursively from 'repo_path'
and cache if no_cache is set to False, excluding those in
Expand All @@ -85,6 +91,7 @@ def get_po_stats_from_repo_or_cache(
file
for file in repo_path.rglob("*.po")
if not any(is_within(file, excluded) for excluded in exclude)
and not ignore_matches(str(file))
]

# Group files by directory
Expand All @@ -107,7 +114,8 @@ def get_po_stats_from_repo_or_cache(
}
else:
cached_files = get_cache_file_content(
str(repo_path.resolve()) + "/.potodo/cache.pickle"
cache_args=cache_args,
path=str(repo_path.resolve()) + "/.potodo/cache.pickle",
)
po_stats_per_directory = dict()
for directory, po_files in po_files_per_directory.items():
Expand All @@ -121,7 +129,9 @@ def get_po_stats_from_repo_or_cache(
cached_files[po_file.resolve()] = cached_file = PoFileStats(po_file)
po_stats_per_directory[directory].append(cached_file)
set_cache_content(
cached_files, path=str(repo_path.resolve()) + "/.potodo/cache.pickle"
cached_files,
cache_args,
path=str(repo_path.resolve()) + "/.potodo/cache.pickle",
)

return po_stats_per_directory
34 changes: 29 additions & 5 deletions potodo/potodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from typing import List
from typing import Sequence
from typing import Tuple
from gitignore_parser import parse_gitignore

from potodo import __version__
from potodo.arguments_handling import check_args
from potodo.github import get_issue_reservations
from potodo.ignore import get_ignore_content
from potodo.interactive import _confirmation_menu
from potodo.interactive import _directory_list_menu
from potodo.interactive import _file_list_menu
Expand Down Expand Up @@ -99,15 +99,37 @@ def exec_potodo(
:param is_interactive: Switches output to an interactive CLI menu
"""

ignore_file_content = get_ignore_content(repo_path=path)
exclude.extend(ignore_file_content)
cache_args = {
"path": path,
"exclude": exclude,
"above": above,
"below": below,
"only_fuzzy": only_fuzzy,
"offline": offline,
"hide_reserved": hide_reserved,
"counts": counts,
"json_format": json_format,
"exclude_fuzzy": exclude_fuzzy,
"exclude_reserved": exclude_reserved,
"only_reserved": only_reserved,
"show_reservation_dates": show_reservation_dates,
"no_cache": no_cache,
"is_interactive": is_interactive,
}

try:
ignore_matches = parse_gitignore(path / ".potodoignore")
except FileNotFoundError:
ignore_matches = parse_gitignore("/dev/null")

# Initialize the arguments
issue_reservations = get_issue_reservations(offline, hide_reserved, path)

dir_stats: List[Any] = []
if is_interactive:
directory_options = get_dir_list(repo_path=path, exclude=exclude)
directory_options = get_dir_list(
repo_path=path, exclude=exclude, ignore_matches=ignore_matches
)
while True:
selected_dir = _directory_list_menu(directory_options)
if selected_dir == (len(directory_options) - 1):
Expand Down Expand Up @@ -139,7 +161,9 @@ def exec_potodo(
else:
exit()
else:
po_files_and_dirs = get_po_stats_from_repo_or_cache(path, exclude, no_cache)
po_files_and_dirs = get_po_stats_from_repo_or_cache(
path, exclude, cache_args, ignore_matches, no_cache
)
for directory_name, po_files in sorted(po_files_and_dirs.items()):
# For each directory and files in this directory
buffer: List[Any] = []
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
polib==1.1.0
requests==2.24.0
simple-term-menu==0.10.2
gitignore_parser==0.0.8
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package_dir={"potodo": "potodo"},
entry_points={"console_scripts": ["potodo=potodo.potodo:main"]},
include_package_data=True,
install_requires=["polib", "requests", "simple-term-menu"],
install_requires=["polib", "requests", "simple-term-menu", "gitignore_parser"],
license="MIT license",
zip_safe=False,
keywords="potodo",
Expand Down

0 comments on commit 4fafbc7

Please sign in to comment.