sscli 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python3
  2. __doc__ = """ Scriptshifter command line interface. """
  3. import click
  4. from glob import glob
  5. from os import path
  6. from scriptshifter import DB_PATH
  7. from scriptshifter.tables import get_language, list_tables, init_db as _init_db
  8. from scriptshifter.trans import transliterate as _transliterate
  9. from test.integration import test_sample
  10. @click.group()
  11. def cli():
  12. """ Scriptshifter CLI. """
  13. pass
  14. @cli.group(name="admin")
  15. def admin_grp():
  16. """ Admin operations. """
  17. pass
  18. @admin_grp.command()
  19. def init_db():
  20. """ Initialize SS database. """
  21. _init_db()
  22. click.echo(f"Initialized Scriptshifter DB in {DB_PATH}")
  23. @cli.group(name="tables")
  24. def table_grp():
  25. """ Commands to display table information. """
  26. pass
  27. @table_grp.command()
  28. def list():
  29. """ List all languages. """
  30. click.echo("\nLanguage and script tables available:")
  31. for tcode, tdata in list_tables().items():
  32. click.echo()
  33. click.echo(click.style(tcode, fg="green"))
  34. for k, v in tdata.items():
  35. if v is not None:
  36. click.echo(f"\t{k}: {v}")
  37. @table_grp.command()
  38. @click.argument("lang")
  39. @click.argument("t_dir", default="s2r")
  40. def show(lang, t_dir):
  41. """
  42. Show character mappings for a language.
  43. Only one direction (script to Roman or Roman to script) is diplayed. The
  44. mappings are in descending order of priority.
  45. LANG is one of the language codes obtained by `sscli tables list`.
  46. T_DIR is the direction to be displayed (`s2r` or `r2s`). Default is `s2r`.
  47. """
  48. try:
  49. lang_md = get_language(lang)
  50. except KeyError:
  51. click.echo(
  52. click.style("ERROR: ", fg="red") +
  53. f"{lang} table does not exist.")
  54. exit(1)
  55. dir_k = "script_to_roman" if t_dir == "s2r" else "roman_to_script"
  56. if dir_k not in lang_md:
  57. click.echo(
  58. click.style("ERROR: ", fg="red") +
  59. f"{lang} table has no `{dir_k}` section.")
  60. exit(1)
  61. out = lang_md[dir_k].get("map")
  62. click.echo(f"\nMapping table for {lang}, direction: {t_dir}")
  63. click.echo("Tokens are listed in descending order of priority.")
  64. click.echo("\nsrc\tdest")
  65. click.echo("-" * 12)
  66. for src, dest in out:
  67. click.echo(f"{src}\t{dest}")
  68. @cli.group(name="test")
  69. def test_grp():
  70. """ Test operations. """
  71. pass
  72. @test_grp.command()
  73. def list_samples():
  74. """ List string sample sets that can be tested. """
  75. path_ptn = path.join(
  76. path.dirname(path.realpath(__file__)),
  77. "tests", "data", "script_samples", "*.csv")
  78. click.echo("Sample string sets available for batch testing:")
  79. for fn in glob(path_ptn):
  80. click.echo(path.splitext(path.basename(fn))[0])
  81. @test_grp.command()
  82. @click.argument("lang")
  83. def samples(lang):
  84. """
  85. Test sample strings for language LANG.
  86. LANG must match one of the names obtained with `test list-samples` command.
  87. The command will generate a test report file.
  88. """
  89. return test_sample(lang)
  90. @cli.command(name="trans")
  91. @click.argument("lang")
  92. @click.argument("src", type=click.File("r"), default="-")
  93. @click.option(
  94. "-c", "--capitalize", default=None,
  95. help="Capitalize output: `first`, `all`, ot none (the default).")
  96. @click.option(
  97. "-d", "--t-dir", default="s2r",
  98. help="Transliteration direction: `s2r' (default)) or `r2s'")
  99. @click.option(
  100. "-o", "--option", multiple=True,
  101. help=(
  102. "Language=specific option. Format: key=value. Multiple -o entries "
  103. "are possible."))
  104. def trans_(src, lang, t_dir, capitalize, option):
  105. """
  106. Transliterate text from standard input.
  107. e.g.: `echo "王正强" | /path/to/sscli trans chinese -o "marc_field=100"'
  108. """
  109. options = {}
  110. for opt in option:
  111. k, v = opt.split("=", 1)
  112. options[k] = v
  113. trans, warnings = _transliterate(
  114. src.read(), lang, t_dir, capitalize, options)
  115. for w in warnings:
  116. click.echo(click.style("WARNING: ", fg="yellow") + w)
  117. click.echo(trans)
  118. if __name__ == "__main__":
  119. cli()