Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an attrs-based parser API #55

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ strict_equality = True
warn_redundant_casts = True
warn_return_any = True
warn_unreachable = True
plugins = headerparser.mypy
25 changes: 23 additions & 2 deletions src/headerparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .errors import (
BodyNotAllowedError,
DuplicateBodyError,
DuplicateFieldError,
Error,
FieldTypeError,
Expand All @@ -29,6 +30,15 @@
UnknownFieldError,
)
from .normdict import NormalizedDict
from .parscls import (
BodyField,
ExtraFields,
Field,
MultiExtraFields,
MultiField,
parsable,
parse,
)
from .parser import HeaderParser
from .scanner import (
Scanner,
Expand All @@ -39,7 +49,7 @@
scan_stanzas_string,
scan_string,
)
from .types import BOOL, lower, unfold
from .types import BOOL, decode_bool, decode_value, lower, multidict, unfold

__version__ = "0.5.1"
__author__ = "John Thorvald Wodder II"
Expand All @@ -49,23 +59,34 @@

__all__ = [
"BOOL",
"BodyField",
"BodyNotAllowedError",
"DuplicateBodyError",
"DuplicateFieldError",
"Error",
"HeaderParser",
"ExtraFields",
"Field",
"FieldTypeError",
"HeaderParser",
"InvalidChoiceError",
"MalformedHeaderError",
"MissingBodyError",
"MissingFieldError",
"MultiExtraFields",
"MultiField",
"NormalizedDict",
"ParserError",
"Scanner",
"ScannerEOFError",
"ScannerError",
"UnexpectedFoldingError",
"UnknownFieldError",
"decode_bool",
"decode_value",
"lower",
"multidict",
"parsable",
"parse",
"scan",
"scan_next_stanza",
"scan_next_stanza_string",
Expand Down
7 changes: 7 additions & 0 deletions src/headerparser/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def __str__(self) -> str:
return f"Header field {self.name!r} occurs more than once"


class DuplicateBodyError(ParserError):
"""Raised when a body field occurs two or more times in the input"""

def __str__(self) -> str:
return "Body field occurs more than once"


class FieldTypeError(ParserError):
"""Raised when a ``type`` callable raises an exception"""

Expand Down
19 changes: 19 additions & 0 deletions src/headerparser/mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations
from mypy.plugin import Plugin
from mypy.plugins.attrs import attr_attrib_makers, attr_define_makers

attr_define_makers.add("headerparser.parscls.parsable")

attr_attrib_makers.add("headerparser.parscls.Field")
attr_attrib_makers.add("headerparser.parscls.MultiField")
attr_attrib_makers.add("headerparser.parscls.ExtraFields")
attr_attrib_makers.add("headerparser.parscls.MultiExtraFields")
attr_attrib_makers.add("headerparser.parscls.BodyField")


class HeaderParserPlugin(Plugin):
pass


def plugin(_version: str) -> type[Plugin]:
return HeaderParserPlugin
Loading