Skip to content

Commit

Permalink
Improve model for BinData (#11)
Browse files Browse the repository at this point in the history
* Add special-case hacks for BinData class

In the schema BinData is an extension of string, but modeling this in python
as a subclass of str with extra constructor args is not feasible. Instead
this change moves the string content to a `value` attribute which aligns
with how our to_dict converter presents element text content.

Fixes #9

* Remove xfail from many sample xml files and tidy

* Now that BinData parses properly we can remove the xfail marker from
  about half of the sample ome-xml files
* Refactor filename stem extraction
* Invert SHOULD_PASS to SHOULD_FAIL for easier updating
* Remove +x mode bit from sample xml files
  • Loading branch information
jmuhlich authored Jul 21, 2020
1 parent 40d5492 commit 4dbadfa
Show file tree
Hide file tree
Showing 34 changed files with 37 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/ome_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def local_import(item_type):

def make_dataclass(component) -> List[str]:
lines = ["from pydantic.dataclasses import dataclass", ""]
if isinstance(component, XsdType):
# FIXME: Refactor to remove BinData special-case.
if component.local_name == "BinData":
base_type = None
elif isinstance(component, XsdType):
base_type = component.base_type
else:
base_type = component.type.base_type
Expand Down Expand Up @@ -105,6 +108,9 @@ def make_dataclass(component) -> List[str]:
lines += ["_no_default = object()", ""]

lines += ["@dataclass", f"class {component.local_name}{base_name}:"]
# FIXME: Refactor to remove BinData special-case.
if component.local_name == "BinData":
lines.append(" value: str")
lines += members.lines(
indent=1,
force_defaults=" = _no_default # type: ignore"
Expand Down Expand Up @@ -497,7 +503,8 @@ def _abstract_class(self) -> List[str]:
return lines

def lines(self) -> str:
if not self.is_complex:
# FIXME: Refactor to remove BinData special-case.
if not self.is_complex and self.elem.local_name != "BinData":
lines = self._simple_class()
elif self.elem.abstract:
lines = self._abstract_class()
Expand Down
Empty file modified testing/data/ROI.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/commentannotation.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/filter.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/folders-larger-taxonomy.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/folders-simple-taxonomy.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/hcs.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/instrument-units-alternate.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/instrument-units-default.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/instrument.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/mapannotation.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/metadata-only.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/minimum-specification.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/multi-channel-time-series.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/multi-channel-z-series-time-series.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/multi-channel-z-series.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/multi-channel.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/no-date.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/one-screen-one-plate-four-wells.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/single-image.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/spim.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/tagannotation.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/time-series.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/timestampannotation-posix-only.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/timestampannotation.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/transformations-downgrade.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/transformations-upgrade.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/two-screens-two-plates-four-wells.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/xmlannotation-body-space.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/xmlannotation-multi-value.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/xmlannotation-svg.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/z-series-time-series.ome.xml
100755 → 100644
Empty file.
Empty file modified testing/data/z-series.ome.xml
100755 → 100644
Empty file.
34 changes: 28 additions & 6 deletions testing/test_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,27 @@ def model(tmp_path_factory):
return importlib.import_module(target_dir.name)


SHOULD_PASS = {"example.ome", "small.ome"}
SHOULD_RAISE = {"bad.ome"}
SHOULD_FAIL = {
"ROI",
"commentannotation",
"filter",
"hcs",
"instrument",
"instrument-units-alternate",
"instrument-units-default",
"mapannotation",
"metadata-only",
"spim",
"tagannotation",
"timestampannotation",
"timestampannotation-posix-only",
"transformations-downgrade",
"transformations-upgrade",
"xmlannotation-body-space",
"xmlannotation-multi-value",
"xmlannotation-svg",
}
SHOULD_RAISE = {"bad"}


def mark_xfail(fname):
Expand All @@ -35,16 +54,19 @@ def mark_xfail(fname):
),
)

def true_stem(p):
return p.name.partition(".")[0]


params = [
f if f.stem in SHOULD_PASS.union(SHOULD_RAISE) else mark_xfail(f)
for f in TESTING_DIR.glob("data/*.xml")
mark_xfail(f) if true_stem(f) in SHOULD_FAIL else f
for f in TESTING_DIR.glob("data/*.ome.xml")
]


@pytest.mark.parametrize("xml", params, ids=lambda x: x.stem)
@pytest.mark.parametrize("xml", params, ids=true_stem)
def test_convert_schema(model, xml):
if xml.stem in SHOULD_RAISE:
if true_stem(xml) in SHOULD_RAISE:
with pytest.raises(XMLSchemaValidationError):
assert from_xml(xml, model.OME)
else:
Expand Down

0 comments on commit 4dbadfa

Please sign in to comment.