Skip to content

Commit 1a8e337

Browse files
committed
Extend paratext project settings class to support daughter translations
1 parent 1c6bde6 commit 1a8e337

File tree

7 files changed

+67
-5
lines changed

7 files changed

+67
-5
lines changed

machine/corpora/paratext_project_settings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
@dataclass
1010
class ParatextProjectSettings:
11+
guid: str
1112
name: str
1213
full_name: str
1314
encoding: str
@@ -20,6 +21,9 @@ class ParatextProjectSettings:
2021
biblical_terms_project_name: str
2122
biblical_terms_file_name: str
2223
language_code: Optional[str]
24+
translation_type: str
25+
parent_guid: Optional[str] = None
26+
parent_name: Optional[str] = None
2327

2428
def get_book_id(self, file_name: str) -> Optional[str]:
2529
"""Returns None when the file name doesn't match the pattern of a book file name for the project."""
@@ -57,6 +61,18 @@ def get_all_scripture_book_ids(self) -> Iterable[str]:
5761
for book_id in get_scripture_books():
5862
yield book_id
5963

64+
def has_parent(self) -> bool:
65+
return self.parent_guid is not None
66+
67+
def is_daughter_project_of(self, other_project: "ParatextProjectSettings") -> bool:
68+
if not self.has_parent():
69+
return False
70+
return self.parent_guid == other_project.guid
71+
72+
def set_parent_project(self, other_project: "ParatextProjectSettings"):
73+
# TODO anything else we should set? From what I can tell, stylesheets are not inherited
74+
self.versification = other_project.versification
75+
6076

6177
def _get_book_file_name_digits(book_id: str) -> str:
6278
book_num = book_id_to_number(book_id)

machine/corpora/paratext_project_settings_parser_base.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABC
2+
from typing import Optional
23
from xml.etree import ElementTree
34

45
from ..scripture.verse_ref import Versification
@@ -9,8 +10,13 @@
910

1011

1112
class ParatextProjectSettingsParserBase(ABC):
12-
def __init__(self, paratext_project_file_handler: ParatextProjectFileHandler):
13+
def __init__(
14+
self,
15+
paratext_project_file_handler: ParatextProjectFileHandler,
16+
parent_paratext_project_file_handler: Optional[ParatextProjectFileHandler] = None,
17+
):
1318
self._paratext_project_file_handler = paratext_project_file_handler
19+
self._parent_paratext_project_file_handler = parent_paratext_project_file_handler
1420

1521
def parse(self) -> ParatextProjectSettings:
1622
settings_file_name = "Settings.xml"
@@ -21,6 +27,7 @@ def parse(self) -> ParatextProjectSettings:
2127
with self._paratext_project_file_handler.open(settings_file_name) as stream:
2228
settings_tree = ElementTree.parse(stream)
2329

30+
guid = settings_tree.getroot().findtext("Guid", "")
2431
name = settings_tree.getroot().findtext("Name", "")
2532
full_name = settings_tree.getroot().findtext("FullName", "")
2633
encoding_str = settings_tree.getroot().findtext("Encoding", "65001")
@@ -36,7 +43,6 @@ def parse(self) -> ParatextProjectSettings:
3643
versification_type = int(settings_tree.getroot().findtext("Versification", "4"))
3744
versification = Versification.get_builtin(versification_type)
3845
if self._paratext_project_file_handler.exists("custom.vrs"):
39-
guid = settings_tree.getroot().findtext("Guid", "")
4046
versification_name = f"{versification.name}-{guid}"
4147
versification = Versification.load(
4248
self._paratext_project_file_handler.open("custom.vrs"),
@@ -77,12 +83,23 @@ def parse(self) -> ParatextProjectSettings:
7783
)
7884
language_code = None
7985
language_iso_code_setting = settings_tree.getroot().findtext("LanguageIsoCode", "")
80-
if language_iso_code_setting:
81-
language_iso_code_setting_parts = settings_tree.getroot().findtext("LanguageIsoCode", "").split(":")
86+
if language_iso_code_setting is not None:
87+
language_iso_code_setting_parts = language_iso_code_setting.split(":")
8288
if language_iso_code_setting_parts:
8389
language_code = language_iso_code_setting_parts[0]
8490

