Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recommendations": [
"ms-python.python",
"ms-python.debugpy",
"detachhead.basedpyright",
"charliermarsh.ruff",
],
"unwantedRecommendations": []
}
23 changes: 18 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv",
"python.testing.pytestEnabled": true,
"python.experiments.optInto": [
"pythonTestAdapter"
],
"python.defaultInterpreterPath": "${workspaceFolder}/.venv",
"python.experiments.enabled": false,
"python.experiments.optInto": [
"pythonTestAdapter"
],
"python.testing.pytestEnabled": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
},
},
"ruff.organizeImports": false,
}
5 changes: 3 additions & 2 deletions magicparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Builder,
builtins as builtins_composite_processors,
)
from .transform import Transform
from .transform import ParsingTransform, Transform
from .type_converters import TypeConverter, builtins as builtins_type_converters
from typing import Any
from .validators import Validator, builtins as builtins_validators
Expand All @@ -30,6 +30,7 @@
"RowParsed",
"RowSkipped",
"RowFailed",
"Transform",
"Validator",
]

Expand All @@ -44,7 +45,7 @@ def stream_parse(data: bytes | BytesIO, schema_options: dict[str, Any]) -> Itera
return schema_definition.stream_parse(data)


Registrable = type[Schema] | type[Transform]
Registrable = type[Schema] | type[ParsingTransform]


def register(items: Registrable | Sequence[Registrable]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions magicparse/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from decimal import Decimal
from typing import Any, cast

from .transform import Transform, OnError
from .transform import ParsingTransform, OnError


class Builder(Transform, ABC):
class Builder(ParsingTransform, ABC):
registry = dict[str, type["Builder"]]()

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions magicparse/post_processors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .transform import Transform, OnError
from .transform import ParsingTransform, OnError
from decimal import Decimal
from typing import Any


class PostProcessor(Transform):
class PostProcessor(ParsingTransform):
registry = dict[str, type["PostProcessor"]]()

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions magicparse/pre_processors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import re
from typing import Any
from .transform import Transform, OnError
from .transform import ParsingTransform, OnError


class PreProcessor(Transform):
class PreProcessor(ParsingTransform):
registry = dict[str, type["PreProcessor"]]()

@classmethod
Expand Down
11 changes: 9 additions & 2 deletions magicparse/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dataclasses import dataclass
from enum import StrEnum
from typing import Any, Self
from jsonata import Jsonata # pyright: ignore[reportMissingTypeStubs]


@dataclass(frozen=True, slots=True)
Expand All @@ -22,15 +23,15 @@ class OnError(StrEnum):
SKIP_ROW = "skip-row"


class Transform(ABC):
class ParsingTransform(ABC):
registry: dict[str, type[Self]]

def __init__(self, on_error: OnError) -> None:
self.on_error = on_error

@classmethod
@abstractmethod
def build(cls, options: dict[str, Any]) -> "Transform":
def build(cls, options: dict[str, Any]) -> "ParsingTransform":
pass

@abstractmethod
Expand All @@ -45,3 +46,9 @@ def key() -> str:
@classmethod
def register(cls, transform: type[Self]) -> None:
cls.registry[transform.key()] = transform


class Transform(Jsonata):
@classmethod
def build(cls, expression: str) -> "Transform":
return Transform(expr=expression)
6 changes: 3 additions & 3 deletions magicparse/type_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from decimal import Decimal
from typing import Any, cast

from .transform import Transform
from .transform import ParsingTransform
from .transform import OnError


class TypeConverter(Transform):
class TypeConverter(ParsingTransform):
registry = dict[str, type["TypeConverter"]]()

def __init__(self, nullable: bool, on_error: OnError) -> None:
Expand All @@ -28,7 +28,7 @@ def convert(self, value: str) -> Any:
pass

@classmethod
def build(cls, options: dict[str, Any]) -> "Transform":
def build(cls, options: dict[str, Any]) -> "ParsingTransform":
try:
type = cast(str | dict[str, Any], options["type"])
if isinstance(type, str):
Expand Down
4 changes: 2 additions & 2 deletions magicparse/validators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from decimal import Decimal
from typing import Any
from .transform import Transform, OnError
from .transform import ParsingTransform, OnError
import re


class Validator(Transform):
class Validator(ParsingTransform):
registry = dict[str, type["Validator"]]()

@classmethod
Expand Down
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/ZeroGachis/magicparse"

[tool.poetry.dependencies]
python = "^3.13"
jsonata-python = "^0.6.1"

[tool.poetry.group.dev.dependencies]
pytest = "^9"
Expand Down