Skip to content

Commit

Permalink
Merge branch 'dev.Windows-compat'
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise committed Aug 25, 2021
2 parents ad10761 + 2598647 commit 72d3ba8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
5 changes: 4 additions & 1 deletion readalongs/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ def align_audio( # noqa: C901
save_temps + "_processed" + ext, format=ext[1:]
)
except CouldntEncodeError:
os.remove(save_temps + "_processed" + ext)
try:
os.remove(save_temps + "_processed" + ext)
except:
pass
LOGGER.warning(
f"Couldn't find encoder for '{ext[1:]}', defaulting to 'wav'"
)
Expand Down
42 changes: 24 additions & 18 deletions readalongs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,23 @@ def create_app():


def get_click_file_name(click_file):
""" Return click_file.name, falling back to <stdin> if the .name attribute is missing. """
""" Wrapper around click_file.name with consistent handling for stdin
On Windows, if click_file is stdin, click_file.name == "-".
On Linux, if click_file is stdin, click_file.name == "<stdin>".
During unit testing, the simulated stdin stream has no .name attribute
Args:
click_file(click.File): the click file whose name we need
Returns:
"-" if click_file represents stdin, click_file.name otherwise
"""
try:
return click_file.name
except Exception: # For unit testing: simulated stdin stream has no .name attrib
return "<stdin>"
name = click_file.name
except Exception:
name = "-"
return "-" if name == "<stdin>" else name


def parse_g2p_fallback(g2p_fallback_arg):
Expand Down Expand Up @@ -304,7 +316,7 @@ def epub(**kwargs):
context_settings=CONTEXT_SETTINGS,
short_help="Convert a plain text file into the XML format for alignment.",
)
@click.argument("plaintextfile", type=click.File("r"))
@click.argument("plaintextfile", type=click.File("r", encoding="utf8", lazy=True))
@click.argument("xmlfile", type=click.Path(), required=False, default="")
@click.option("-d", "--debug", is_flag=True, help="Add debugging messages to logger")
@click.option(
Expand Down Expand Up @@ -344,18 +356,16 @@ def prepare(**kwargs):
out_file = kwargs["xmlfile"]
if not out_file:
out_file = get_click_file_name(input_file)
if out_file == "<stdin>": # actual intput_file.name when cli input is "-"
out_file = "-"
else:
if out_file != "-":
if out_file.endswith(".txt"):
out_file = out_file[:-4]
out_file += ".xml"

if out_file == "-":
filehandle, filename = create_input_tei(
_, filename = create_input_tei(
input_file_handle=input_file, text_language=kwargs["language"],
)
with io.open(filename) as f:
with io.open(filename, encoding="utf8") as f:
sys.stdout.write(f.read())
else:
if not out_file.endswith(".xml"):
Expand All @@ -365,7 +375,7 @@ def prepare(**kwargs):
"Output file %s exists already, use -f to overwrite." % out_file
)

filehandle, filename = create_input_tei(
_, filename = create_input_tei(
input_file_handle=input_file,
text_language=kwargs["language"],
output_file=out_file,
Expand Down Expand Up @@ -407,9 +417,7 @@ def tokenize(**kwargs):

if not kwargs["tokfile"]:
output_path = get_click_file_name(input_file)
if output_path == "<stdin>":
output_path = "-"
else:
if output_path != "-":
if output_path.endswith(".xml"):
output_path = output_path[:-4]
output_path += ".tokenized.xml"
Expand Down Expand Up @@ -446,7 +454,7 @@ def tokenize(**kwargs):
short_help="Apply g2p to a tokenized file, like 'align' does.",
# NOT TRUE YET: "Apply g2p to a tokenized file, in preparation for alignment."
)
@click.argument("tokfile", type=click.File("rb"))
@click.argument("tokfile", type=click.File("rb", encoding="utf8", lazy=True))
@click.argument("g2pfile", type=click.Path(), required=False, default="")
@click.option(
"--g2p-fallback",
Expand Down Expand Up @@ -487,9 +495,7 @@ def g2p(**kwargs):

if not kwargs["g2pfile"]:
output_path = get_click_file_name(input_file)
if output_path == "<stdin>":
output_path = "-"
else:
if output_path != "-":
if output_path.endswith(".xml"):
output_path = output_path[:-4]
if output_path.endswith(".tokenized"):
Expand Down
6 changes: 6 additions & 0 deletions test/test_align_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ def test_invoke_align(self):
)

def test_permission_denied(self):
import platform

if platform.system() == "Windows":
# Cannot change the permission on a directory in Windows though
# os.mkdir() or os.chmod(), so skip this test
return
dir = join(self.tempdir, "permission_denied")
os.mkdir(dir, mode=0o444)
results = self.runner.invoke(
Expand Down
6 changes: 3 additions & 3 deletions test/test_g2p_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_mixed_langs(self):
# saving the final results into filename.
# filename is assumed to be inside self.tempdir, so we count on tearDown() to clean up.
def write_prepare_tokenize(self, text, lang, filename):
with open(filename + ".input.txt", "w") as f:
with open(filename + ".input.txt", "w", encoding="utf8") as f:
print(text, file=f)
self.runner.invoke(
prepare, ["-l", lang, filename + ".input.txt", filename + ".prepared.xml"]
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_three_way_fallback(self):

def test_align_with_error(self):
text_file = os.path.join(self.tempdir, "input.txt")
with io.open(text_file, "w") as f:
with io.open(text_file, "w", encoding="utf8") as f:
print("In French été works but Nunavut ᓄᓇᕗᑦ does not.", file=f)
empty_wav = os.path.join(self.tempdir, "empty.wav")
with io.open(empty_wav, "wb"):
Expand Down Expand Up @@ -241,7 +241,7 @@ def test_align_with_error(self):

def test_with_stdin(self):
input_file = os.path.join(self.data_dir, "fra-tokenized.xml")
with io.open(input_file) as f:
with io.open(input_file, encoding="utf8") as f:
inputtext = f.read()
results = self.runner.invoke(g2p, "-", input=inputtext)
self.assertEqual(results.exit_code, 0)
Expand Down

0 comments on commit 72d3ba8

Please sign in to comment.