Beispiel #1
0
def load(filename, encoding='utf-8'):
    dirname = os.path.dirname(filename)

    def include(loader, node):
        try:
            filename = loader.construct_scalar(node)
        except:
            try:
                filename, nodename = loader.construct_sequence(node)
            except:
                raise ConstructorError(None, None,
                    'expected a string or sequence(2 elements), but found %s' % node.id,
                    node.start_mark)
        else:
            nodename = None

        with open(os.path.join(dirname, filename), encoding=encoding) as f:
            document = yaml.load(f)

        if nodename:
            return document[nodename]

        return document

    Constructor.add_constructor(INCLUDE_TAG, include)

    with open(filename, encoding=encoding) as f:
        data = yaml.load(f)

    del Constructor.yaml_constructors[INCLUDE_TAG]

    return data
Beispiel #2
0
 def __init__(self, stream):
     Reader.__init__(self, stream)
     PyonYamlScanner.__init__(self)
     Parser.__init__(self)
     Composer.__init__(self)
     Constructor.__init__(self)
     Resolver.__init__(self)
Beispiel #3
0
def load(filename):
    dirname = os.path.dirname(filename)

    def include(loader, node):
        try:
            filename = loader.construct_scalar(node)
        except:
            try:
                filename, nodename = loader.construct_sequence(node)
            except:
                raise ConstructorError(
                    None, None,
                    'expected a string or sequence(2 elements), but found %s' %
                    node.id, node.start_mark)
        else:
            nodename = None

        with open(os.path.join(dirname, filename)) as f:
            document = yaml.load(f)

        if nodename:
            return document[nodename]

        return document

    Constructor.add_constructor(INCLUDE_TAG, include)

    with open(filename) as f:
        data = yaml.load(f)

    del Constructor.yaml_constructors[INCLUDE_TAG]

    return data
Beispiel #4
0
 def __init__(self, stream):
     Reader.__init__(self, stream)
     PyonYamlScanner.__init__(self)
     Parser.__init__(self)
     Composer.__init__(self)
     Constructor.__init__(self)
     Resolver.__init__(self)
Beispiel #5
0
    def __init__(self, stream):
        self.m_dictFileID2NodeData = {}
        self.dictNode2FileID = {}

        Reader.__init__(self, stream)
        Scanner.__init__(self)
        _PrefabParserInner.__init__(self)
        PrefabComposer.__init__(self)
        Constructor.__init__(self)
        Resolver.__init__(self)
Beispiel #6
0
    def __init__(self, filename):
        self.filename = filename
        with open(self.filename, encoding='utf-8') as fp:
            stream = self.preprocess(fp.read())
        Reader.__init__(self, stream)
        self.name = self.filename

        Scanner.__init__(self)
        Parser.__init__(self)
        Composer.__init__(self)
        Constructor.__init__(self)
        Resolver.__init__(self)
Beispiel #7
0
    def construct_mapping(loader, node, deep=False):
        loader.flatten_mapping(node)
        mapping = Constructor.construct_pairs(loader, node, deep=deep)

        ordered_hook = object_pairs_hook(mapping)

        return RawDict(adict=ordered_hook, source=stream.name, lineno=node.__lineno__)
Beispiel #8
0
 def construct_mapping(node, deep=False):
     mapping = Constructor.construct_mapping(loader, node, deep=deep)
     mapping['__line__'] = node.__line__
     mapping['__column__'] = node.__column__
     mapping['__basename__'] = node.__basename__
     mapping['__location__'] = node.__location__
     return mapping
Beispiel #9
0
 def construct_object(node, deep=False):
     line = node.value['__line__']
     node.value = node.value['__val__']
     data = Constructor.construct_object(loader, node, deep)
     if isinstance(data, str):
         data = LineStr(data)
         data.line = line
     return data
