123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #!/usr/bin/env python3
- __doc__ = """ Scriptshifter command line interface. """
- import click
- from glob import glob
- from os import path
- from scriptshifter import DB_PATH
- from scriptshifter.tables import init_db as _init_db
- from scriptshifter.trans import transliterate as _transliterate
- from test.integration import test_sample
- @click.group()
- def cli():
- """ Scriptshifter CLI. """
- pass
- @cli.group(name="admin")
- def admin_grp():
- """ Admin operations. """
- pass
- @admin_grp.command()
- def init_db():
- """ Initialize SS database. """
- _init_db()
- click.echo(f"Initialized Scriptshifter DB in {DB_PATH}")
- @cli.group(name="test")
- def test_grp():
- """ Test operations. """
- pass
- @test_grp.command()
- def list_samples():
- """ List string sample sets that can be tested. """
- path_ptn = path.join(
- path.dirname(path.realpath(__file__)),
- "tests", "data", "script_samples", "*.csv")
- click.echo("Sample string sets available for batch testing:")
- for fn in glob(path_ptn):
- click.echo(path.splitext(path.basename(fn))[0])
- @test_grp.command()
- @click.argument("lang")
- def samples(lang):
- """
- Test sample strings for language LANG.
- LANG must match one of the names obtained with `test list-samples` command.
- The command will generate a test report file.
- """
- return test_sample(lang)
- @cli.group(name="trans")
- def trans_grp():
- """ Transliteration and transcription operations. """
- pass
- @trans_grp.command()
- @click.argument("src", type=click.File("r"))
- @click.argument("lang")
- @click.option(
- "-c", "--capitalize", default=None,
- help="Capitalize output: `first`, `all`, ot none (the default).")
- @click.option(
- "-d", "--t-dir", default="s2r",
- help="Transliteration direction: `s2r' (default)) or `r2s'")
- @click.option(
- "-o", "--option", multiple=True,
- help=(
- "Language=specific option. Format: key=value. Multiple -o entries "
- "are possible."))
- def transliterate(src, lang, t_dir, capitalize, option):
- """
- Transliterate text from standard input.
- e.g.: `echo "王正强" | /path/to/sscli trans transliterate chinese
- -o "marc_field=100"'
- """
- options = {}
- for opt in option:
- k, v = opt.split("=", 1)
- options[k] = v
- trans, warnings = _transliterate(
- src.read(), lang, t_dir, capitalize, options)
- for w in warnings:
- click.echo(click.style("WARNING: ", fg="yellow") + w)
- click.echo(trans)
- if __name__ == "__main__":
- cli()
|