refactor(cli): refactor output formats to -o argument · ReadAlongs/Studio@f34f426 · GitHub
Skip to content

Commit f34f426

Browse files
roedoejetjoanise
authored andcommitted
refactor(cli): refactor output formats to -o argument
1 parent 3002626 commit f34f426

4 files changed

Lines changed: 83 additions & 49 deletions

File tree

readalongs/align.py

Lines changed: 29 additions & 23 deletions

readalongs/cli.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@
4545
LANGS, _ = getLangs()
4646
ensure_using_supported_python_version()
4747

48+
SUPPORTED_OUTPUT_FORMATS = {
49+
"eaf": "ELAN file",
50+
"html": "Single-file, offline HTML",
51+
"srt": "SRT subtitle",
52+
"TextGrid": "Praat TextGrid",
53+
"vtt": "WebVTT subtitle",
54+
"xhtml": "Simple XHTML",
55+
}
56+
57+
SUPPORTED_OUTPUT_FORMATS_DESC = "\n\n".join(
58+
k + " - " + v for k, v in SUPPORTED_OUTPUT_FORMATS.items()
59+
)
60+
4861

4962
def create_app():
5063
"""Returns the app"""
@@ -99,10 +112,12 @@ def cli():
99112
help="Use ReadAlong-Studio configuration file (in JSON format)",
100113
)
101114
@click.option(
102-
"-C",
103-
"--closed-captioning",
104-
is_flag=True,
105-
help="Export sentences to WebVTT and SRT files",
115+
"-o",
116+
"--output-formats",
117+
type=click.Choice(SUPPORTED_OUTPUT_FORMATS.keys()),
118+
multiple=True,
119+
help="The output file formats to export to. By default the text is exported as XML and alignments are exported as SMIL. "
120+
+ f"Other formats include:\b \n\n{SUPPORTED_OUTPUT_FORMATS_DESC}",
106121
)
107122
@click.option("-d", "--debug", is_flag=True, help="Add debugging messages to logger")
108123
@click.option(
@@ -126,13 +141,6 @@ def cli():
126141
is_flag=True,
127142
help="Save intermediate stages of processing and temporary files (dictionary, FSG, tokenization, etc)",
128143
)
129-
@click.option(
130-
"-t", "--text-grid", is_flag=True, help="Export to Praat TextGrid & ELAN eaf file"
131-
)
132-
@click.option("-H", "--html", is_flag=True, help="Export to a single-file HTML format")
133-
@click.option(
134-
"-x", "--output-xhtml", is_flag=True, help="Output simple XHTML instead of XML"
135-
)
136144
@click.option(
137145
"--g2p-fallback",
138146
default=None,
@@ -259,17 +267,17 @@ def align(**kwargs): # noqa: C901
259267
except RuntimeError as e:
260268
LOGGER.error(e)
261269
sys.exit(1)
270+
271+
output_formats = kwargs["output_formats"]
272+
262273
save_readalong(
263274
align_results=results,
264275
output_dir=output_dir,
265276
output_basename=output_basename,
266277
config=config,
267-
text_grid=kwargs["text_grid"],
268-
closed_captioning=kwargs["closed_captioning"],
269-
output_xhtml=kwargs["output_xhtml"],
270278
audiofile=kwargs["audiofile"],
271279
audiosegment=results["audio"],
272-
html=kwargs["html"],
280+
output_formats=output_formats,
273281
)
274282

275283

test/test_align_cli.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ def test_invoke_align(self):
2929
[
3030
"-i",
3131
"-s",
32-
"-C",
33-
"-t",
32+
"-o",
33+
"vtt",
34+
"-o",
35+
"srt",
36+
"-o",
37+
"TextGrid",
38+
"-o",
39+
"eaf",
3440
"-l",
3541
"fra",
3642
"--config",
@@ -87,7 +93,8 @@ def test_invoke_align(self):
8793
results_dna = self.runner.invoke(
8894
align,
8995
[
90-
"-x",
96+
"-o",
97+
"xhtml",
9198
"-s",
9299
"--config",
93100
join(self.data_dir, "sample-config.json"),
@@ -104,7 +111,7 @@ def test_invoke_align(self):
104111
)
105112
self.assertTrue(
106113
exists(join(output, "output.xhtml")),
107-
"successful alignment with -x should have created output.xhtml",
114+
"successful alignment with -o xhtml should have created output.xhtml",
108115
)
109116
self.assertIn("Please copy image-for-page1.jpg to ", results_dna.stdout)
110117
self.assertFalse(
@@ -114,7 +121,7 @@ def test_invoke_align(self):
114121

115122
# Functionally the same as self.assertTrue(filecmp.cmp(f1, f2)), but show where
116123
# the differences are if the files are not identical
117-
# Since f2 was created using -x, we need to substitute .xhtml back to .xml during
124+
# Since f2 was created using -o xhtml, we need to substitute .xhtml back to .xml during
118125
# the comparison of the contents of the .smil files.
119126
with open(join(output1, "output.smil"), encoding="utf8") as f1, open(
120127
join(output, "output.smil"), encoding="utf8"
@@ -153,7 +160,7 @@ def test_invoke_align(self):
153160
)
154161

155162
def test_align_with_package(self):
156-
"""Test creating a single-file package, with --html"""
163+
"""Test creating a single-file package, with -o html"""
157164

158165
output = join(self.tempdir, "html")
159166
with SoundSwallowerStub("t0b0d0p0s0w0:920:1620", "t0b0d0p0s1w0:1620:1690"):
@@ -163,7 +170,8 @@ def test_align_with_package(self):
163170
join(self.data_dir, "ej-fra-package.xml"),
164171
join(self.data_dir, "ej-fra.m4a"),
165172
output,
166-
"--html",
173+
"-o",
174+
"html",
167175
],
168176
)
169177
# print(results_html.output)

test/test_silence.py

Lines changed: 16 additions & 4 deletions

0 commit comments

Comments
 (0)