Skip to content

Commit

Permalink
refactor: simplify parsing input_text in web_api /assemble
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise committed Jun 19, 2024
1 parent 94d49ae commit 396b2f1
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions readalongs/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"""

import io
import os
import tempfile
from enum import Enum
Expand All @@ -48,6 +47,7 @@
from readalongs.text.convert_xml import convert_xml
from readalongs.text.make_dict import make_dict_list
from readalongs.text.tokenize_xml import tokenize_xml
from readalongs.text.util import parse_xml
from readalongs.util import get_langs

# Create the app
Expand Down Expand Up @@ -191,10 +191,7 @@ async def assemble(
with capture_logs() as captured_logs:
if request.mime_type == InputFormat.RAS:
try:
parsed = etree.fromstring(
bytes(request.input_text or "", encoding="utf-8"),
parser=etree.XMLParser(resolve_entities=False),
)
parsed = parse_xml(request.input_text or "")
except etree.ParseError as e:
raise HTTPException(
status_code=422, detail="XML provided is not well-formed"
Expand All @@ -208,13 +205,11 @@ async def assemble(
),
)
elif request.mime_type == InputFormat.TEXT:
parsed = io.StringIO(request.input_text).readlines()
parsed = etree.fromstring(
bytes(
create_ras_from_text(parsed, text_languages=request.text_languages),
encoding="utf-8",
),
parser=etree.XMLParser(resolve_entities=False),
parsed = parse_xml(
create_ras_from_text(
(request.input_text or "").splitlines(keepends=True),
text_languages=request.text_languages,
)
)

else: # pragma: no cover
Expand Down Expand Up @@ -391,10 +386,7 @@ async def convert_alignment( # noqa: C901
Returns: a file in the format requested
"""
try:
parsed_xml = etree.fromstring(
bytes(request.ras, encoding="utf-8"),
parser=etree.XMLParser(resolve_entities=False),
)
parsed_xml = parse_xml(request.ras)
except etree.XMLSyntaxError as e:
raise HTTPException(
status_code=422, detail="ReadAlong provided is not well formed"
Expand Down

0 comments on commit 396b2f1

Please sign in to comment.