Created
May 22, 2013 03:44
-
-
Save agrif/5625100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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