Beispiel #10
0
 def construct_object(node, deep=False):
     line = node.value['__line__']
     node.value = node.value['__val__']
     data = Constructor.construct_object(loader, node, deep)
     if isinstance(data, str):
         data = LineStr(data)
         data.line = line
     return data
Beispiel #11
0
 def construct_mapping(node, deep=False):
     if ANSIBLE_VERSION < 2:
         mapping = Constructor.construct_mapping(loader, node, deep=deep)
     else:
         mapping = AnsibleConstructor.construct_mapping(loader, node, deep=deep)
     if hasattr(node, '__line__'):
         mapping[LINE_NUMBER_KEY] = node.__line__
     else:
         mapping[LINE_NUMBER_KEY] = mapping._line_number
     mapping[FILENAME_KEY] = filename
     return mapping
Beispiel #12
0
 def construct_mapping(node, deep=False):
     if ANSIBLE_VERSION < 2:
         mapping = Constructor.construct_mapping(loader, node, deep=deep)
     else:
         mapping = AnsibleConstructor.construct_mapping(loader, node, deep=deep)
     if hasattr(node, '__line__'):
         mapping[LINE_NUMBER_KEY] = node.__line__
     else:
         mapping[LINE_NUMBER_KEY] = mapping._line_number
     mapping[FILENAME_KEY] = filename
     return mapping
Beispiel #13
0
    def construct_mapping(loader, node, deep=False):
        loader.flatten_mapping(node)
        mapping = Constructor.construct_pairs(loader, node, deep=deep)

        ordered_hook = object_pairs_hook(mapping)

        # assert not hasattr(ordered_hook, "__lineno__"), \
        #     "Expected ordered mapping to have no __lineno__ attribute set before"
        # setattr(ordered_hook, "__lineno__", node.__lineno__)

        return RawDict(adict=ordered_hook,
                       source=stream.name,
                       lineno=node.__lineno__)
Beispiel #14
0
        def construct_object(node, deep=False):
            """ Invoked when PyYAML's internal nodes are converted to python types
            (e.g. ScalarNode -> str/int/float, SequenceNode -> list, MappingNode -> dictionary)
            """

            # In case we are dealing with an Alias, just convert it to a string (prepend the * again because PyYaml
            # strips it). Also add a line number.
            if isinstance(node, AliasEvent):
                data = LineStr("*" + node.anchor)
                data.line = node.line
                return data

            # retrieve previously stored line number and value, restore node.value to original value
            line = node.value['__line__']
            node.value = node.value['__val__']

            # call the original construct_object method
            data = Constructor.construct_object(loader, node, deep)

            # if the original construct_object method creating anything else then a list or dict (i.e. a str, int, float
            # datetime, etc), just wrap it in a LineStr object.
            if not (isinstance(data, dict) or isinstance(data, list)):
                if node.tag == "tag:yaml.org,2002:int":
                    data = LineInt(data)
                elif node.tag == "tag:yaml.org,2002:float":
                    data = LineFloat(data)
                else:
                    data = LineStr(data)

                # TODO(jroovers): node.start_mark.line and node.end_mark.line provide a lot of what we need. We can
                # definitely simplify this a lot. Stupid me for now finding this earlier. It doesn't provide exactly
                # what we need, especially when anchors are used (node.start_mark and node.end_mark then include
                # everything that is referenced as well). We should look into this (I've already spend 2 hrs on this
                # and wasn't able to fully figure it out)
                # data.line = node.start_mark.line
                # data.line_end = node.end_mark.line

                data.line = line
                data.line_end = node.end_mark.line

                # If the scalar node has a style, then keep that information around later
                if hasattr(node, 'style'):
                    data.style = node.style

            return data
