Skip to content

Commit

Permalink
Merge pull request #270 from mlte-team/feature/enum-serialization
Browse files Browse the repository at this point in the history
Refactor Enum Serialization
  • Loading branch information
turingcompl33t authored Oct 5, 2023
2 parents b4601db + cbfd19b commit 043146a
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 53 deletions.
3 changes: 3 additions & 0 deletions mlte/artifact/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ArtifactHeaderModel(BaseModel):
timestamp: Optional[int] = -1
"""The timestamp of creation of this artifact, as Unix time."""

class Config:
use_enum_values = True


class ArtifactModel(BaseModel):
"""The base model for all MLTE artifacts."""
Expand Down
12 changes: 6 additions & 6 deletions mlte/artifact/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
the artifact base model, individual artifact models, and the type enum.
"""

from enum import Enum, auto
from enum import Enum


class ArtifactType(str, Enum):
"""Enumerates all supported artifact types."""

NEGOTIATION_CARD = auto()
NEGOTIATION_CARD = "negotiation_card"
"""The negotiation card artifact type."""

VALUE = auto()
VALUE = "value"
"""The value card artifact type."""

SPEC = auto()
SPEC = "spec"
"""The specification artifact type."""

VALIDATED_SPEC = auto()
VALIDATED_SPEC = "validated_spec"
"""The validated specification artifact type."""

REPORT = auto()
REPORT = "report"
"""The report artifact type."""
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@
"description": "The model implementation for the NegotiationCard artifact.",
"properties": {
"artifact_type": {
"const": "1",
"const": "negotiation_card",
"title": "Artifact Type"
},
"system": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@
"description": "The model implementation for the MLTE report artifact.",
"properties": {
"artifact_type": {
"const": "5",
"const": "report",
"title": "Artifact Type"
},
"summary": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"description": "The model implementation for the Spec artifact.",
"properties": {
"artifact_type": {
"const": "3",
"const": "spec",
"title": "Artifact Type"
},
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"description": "The model implementation for the ValidatedSpec artifact.",
"properties": {
"artifact_type": {
"const": "4",
"const": "validated_spec",
"title": "Artifact Type"
},
"spec_identifier": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"description": "The model implementation for MLTE image values.",
"properties": {
"value_type": {
"const": "4",
"const": "image",
"title": "Value Type"
},
"data": {
Expand All @@ -67,7 +67,7 @@
"description": "The model implementation for MLTE integer values.",
"properties": {
"value_type": {
"const": "1",
"const": "integer",
"title": "Value Type"
},
"integer": {
Expand All @@ -86,7 +86,7 @@
"description": "The model implementation for MLTE opaque values.",
"properties": {
"value_type": {
"const": "3",
"const": "opaque",
"title": "Value Type"
},
"data": {
Expand All @@ -105,7 +105,7 @@
"description": "The model implementation for MLTE real values.",
"properties": {
"value_type": {
"const": "2",
"const": "real",
"title": "Value Type"
},
"real": {
Expand All @@ -124,7 +124,7 @@
"description": "The model implementation for MLTE values.",
"properties": {
"artifact_type": {
"const": "2",
"const": "value",
"title": "Artifact Type"
},
"metadata": {
Expand All @@ -133,10 +133,10 @@
"value": {
"discriminator": {
"mapping": {
"1": "#/$defs/IntegerValueModel",
"2": "#/$defs/RealValueModel",
"3": "#/$defs/OpaqueValueModel",
"4": "#/$defs/ImageValueModel"
"image": "#/$defs/ImageValueModel",
"integer": "#/$defs/IntegerValueModel",
"opaque": "#/$defs/OpaqueValueModel",
"real": "#/$defs/RealValueModel"
},
"propertyName": "value_type"
},
Expand Down
2 changes: 1 addition & 1 deletion mlte/schema/artifact/negotiation/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@
"description": "The model implementation for the NegotiationCard artifact.",
"properties": {
"artifact_type": {
"const": "1",
"const": "negotiation_card",
"title": "Artifact Type"
},
"system": {
Expand Down
2 changes: 1 addition & 1 deletion mlte/schema/artifact/report/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@
"description": "The model implementation for the MLTE report artifact.",
"properties": {
"artifact_type": {
"const": "5",
"const": "report",
"title": "Artifact Type"
},
"summary": {
Expand Down
2 changes: 1 addition & 1 deletion mlte/schema/artifact/spec/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"description": "The model implementation for the Spec artifact.",
"properties": {
"artifact_type": {
"const": "3",
"const": "spec",
"title": "Artifact Type"
},
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion mlte/schema/artifact/validated/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"description": "The model implementation for the ValidatedSpec artifact.",
"properties": {
"artifact_type": {
"const": "4",
"const": "validated_spec",
"title": "Artifact Type"
},
"spec_identifier": {
Expand Down
18 changes: 9 additions & 9 deletions mlte/schema/artifact/value/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"description": "The model implementation for MLTE image values.",
"properties": {
"value_type": {
"const": "4",
"const": "image",
"title": "Value Type"
},
"data": {
Expand All @@ -67,7 +67,7 @@
"description": "The model implementation for MLTE integer values.",
"properties": {
"value_type": {
"const": "1",
"const": "integer",
"title": "Value Type"
},
"integer": {
Expand All @@ -86,7 +86,7 @@
"description": "The model implementation for MLTE opaque values.",
"properties": {
"value_type": {
"const": "3",
"const": "opaque",
"title": "Value Type"
},
"data": {
Expand All @@ -105,7 +105,7 @@
"description": "The model implementation for MLTE real values.",
"properties": {
"value_type": {
"const": "2",
"const": "real",
"title": "Value Type"
},
"real": {
Expand All @@ -124,7 +124,7 @@
"description": "The model implementation for MLTE values.",
"properties": {
"artifact_type": {
"const": "2",
"const": "value",
"title": "Artifact Type"
},
"metadata": {
Expand All @@ -133,10 +133,10 @@
"value": {
"discriminator": {
"mapping": {
"1": "#/$defs/IntegerValueModel",
"2": "#/$defs/RealValueModel",
"3": "#/$defs/OpaqueValueModel",
"4": "#/$defs/ImageValueModel"
"image": "#/$defs/ImageValueModel",
"integer": "#/$defs/IntegerValueModel",
"opaque": "#/$defs/OpaqueValueModel",
"real": "#/$defs/RealValueModel"
},
"propertyName": "value_type"
},
Expand Down
22 changes: 17 additions & 5 deletions mlte/value/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import annotations

from enum import Enum, auto
from enum import Enum
from typing import Any, Dict, Literal, Union

from pydantic import Field
Expand All @@ -19,16 +19,16 @@
class ValueType(str, Enum):
"""An enumeration over supported value types."""

INTEGER = auto()
INTEGER = "integer"
"""An integral type."""

REAL = auto()
REAL = "real"
"""A real type."""

OPAQUE = auto()
OPAQUE = "opaque"
"""An opaque type."""

IMAGE = auto()
IMAGE = "image"
"""An image media type."""


Expand Down Expand Up @@ -59,6 +59,9 @@ class IntegerValueModel(BaseModel):
integer: int
"""The encapsulated value."""

class Config:
use_enum_values = True


class RealValueModel(BaseModel):
"""The model implementation for MLTE real values."""
Expand All @@ -69,6 +72,9 @@ class RealValueModel(BaseModel):
real: float
"""The encapsulated value."""

class Config:
use_enum_values = True


class OpaqueValueModel(BaseModel):
"""The model implementation for MLTE opaque values."""
Expand All @@ -79,6 +85,9 @@ class OpaqueValueModel(BaseModel):
data: Dict[str, Any]
"""Encapsulated, opaque data."""

class Config:
use_enum_values = True


class ImageValueModel(BaseModel):
"""The model implementation for MLTE image values."""
Expand All @@ -89,5 +98,8 @@ class ImageValueModel(BaseModel):
data: str
"""The image data as base64-encoded string."""

class Config:
use_enum_values = True


ValueModel.model_rebuild()
23 changes: 8 additions & 15 deletions testbed/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,23 @@

sys.path.append(package_root())

import mlte
import mlte.api as api
from mlte.measurement.storage import LocalObjectSize
from mlte.value.types.integer import Integer
from mlte.negotiation.artifact import NegotiationCard
from mlte.session import set_context, set_store

# Script exit codes
EXIT_SUCCESS = 0
EXIT_FAILURE = 1


def main() -> int:
here = os.path.abspath(os.getcwd())
here = os.path.join(here, "deleteme")
uri = f"local://{here}"
store_path = os.path.join(os.getcwd(), "store")
os.makedirs(store_path, exist_ok=True)

mlte.set_model("FakeModel", "0.0.1")
mlte.set_artifact_store_uri(uri)
set_context("ns", "IrisClassifier", "0.0.1")
set_store(f"local://{store_path}")

m = LocalObjectSize("file size")
r: Integer = m.evaluate(os.path.abspath(__file__))
r.save()

result = api.read_value(uri, "FakeModel", "0.0.1", "file size")
print(result)
card = NegotiationCard()
card.save(force=True, parents=True)

return EXIT_SUCCESS

Expand Down
2 changes: 1 addition & 1 deletion testbed/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def package_root() -> str:
path = os.path.dirname(os.path.abspath(__file__))
while os.path.basename(os.path.abspath(path)) != "testbed":
path = os.path.join(path, "..")
return os.path.abspath(os.path.join(path, "..", "src/"))
return os.path.abspath(os.path.join(path, "..", "mlte/"))

0 comments on commit 043146a

Please sign in to comment.