Skip to content

Commit

Permalink
feat: add stub generic (de)serializer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaraney committed Aug 26, 2024
1 parent f977915 commit 447aef8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/ngen_init_config/src/ngen/init_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
YamlDeserializer,
TomlDeserializer,
JsonDeserializer,
GenericDeserializer,
)
from .serializer import (
IniSerializer,
NamelistSerializer,
YamlSerializer,
TomlSerializer,
JsonSerializer,
GenericSerializer,
)

from .serializer_deserializer import (
Expand All @@ -23,4 +25,5 @@
YamlSerializerDeserializer,
TomlSerializerDeserializer,
JsonSerializerDeserializer,
GenericSerializerDeserializer,
)
20 changes: 20 additions & 0 deletions python/ngen_init_config/src/ngen/init_config/deserializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod

from .core import Base
from .utils import merge_class_attr
from ._deserializers import (
Expand Down Expand Up @@ -105,3 +107,21 @@ def from_json(cls, p: Path) -> Self:
@classmethod
def from_json_str(cls, s: str) -> Self:
return cls.parse_raw(s)


class GenericDeserializer(Base, ABC):
"""
Stub for deserializing from a generic format.
Subclasses must implement the `from_file` and `from_str` abstract class methods.
See `ngen.init_config.core.Base` and `pydantic`'s documentation for configuration options.
"""

@classmethod
@abstractmethod
def from_file(cls, p: Path, *_) -> Self:
...

@classmethod
@abstractmethod
def from_str(cls, s: str, *_) -> Self:
...
17 changes: 17 additions & 0 deletions python/ngen_init_config/src/ngen/init_config/serializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
from abc import ABC, abstractmethod

from . import core, format_serializers, utils
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -146,3 +147,19 @@ def to_json_str(self, *, indent: int = 0) -> str:
options = {} if not indent else {"indent": indent}
# remove trailing eol
return self.json(by_alias=True, **options).rstrip()


class GenericSerializer(core.Base, ABC):
"""
Stub for serializing a generic format.
Subclasses must implement the `to_file` and `to_str` abstract class methods.
See `ngen.init_config.core.Base` and `pydantic`'s documentation for configuration options.
"""

@abstractmethod
def to_file(self, p: pathlib.Path, *_) -> None:
...

@abstractmethod
def to_str(self, *_) -> str:
...
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from abc import ABC

from . import serializer as ser
from . import deserializer as de

Expand Down Expand Up @@ -70,3 +72,13 @@ class JsonSerializerDeserializer(ser.JsonSerializer, de.JsonDeserializer):
"""

...


class GenericSerializerDeserializer(ser.GenericSerializer, de.GenericDeserializer, ABC):
"""
Stub for deserializing from and serializing to a generic format.
Subclasses must implement the `to_file`, `to_str`, `from_file`, and `from_str` abstract methods.
See `ngen.init_config.core.Base` and `pydantic`'s documentation for configuration options.
"""

...

0 comments on commit 447aef8

Please sign in to comment.