Skip to content

Instantly share code, notes, and snippets.

@agrif
Created May 22, 2013 03:44
Show Gist options
  • Select an option

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

Select an option

Save agrif/5625100 to your computer and use it in GitHub Desktop.
from collections import namedtuple
import yaml
from annotations import Annotation, AnnotatedValue
from parser import Parser
class YAMLParser(Parser):
def parse(self):
tree = yaml.compose(self.fileobj)
self._constructor = yaml.constructor.SafeConstructor()
return self._annotate(tree)
def _annotate(self, node):
ann = Annotation(self.filename,
node.start_mark.line + 1, node.end_mark.line + 1)
if isinstance(node, yaml.SequenceNode):
value = [self._annotate(n) for n in node.value]
elif isinstance(node, yaml.MappingNode):
value = dict((self._annotate(n) for n in l) for l in node.value)
elif isinstance(node, yaml.ScalarNode):
value = self._constructor.construct_object(node)
else:
raise RuntimeError("unhandled node type")
return AnnotatedValue(value, ann)
def get_description(self, annotation):
return "in file {}, line"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment