Skip to content

Instantly share code, notes, and snippets.

@agrif
Created May 23, 2013 01:45
Show Gist options
  • Select an option

  • Save agrif/5632261 to your computer and use it in GitHub Desktop.

Select an option

Save agrif/5632261 to your computer and use it in GitHub Desktop.
import parseyaml
import validators
import annotations
class WidgetValidator(validators.ObjectValidator):
foo = validators.StringValidator()
bar = validators.StringValidator()
def validate_object(self):
if self.bar != "racuda":
raise validators.ValidationError("bar must be 'racuda'")
return self.validated
p = parseyaml.YAMLParser(open('data.yml'))
v = WidgetValidator()
try:
print v.validate(p.parse())
except annotations.AnnotatedError, e:
print e
# some example input files (<--) and output (-->)
#
# (<--) foo: something (-->) {'foo': u'something', 'bar': u'racuda'}
# bar: racuda
#
# (<--) foo: something (-->) in data.yml, lines 1-2:
# bar: else invalid widget: bar must be 'racuda'
#
# (<--) foo: something (-->) in data.yml, line 1:
# bar: racuda duplicate setting foo in widget (first was in data.yml, line 3)
# foo: another
#
# (<--) foo: something (-->) in data.yml, line 2:
# baz: racuda unknown setting baz in widget (did you mean bar?)
#
# (<--) foo: 1.0 (-->) in data.yml, lines 1-2:
# bar: racuda invalid string
# in data.yml, lines 1-3:
# validation failed for setting foo in widget
#
# (<--) bar: racuda (-->) in data.yml, lines 1-2:
# missing setting foo in widget: expected string
#
@agrif

agrif commented May 24, 2013

Copy link
Copy Markdown
Author

input:

foo: "something
bar: racuda

current output:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    print v.validate(p.parse())
  File "/Users/agrif/devel/minecraft/ovrconfig/parseyaml.py", line 9, in parse
    tree = yaml.compose(self.fileobj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/__init__.py", line 48, in compose
    return loader.get_single_node()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/composer.py", line 36, in get_single_node
    document = self.compose_document()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/composer.py", line 55, in compose_document
    node = self.compose_node(None, None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/composer.py", line 133, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/composer.py", line 64, in compose_node
    if self.check_event(AliasEvent):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/parser.py", line 449, in parse_block_mapping_value
    if not self.check_token(KeyToken, ValueToken, BlockEndToken):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/scanner.py", line 248, in fetch_more_tokens
    return self.fetch_double()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/scanner.py", line 656, in fetch_double
    self.fetch_flow_scalar(style='"')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/scanner.py", line 667, in fetch_flow_scalar
    self.tokens.append(self.scan_flow_scalar(style))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/scanner.py", line 1158, in scan_flow_scalar
    chunks.extend(self.scan_flow_scalar_spaces(double, start_mark))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.10-py2.6-macosx-10.6-x86_64.egg/yaml/scanner.py", line 1245, in scan_flow_scalar_spaces
    "found unexpected end of stream", self.get_mark())
yaml.scanner.ScannerError: while scanning a quoted scalar
  in "data.yml", line 1, column 6
found unexpected end of stream
  in "data.yml", line 3, column 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment