scripts/set-alias-page.py: add language option, fix script (#12745)

* scripts/set-alias-page.py: add language option, fix script

---------

Signed-off-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
Co-authored-by: Vítor Henrique <87824454+vitorhcl@users.noreply.github.com>
Co-authored-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
pull/28/head
Sebastiaan Speck 2024-05-09 13:01:01 +02:00 committed by GitHub
parent 05b8dbd0e3
commit 336507ac32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 13 deletions

View File

@ -6,17 +6,19 @@ A Python script to generate or update alias pages.
Disclaimer: This script generates a lot of false positives so it
isn't suggested to use the sync option. If used, only stage changes
and commit verified changes for your language.
and commit verified changes for your language by using -l LANGUAGE.
Note: If there is a symlink error when using the stage flag remove the `pages.en`
directory temporarily and try executing it again.
Usage:
python3 scripts/set-alias-page.py [-p PAGE] [-s] [-S] [-n] [COMMAND]
python3 scripts/set-alias-page.py [-p PAGE] [-l LANGUAGE] [-s] [-S] [-n] [COMMAND]
Options:
-p, --page PAGE
Specify the alias page in the format "platform/alias_command.md".
-l, --language LANGUAGE
Specify the language, a POSIX Locale Name in the form of "ll" or "ll_CC" (e.g. "fr" or "pt_BR").
-s, --stage
Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository).
-S, --sync
@ -35,6 +37,10 @@ Examples:
3. Read English alias pages and show what changes would be made:
python3 scripts/set-alias-page.py -Sn
python3 scripts/set-alias-page.py --sync --dry-run
4. Read English alias pages and synchronize them for Brazilian Portuguese pages only:
python3 scripts/set-alias-page.py -S -l pt_BR
python3 scripts/set-alias-page.py --sync --language pt_BR
"""
import argparse
@ -78,7 +84,7 @@ def get_templates(root):
template_file = os.path.join(
root, "contributing-guides/translation-templates/alias-pages.md"
)
with open(template_file) as f:
with open(template_file, encoding="utf-8") as f:
lines = f.readlines()
# Parse alias-pages.md
@ -123,21 +129,22 @@ def get_alias_page(file):
if not os.path.isfile(file):
return ""
with open(file) as f:
with open(file, "r", encoding="utf-8") as f:
for line in f:
if match := re.search(r"^`tldr (.+)`", line):
return match[1]
return ""
def set_alias_page(file, command, dry_run=False):
def set_alias_page(file, command, dry_run=False, language_to_update=""):
"""
Write an alias page to disk.
Parameters:
file (string): Path to an alias page
command (string): The command that the alias stands for.
dry_run (bool): Whether to perform a dry-run.
dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
language_to_update (string): Optionally, the language of the translation to be updated.
Returns:
str: Execution status
@ -152,7 +159,9 @@ def set_alias_page(file, command, dry_run=False):
_, locale = pages_dir.split(".")
else:
locale = "en"
if locale not in templates:
if locale not in templates or (
language_to_update != "" and locale != language_to_update
):
return ""
# Test if the alias page already exists
@ -182,13 +191,15 @@ def set_alias_page(file, command, dry_run=False):
.replace("example", command)
)
os.makedirs(os.path.dirname(file), exist_ok=True)
with open(file, "w") as f:
with open(file, "w", encoding="utf-8") as f:
f.write(text)
return status
def sync(root, pages_dirs, alias_name, orig_command, dry_run=False):
def sync(
root, pages_dirs, alias_name, orig_command, dry_run=False, language_to_update=""
):
"""
Synchronize an alias page into all translations.
@ -197,7 +208,8 @@ def sync(root, pages_dirs, alias_name, orig_command, dry_run=False):
pages_dirs (list of str): Strings of page entry and platform, e.g. "page.fr/common".
alias_name (str): An alias command with .md extension like "vi.md".
orig_command (string): An Original command like "vim".
dry_run (bool): Whether to perform a dry-run.
dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
language_to_update (string): Optionally, the language of the translation to be updated.
Returns:
list: A list of paths to be staged into git, using by --stage option.
@ -205,7 +217,7 @@ def sync(root, pages_dirs, alias_name, orig_command, dry_run=False):
rel_paths = []
for page_dir in pages_dirs:
path = os.path.join(root, page_dir, alias_name)
status = set_alias_page(path, orig_command, dry_run)
status = set_alias_page(path, orig_command, dry_run, language_to_update)
if status != "":
rel_path = path.replace(f"{root}/", "")
rel_paths.append(rel_path)
@ -225,6 +237,14 @@ def main():
default="",
help='page name in the format "platform/alias_command.md"',
)
parser.add_argument(
"-l",
"--language",
type=str,
required=False,
default="",
help='language in the format "ll" or "ll_CC" (e.g. "fr" or "pt_BR")',
)
parser.add_argument(
"-s",
"--stage",
@ -268,7 +288,7 @@ def main():
for path in target_paths:
rel_path = path.replace(f"{root}/", "")
rel_paths.append(rel_path)
status = set_alias_page(path, args.command)
status = set_alias_page(path, args.command, args.language)
if status != "":
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
@ -288,7 +308,12 @@ def main():
orig_command = get_alias_page(os.path.join(root, "pages", command))
if orig_command != "":
rel_paths += sync(
root, pages_dirs, command, orig_command, args.dry_run
root,
pages_dirs,
command,
orig_command,
args.dry_run,
args.language,
)
# Use '--stage' option