-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsscli
More file actions
executable file
·160 lines (119 loc) · 4.04 KB
/
sscli
File metadata and controls
executable file
·160 lines (119 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/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 get_language, list_tables, 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="tables")
def table_grp():
""" Commands to display table information. """
pass
@table_grp.command()
def list():
""" List all languages. """
click.echo("\nLanguage and script tables available:")
for tcode, tdata in list_tables().items():
click.echo()
click.echo(click.style(tcode, fg="green"))
for k, v in tdata.items():
if v is not None:
click.echo(f"\t{k}: {v}")
@table_grp.command()
@click.argument("lang")
@click.argument("t_dir", default="s2r")
def show(lang, t_dir):
"""
Show character mappings for a language.
Only one direction (script to Roman or Roman to script) is diplayed. The
mappings are in descending order of priority.
LANG is one of the language codes obtained by `sscli tables list`.
T_DIR is the direction to be displayed (`s2r` or `r2s`). Default is `s2r`.
"""
try:
lang_md = get_language(lang)
except KeyError:
click.echo(
click.style("ERROR: ", fg="red") +
f"{lang} table does not exist.")
exit(1)
dir_k = "script_to_roman" if t_dir == "s2r" else "roman_to_script"
if dir_k not in lang_md:
click.echo(
click.style("ERROR: ", fg="red") +
f"{lang} table has no `{dir_k}` section.")
exit(1)
out = lang_md[dir_k].get("map")
click.echo(f"\nMapping table for {lang}, direction: {t_dir}")
click.echo("Tokens are listed in descending order of priority.")
click.echo("\nsrc\tdest")
click.echo("-" * 12)
for src, dest in out:
click.echo(f"{src}\t{dest}")
@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.command(name="trans")
@click.argument("lang")
@click.argument("src", type=click.File("r"), default="-")
@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 trans_(src, lang, t_dir, capitalize, option):
"""
Transliterate text from standard input.
e.g.: `echo "王正强" | /path/to/sscli trans 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()