Skip to content

Commit

Permalink
feat: update .ras to .readalong
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Feb 16, 2023
1 parent b9bf023 commit 525facf
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion readalongs/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.20230208"
__version__ = "0.2.20230215"
12 changes: 6 additions & 6 deletions readalongs/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ def parse_and_make_xml(
xml = add_supplementary_xml(xml, config)
xml = tokenize_xml(xml)
if save_temps is not None:
save_xml(save_temps + ".tokenized.ras", xml)
save_xml(save_temps + ".tokenized.readalong", xml)
xml = add_ids(xml)
if save_temps is not None:
save_xml(save_temps + ".ids.ras", xml)
save_xml(save_temps + ".ids.readalong", xml)
xml, valid = convert_xml(
xml,
verbose_warnings=verbose_g2p_warnings,
output_orthography=output_orthography,
)
if save_temps is not None:
save_xml(save_temps + ".g2p.ras", xml)
save_xml(save_temps + ".g2p.readalong", xml)
if not valid:
raise RuntimeError(
"Some words could not be g2p'd correctly. Aborting. "
Expand Down Expand Up @@ -927,7 +927,7 @@ def save_readalong(
output_formats=output_formats,
)

ras_path = output_base + ".ras"
ras_path = output_base + ".readalong"
save_xml(ras_path, align_results["tokenized"])

if "xhtml" in output_formats:
Expand Down Expand Up @@ -1244,11 +1244,11 @@ def create_input_ras(**kwargs):
filename = kwargs.get("output_file")
outfile = io.open(filename, "wb")
elif save_temps is not None:
filename = save_temps + ".input.ras"
filename = save_temps + ".input.readalong"
outfile = io.open(filename, "wb")
else:
outfile = PortableNamedTemporaryFile(
prefix="readalongs_xml_", suffix=".ras", delete=True
prefix="readalongs_xml_", suffix=".readalong", delete=True
)
filename = outfile.name
xml = create_ras_from_text(text, text_langs)
Expand Down
54 changes: 30 additions & 24 deletions readalongs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def cli():
default=None,
help="OBSOLETE; the input format is now guessed by extension or contents",
callback=get_obsolete_callback_for_click(
".txt files are now read as plain text, .xml or .ras as XML, and other files based on\n"
".txt files are now read as plain text, .xml or .readalong as XML, and other files based on\n"
"whether they start with <?xml or not."
),
)
Expand Down Expand Up @@ -257,7 +257,7 @@ def align(**kwargs): # noqa: C901 # some versions of flake8 need this here ins
TEXTFILE: Input text file path (in XML or plain text)
\b
If TEXTFILE has a .xml or .ras extension or starts with an XML declaration line,
If TEXTFILE has a .xml or .readalong extension or starts with an XML declaration line,
it is parsed as XML and can be in one of three formats:
- the output of 'readalongs make-xml',
- the output of 'readalongs tokenize', or
Expand Down Expand Up @@ -337,8 +337,8 @@ def align(**kwargs): # noqa: C901 # some versions of flake8 need this here ins
textfile_name = kwargs["textfile"]
if str(textfile_name).endswith(".xml"):
textfile_is_plaintext = False # .xml is XML
elif str(textfile_name).endswith(".ras"):
textfile_is_plaintext = False # .ras is XML
elif str(textfile_name).endswith(".readalong"):
textfile_is_plaintext = False # .readalong is XML
elif str(textfile_name).endswith(".txt"):
textfile_is_plaintext = True # .txt is plain text
else:
Expand Down Expand Up @@ -456,7 +456,7 @@ def prepare(**kwargs):
PLAINTEXTFILE: Path to the plain text input file, or - for stdin
XMLFILE: Path to the XML output file, or - for stdout [default: PLAINTEXTFILE.ras]
XMLFILE: Path to the XML output file, or - for stdout [default: PLAINTEXTFILE.readalong]
"""
LOGGER.warning(
'WARNING: "readalongs prepare" is deprecated. Use "readalongs make-xml" instead.'
Expand Down Expand Up @@ -504,7 +504,7 @@ def make_xml(**kwargs):
PLAINTEXTFILE: Path to the plain text input file, or - for stdin
XMLFILE: Path to the XML output file, or - for stdout [default: PLAINTEXTFILE.ras]
XMLFILE: Path to the XML output file, or - for stdout [default: PLAINTEXTFILE.readalong]
"""

if kwargs["debug"]:
Expand All @@ -524,9 +524,10 @@ def make_xml(**kwargs):
if not out_file:
out_file = get_click_file_name(input_file)
if out_file != "-":
if str(out_file).endswith(".txt"):
out_file = out_file[:-4]
out_file += ".ras"
base, ext = os.path.splitext(out_file)
if ext == ".txt":
out_file = base
out_file += ".readalong"

languages = list(kwargs["language"])
if not kwargs["lang_no_append_und"] and "und" not in languages:
Expand All @@ -540,8 +541,8 @@ def make_xml(**kwargs):
with io.open(filename, encoding="utf-8-sig") as f:
sys.stdout.write(f.read())
else:
if not str(out_file).endswith(".ras"):
out_file += ".ras"
if not str(out_file).endswith(".readalong"):
out_file += ".readalong"
if os.path.exists(out_file) and not kwargs["force_overwrite"]:
raise click.BadParameter(
"Output file %s exists already, use -f to overwrite." % out_file
Expand Down Expand Up @@ -577,7 +578,7 @@ def tokenize(**kwargs):
XMLFILE: Path to the XML file to tokenize, or - for stdin
TOKFILE: Output path for the tok'd XML, or - for stdout [default: XMLFILE.tokenized.ras]
TOKFILE: Output path for the tok'd XML, or - for stdout [default: XMLFILE.tokenized.readalong]
"""

if kwargs["debug"]:
Expand All @@ -593,13 +594,15 @@ def tokenize(**kwargs):
if not kwargs["tokfile"]:
output_path = get_click_file_name(input_file)
if output_path != "-":
if str(output_path).endswith(".ras"):
output_path = output_path[:-4]
output_path += ".tokenized.ras"
base, ext = os.path.splitext(str(output_path))
if ext == ".readalong":
output_path = base
output_path += ".tokenized.readalong"
else:
output_path = kwargs["tokfile"]
if not str(output_path).endswith(".ras") and not output_path == "-":
output_path += ".ras"
base, ext = os.path.splitext(str(output_path))
if ext != ".readalong" and output_path != "-":
output_path += ".readalong"

if os.path.exists(output_path) and not kwargs["force_overwrite"]:
raise click.BadParameter(
Expand Down Expand Up @@ -697,15 +700,18 @@ def g2p(**kwargs):
if not kwargs["g2pfile"]:
output_path = get_click_file_name(input_file)
if output_path != "-":
if str(output_path).endswith(".ras"):
output_path = output_path[:-4]
if str(output_path).endswith(".tokenized"):
output_path = output_path[: -len(".tokenized")]
output_path += ".g2p.ras"
base, ext = os.path.splitext(output_path)
if ext == ".readalong":
output_path = base
base, ext = os.path.splitext(output_path)
if ext == ".tokenized":
output_path = base
output_path += ".g2p.readalong"
else:
output_path = kwargs["g2pfile"]
if not str(output_path).endswith(".ras") and not output_path == "-":
output_path += ".ras"
base, ext = os.path.splitext(output_path)
if ext != ".readalong" and output_path != "-":
output_path += ".readalong"

if os.path.exists(output_path) and not kwargs["force_overwrite"]:
raise click.BadParameter(
Expand Down
4 changes: 2 additions & 2 deletions readalongs/templates/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ <h1 class='title'>Code</h1>
<code>
&lt;script type="module" src='https://unpkg.com/@roedoejet/readalong@^0.1.6/dist/read-along/read-along.esm.js'&gt;&lt;/script&gt;
&lt;script nomodule src='https://unpkg.com/@roedoejet/readalong@^0.1.6/dist/read-along/read-along.js'&gt;&lt;/script&gt;
&lt;read-along text="aligned.ras" alignment="aligned.smil" audio="aligned{{data.audio_ext}}"&gt;&lt;/read-along&gt;
&lt;read-along text="aligned.readalong" alignment="aligned.smil" audio="aligned{{data.audio_ext}}"&gt;&lt;/read-along&gt;
</code>
</div>
<div class="container readalong">
<h1 class='title'>ReadAlong</h1>
<p>Here's your interactive read along!</p>
<read-along href='{{data.text_fn}}' audio='{{data.audio_fn}}'></read-along>
{# <read-along href="/file/aligned.ras" audio="/file/aligned{{data.audio_ext}}"></read-along> #}
{# <read-along href="/file/aligned.readalong" audio="/file/aligned{{data.audio_ext}}"></read-along> #}
</div>
<div class="columns is-mobile is-centered download">
<div class="column is-half is-centered has-text-centered">
Expand Down
4 changes: 2 additions & 2 deletions readalongs/templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ <h1 class="title">Text</h1>
<h2 class="subtitle">
Upload the text of your ReadAlong</h2>
<p>You can upload text in a number of ways. You can upload plain text files (.txt), Microsoft Word Documents
(.docx), or ReadAlong XML files (.ras)</p>
(.docx), or ReadAlong XML files (.readalong)</p>
<form id='textForm'>
<input type="file" name="text" accept=".docx,.txt,.ras">
<input type="file" name="text" accept=".docx,.txt,.readalong">
<input type="submit">
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion readalongs/text/end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def end_to_end(xml, input_filename, unit, word_unit, out_orth):
xml = tokenize_xml(xml)
xml = add_ids(xml)
converted_xml, valid = convert_xml(xml, word_unit, out_orth)
# save_xml("test.ras", converted_xml)
# save_xml("test.readalong", converted_xml)
fsg = make_fsg(converted_xml, input_filename, unit)
pronouncing_dictionary = make_dict(converted_xml, input_filename, unit)
return xml, fsg, pronouncing_dictionary
Expand Down
4 changes: 2 additions & 2 deletions readalongs/text/make_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def encode_from_path(path: str) -> str:

with open(path, "rb") as f:
path_bytes = f.read()
if str(path).endswith("xml") or str(path).endswith(".ras"):
if str(path).endswith("xml") or str(path).endswith(".readalong"):
root = etree.fromstring(
path_bytes, parser=etree.XMLParser(resolve_entities=False)
)
Expand Down Expand Up @@ -92,7 +92,7 @@ def encode_from_path(path: str) -> str:
# TODO: Check other popular audio formats, .wav, .mp3, .ogg, etc...
mime_type = "audio/mp4"
if str(path).endswith(
".ras"
".readalong"
): # We declare it to be application/readalong+xml, not what mimetypes thinks
mime_type = "application/readalong+xml"
elif mime[0]:
Expand Down
6 changes: 3 additions & 3 deletions readalongs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def uploaded_files(dir_path: str) -> dict:
audio = list(upload_dir.glob("*.wav")) + list(upload_dir.glob("*.mp3"))
text = (
list(upload_dir.glob("*.txt"))
+ list(upload_dir.glob("*.ras"))
+ list(upload_dir.glob("*.readalong"))
+ list(upload_dir.glob("*.docx"))
)
maps = list(upload_dir.glob("*.csv")) + list(upload_dir.glob("*.xlsx"))
Expand Down Expand Up @@ -208,9 +208,9 @@ def steps(step):
)
data["audio_fn"] = f"/file/{output_base}" + audio_ext
data["text_path"] = os.path.join(
session["temp_dir"], output_base, output_base + ".ras"
session["temp_dir"], output_base, output_base + ".readalong"
)
data["text_fn"] = f"/file/{output_base}" + ".ras"
data["text_fn"] = f"/file/{output_base}" + ".readalong"
return render_template("export.html", data=data)
else:
abort(404)
Expand Down

0 comments on commit 525facf

Please sign in to comment.