Use a selector function instead of 6 arguments.
This commit is contained in:
parent
ede1868dbb
commit
7aa68d9db3
|
@ -1,39 +1,22 @@
|
|||
import logging
|
||||
import os
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
from typing import Any, List, Mapping
|
||||
|
||||
|
||||
def check_args(
|
||||
path: str,
|
||||
exclude: List[str],
|
||||
below: int,
|
||||
above: int,
|
||||
verbose: int,
|
||||
only_fuzzy: bool,
|
||||
hide_reserved: bool,
|
||||
counts: bool,
|
||||
json_format: bool,
|
||||
exclude_fuzzy: bool,
|
||||
exclude_reserved: bool,
|
||||
only_reserved: bool,
|
||||
show_reservation_dates: bool,
|
||||
no_cache: bool,
|
||||
is_interactive: bool,
|
||||
**kwargs: Any,
|
||||
) -> Mapping[str, Any]:
|
||||
def check_args(args: Namespace) -> None:
|
||||
# If below is lower than above, raise an error
|
||||
if below < above:
|
||||
if args.below < args.above:
|
||||
print("Potodo: 'below' value must be greater than 'above' value.")
|
||||
exit(1)
|
||||
|
||||
if json_format and is_interactive:
|
||||
if args.json_format and args.is_interactive:
|
||||
print(
|
||||
"Potodo: Json format and interactive modes cannot be activated at the same time."
|
||||
)
|
||||
exit(1)
|
||||
|
||||
if is_interactive:
|
||||
if args.is_interactive:
|
||||
try:
|
||||
import termios # noqa
|
||||
except ImportError:
|
||||
|
@ -45,38 +28,33 @@ def check_args(
|
|||
)
|
||||
)
|
||||
|
||||
if exclude_fuzzy and only_fuzzy:
|
||||
if args.exclude_fuzzy and args.only_fuzzy:
|
||||
print("Potodo: Cannot pass --exclude-fuzzy and --only-fuzzy at the same time.")
|
||||
exit(1)
|
||||
|
||||
if exclude_reserved and only_reserved:
|
||||
if args.exclude_reserved and args.only_reserved:
|
||||
print(
|
||||
"Potodo: Cannot pass --exclude-reserved and --only-reserved at the same time."
|
||||
)
|
||||
exit(1)
|
||||
|
||||
# If no path is specified, use current directory
|
||||
if not path:
|
||||
path = os.getcwd()
|
||||
if not args.path:
|
||||
args.path = os.getcwd()
|
||||
|
||||
logging_level = None
|
||||
if verbose:
|
||||
if verbose == 1:
|
||||
args.path = Path(args.path).resolve()
|
||||
|
||||
args.logging_level = None
|
||||
if args.verbose:
|
||||
if args.verbose == 1:
|
||||
# Will only show ERROR and CRITICAL
|
||||
logging_level = logging.WARNING
|
||||
if verbose == 2:
|
||||
args.logging_level = logging.WARNING
|
||||
if args.verbose == 2:
|
||||
# Will only show ERROR, CRITICAL and WARNING
|
||||
logging_level = logging.INFO
|
||||
if verbose >= 3:
|
||||
args.logging_level = logging.INFO
|
||||
if args.verbose >= 3:
|
||||
# Will show INFO WARNING ERROR DEBUG CRITICAL
|
||||
logging_level = logging.DEBUG
|
||||
args.logging_level = logging.DEBUG
|
||||
else:
|
||||
# Disable all logging
|
||||
logging.disable(logging.CRITICAL)
|
||||
|
||||
# Convert strings to `Path` objects and make them absolute
|
||||
return {
|
||||
"path": Path(path).resolve(),
|
||||
"exclude": exclude,
|
||||
"logging_level": logging_level,
|
||||
}
|
||||
|
|
138
potodo/potodo.py
138
potodo/potodo.py
|
@ -11,7 +11,7 @@ from potodo.arguments_handling import check_args
|
|||
from potodo.forge_api import get_issue_reservations
|
||||
from potodo.json import json_dateconv
|
||||
from potodo.logging import setup_logging
|
||||
from potodo.po_file import PoDirectoryStats, PoProjectStats
|
||||
from potodo.po_file import PoDirectoryStats, PoFileStats, PoProjectStats
|
||||
|
||||
|
||||
def print_dir_stats(
|
||||
|
@ -69,15 +69,10 @@ def scan_path(
|
|||
def non_interactive_output(
|
||||
path: Path,
|
||||
exclude: List[str],
|
||||
above: int,
|
||||
below: int,
|
||||
only_fuzzy: bool,
|
||||
hide_reserved: bool,
|
||||
counts: bool,
|
||||
json_format: bool,
|
||||
exclude_fuzzy: bool,
|
||||
exclude_reserved: bool,
|
||||
only_reserved: bool,
|
||||
select: Callable[[PoFileStats], bool],
|
||||
show_reservation_dates: bool,
|
||||
no_cache: bool,
|
||||
is_interactive: bool,
|
||||
|
@ -89,41 +84,23 @@ def non_interactive_output(
|
|||
if json_format:
|
||||
print_po_project_as_json(
|
||||
po_project,
|
||||
above,
|
||||
below,
|
||||
only_fuzzy,
|
||||
hide_reserved,
|
||||
counts,
|
||||
exclude_fuzzy,
|
||||
exclude_reserved,
|
||||
only_reserved,
|
||||
select,
|
||||
matching_files,
|
||||
)
|
||||
else:
|
||||
print_po_project(
|
||||
po_project,
|
||||
above,
|
||||
below,
|
||||
only_fuzzy,
|
||||
hide_reserved,
|
||||
counts,
|
||||
exclude_fuzzy,
|
||||
exclude_reserved,
|
||||
only_reserved,
|
||||
select,
|
||||
matching_files,
|
||||
)
|
||||
|
||||
|
||||
def print_po_project(
|
||||
po_project: PoProjectStats,
|
||||
above: int,
|
||||
below: int,
|
||||
only_fuzzy: bool,
|
||||
hide_reserved: bool,
|
||||
counts: bool,
|
||||
exclude_fuzzy: bool,
|
||||
exclude_reserved: bool,
|
||||
only_reserved: bool,
|
||||
select: Callable[[PoFileStats], bool],
|
||||
matching_files: bool,
|
||||
) -> None:
|
||||
for directory in sorted(po_project.stats_by_directory()):
|
||||
|
@ -132,22 +109,8 @@ def print_po_project(
|
|||
printed_list: List[bool] = []
|
||||
|
||||
for po_file in sorted(directory.files):
|
||||
# For each file in those files from that directory
|
||||
if only_fuzzy and not po_file.fuzzy_entries:
|
||||
continue
|
||||
if exclude_fuzzy and po_file.fuzzy_entries:
|
||||
continue
|
||||
if (
|
||||
po_file.percent_translated == 100
|
||||
or po_file.percent_translated < above
|
||||
or po_file.percent_translated > below
|
||||
):
|
||||
continue
|
||||
|
||||
# unless the offline/hide_reservation are enabled
|
||||
if exclude_reserved and po_file.reserved_by:
|
||||
continue
|
||||
if only_reserved and not po_file.reserved_by:
|
||||
if not select(po_file):
|
||||
continue
|
||||
|
||||
if matching_files:
|
||||
|
@ -174,33 +137,15 @@ def print_po_project(
|
|||
|
||||
def print_po_project_as_json(
|
||||
po_project: PoProjectStats,
|
||||
above: int,
|
||||
below: int,
|
||||
only_fuzzy: bool,
|
||||
hide_reserved: bool,
|
||||
counts: bool,
|
||||
exclude_fuzzy: bool,
|
||||
exclude_reserved: bool,
|
||||
only_reserved: bool,
|
||||
select: Callable[[PoFileStats], bool],
|
||||
matching_files: bool,
|
||||
) -> None:
|
||||
dir_stats: List[Any] = []
|
||||
for directory in sorted(po_project.stats_by_directory()):
|
||||
buffer: List[Any] = []
|
||||
for po_file in sorted(directory.files):
|
||||
if only_fuzzy and not po_file.fuzzy_entries:
|
||||
continue
|
||||
if exclude_fuzzy and po_file.fuzzy_entries:
|
||||
continue
|
||||
if (
|
||||
po_file.percent_translated == 100
|
||||
or po_file.percent_translated < above
|
||||
or po_file.percent_translated > below
|
||||
):
|
||||
continue
|
||||
if exclude_reserved and po_file.reserved_by:
|
||||
continue
|
||||
if only_reserved and not po_file.reserved_by:
|
||||
if not select(po_file):
|
||||
continue
|
||||
buffer.append(po_file.as_dict())
|
||||
|
||||
|
@ -245,15 +190,10 @@ def build_ignore_matcher(path: Path, exclude: List[str]) -> Callable[[str], bool
|
|||
def exec_potodo(
|
||||
path: Path,
|
||||
exclude: List[str],
|
||||
above: int,
|
||||
below: int,
|
||||
only_fuzzy: bool,
|
||||
hide_reserved: bool,
|
||||
counts: bool,
|
||||
json_format: bool,
|
||||
exclude_fuzzy: bool,
|
||||
exclude_reserved: bool,
|
||||
only_reserved: bool,
|
||||
select: Callable[[PoFileStats], bool],
|
||||
show_reservation_dates: bool,
|
||||
no_cache: bool,
|
||||
is_interactive: bool,
|
||||
|
@ -265,15 +205,9 @@ def exec_potodo(
|
|||
|
||||
:param path: The path to search into
|
||||
:param exclude: folders or files to be ignored
|
||||
:param above: The above threshold
|
||||
:param below: The below threshold
|
||||
:param only_fuzzy: Should only fuzzies be printed
|
||||
:param hide_reserved: Will not show the reserved files
|
||||
:param counts: Render list with counts not percentage
|
||||
:param json_format: Format output as JSON.
|
||||
:param exclude_fuzzy: Will exclude files with fuzzies in output.
|
||||
:param exclude_reserved: Will print out only files that aren't reserved
|
||||
:param only_reserved: Will print only reserved files
|
||||
:param show_reservation_dates: Will show the reservation dates
|
||||
:param no_cache: Disables cache (Cache is disabled when files are modified)
|
||||
:param is_interactive: Switches output to an interactive CLI menu
|
||||
|
@ -290,15 +224,10 @@ def exec_potodo(
|
|||
non_interactive_output(
|
||||
path,
|
||||
exclude,
|
||||
above,
|
||||
below,
|
||||
only_fuzzy,
|
||||
hide_reserved,
|
||||
counts,
|
||||
json_format,
|
||||
exclude_fuzzy,
|
||||
exclude_reserved,
|
||||
only_reserved,
|
||||
select,
|
||||
show_reservation_dates,
|
||||
no_cache,
|
||||
is_interactive,
|
||||
|
@ -449,18 +378,47 @@ def main() -> None:
|
|||
)
|
||||
|
||||
# Initialize args and check consistency
|
||||
args = vars(parser.parse_args())
|
||||
args.update(check_args(**args))
|
||||
args = parser.parse_args()
|
||||
check_args(args)
|
||||
|
||||
if args["logging_level"]:
|
||||
setup_logging(args["logging_level"])
|
||||
def select(po_file: PoFileStats) -> bool:
|
||||
"""Return True if the po_file should be displayed, False otherwise."""
|
||||
if args.only_fuzzy and not po_file.fuzzy_entries:
|
||||
return False
|
||||
if args.exclude_fuzzy and po_file.fuzzy_entries:
|
||||
return False
|
||||
if (
|
||||
po_file.percent_translated == 100
|
||||
or po_file.percent_translated < args.above
|
||||
or po_file.percent_translated > args.below
|
||||
):
|
||||
return False
|
||||
|
||||
# unless the offline/hide_reservation are enabled
|
||||
if args.exclude_reserved and po_file.reserved_by:
|
||||
return False
|
||||
if args.only_reserved and not po_file.reserved_by:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
if args.logging_level:
|
||||
setup_logging(args.logging_level)
|
||||
|
||||
logging.info("Logging activated.")
|
||||
logging.debug("Executing potodo with args %s", args)
|
||||
|
||||
# Removing useless args before running the process
|
||||
del args["verbose"]
|
||||
del args["logging_level"]
|
||||
|
||||
# Launch the processing itself
|
||||
exec_potodo(**args)
|
||||
exec_potodo(
|
||||
args.path,
|
||||
args.exclude,
|
||||
args.hide_reserved,
|
||||
args.counts,
|
||||
args.json_format,
|
||||
select,
|
||||
args.show_reservation_dates,
|
||||
args.no_cache,
|
||||
args.is_interactive,
|
||||
args.matching_files,
|
||||
args.api_url,
|
||||
)
|
||||
|
|
|
@ -10,21 +10,24 @@ def repo_dir():
|
|||
|
||||
@pytest.fixture
|
||||
def base_config(repo_dir):
|
||||
def select(po_file) -> bool:
|
||||
"""Return True if the po_file should be displayed, False otherwise."""
|
||||
return not (
|
||||
po_file.percent_translated == 100
|
||||
or po_file.percent_translated < 0
|
||||
or po_file.percent_translated > 100
|
||||
)
|
||||
|
||||
return {
|
||||
"path": repo_dir,
|
||||
"exclude": ["excluded/", "excluded.po"],
|
||||
"above": 0,
|
||||
"below": 100,
|
||||
"only_fuzzy": False,
|
||||
"hide_reserved": False,
|
||||
"counts": False,
|
||||
"is_interactive": False,
|
||||
"exclude_fuzzy": False,
|
||||
"only_reserved": False,
|
||||
"exclude_reserved": False,
|
||||
"json_format": False,
|
||||
"select": select,
|
||||
"show_reservation_dates": False,
|
||||
"no_cache": True,
|
||||
"is_interactive": False,
|
||||
"matching_files": False,
|
||||
"json_format": False,
|
||||
"api_url": "",
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user