Skip to content

Commit

Permalink
Renames POF to FormatOptions and fixes gram. issue
Browse files Browse the repository at this point in the history
  • Loading branch information
funkyfuture committed Sep 20, 2024
1 parent 348d9ba commit e073716
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 116 deletions.
55 changes: 26 additions & 29 deletions _delb/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@ class NodeBase(ABC):

def __str__(self) -> str:
return self.serialize(
format_options=DefaultStringOptions.format_options,
namespaces=DefaultStringOptions.namespaces,
newline=DefaultStringOptions.newline,
pretty_format_options=DefaultStringOptions.pretty_format_options,
)

def add_following_siblings(self, *node: NodeSource, clone: bool = False):
Expand Down Expand Up @@ -1274,27 +1274,27 @@ def replace_with(self, node: NodeSource, clone: bool = False) -> NodeBase:
def serialize(
self,
*,
format_options: Optional[FormatOptions] = None,
namespaces: Optional[NamespaceDeclarations] = None,
newline: Optional[str] = None,
pretty_format_options: Optional[PrettyFormatOptions] = None,
):
"""
Returns a string that contains the serialization of the node. See
:doc:`/api/serialization` for details.
:param format_options: An instance of :class:`FormatOptions` can be provided to
configure formatting.
:param namespaces: A mapping of prefixes to namespaces. These are overriding
possible declarations from a parsed serialisat that the
document instance stems from. Prefixes for undeclared
namespaces are enumerated with the prefix ``ns``.
:param newline: See :class:`io.TextIOWrapper` for a detailed explanation of the
parameter with the same name.
:param pretty_format_options: An instance of :class:`PrettyFormatOptions` can be
provided to configure formatting.
"""
serializer = _get_serializer(
_StringWriter(newline=newline),
format_options=format_options,
namespaces=namespaces,
pretty_format_options=pretty_format_options,
)
with _wrapper_cache:
serializer.serialize_node(self)
Expand Down Expand Up @@ -2475,14 +2475,14 @@ def _reduce_whitespace_content(
def serialize(
self,
*,
format_options: Optional[FormatOptions] = None,
namespaces: Optional[NamespaceDeclarations] = None,
newline: Optional[str] = None,
pretty_format_options: Optional[PrettyFormatOptions] = None,
):
serializer = _get_serializer(
_StringWriter(newline=newline),
format_options=format_options,
namespaces=namespaces,
pretty_format_options=pretty_format_options,
)
with _wrapper_cache:
serializer.serialize_root(self)
Expand Down Expand Up @@ -3115,31 +3115,28 @@ def not_wrapper(node: NodeBase) -> bool:

def _get_serializer(
writer: _SerializationWriter,
format_options: Optional[FormatOptions],
namespaces: Optional[NamespaceDeclarations],
pretty_format_options: Optional[PrettyFormatOptions],
) -> Serializer:
if pretty_format_options is None:
if format_options is None:
return Serializer(
writer=writer,
namespaces=namespaces,
)

if (
pretty_format_options.indentation
and not pretty_format_options.indentation.isspace()
):
if format_options.indentation and not format_options.indentation.isspace():
raise ValueError("Invalid indentation characters.")

if pretty_format_options.text_width:
if format_options.text_width:
return TextWrappingSerializer(
writer=writer,
pretty_format_options=pretty_format_options,
format_options=format_options,
namespaces=namespaces,
)
else:
return PrettySerializer(
writer=writer,
pretty_format_options=pretty_format_options,
format_options=format_options,
namespaces=namespaces,
)

Expand Down Expand Up @@ -3343,13 +3340,13 @@ class PrettySerializer(Serializer):
def __init__(
self,
writer: _SerializationWriter,
pretty_format_options: PrettyFormatOptions,
format_options: FormatOptions,
*,
namespaces: Optional[NamespaceDeclarations] = None,
):
super().__init__(writer, namespaces=namespaces)
self._align_attributes = pretty_format_options.align_attributes
self.indentation = pretty_format_options.indentation
self._align_attributes = format_options.align_attributes
self.indentation = format_options.indentation
self._level = 0
self._serialization_root: None | TagNode = None
self._space_preserving_serializer = Serializer(
Expand Down Expand Up @@ -3502,22 +3499,22 @@ class TextWrappingSerializer(PrettySerializer):
def __init__(
self,
writer: _SerializationWriter,
pretty_format_options: PrettyFormatOptions,
format_options: FormatOptions,
*,
namespaces: Optional[NamespaceDeclarations] = None,
):
if pretty_format_options.text_width < 1:
if format_options.text_width < 1:
raise ValueError
self.writer: _LengthTrackingWriter
super().__init__(
writer=_LengthTrackingWriter(writer.buffer),
pretty_format_options=pretty_format_options,
format_options=format_options,
namespaces=namespaces,
)
self._line_fitting_serializer = _LineFittingSerializer(
self.writer, namespaces=self._namespaces
)
self._width = pretty_format_options.text_width
self._width = format_options.text_width

@property
def _available_space(self):
Expand Down Expand Up @@ -3830,7 +3827,7 @@ def _wrap_text(text: str, width: int) -> Iterator[str]:
yield text


class PrettyFormatOptions(NamedTuple):
class FormatOptions(NamedTuple):
"""
Instances of this class can be used to define serialization formatting that is
not so hard to interpret for instances of Homo sapiens s., but more costly to
Expand Down Expand Up @@ -3888,25 +3885,25 @@ class DefaultStringOptions:
See :class:`io.TextIOWrapper` for a detailed explanation of the parameter with the
same name.
"""
pretty_format_options: ClassWar[None | PrettyFormatOptions] = None
format_options: ClassWar[None | FormatOptions] = None
"""
An instance of :class:`PrettyFormatOptions` can be provided to configure formatting.
An instance of :class:`FormatOptions` can be provided to configure formatting.
"""

@classmethod
def _get_serializer(cls) -> Serializer:
return _get_serializer(
_StringWriter(newline=cls.newline),
format_options=cls.format_options,
namespaces=cls.namespaces,
pretty_format_options=cls.pretty_format_options,
)

@classmethod
def reset_defaults(cls):
"""Restores the factory settings."""
cls.format_options = None
cls.namespaces = None
cls.newline = None
cls.pretty_format_options = None


class _SerializationWriter(ABC):
Expand Down Expand Up @@ -3973,8 +3970,8 @@ def __init__(
Attribute.__name__,
CommentNode.__name__,
DefaultStringOptions.__name__,
FormatOptions.__name__,
NodeBase.__name__,
PrettyFormatOptions.__name__,
ProcessingInstructionNode.__name__,
QueryResults.__name__,
TagAttributes.__name__,
Expand Down
22 changes: 11 additions & 11 deletions delb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
tag,
CommentNode,
DefaultStringOptions,
FormatOptions,
NodeBase,
PrettyFormatOptions,
PrettySerializer,
ProcessingInstructionNode,
Serializer,
Expand Down Expand Up @@ -501,9 +501,9 @@ def save(
pretty: Optional[bool] = None,
*,
encoding: str = "utf-8",
format_options: Optional[FormatOptions] = None,
namespaces: Optional[NamespaceDeclarations] = None,
newline: None | str = None,
pretty_format_options: Optional[PrettyFormatOptions] = None,
):
"""
Saves the serialized document contents to a file. See :doc:`/api/serialization`
Expand All @@ -513,23 +513,23 @@ def save(
:param pretty: *Deprecated.* Adds indentation for human consumers when
:obj:`True`.
:param encoding: The desired text encoding.
:param format_options: An instance of :class:`FormatOptions` can be
provided to configure formatting.
:param namespaces: A mapping of prefixes to namespaces. These are overriding
possible declarations from a parsed serialisat that the
document instance stems from. Prefixes for undeclared
namespaces are enumerated with the prefix ``ns``.
:param newline: See :class:`io.TextIOWrapper` for a detailed explanation of the
parameter with the same name.
:param pretty_format_options: An instance of :class:`PrettyFormatOptions` can be
provided to configure formatting.
"""
with path.open("bw") as file:
self.write(
buffer=file,
pretty=pretty,
encoding=encoding,
format_options=format_options,
namespaces=namespaces,
newline=newline,
pretty_format_options=pretty_format_options,
)

@altered_default_filters()
Expand Down Expand Up @@ -561,9 +561,9 @@ def write(
pretty: Optional[bool] = None,
*,
encoding: str = "utf-8",
format_options: Optional[FormatOptions] = None,
namespaces: Optional[NamespaceDeclarations] = None,
newline: None | str = None,
pretty_format_options: Optional[PrettyFormatOptions] = None,
):
"""
Writes the serialized document contents to a :term:`file-like object`. See
Expand All @@ -573,21 +573,21 @@ def write(
:param pretty: *Deprecated.* Adds indentation for human consumers when
:obj:`True`.
:param encoding: The desired text encoding.
:param format_options: An instance of :class:`FormatOptions` can be provided to
configure formatting.
:param namespaces: A mapping of prefixes to namespaces. These are overriding
possible declarations from a parsed serialisat that the
document instance stems from. Prefixes for undeclared
namespaces are enumerated with the prefix ``ns``.
:param newline: See :class:`io.TextIOWrapper` for a detailed explanation of the
parameter with the same name.
:param pretty_format_options: An instance of :class:`PrettyFormatOptions` can be
provided to configure formatting.
"""
if pretty is not None:
warn(
"The `pretty` argument is deprecated, for the legacy behaviour provide "
"`indentation` as two spaces instead."
)
pretty_format_options = PrettyFormatOptions(
format_options = FormatOptions(
align_attributes=False, indentation=" " if pretty else "", text_width=0
)

Expand All @@ -596,8 +596,8 @@ def write(
_TextBufferWriter(
TextIOWrapper(buffer), encoding=encoding, newline=newline
),
format_options=format_options,
namespaces=namespaces,
pretty_format_options=pretty_format_options,
),
encoding=encoding,
)
Expand All @@ -617,9 +617,9 @@ def xpath(
CommentNode.__name__,
DefaultStringOptions.__name__,
Document.__name__,
FormatOptions.__name__,
Namespaces.__name__,
ParserOptions.__name__,
PrettyFormatOptions.__name__,
ProcessingInstructionNode.__name__,
QueryResults.__name__,
TagNode.__name__,
Expand Down
18 changes: 9 additions & 9 deletions docs/api/serialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Overview
*delb* allows users to produce well-readable, content-agnostic XML
serializations as well-readable as they can get.

The formatting options are controlled with the :class:`delb.PrettyFormatOptions`
that are either passed to serialization methods like :meth:`delb.Document.save`
and :meth:`delb.TagNode.serialize` or setting the class property
:obj:`delb.DefaultStringOptions.pretty_format_options` for any conversions of
The formatting options are controlled with the :class:`delb.FormatOptions` that
are either passed to serialization methods like :meth:`delb.Document.save` and
:meth:`delb.TagNode.serialize` or setting the class property
:obj:`delb.DefaultStringOptions.format_options` for any conversions of
documents and nodes to strings (e.g. with :func:`print` or :class:`str`) on the
general application level. Passing / Setting :obj:`None` lets the serializer
simply dump a tree's contents to an XML stream without any extra efforts.
Expand Down Expand Up @@ -40,8 +40,8 @@ standalone units, neither are the contributed implementations suited for
derivations nor is the architecture ready for extensions in that regard yet.


Example and comparison to other means of production
---------------------------------------------------
Examples and comparison to other means of production
----------------------------------------------------

As an example this input is given:

Expand All @@ -65,7 +65,7 @@ Just indentation, no content wrapping and aligned attributes
document.save(
path,
pretty_format_options = PrettyFormatOptions(
format_options = FormatOptions(
align_attributes=True,
indentation=" ",
text_width=0
Expand Down Expand Up @@ -93,7 +93,7 @@ With text wrapping
document.save(
path,
pretty_format_options = PrettyFormatOptions(
format_options = FormatOptions(
align_attributes=False,
indentation=" ",
text_width=59
Expand Down Expand Up @@ -142,7 +142,7 @@ Configuration interfaces

.. autoclass:: delb.DefaultStringOptions

.. autoclass:: delb.PrettyFormatOptions
.. autoclass:: delb.FormatOptions
:no-inherited-members:


Expand Down
12 changes: 5 additions & 7 deletions integration-tests/test-parse-serialize-equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
compare_trees,
Document,
FailedDocumentLoading,
FormatOptions,
ParserOptions,
PrettyFormatOptions,
)


Expand Down Expand Up @@ -57,12 +57,10 @@ def save_and_compare_file(
origin: Document,
work_fs: MemoryFS,
result_file: Path,
pretty_format_options: None | PrettyFormatOptions,
format_options: None | FormatOptions,
reduce_whitespace: bool,
):
if not save_file(
origin, work_fs, result_file, pretty_format_options=pretty_format_options
):
if not save_file(origin, work_fs, result_file, format_options=format_options):
return

if (
Expand Down Expand Up @@ -106,15 +104,15 @@ def parse_and_serialize_and_compare(src_fs: OSFS, work_fs: MemoryFS, file: Path)
origin,
work_fs,
file.with_stem(f"{file.stem}-tabbed"),
PrettyFormatOptions(align_attributes=False, indentation="\t", text_width=0),
FormatOptions(align_attributes=False, indentation="\t", text_width=0),
True,
)

save_and_compare_file(
origin,
work_fs,
file.with_stem(f"{file.stem}-wrapped"),
PrettyFormatOptions(align_attributes=False, indentation=" ", text_width=77),
FormatOptions(align_attributes=False, indentation=" ", text_width=77),
True,
)

Expand Down
Loading

0 comments on commit e073716

Please sign in to comment.