Beispiel #15
0
        def construct_object(node, deep=False):
            """ Invoked when PyYAML's internal nodes are converted to python types
            (e.g. ScalarNode -> str/int/float, SequenceNode -> list, MappingNode -> dictionary)
            """

            # In case we are dealing with an Alias, just convert it to a string (prepend the * again because PyYaml
            # strips it). Also add a line number.
            if isinstance(node, AliasEvent):
                data = LineStr("*" + node.anchor)
                data.line = node.line
                return data

            # retrieve previously stored line number and value, restore node.value to original value
            line = node.value['__line__']
            node.value = node.value['__val__']

            # call the original construct_object method
            data = Constructor.construct_object(loader, node, deep)

            # if the original construct_object method creating anything else then a list or dict (i.e. a str, int, float
            # datetime, etc), just wrap it in a LineStr object.
            if not (isinstance(data, dict) or isinstance(data, list)):
                if node.tag == "tag:yaml.org,2002:int":
                    data = LineInt(data)
                elif node.tag == "tag:yaml.org,2002:float":
                    data = LineFloat(data)
                else:
                    data = LineStr(data)

                # TODO(jroovers): node.start_mark.line and node.end_mark.line provide a lot of what we need. We can
                # definitely simplify this a lot. Stupid me for now finding this earlier. It doesn't provide exactly
                # what we need, especially when anchors are used (node.start_mark and node.end_mark then include
                # everything that is referenced as well). We should look into this (I've already spend 2 hrs on this
                # and wasn't able to fully figure it out)
                # data.line = node.start_mark.line
                # data.line_end = node.end_mark.line

                data.line = line
                data.line_end = node.end_mark.line

                # If the scalar node has a style, then keep that information around later
                if hasattr(node, 'style'):
                    data.style = node.style

            return data
Beispiel #16
0
 def construct_mapping(node, deep=False):
     mapping = Constructor.construct_mapping(loader, node, deep=deep)
     mapping['__line__'] = node.__line__
     return mapping
Beispiel #17
0
 def construct_mapping(self, node, deep=False):
     mapping = Constructor.construct_mapping(self, node, deep=deep)
     mapping["__line_start__"] = node.__line_start__
     mapping["__line_end__"] = node.__line_end__
     return mapping
Beispiel #18
0
 def construct_mapping(node, deep=False):
     mapping = Constructor.construct_mapping(loader, node, deep=deep)
     mapping['__line__'] = node.__line__
     return mapping
 def construct_mapping(self, node, deep=False):
     mapping = Constructor.construct_mapping(self.loader, node, deep=deep)
     return mapping
Beispiel #20
0
class FloatNode(float, Node):
    pass


class ListNode(list, Node):
    pass


class DictNode(OrderedDict, Node):
    pass


from yaml.representer import Representer
from yaml.constructor import Constructor
yaml_representer = Representer()
yaml_constructor = Constructor()


def yaml_to_node(yaml_obj):
    if isinstance(yaml_obj, yaml.MappingNode):
        v = [(yaml_to_node(k), yaml_to_node(v)) for k, v in yaml_obj.value]
        node = DictNode(v)
    elif isinstance(yaml_obj, yaml.SequenceNode):
        v = [yaml_to_node(v) for v in yaml_obj.value]
        node = ListNode(v)
    elif isinstance(yaml_obj, yaml.ScalarNode):
        # 'tag:yaml.org,2002:bool'
        type = yaml_obj.tag.split(':')[2]
        type = type[0].upper() + type[1:] + 'Node'
        v = yaml_obj.value
        node = globals()[type](v)
Beispiel #21
0
 def construct_mapping(self, node, deep=False):
     mapping = Constructor.construct_mapping(self.loader, node, deep=deep)
     mapping['yaml_line'] = node.__line__
     return mapping
Beispiel #22
0
    return mapping


BaseConstructor.construct_mapping = construct_ordered_mapping


def construct_yaml_map_with_ordered_dict(self, node):
    data = OrderedDict()
    yield data
    value = self.construct_mapping(node)
    data.update(value)


for t in [u'tag:yaml.org,2002:map', u'tag:yaml.org,2002:omap']:
    SafeConstructor.add_constructor(t, construct_yaml_map_with_ordered_dict)
    Constructor.add_constructor(t, construct_yaml_map_with_ordered_dict)
    yaml.add_constructor(t, construct_yaml_map_with_ordered_dict)


def represent_ordered_mapping(self, tag, mapping, flow_style=None):
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    best_style = True

    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node

    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())

    for item_key, item_value in mapping:
Beispiel #23
0
        mapping[key] = value
    
    return mapping

BaseConstructor.construct_mapping = construct_ordered_mapping


def construct_yaml_map_with_ordered_dict(self, node):
    data = OrderedDict()
    yield data
    value = self.construct_mapping(node)
    data.update(value)

for t in [u'tag:yaml.org,2002:map', u'tag:yaml.org,2002:omap']:
    SafeConstructor.add_constructor( t, construct_yaml_map_with_ordered_dict )
    Constructor.add_constructor( t, construct_yaml_map_with_ordered_dict )
    yaml.add_constructor( t, construct_yaml_map_with_ordered_dict )


def represent_ordered_mapping(self, tag, mapping, flow_style=None):
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    best_style = True
    
    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node
    
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
    
    for item_key, item_value in mapping:
Beispiel #24
0
 def construct_mapping(node, deep=False):
     mapping = Constructor.construct_mapping(loader, node, deep=deep)
     mapping = InfoDict(mapping)
     mapping.lineno = node.__lineno__
     return mapping
Beispiel #25
0
 def construct_mapping(node, deep=False):
     mapping = Constructor.construct_mapping(loader, node, deep=deep)
     mapping[LINE_NUMBER_KEY] = node.__line__
     return mapping
Beispiel #26
0
 def construct_mapping(self, node, deep=False):
     mapping = Constructor.construct_mapping(self.loader, node, deep=deep)
     mapping['yaml_line'] = node.__line__
     return mapping
Beispiel #27
0
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

    return False


def add_bool(self, node):
    return self.construct_scalar(node)


Constructor.add_constructor(u'tag:yaml.org,2002:bool', add_bool)


def main():
    inputFname = 'content.yaml'
    with open(inputFname, 'r') as f:
        content = yaml.load(f)

    # pp.pprint(content)
    validate(content)
    html = htmlize(content)
    # print(html)

    with open('stub.html', 'w') as f:
        f.write(html)
    print('done')
Beispiel #28
0
 def construct_mapping(node, deep=False):
     mapping = Constructor.construct_mapping(loader, node, deep=deep)
     mapping[LINE_NUMBER_KEY] = node.__line__
     mapping[FILENAME_KEY] = filename
     return mapping
Beispiel #29
0
 def construct_mapping(self, node, deep=False):
     mapping = Constructor.construct_mapping(self.loader, node, deep=deep)
     return mapping
Beispiel #30
0
from yaml.constructor import Constructor
from prestring.python import Module

m = Module()
m.from_("yaml.constructor", "Constructor")
m.sep()
with m.class_("WrappedConstructor", "Constructor"):
    with m.def_("wrap", "self", "path", "name", "node", "r"):
        with m.if_("r is None"):
            m.stmt("return r")
        m.stmt('# print("@", id(r), repr(r))')
        m.stmt("mem[id(r)] = node")
        m.stmt("return r")

    seen = set()
    for cls in Constructor.mro():
        for name, attr in cls.__dict__.items():
            if name in seen:
                continue
            seen.add(name)
            if name.startswith("construct_") and callable(attr):
                sigs = inspect.signature(attr)
                m.stmt("def {}{}:", name, sigs)
                with m.scope():
                    args = []
                    for v in sigs.parameters.values():
                        if v.name == "self":
                            continue
                        if v.default is inspect._empty:
                            args.append(str(v))
                        else:
Beispiel #31
0
 def find_python_name(self, name, mark):
     if sys.version_info.major == 3:
         if name == '__builtin__.dict':
             name = 'builtins.dict'
     return Constructor.find_python_name(self, name, mark)