Use argparse instead of click

parent 9b8b46f5
...@@ -11,7 +11,7 @@ function export_pot_file # First parameter will be the path of the pot file we h ...@@ -11,7 +11,7 @@ function export_pot_file # First parameter will be the path of the pot file we h
function import_po_files # First parameter will be a path that will contain several .po files with the format LANG.po function import_po_files # First parameter will be a path that will contain several .po files with the format LANG.po
{ {
podir=$1 podir=$1
python3 translations.py import-po $podir python3 translations.py import $podir
python3 translations.py generate-translations python3 translations.py generate-translations
rm -rf locale rm -rf locale
} }
......
import click
import os import os
import subprocess import subprocess
import shutil import shutil
import argparse
@click.group()
@click.pass_context
def cli(ctx: dict):
ctx.ensure_object(dict)
def webpage_list(): def webpage_list():
""" """
...@@ -14,12 +9,11 @@ def webpage_list(): ...@@ -14,12 +9,11 @@ def webpage_list():
""" """
return ['content/about-kate.md'] return ['content/about-kate.md']
@cli.command() def extract(args):
@click.argument("pot")
def extract(pot):
""" """
First parameter will be the path of the pot file we have to create First parameter will be the path of the pot file we have to create
""" """
pot = args.pot
webpages = "" webpages = ""
for webpage in webpage_list(): for webpage in webpage_list():
webpages += " --master \"" + webpage + "\"" webpages += " --master \"" + webpage + "\""
...@@ -27,19 +21,17 @@ def extract(pot): ...@@ -27,19 +21,17 @@ def extract(pot):
subprocess.run(command, shell=True, check=True, text=True) subprocess.run(command, shell=True, check=True, text=True)
@cli.command() def import_po(args):
@click.argument("directory")
def import_po(directory):
""" """
First parameter will be a path that will contain several .po files with the format LANG.po First parameter will be a path that will contain several .po files with the format LANG.po
""" """
directory = args.directory
for translation in os.listdir(directory): for translation in os.listdir(directory):
lang = os.path.splitext(translation)[0] lang = os.path.splitext(translation)[0]
os.makedirs("locale/" + lang + "/LC_MESSAGES/") os.makedirs("locale/" + lang + "/LC_MESSAGES/")
shutil.copyfile(directory + "/" + translation, "locale/" + lang + "/LC_MESSAGES/kate-editor-org.po") shutil.copyfile(directory + "/" + translation, "locale/" + lang + "/LC_MESSAGES/kate-editor-org.po")
@cli.command() def generate_translations(args):
def generate_translations():
""" """
Assume translation located at `locale/$LANG/LC_MESSAGES/` Assume translation located at `locale/$LANG/LC_MESSAGES/`
""" """
...@@ -52,5 +44,19 @@ def generate_translations(): ...@@ -52,5 +44,19 @@ def generate_translations():
subprocess.run(command, shell=True, check=True, text=True) subprocess.run(command, shell=True, check=True, text=True)
if __name__ == "__main__": if __name__ == "__main__":
cli(obj={}) parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='sub-command help')
extract_cmd = subparsers.add_parser('extract', help='extract strings for translations')
extract_cmd.add_argument('pot')
extract_cmd.set_defaults(func=extract)
import_po_cmd = subparsers.add_parser('import', help='import translated strings')
import_po_cmd.add_argument('directory')
import_po_cmd.set_defaults(func=import_po)
generate_translations_cmd = subparsers.add_parser('generate-translations', help='generate translated content')
generate_translations_cmd.set_defaults(func=generate_translations)
args = parser.parse_args()
args.func(args)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment