Skip to content

Commit d39c7b0

Browse files
committed
Use correct filename for custom stylesheet
- fix loading of custom stylesheet from zip archive - fixes #204
1 parent f1778e1 commit d39c7b0

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

machine/corpora/file_paratext_project_settings_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, project_dir: StrPath) -> None:
1111
self._project_dir = Path(project_dir)
1212

1313
def _create_stylesheet(self, file_name: StrPath) -> UsfmStylesheet:
14-
custom_stylesheet_filename = self._project_dir / file_name
14+
custom_stylesheet_filename = self._project_dir / "custom.sty"
1515
return UsfmStylesheet(
1616
file_name,
1717
custom_stylesheet_filename if custom_stylesheet_filename.is_file() else None,
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from tempfile import TemporaryFile
1+
import os
2+
from tempfile import mkstemp
23
from typing import Optional
34

45
from .paratext_project_settings_parser_base import ParatextProjectSettingsParserBase
@@ -7,15 +8,36 @@
78

89
class ZipParatextProjectSettingsParserBase(ParatextProjectSettingsParserBase):
910
def _create_stylesheet(self, file_name: str) -> UsfmStylesheet:
10-
with TemporaryFile() as stylesheet_temp_file, TemporaryFile() as custom_stylesheet_temp_file:
11+
stylesheet_temp_path: Optional[str] = None
12+
stylesheet_temp_fd: Optional[int] = None
13+
custom_stylesheet_temp_path: Optional[str] = None
14+
custom_stylesheet_temp_fd: Optional[int] = None
15+
try:
1116
stylesheet_path: str = file_name
1217
if self._exists(file_name):
13-
with self._open(file_name) as source:
18+
stylesheet_temp_fd, stylesheet_temp_path = mkstemp()
19+
with (
20+
self._open(file_name) as source,
21+
open(stylesheet_temp_fd, "wb", closefd=False) as stylesheet_temp_file,
22+
):
1423
stylesheet_temp_file.write(source.read())
15-
stylesheet_path = stylesheet_temp_file.name
24+
stylesheet_path = stylesheet_temp_path
1625
custom_stylesheet_path: Optional[str] = None
1726
if self._exists("custom.sty"):
18-
with self._open("custom.sty") as source:
27+
custom_stylesheet_temp_fd, custom_stylesheet_temp_path = mkstemp()
28+
with (
29+
self._open("custom.sty") as source,
30+
open(custom_stylesheet_temp_fd, "wb", closefd=False) as custom_stylesheet_temp_file,
31+
):
1932
custom_stylesheet_temp_file.write(source.read())
20-
custom_stylesheet_path = custom_stylesheet_temp_file.name
33+
custom_stylesheet_path = custom_stylesheet_temp_path
2134
return UsfmStylesheet(stylesheet_path, custom_stylesheet_path)
35+
finally:
36+
if stylesheet_temp_fd is not None:
37+
os.close(stylesheet_temp_fd)
38+
if stylesheet_temp_path is not None:
39+
os.remove(stylesheet_temp_path)
40+
if custom_stylesheet_temp_fd is not None:
41+
os.close(custom_stylesheet_temp_fd)
42+
if custom_stylesheet_temp_path is not None:
43+
os.remove(custom_stylesheet_temp_path)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from testutils.corpora_test_helpers import USFM_TEST_PROJECT_PATH
2+
3+
from machine.corpora import FileParatextProjectSettingsParser, UsfmStyleType, UsfmTextType
4+
5+
6+
def test_parse_custom_stylesheet() -> None:
7+
parser = FileParatextProjectSettingsParser(USFM_TEST_PROJECT_PATH)
8+
settings = parser.parse()
9+
test_tag = settings.stylesheet.get_tag("test")
10+
assert test_tag.style_type is UsfmStyleType.CHARACTER
11+
assert test_tag.text_type is UsfmTextType.OTHER
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
from tempfile import TemporaryDirectory
5+
from typing import Any, ContextManager
6+
from zipfile import ZipFile
7+
8+
from testutils.corpora_test_helpers import create_test_paratext_backup
9+
10+
from machine.corpora import UsfmStyleType, UsfmTextType, ZipParatextProjectSettingsParser
11+
12+
13+
def test_parse_custom_stylesheet() -> None:
14+
with _TestEnvironment() as env:
15+
settings = env.parser.parse()
16+
test_tag = settings.stylesheet.get_tag("test")
17+
assert test_tag.style_type is UsfmStyleType.CHARACTER
18+
assert test_tag.text_type is UsfmTextType.OTHER
19+
20+
21+
class _TestEnvironment(ContextManager["_TestEnvironment"]):
22+
def __init__(self) -> None:
23+
self._temp_dir = TemporaryDirectory()
24+
archive_filename = create_test_paratext_backup(Path(self._temp_dir.name))
25+
self._zip_file = ZipFile(archive_filename)
26+
self._parser = ZipParatextProjectSettingsParser(self._zip_file)
27+
28+
@property
29+
def parser(self) -> ZipParatextProjectSettingsParser:
30+
return self._parser
31+
32+
def __enter__(self) -> _TestEnvironment:
33+
return self
34+
35+
def __exit__(self, type: Any, value: Any, traceback: Any) -> None:
36+
self._zip_file.close()
37+
self._temp_dir.cleanup()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\Marker test
2+
\Endmarker test*
3+
\TextType Other
4+
\StyleType Character

0 commit comments

Comments
 (0)