potodo/potodo/po_file.py

125 lines
4.6 KiB
Python
Raw Permalink Normal View History

2020-06-03 20:18:36 +00:00
import itertools
2020-10-12 13:29:56 +00:00
import logging
2020-10-12 11:04:08 +00:00
import os
2019-12-10 14:38:53 +00:00
from pathlib import Path
2020-11-26 14:05:45 +00:00
from typing import Callable
2020-09-15 08:46:06 +00:00
from typing import Dict
from typing import List
from typing import Mapping
from typing import Sequence
from typing import Set
2019-12-10 14:38:53 +00:00
2019-12-11 23:36:35 +00:00
import polib
2019-12-10 14:38:53 +00:00
2019-12-11 18:57:55 +00:00
class PoFileStats:
"""Class for each `.po` file containing all the necessary information about its progress""" # noqa
2019-12-10 22:22:30 +00:00
2019-12-11 16:36:35 +00:00
def __init__(self, path: Path):
"""Initializes the class with all the correct information"""
2019-12-10 14:38:53 +00:00
self.path: Path = path
self.filename: str = path.name
2020-10-12 11:37:56 +00:00
self.mtime = os.path.getmtime(path)
self.pofile: polib.POFile = polib.pofile(str(self.path))
2019-12-11 16:36:35 +00:00
self.directory: str = self.path.parent.name
2019-12-10 14:38:53 +00:00
self.obsolete_entries: Sequence[polib.POEntry] = self.pofile.obsolete_entries()
self.obsolete_nb: int = len(self.pofile.obsolete_entries())
2019-12-13 15:54:54 +00:00
self.fuzzy_entries: List[polib.POEntry] = [
entry for entry in self.pofile if entry.fuzzy and not entry.obsolete
]
2019-12-10 14:38:53 +00:00
self.fuzzy_nb: int = len(self.fuzzy_entries)
2019-12-13 14:12:35 +00:00
self.translated_entries: Sequence[
polib.POEntry
] = self.pofile.translated_entries()
2019-12-10 14:38:53 +00:00
self.translated_nb: int = len(self.translated_entries)
2019-12-13 14:12:35 +00:00
self.untranslated_entries: Sequence[
polib.POEntry
] = self.pofile.untranslated_entries()
2019-12-10 14:38:53 +00:00
self.untranslated_nb: int = len(self.untranslated_entries)
2019-12-10 20:04:18 +00:00
self.entries_count: int = len([e for e in self.pofile if not e.obsolete])
2019-12-13 15:54:54 +00:00
self.percent_translated: int = self.pofile.percent_translated()
self.po_file_size = len(self.pofile) - self.obsolete_nb
self.filename_dir: str = self.directory + "/" + self.filename
2019-12-10 20:04:18 +00:00
def __str__(self) -> str:
2019-12-10 22:22:30 +00:00
return (
f"Filename: {self.filename}\n"
f"Fuzzy Entries: {self.fuzzy_entries}\n"
f"Percent Translated: {self.percent_translated}\n"
f"Translated Entries: {self.translated_entries}\n"
f"Untranslated Entries: {self.untranslated_entries}"
)
2019-12-10 14:38:53 +00:00
2019-12-11 18:57:55 +00:00
def __lt__(self, other: "PoFileStats") -> bool:
"""When two PoFiles are compared, their filenames are compared."""
2019-12-10 20:04:18 +00:00
return self.filename < other.filename
2019-12-10 14:38:53 +00:00
2019-12-10 22:21:44 +00:00
2020-10-13 15:08:35 +00:00
from potodo.cache import get_cache_file_content # noqa
from potodo.cache import set_cache_content # noqa
2020-10-12 11:04:08 +00:00
def get_po_stats_from_repo_or_cache(
repo_path: Path,
ignore_matches: Callable[[str], bool],
no_cache: bool = False,
2020-10-12 11:21:05 +00:00
) -> Mapping[str, List[PoFileStats]]:
2020-10-12 11:04:08 +00:00
"""Gets all the po files recursively from 'repo_path'
and cache if no_cache is set to False, excluding those if ignore_matches match them.
Return a dict with all directories and PoFile instances of
2020-06-03 07:57:09 +00:00
`.po` files in those directories.
"""
# Get all the files matching `**/*.po`
2020-06-03 12:02:03 +00:00
# not being in the exclusion list or in
# any (sub)folder from the exclusion list
2020-10-12 13:29:20 +00:00
logging.debug("Finding all files matching **/*.po in %s", repo_path)
2020-10-12 11:21:05 +00:00
all_po_files: List[Path] = [
file for file in repo_path.rglob("*.po") if not ignore_matches(str(file))
2020-06-04 07:44:15 +00:00
]
2019-12-10 22:21:44 +00:00
2020-06-04 07:37:15 +00:00
# Group files by directory
2020-10-12 13:29:20 +00:00
logging.debug("Grouping files per directory")
2019-12-13 14:12:35 +00:00
po_files_per_directory: Mapping[str, Set[Path]] = {
2020-06-04 07:37:15 +00:00
name: set(files)
2020-06-04 07:55:42 +00:00
# We assume the output of rglob to be sorted,
2020-06-04 07:37:15 +00:00
# so each 'name' is unique within groupby
for name, files in itertools.groupby(
all_po_files, key=lambda path: path.parent.name
)
}
2020-06-03 20:18:36 +00:00
2020-10-12 11:04:08 +00:00
if no_cache:
# Turn paths into stat objects
2020-10-12 13:29:20 +00:00
logging.debug("Creating PoFileStats objects for each file without cache")
2020-10-12 11:21:05 +00:00
po_stats_per_directory: Dict[str, List[PoFileStats]] = {
2020-10-12 11:04:08 +00:00
directory: [PoFileStats(po_file) for po_file in po_files]
for directory, po_files in po_files_per_directory.items()
}
else:
2020-10-13 15:08:35 +00:00
cached_files = get_cache_file_content(
path=str(repo_path.resolve()) + "/.potodo/cache.pickle",
2020-10-12 11:04:08 +00:00
)
po_stats_per_directory = dict()
for directory, po_files in po_files_per_directory.items():
po_stats_per_directory[directory] = []
for po_file in po_files:
2020-10-12 11:42:02 +00:00
cached_file = cached_files.get(po_file.resolve())
2020-10-12 11:37:56 +00:00
if not (
2020-10-12 11:42:02 +00:00
cached_file
and os.path.getmtime(po_file.resolve()) == cached_file.mtime
2020-10-12 11:04:08 +00:00
):
2020-10-12 11:37:56 +00:00
cached_files[po_file.resolve()] = cached_file = PoFileStats(po_file)
po_stats_per_directory[directory].append(cached_file)
2020-10-13 15:08:35 +00:00
set_cache_content(
cached_files,
path=str(repo_path.resolve()) + "/.potodo/cache.pickle",
2020-10-12 11:04:08 +00:00
)
return po_stats_per_directory