Browse Source

guts: yamlstyle for Unicode, StringPattern and StringChoice

tags/v2020.08.18
Sebastian Heimann 6 months ago
parent
commit
293bc1a8aa
2 changed files with 79 additions and 26 deletions
  1. +49
    -4
      src/guts.py
  2. +30
    -22
      test/base/test_guts.py

+ 49
- 4
src/guts.py View File

@@ -104,6 +104,42 @@ for (style, cls) in str_style_map.items():
GutsSafeDumper.add_representer(cls, make_str_presenter(style))


class uliteral(unicode):
pass


class ufolded(unicode):
pass


class usinglequoted(unicode):
pass


class udoublequoted(unicode):
pass


def make_unicode_presenter(style):
def presenter(dumper, data):
return dumper.represent_scalar(
'tag:yaml.org,2002:str', unicode(data), style=style)

return presenter


unicode_style_map = {
None: lambda x: x,
'|': literal,
'>': folded,
"'": singlequoted,
'"': doublequoted}

for (style, cls) in unicode_style_map.items():
if style:
GutsSafeDumper.add_representer(cls, make_unicode_presenter(style))


class blist(list):
pass

@@ -956,6 +992,15 @@ class String(Object):
class Unicode(Object):
dummy_for = newstr

class __T(TBase):
def __init__(self, *args, **kwargs):
yamlstyle = kwargs.pop('yamlstyle', None)
TBase.__init__(self, *args, **kwargs)
self.style_cls = unicode_style_map[yamlstyle]

def to_save(self, val):
return self.style_cls(val)


guts_plain_dummy_types = (String, Unicode, Int, Float, Complex, Bool)

@@ -1246,9 +1291,9 @@ class StringPattern(String):
dummy_for = str
pattern = '.*'

class __T(TBase):
class __T(String.T):
def __init__(self, pattern=None, *args, **kwargs):
TBase.__init__(self, *args, **kwargs)
String.T.__init__(self, *args, **kwargs)

if pattern is not None:
self.pattern = pattern
@@ -1305,9 +1350,9 @@ class StringChoice(String):
choices = []
ignore_case = False

class __T(TBase):
class __T(String.T):
def __init__(self, choices=None, ignore_case=None, *args, **kwargs):
TBase.__init__(self, *args, **kwargs)
String.T.__init__(self, *args, **kwargs)

if choices is not None:
self.choices = choices


+ 30
- 22
test/base/test_guts.py View File

@@ -18,6 +18,12 @@ from pyrocko.guts import StringPattern, Object, Bool, Int, Float, String, \
get_elements, YPathError


try:
unicode
except NameError:
unicode = str


guts_prefix = 'guts_test'


@@ -1177,30 +1183,32 @@ xmlns:f="https://www.w3schools.com/furniture">
assert isinstance(o3.tables1[0], Table1)

def testStyles(self):
class A(Object):
s = String.T(yamlstyle=None)
s_singlequoted = String.T(yamlstyle="'")
s_doublequoted = String.T(yamlstyle='"')
s_literal = String.T(yamlstyle='|')
s_folded = String.T(yamlstyle='>')
l_block = List.T(String.T(yamlstyle="'"), yamlstyle='block')
l_flow = List.T(String.T(yamlstyle="'"), yamlstyle='flow')

a = A(
s='hello',
s_singlequoted='hello',
s_doublequoted='hello',
s_literal='hello\nhello\n',
s_folded='hello',
l_block=['a', 'b', 'c'],
l_flow=['a', 'b', 'c'])

a.validate()
for cls, Class in [(str, String), (unicode, Unicode)]:
class A(Object):
s = Class.T(yamlstyle=None)
s_singlequoted = Class.T(yamlstyle="'")
s_doublequoted = Class.T(yamlstyle='"')
s_literal = Class.T(yamlstyle='|')
s_folded = Class.T(yamlstyle='>')
l_block = List.T(Class.T(yamlstyle="'"), yamlstyle='block')
l_flow = List.T(Class.T(yamlstyle="'"), yamlstyle='flow')

a = A(
s=cls('hello'),
s_singlequoted=cls('hello'),
s_doublequoted=cls('hello'),
s_literal=cls('hello\nhello\n'),
s_folded=cls('hello'),
l_block=[cls('a'), cls('b'), cls('c')],
l_flow=[cls('a'), cls('b'), cls('c')])

a.validate()

a2 = load_string(a.dump())
s2 = ('\n'.join(a2.dump().splitlines()[1:]))
a2 = load_string(a.dump())
s2 = ('\n'.join(a2.dump().splitlines()[1:]))
assert type(a2.s) == cls

assert s2 == '''
assert s2 == '''
s: hello
s_singlequoted: 'hello'
s_doublequoted: "hello"


Loading…
Cancel
Save