Skip to content

Commit

Permalink
feat(excel2xml): make_bitstream_prop(): make file existence check opt…
Browse files Browse the repository at this point in the history
…-in (DEV-3113) (#709)
  • Loading branch information
jnussbaum committed Jan 4, 2024
1 parent 111c6df commit 1f68943
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
13 changes: 6 additions & 7 deletions src/dsp_tools/commands/excel2xml/excel2xml_cli.py
Expand Up @@ -200,14 +200,13 @@ def _append_bitstream_to_resource(
f"'{row['file']}' (Resource ID '{row['id']}', Excel row {row_number}). "
f"An attempt to deduce them from the resource permissions failed."
)
with warnings.catch_warnings(): # ignore warnings about non-existing files
resource.append(
make_bitstream_prop(
path=str(row["file"]),
permissions=str(file_permissions),
calling_resource=row["id"],
)
resource.append(
make_bitstream_prop(
path=str(row["file"]),
permissions=str(file_permissions),
calling_resource=row["id"],
)
)
return resource


Expand Down
6 changes: 4 additions & 2 deletions src/dsp_tools/commands/excel2xml/excel2xml_lib.py
Expand Up @@ -447,6 +447,7 @@ def make_resource( # noqa: D417 (undocumented-param)
def make_bitstream_prop(
path: Union[str, os.PathLike[Any]],
permissions: str = "prop-default",
check: bool = False,
calling_resource: str = "",
) -> etree._Element:
"""
Expand All @@ -455,10 +456,11 @@ def make_bitstream_prop(
Args:
path: path to a valid file that will be uploaded
permissions: permissions string
check: if True, issue a warning if the path doesn't point to an existing file
calling_resource: the name of the parent resource (for better error messages)
Raises:
Warning: if the path doesn't point to an existing file
Warning: if the path doesn't point to an existing file (only if check=True)
Returns:
an etree._Element that can be appended to the parent resource with resource.append(make_*_prop(...))
Expand All @@ -471,7 +473,7 @@ def make_bitstream_prop(
See https://docs.dasch.swiss/latest/DSP-TOOLS/file-formats/xml-data-file/#bitstream
"""

if not Path(path).is_file():
if check and not Path(path).is_file():
warnings.warn(
f"Failed validation in bitstream tag of resource '{calling_resource}': "
f"The following path doesn't point to a file: {path}",
Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/import_scripts
62 changes: 36 additions & 26 deletions test/unittests/commands/excel2xml/test_excel2xml_lib.py
@@ -1,4 +1,5 @@
import unittest
import warnings
from pathlib import Path
from typing import Any, Callable, Optional, Sequence, Union

Expand Down Expand Up @@ -341,32 +342,41 @@ def test_prepare_value(self) -> None:
values_output = excel2xml.prepare_value([excel2xml.PropertyElement(x) for x in values_with_nas])
self.assertEqual([x.value for x in values_output], values_with_nas)

def test_make_bitstream_prop(self) -> None:
path_string = "foo/bar/baz.txt"
path_path = Path(path_string)
test_cases: list[tuple[str, Callable[..., etree._Element]]] = [
(
'<bitstream permissions="prop-default">foo/bar/baz.txt</bitstream>',
lambda: excel2xml.make_bitstream_prop(path_string),
),
(
'<bitstream permissions="prop-default">foo/bar/baz.txt</bitstream>',
lambda: excel2xml.make_bitstream_prop(path_path),
),
(
'<bitstream permissions="prop-restricted">foo/bar/baz.txt</bitstream>',
lambda: excel2xml.make_bitstream_prop(path_string, "prop-restricted"),
),
(
'<bitstream permissions="prop-restricted">foo/bar/baz.txt</bitstream>',
lambda: excel2xml.make_bitstream_prop(path_path, "prop-restricted"),
),
]
for expected, method_call in test_cases:
with self.assertWarnsRegex(UserWarning, ".*Failed validation in bitstream tag.*"):
result = etree.tostring(method_call(), encoding="unicode")
result = regex.sub(r" xmlns(:.+?)?=\".+?\"", "", result)
self.assertEqual(result, expected)
def test_make_bitstream_prop_from_string(self) -> None:
res = excel2xml.make_bitstream_prop("foo/bar/baz.txt")
assert res.tag.endswith("bitstream")
assert res.attrib["permissions"] == "prop-default"
assert res.text == "foo/bar/baz.txt"

def test_make_bitstream_prop_from_path(self) -> None:
res = excel2xml.make_bitstream_prop(Path("foo/bar/baz.txt"))
assert res.tag.endswith("bitstream")
assert res.attrib["permissions"] == "prop-default"
assert res.text == "foo/bar/baz.txt"

def test_make_bitstream_prop_custom_permissions(self) -> None:
res = excel2xml.make_bitstream_prop("foo/bar/baz.txt", "prop-restricted")
assert res.tag.endswith("bitstream")
assert res.attrib["permissions"] == "prop-restricted"
assert res.text == "foo/bar/baz.txt"

def test_make_bitstream_prop_valid_file(self) -> None:
with warnings.catch_warnings():
warnings.filterwarnings("error", category=UserWarning)
try:
res = excel2xml.make_bitstream_prop("testdata/bitstreams/test.jpg", check=True)
except UserWarning as e:
raise AssertionError from e
assert res.tag.endswith("bitstream")
assert res.attrib["permissions"] == "prop-default"
assert res.text == "testdata/bitstreams/test.jpg"

def test_make_bitstream_prop_invalid_file(self) -> None:
with pytest.warns(UserWarning, match=".*Failed validation in bitstream tag.*"):
res = excel2xml.make_bitstream_prop("foo/bar/baz.txt", check=True)
assert res.tag.endswith("bitstream")
assert res.attrib["permissions"] == "prop-default"
assert res.text == "foo/bar/baz.txt"

def test_make_boolean_prop(self) -> None:
# prepare true_values
Expand Down

0 comments on commit 1f68943

Please sign in to comment.