padpo/padpo/padpo.py

141 lines
3.9 KiB
Python
Raw Normal View History

2019-11-22 12:05:54 +00:00
"""Entry point of padpo."""
2019-11-05 15:45:45 +00:00
import argparse
2019-12-11 15:19:48 +00:00
import pkg_resources
2019-11-05 15:45:45 +00:00
import sys
from pathlib import Path
import simplelogging
2019-11-22 11:50:24 +00:00
from padpo.pofile import PoFile
2019-11-22 11:52:42 +00:00
from padpo.checkers import checkers
2019-11-22 12:01:26 +00:00
from padpo.github import pull_request_files
2019-11-18 16:35:37 +00:00
2019-11-22 11:52:42 +00:00
log = None
2019-11-05 15:45:45 +00:00
def check_file(path, pull_request_info=None):
2019-11-22 12:05:54 +00:00
"""Check a `*.po` file."""
pofile = PoFile(path)
2019-11-05 15:45:45 +00:00
for checker in checkers:
2019-11-22 12:05:54 +00:00
checker.check_file(pofile)
2019-11-05 15:45:45 +00:00
2019-11-22 12:05:54 +00:00
return pofile.display_warnings(pull_request_info)
2019-11-05 15:45:45 +00:00
def check_directory(path, pull_request_info=None):
2019-11-22 12:05:54 +00:00
"""Check a directory containing `*.po` files."""
2019-11-05 15:45:45 +00:00
path = Path(path)
2019-12-02 17:57:27 +00:00
result_errors = []
result_warnings = []
2019-11-22 12:05:54 +00:00
for filepath in path.rglob("*.po"):
2019-12-02 17:57:27 +00:00
errors, warnings = check_file(filepath, pull_request_info)
result_errors.extend(errors)
result_warnings.extend(warnings)
return result_errors, result_warnings
2019-11-05 15:45:45 +00:00
def check_path(path, pull_request_info=None):
2019-11-22 12:05:54 +00:00
"""Check a path (`*.po` file or directory)."""
2019-11-05 15:45:45 +00:00
path = Path(path)
if path.is_dir():
return check_directory(path, pull_request_info)
2019-11-05 15:45:45 +00:00
else:
return check_file(path, pull_request_info)
2019-12-11 14:51:03 +00:00
def check_paths(paths, pull_request_info=None):
"""Check a list of paths (`*.po` file or directory)."""
result_errors = []
result_warnings = []
for path in paths:
errors, warnings = check_path(path, pull_request_info)
result_errors.extend(errors)
result_warnings.extend(warnings)
return result_errors, result_warnings
2019-11-18 13:59:05 +00:00
def main():
2019-11-22 12:05:54 +00:00
"""Entry point."""
2019-11-18 17:16:51 +00:00
global log
2019-11-05 15:45:45 +00:00
parser = argparse.ArgumentParser(description="Linter for *.po files.")
parser.add_argument("-v", "--verbose", action="count", default=0)
2019-11-19 09:37:08 +00:00
files = parser.add_mutually_exclusive_group(required=True)
2019-11-16 10:25:22 +00:00
files.add_argument(
"-i",
"--input-path",
2019-11-05 15:45:45 +00:00
metavar="PATH",
type=str,
help="path of the file or directory to check",
2019-12-11 14:51:03 +00:00
default=[],
# allow the user to provide no path at all,
# this helps writing scripts
nargs="*",
2019-11-16 10:25:22 +00:00
)
files.add_argument(
"-g",
"--github",
metavar="python/python-docs-fr/pull/978",
type=str,
help="path of pull request in GitHub to check",
default="",
)
files.add_argument(
"-p",
"--python-docs-fr",
metavar="978",
type=int,
help="ID of pull request in python-docs-fr repository",
default=0,
2019-11-05 15:45:45 +00:00
)
2019-12-11 15:19:48 +00:00
files.add_argument("--version", action="store_true", help="Return version")
2020-08-25 14:58:30 +00:00
parser.add_argument("-c", "--color", action="store_true", help="color output")
for checker in checkers:
checker.add_arguments(parser)
2019-11-05 15:45:45 +00:00
args = parser.parse_args()
2019-12-11 15:19:48 +00:00
if args.version:
print(pkg_resources.get_distribution("padpo").version)
2019-12-11 15:19:48 +00:00
sys.exit(0)
if args.color:
2020-08-25 14:58:30 +00:00
console_format = (
"%(log_color)s[%(levelname)-8s]%(reset)s "
"%(green)s%(pofile)s:%(poline)s: "
2020-08-25 15:05:23 +00:00
"%(cyan)s[%(checker)s] %(message)s%(reset)s"
2020-08-25 14:58:30 +00:00
)
else:
2020-08-25 15:05:23 +00:00
console_format = "%(pofile)s:%(poline)s: %(leveldesc)s: %(message)s"
log = simplelogging.get_logger("__main__", console_format=console_format)
2019-11-05 15:45:45 +00:00
if args.verbose < 1:
log.reduced_logging()
elif args.verbose < 2:
log.normal_logging()
else:
log.full_logging()
if args.github or args.python_docs_fr:
2019-11-16 10:25:22 +00:00
pull_request = ""
if args.github:
pull_request = args.github
if args.python_docs_fr:
pull_request = f"python/python-docs-fr/pull/{args.python_docs_fr}"
2019-11-22 12:01:26 +00:00
pull_request_info = pull_request_files(pull_request)
2019-12-11 14:51:03 +00:00
path = [pull_request_info.download_directory]
else:
path = args.input_path
pull_request_info = None
2019-11-16 10:25:22 +00:00
for checker in checkers:
checker.configure(args)
2019-12-11 14:51:03 +00:00
errors, warnings = check_paths(path, pull_request_info=pull_request_info)
2019-12-02 17:57:27 +00:00
if errors:
2019-11-05 15:45:45 +00:00
sys.exit(1)