-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scripts/_common.py
: add a shared Python file to move duplicated code
- Loading branch information
1 parent
b6ac034
commit 25f7717
Showing
4 changed files
with
107 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env python3 | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
A Python file that makes some commonly used functions available for other scripts to use. | ||
""" | ||
|
||
from pathlib import Path | ||
import os | ||
import argparse | ||
|
||
|
||
def get_tldr_root(): | ||
""" | ||
Get the path of local tldr repository for environment variable TLDR_ROOT. | ||
""" | ||
|
||
# If this script is running from tldr/scripts, the parent's parent is the root | ||
f = Path(__file__).resolve() | ||
if ( | ||
tldr_root := next((path for path in f.parents if path.name == "tldr"), None) | ||
) is not None: | ||
return tldr_root | ||
elif "TLDR_ROOT" in os.environ: | ||
return Path(os.environ["TLDR_ROOT"]) | ||
raise SystemExit( | ||
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr." | ||
) | ||
|
||
|
||
def get_locale(path): | ||
""" | ||
Get the locale from the path. | ||
""" | ||
|
||
# compute locale | ||
pages_dirname = path.parents[1].name | ||
if "." in pages_dirname: | ||
_, locale = pages_dirname.split(".") | ||
else: | ||
locale = "en" | ||
|
||
return locale | ||
|
||
|
||
def create_argument_parser(description): | ||
""" | ||
Create an argument parser that can be extended. | ||
""" | ||
|
||
parser = argparse.ArgumentParser(description=description) | ||
parser.add_argument( | ||
"-p", | ||
"--page", | ||
type=str, | ||
required=False, | ||
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", | ||
action="store_true", | ||
default=False, | ||
help="stage modified pages (requires `git` to be on $PATH and TLDR_ROOT to be a Git repository)", | ||
) | ||
parser.add_argument( | ||
"-S", | ||
"--sync", | ||
action="store_true", | ||
default=False, | ||
help="synchronize each translation's alias page (if exists) with that of English page", | ||
) | ||
parser.add_argument( | ||
"-n", | ||
"--dry-run", | ||
action="store_true", | ||
default=False, | ||
help="show what changes would be made without actually modifying the pages", | ||
) | ||
|
||
return parser |
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