Skip to content

Commit 4073d2d

Browse files
authored
refactor: Factoring specification revision and extensions out of Model… (#189)
refactor: Factoring specification revision and extensions out of ModelParsingContextInterface into a public data class for use in evaluating supported behaviors and capabilities. Signed-off-by: Brian Axelson <86568017+baxeaz@users.noreply.github.com>
1 parent 460656a commit 4073d2d

2 files changed

Lines changed: 87 additions & 7 deletions

File tree

src/openjd/model/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
OpenJDModel,
4040
ParameterValue,
4141
ParameterValueType,
42+
RevisionExtensions,
4243
SpecificationRevision,
4344
Step,
4445
StepParameterSpace,
@@ -75,6 +76,7 @@
7576
"OpenJDModel",
7677
"ParameterValue",
7778
"ParameterValueType",
79+
"RevisionExtensions",
7880
"SpecificationRevision",
7981
"Step",
8082
"StepDependencyGraph",

src/openjd/model/_types.py

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,17 @@ def _check_constraints(self, value: Any) -> None:
323323
pass
324324

325325

326-
class ModelParsingContextInterface(ABC):
327-
"""Context required while parsing an OpenJDModel. A subclass
328-
must be provided when calling model_validate.
326+
class RevisionExtensions:
327+
"""
328+
Data class for representing a specific OpenJD Specification Revision and set of extensions
329+
in order to evaluate supported capabilities.
329330
330-
OpenJDModelSubclass.model_validate(data, context=ModelParsingContext())
331+
This class encapsulates both the specification revision and the set of extensions that are
332+
supported or requested by a template.
331333
332-
Individual validators receive this value as ValidationInfo.context.
334+
Attributes:
335+
spec_rev: The revision of the Open Job Description specification being used.
336+
extensions: The set of extension names that are supported or requested.
333337
"""
334338

335339
spec_rev: SpecificationRevision
@@ -343,14 +347,88 @@ class ModelParsingContextInterface(ABC):
343347
The 'extensions' field is second in the list of model properties for both the job template
344348
and environment template, and when that field is processed it becomes the set of extensions
345349
that the template requested.
350+
"""
351+
352+
def __init__(
353+
self, *, spec_rev: SpecificationRevision, supported_extensions: Optional[Iterable[str]]
354+
) -> None:
355+
"""
356+
Initialize a RevisionExtensions instance.
357+
358+
Args:
359+
spec_rev: The specification revision to use.
360+
supported_extensions: An optional iterable of extension names that are supported.
361+
If None, an empty set will be used.
362+
"""
363+
self.spec_rev = spec_rev
364+
self.extensions = set(supported_extensions or [])
365+
366+
367+
class ModelParsingContextInterface(ABC):
368+
"""Context required while parsing an OpenJDModel. A subclass
369+
must be provided when calling model_validate.
370+
371+
OpenJDModelSubclass.model_validate(data, context=ModelParsingContext())
372+
373+
Individual validators receive this value as ValidationInfo.context.
374+
375+
This interface defines the contract for model parsing contexts across different
376+
specification revisions. It provides access to the specification revision and
377+
extensions that are supported or requested by a template, which allows validators
378+
to adjust their behavior based on the specification version and enabled extensions.
379+
"""
380+
381+
revision_extensions: RevisionExtensions
382+
"""Contains information about the specification revision and supported extensions.
383+
This allows shared code like the FormatString class to perform version-specific
384+
processing and extension-dependent validation.
346385
347386
When fields of a model that depend on an extension are processed, its validators should
348387
check whether the needed extension is included in the context and adjust its parsing
349388
as written in the specification.
350389
"""
351390

391+
@property
392+
def spec_rev(self) -> SpecificationRevision:
393+
"""
394+
Get the specification revision being used.
395+
396+
Returns:
397+
The specification revision from the revision_extensions.
398+
"""
399+
return self.revision_extensions.spec_rev
400+
401+
@property
402+
def extensions(self) -> set[str]:
403+
"""
404+
Get the set of supported extensions.
405+
406+
Returns:
407+
The set of extension names from the revision_extensions.
408+
"""
409+
return self.revision_extensions.extensions
410+
411+
@extensions.setter
412+
def extensions(self, extension_set: set[str]):
413+
"""
414+
Set the supported extensions.
415+
416+
Args:
417+
extension_set: The new set of extension names to use.
418+
"""
419+
self.revision_extensions.extensions = extension_set
420+
352421
def __init__(
353422
self, *, spec_rev: SpecificationRevision, supported_extensions: Optional[Iterable[str]]
354423
) -> None:
355-
self.spec_rev = spec_rev
356-
self.extensions = set(supported_extensions or [])
424+
"""
425+
Initialize a ModelParsingContextInterface instance.
426+
427+
Args:
428+
spec_rev: The specification revision to use.
429+
supported_extensions: An optional iterable of extension names that are supported.
430+
If None, an empty set will be used.
431+
"""
432+
self.revision_extensions = RevisionExtensions(
433+
spec_rev=spec_rev, supported_extensions=supported_extensions
434+
)

0 commit comments

Comments
 (0)