Skip to content

Commit

Permalink
Merge branch 'development' into feature/more-set-caps-to-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
wwkimball committed Apr 10, 2021
2 parents a7e3e1e + ed566ce commit 85c29ee
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 115 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Enhancements:
Parsers::get_yaml_data(...) or Parsers::get_yaml_multidoc_data(...) to
indicate that `source` is literal serialized (String) YAML data rather than a
file-spec. This mode is implied when reading from STDIN (source is "-").
* The emitter_write_folded_fix.py patch file for ruamel.yaml has been removed
in favor of an author-supplied solution to the problem --
https://sourceforge.net/p/ruamel-yaml/tickets/383/ -- for which the patch was
originally written.

Known Issues:
* ruamel.yaml version 0.17.x is a major refactoring effort by the project's
Expand Down
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1098,14 +1098,6 @@ and editing with ruamel.yaml. When you need to process EYAML encrypted data,
replace `yamlpath.Processor` with `yamlpath.eyaml.EYAMLProcessor` and add error
handling for `yamlpath.eyaml.EYAMLCommandException`.

Note that `import yamlpath.patches` is entirely optional. I wrote and use it to
block ruamel.yaml's Emitter from injecting unnecessary newlines into folded
values (it improperly converts every single new-line into two for left-flushed
multi-line values, at the time of this writing). Since "block" output EYAML
values are left-flushed multi-line folded strings, this fix is necessary when
using EYAML features. At least, until ruamel.yaml has its own fix for this
issue.

Note also that these examples use `ConsolePrinter` to handle STDOUT and STDERR
messaging. You don't have to. However, some kind of logger must be passed to
these libraries so they can write messages _somewhere_. Your custom message
Expand All @@ -1121,7 +1113,6 @@ import sys
from ruamel.yaml import YAML
from ruamel.yaml.parser import ParserError

import yamlpath.patches
from yamlpath.func import get_yaml_data, get_yaml_editor
from yamlpath.wrappers import ConsolePrinter
from yamlpath import Processor
Expand Down
1 change: 0 additions & 1 deletion tests/test_eyaml_eyamlprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from ruamel.yaml import YAML

import yamlpath.patches
from yamlpath.func import unwrap_node_coords
from yamlpath.enums import YAMLValueFormats
from yamlpath.eyaml.enums import EYAMLOutputFormats
Expand Down
3 changes: 0 additions & 3 deletions yamlpath/commands/eyaml_rotate_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
from yamlpath.common import Parsers
from yamlpath.eyaml.exceptions import EYAMLCommandException
from yamlpath.eyaml import EYAMLProcessor

# pylint: disable=locally-disabled,unused-import
import yamlpath.patches
from yamlpath.wrappers import ConsolePrinter

def processcli():
Expand Down
5 changes: 1 addition & 4 deletions yamlpath/commands/yaml_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
from yamlpath.eyaml.exceptions import EYAMLCommandException
from yamlpath.eyaml.enums import EYAMLOutputFormats
from yamlpath.eyaml import EYAMLProcessor

# pylint: disable=locally-disabled,unused-import
import yamlpath.patches
from yamlpath.wrappers import ConsolePrinter, NodeCoords
from yamlpath.wrappers import ConsolePrinter

def processcli():
"""Process command-line arguments."""
Expand Down
20 changes: 20 additions & 0 deletions yamlpath/common/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ def make_new_node(
elif valform == YAMLValueFormats.FOLDED:
new_type = FoldedScalarString
new_value = str(value)
preserve_folds = []

# Scan all except the very last character because if that last
# character is a newline, ruamel.yaml will crash when told to fold
# there.
for index, fold_char in enumerate(new_value[:-1]):
if fold_char == "\n":
preserve_folds.append(index)

# Replace all except the very last character
new_value = new_value[:-1].replace("\n", " ") + new_value[-1]

if hasattr(source_node, "anchor") and source_node.anchor.value:
new_node = new_type(new_value, anchor=source_node.anchor.value)
else:
new_node = new_type(new_value)

if preserve_folds:
new_node.fold_pos = preserve_folds

elif valform == YAMLValueFormats.LITERAL:
new_type = LiteralScalarString
new_value = str(value)
Expand Down
2 changes: 0 additions & 2 deletions yamlpath/patches/__init__.py

This file was deleted.

95 changes: 0 additions & 95 deletions yamlpath/patches/emitter_write_folded_fix.py

This file was deleted.

7 changes: 6 additions & 1 deletion yamlpath/wrappers/consoleprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
CommentedSet,
TaggedScalar
)
from ruamel.yaml.scalarstring import FoldedScalarString

from yamlpath.wrappers.nodecoords import NodeCoords

Expand Down Expand Up @@ -251,7 +252,7 @@ def _debug_scalar(data: Any, **kwargs) -> str:
print_anchor = kwargs.pop("print_anchor", True)
print_tag = kwargs.pop("print_tag", True)
print_type = kwargs.pop("print_type", False)
dtype = type(data) if print_type else ""
dtype: str = str(type(data)) if print_type else ""
anchor_prefix = ""
print_prefix = prefix

Expand All @@ -272,6 +273,10 @@ def _debug_scalar(data: Any, **kwargs) -> str:
if isinstance(data, TaggedScalar):
dtype = "{}({})".format(dtype, type(data.value))

# Report fold points, if present
if isinstance(data, FoldedScalarString) and hasattr(data, 'fold_pos'):
dtype += ",folded@{}".format(data.fold_pos)

print_prefix += anchor_prefix
return ConsolePrinter._debug_prefix_lines(
"{}{}{}".format(print_prefix, data, dtype))
Expand Down

0 comments on commit 85c29ee

Please sign in to comment.