Skip to content

Commit

Permalink
Revert "raise error when id tag doesn't match filename book id"
Browse files Browse the repository at this point in the history
This reverts commit 8679b78.
  • Loading branch information
mshannon-sil committed Nov 12, 2024
1 parent 8679b78 commit d91eeeb
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 230 deletions.
32 changes: 12 additions & 20 deletions machine/corpora/paratext_backup_text_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@ def __init__(self, filename: StrPath, include_markers: bool = False, include_all
for sfm_entry in archive.filelist:
book_id = settings.get_book_id(sfm_entry.filename)
if book_id:
text = UsfmZipText(
settings.stylesheet,
settings.encoding,
book_id,
filename,
sfm_entry.filename,
versification,
include_markers,
include_all_text,
settings.name,
texts.append(
UsfmZipText(
settings.stylesheet,
settings.encoding,
book_id,
filename,
sfm_entry.filename,
versification,
include_markers,
include_all_text,
settings.name,
)
)
with text.get_rows() as rows:
row = next(rows, None)
if row and row.ref.book != book_id:
if row.ref.book == "":
raise ValueError(f"The \\id tag in {sfm_entry.filename} is invalid.")
raise ValueError(
f"The \\id tag {row.ref.book} in {sfm_entry.filename}"
f" does not match filename book id {book_id}."
)
texts.append(text)

super().__init__(versification, texts)
29 changes: 11 additions & 18 deletions machine/corpora/paratext_text_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@ def __init__(self, project_dir: StrPath, include_markers: bool = False, include_
for sfm_filename in Path(project_dir).glob(f"{settings.file_name_prefix}*{settings.file_name_suffix}"):
book_id = settings.get_book_id(sfm_filename.name)
if book_id:
text = UsfmFileText(
settings.stylesheet,
settings.encoding,
book_id,
sfm_filename,
versification,
include_markers,
include_all_text,
settings.name,
texts.append(
UsfmFileText(
settings.stylesheet,
settings.encoding,
book_id,
sfm_filename,
versification,
include_markers,
include_all_text,
settings.name,
)
)
with text.get_rows() as rows:
row = next(rows, None)
if row and row.ref.book != book_id:
if row.ref.book == "":
raise ValueError(f"The \\id tag in {sfm_filename} is invalid.")
raise ValueError(
f"The \\id tag {row.ref.book} in {sfm_filename} does not match filename book id {book_id}."
)
texts.append(text)

super().__init__(versification, texts)
30 changes: 4 additions & 26 deletions tests/corpora/test_paratext_backup_text_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, ContextManager, Optional
from typing import Any, ContextManager

from pytest import raises
from testutils.corpora_test_helpers import (
create_test_paratext_backup,
create_test_paratext_backup_invalid_id,
create_test_paratext_backup_mismatch_id,
)
from testutils.corpora_test_helpers import create_test_paratext_backup

from machine.corpora import ParatextBackupTextCorpus

Expand All @@ -33,27 +28,10 @@ def test_get_text() -> None:
assert not any(jhn.get_rows())


def test_invalid_id() -> None:
with raises(ValueError, match=r"The \\id tag in .* is invalid."):
with _TestEnvironment("invalid_id") as env:
env.corpus.get_text("JDG")


def test_mismatch_id() -> None:
with raises(ValueError, match=r"The \\id tag .* in .* does not match filename book id .*"):
with _TestEnvironment("mismatch_id") as env:
env.corpus.get_text("JDG")


class _TestEnvironment(ContextManager["_TestEnvironment"]):
def __init__(self, project_folder_name: Optional[str] = None) -> None:
def __init__(self) -> None:
self._temp_dir = TemporaryDirectory()
if project_folder_name == "invalid_id":
archive_filename = create_test_paratext_backup_invalid_id(Path(self._temp_dir.name))
elif project_folder_name == "mismatch_id":
archive_filename = create_test_paratext_backup_mismatch_id(Path(self._temp_dir.name))
else:
archive_filename = create_test_paratext_backup(Path(self._temp_dir.name))
archive_filename = create_test_paratext_backup(Path(self._temp_dir.name))
self._corpus = ParatextBackupTextCorpus(archive_filename)

@property
Expand Down
14 changes: 0 additions & 14 deletions tests/corpora/test_paratext_text_corpus.py

This file was deleted.

12 changes: 0 additions & 12 deletions tests/testutils/corpora_test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
USFM_TEST_PROJECT_PATH = TEST_DATA_PATH / "usfm" / "Tes"
USFM_TARGET_PROJECT_PATH = TEST_DATA_PATH / "usfm" / "target"
USFM_SOURCE_PROJECT_PATH = TEST_DATA_PATH / "usfm" / "source"
USFM_MISMATCH_ID_PROJECT_PATH = TEST_DATA_PATH / "usfm" / "mismatch_id"
USFM_INVALID_ID_PROJECT_PATH = TEST_DATA_PATH / "usfm" / "invalid_id"
USX_TEST_PROJECT_PATH = TEST_DATA_PATH / "usx" / "Tes"
TEXT_TEST_PROJECT_PATH = TEST_DATA_PATH / "txt"
CUSTOM_VERS_PATH = TEST_DATA_PATH / "custom.vrs"
Expand All @@ -26,16 +24,6 @@ def create_test_paratext_backup(temp_dir: Path) -> Path:
return temp_dir / "Tes.zip"


def create_test_paratext_backup_invalid_id(temp_dir: Path) -> Path:
shutil.make_archive(str(temp_dir / "invalid_id"), "zip", USFM_INVALID_ID_PROJECT_PATH)
return temp_dir / "invalid_id.zip"


def create_test_paratext_backup_mismatch_id(temp_dir: Path) -> Path:
shutil.make_archive(str(temp_dir / "mismatch_id"), "zip", USFM_MISMATCH_ID_PROJECT_PATH)
return temp_dir / "mismatch_id.zip"


def verse_ref(segment: TextRow) -> VerseRef:
assert isinstance(segment.ref, VerseRef)
return segment.ref
Expand Down
5 changes: 0 additions & 5 deletions tests/testutils/data/usfm/invalid_id/07JDG.SFM

This file was deleted.

34 changes: 0 additions & 34 deletions tests/testutils/data/usfm/invalid_id/Settings.xml

This file was deleted.

31 changes: 0 additions & 31 deletions tests/testutils/data/usfm/invalid_id/custom.vrs

This file was deleted.

5 changes: 0 additions & 5 deletions tests/testutils/data/usfm/mismatch_id/07JDG.SFM

This file was deleted.

34 changes: 0 additions & 34 deletions tests/testutils/data/usfm/mismatch_id/Settings.xml

This file was deleted.

31 changes: 0 additions & 31 deletions tests/testutils/data/usfm/mismatch_id/custom.vrs

This file was deleted.

0 comments on commit d91eeeb

Please sign in to comment.