91+
translation_info_setting = settings_tree.getroot().findtext("TranslationInfo")
92+
translation_type = "Standard"
93+
parent_name = None
94+
parent_guid = None
95+
if translation_info_setting is not None:
96+
translation_info_setting_parts = translation_info_setting.split(":")
97+
translation_type = translation_info_setting_parts[0]
98+
parent_name = translation_info_setting_parts[1] if translation_info_setting_parts[1] != "" else None
99+
parent_guid = translation_info_setting_parts[2] if translation_info_setting_parts[2] != "" else None
100+
85101
return ParatextProjectSettings(
102+
guid,
86103
name,
87104
full_name,
88105
encoding,
@@ -95,4 +112,7 @@ def parse(self) -> ParatextProjectSettings:
95112
parts[1],
96113
parts[2],
97114
language_code,
115+
translation_type,
116+
parent_guid,
117+
parent_name,
98118
)

tests/corpora/test_file_paratext_project_settings_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ def test_parse_custom_stylesheet() -> None:
99
test_tag = settings.stylesheet.get_tag("test")
1010
assert test_tag.style_type is UsfmStyleType.CHARACTER
1111
assert test_tag.text_type is UsfmTextType.OTHER
12+
13+
14+
def test_is_daughter_project() -> None:
15+
parser = FileParatextProjectSettingsParser(USFM_TEST_PROJECT_PATH)
16+
settings = parser.parse()
17+
assert settings.has_parent()
18+
assert settings.is_daughter_project_of(settings)
19+
assert settings.translation_type == "Standard"

tests/corpora/test_paratext_project_settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def test_get_book_id_wrong_book_part_book_num_book_id() -> None:
115115

116116
def _create_settings(file_name_form: str) -> ParatextProjectSettings:
117117
return ParatextProjectSettings(
118+
"id",
118119
"Name",
119120
"Name",
120121
"utf-8",
@@ -127,4 +128,5 @@ def _create_settings(file_name_form: str) -> ParatextProjectSettings:
127128
"",
128129
"BiblicalTerms.xml",
129130
"en",
131+
"Standard",
130132
)

tests/corpora/test_zip_paratext_project_settings_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def test_parse_custom_stylesheet() -> None:
1818
assert test_tag.text_type is UsfmTextType.OTHER
1919

2020

21+
def test_is_daughter_project() -> None:
22+
with _TestEnvironment() as env:
23+
settings = env.parser.parse()
24+
assert settings.has_parent()
25+
assert settings.is_daughter_project_of(settings)
26+
assert settings.translation_type == "Standard"
27+
28+
2129
class _TestEnvironment(ContextManager["_TestEnvironment"]):
2230
def __init__(self) -> None:
2331
self._temp_dir = TemporaryDirectory()

tests/testutils/data/usfm/Tes/Settings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<AllowReadAccess>F</AllowReadAccess>
2525
<AllowSharingWithSLDR>F</AllowSharingWithSLDR>
2626
<Visibility>Public</Visibility>
27-
<TranslationInfo>Standard::</TranslationInfo>
27+
<TranslationInfo>Standard:Tes:a7e0b3ce0200736062f9f810a444dbfbe64aca35</TranslationInfo>
2828
<EncodingConverter />
2929
<UsfmVersion>3</UsfmVersion>
3030
<ParallelPassagesBooks>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</ParallelPassagesBooks>

tests/testutils/memory_paratext_project_file_handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def create_stylesheet(self, file_name):
2626
class DefaultParatextProjectSettings(ParatextProjectSettings):
2727
def __init__(
2828
self,
29+
guid: str = "id",
2930
name: str = "Test",
3031
full_name: str = "TestProject",
3132
encoding: Optional[str] = None,
@@ -38,9 +39,13 @@ def __init__(
3839
biblical_terms_project_name: str = "Test",
3940
biblical_terms_file_name: str = "ProjectBiblicalTerms.xml",
4041
language_code: str = "en",
42+
translation_type: str = "Standard",
43+
parent_guid: Optional[str] = None,
44+
parent_name: Optional[str] = None,
4145
):
4246

4347
super().__init__(
48+
guid,
4449
name,
4550
full_name,
4651
encoding if encoding is not None else "utf-8",
@@ -53,4 +58,7 @@ def __init__(
5358
biblical_terms_project_name,
5459
biblical_terms_file_name,
5560
language_code,
61+
translation_type,
62+
parent_guid,
63+
parent_name,
5664
)

0 commit comments

Comments
 (0)