-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
149 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env -S pip install -r | ||
|
||
polib | ||
tqdm |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
r"""This module can be called by | ||
`python -m <https://docs.python.org/3/library/__main__.html>`_. | ||
""" | ||
import os | ||
from argparse import ArgumentParser, RawDescriptionHelpFormatter | ||
from datetime import datetime | ||
|
||
import yaml | ||
|
||
from ... import __name__ as NAME | ||
from ... import __version__ | ||
|
||
try: | ||
import shtab | ||
except ImportError: | ||
from ...external import shtab | ||
|
||
NAME = NAME.replace("_", "-") | ||
VERSION = rf"""{NAME} {__version__} | ||
Copyright (C) {datetime.now().year} | ||
Written by Wu Zhenyu | ||
""" | ||
EPILOG = """ | ||
Report bugs to <wuzhenyu@ustc.edu>. | ||
""" | ||
|
||
|
||
def get_parser() -> ArgumentParser: | ||
r"""Get a parser for unit test.""" | ||
action = yaml.safe_load( | ||
open(os.path.join(os.path.dirname(__file__), "action.yml")) | ||
) | ||
parser = ArgumentParser( | ||
description=action["description"], | ||
epilog=EPILOG, | ||
formatter_class=RawDescriptionHelpFormatter, | ||
) | ||
parser.add_argument("--version", version=VERSION, action="version") | ||
shtab.add_argument_to(parser) | ||
for input, info in action["inputs"].items(): | ||
parser.add_argument( | ||
"--" + input, | ||
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs | ||
default=os.getenv( | ||
"INPUT_" + input.upper().replace("-", "_"), info["default"] | ||
), | ||
help=info["description"] + ". default: %(default)s", | ||
) | ||
parser.add_argument( | ||
"workspace", | ||
default=os.getenv("GITHUB_WORKSPACE", "."), | ||
help="the directory which contains *.po. default: %(default)s", | ||
) | ||
return parser | ||
|
||
|
||
def main() -> None: | ||
r"""Parse arguments and provide shell completions.""" | ||
parser = get_parser() | ||
args = parser.parse_args() | ||
|
||
from .main import run | ||
|
||
run(args) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../action.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
r"""Refer ``action.yml``.""" | ||
import os | ||
from argparse import Namespace | ||
from difflib import Differ | ||
from glob import glob | ||
|
||
from polib import pofile | ||
from tqdm import tqdm | ||
|
||
from translate_shell.translate import translate | ||
|
||
|
||
def run(args: Namespace) -> None: | ||
"""Run. | ||
:param args: | ||
:type args: Namespace | ||
:rtype: None | ||
""" | ||
fnames = args.files | ||
default_target_lang = args.target_lang | ||
source_lang = args.source_lang | ||
translator = args.translator | ||
wrapwidth = int(args.wrapwidth) | ||
progress = args.progress.lower() == "true" | ||
verbose = args.verbose.lower() == "true" | ||
dry_run = args.dry_run.lower() == "true" | ||
force = args.force.lower() == "true" | ||
workspace = args.workspace | ||
|
||
differ = Differ() | ||
files = sum( | ||
[ | ||
glob(os.path.join(workspace, fname), recursive=True) | ||
for fname in fnames.splitlines() | ||
], | ||
[], | ||
) | ||
for file in files: | ||
po = pofile(file, wrapwidth=wrapwidth) | ||
target_lang = po.metadata.get("Language", default_target_lang) | ||
entries = po.untranslated_entries() | ||
if force: | ||
entries += po.translated_entries() | ||
print(f"{po.fpath}: {len(entries)}") | ||
if dry_run: | ||
continue | ||
if progress: | ||
entries = tqdm(entries) | ||
for entry in entries: | ||
old = str(entry).splitlines() | ||
try: | ||
entry.msgstr = translate( | ||
entry.msgid, target_lang, source_lang, [translator] | ||
).results[0]["paraphrase"] | ||
except Exception: # skipcq: PYL-W0703 | ||
po.save() | ||
continue | ||
entry.fuzzy = False # type: ignore | ||
if verbose: | ||
new = str(entry).splitlines() | ||
diff = differ.compare(old, new) | ||
print("\n".join(diff)) | ||
if len(entries): | ||
po.save() |