Exemple #1
0
def load_yaml(yaml_data, filename):
    """
    Load YAML data extending it with line number information, nodes get a __line__ attribute
    """
    if yaml_data is None:
        with open(filename, 'r') as data_file:
            yaml_data = data_file.read()

    loader = Loader(yaml_data)

    def compose_node(parent, index):
        # the line number where the previous token has ended (plus empty lines)
        line = loader.line
        node = Composer.compose_node(loader, parent, index)
        node.__line__ = line + 1
        return node

    def construct_mapping(node, deep=False):
        mapping = Constructor.construct_mapping(loader, node, deep=deep)
        mapping['__line__'] = node.__line__
        return mapping

    loader.compose_node = compose_node
    loader.construct_mapping = construct_mapping
    try:
        python_data = loader.get_single_data()
    except ParserError as error:
        print("YAML syntax error parsing file {} :".format(filename),
              file=sys.stderr)
        print(error, file=sys.stderr)
        exit(1)
    return python_data
Exemple #2
0
def load_yaml(data):
    """
    Load YAML data extending it with line number information, nodes get a __line__ attribute
    """
    loader = Loader(data)

    def compose_node(parent, index):
        # the line number where the previous token has ended (plus empty lines)
        line = loader.line
        node = Composer.compose_node(loader, parent, index)
        node.__line__ = line + 1
        return node

    def construct_mapping(node, deep=False):
        mapping = Constructor.construct_mapping(loader, node, deep=deep)
        mapping['__line__'] = node.__line__
        return mapping
    loader.compose_node = compose_node
    loader.construct_mapping = construct_mapping
    data = loader.get_single_data()
    return data
Exemple #3
0
    def load(cls, stream, constructors=None, multi_constructors=None):
        loader = Loader(stream)

        cs = dict(cls._constructors)
        if constructors:
            cs.update(constructors)

        mcs = dict(cls._multi_constructors)
        if multi_constructors:
            mcs.update(multi_constructors)

        if cs:
            for name, constructor in cs.items():
                loader.add_constructor(name, constructor)

        if mcs:
            for name, constructor in mcs.items():
                loader.add_multi_constructor(name, constructor)

        try:
            return loader.get_single_data()
        finally:
            loader.dispose()
Exemple #4
0
    def load(cls, stream, constructors=None, multi_constructors=None):
        loader = Loader(stream)

        cs = dict(cls._constructors)
        if constructors:
            cs.update(constructors)

        mcs = dict(cls._multi_constructors)
        if multi_constructors:
            mcs.update(multi_constructors)

        if cs:
            for name, constructor in cs.items():
                loader.add_constructor(name, constructor)

        if mcs:
            for name, constructor in mcs.items():
                loader.add_multi_constructor(name, constructor)

        try:
            return loader.get_single_data()
        finally:
            loader.dispose()
Exemple #5
0
def load(stream, constructors=None):
    loader = Loader(stream)
    constructors = constructors or {}

    if not "timedelta" in constructors:
        loader.add_constructor("!timedelta", _timedelta_contructor)
    if not "re" in constructors:
        loader.add_constructor("!re", _re_constructor)

    loader.add_multi_constructor("!ref:", _ref_constructor)
    loader.add_multi_constructor("!factory:", _factory_constructor)
    loader.add_multi_constructor("!obj:", _obj_constructor)
    loader.add_multi_constructor("!extends:", _extends_constructor)
    loader.add_multi_constructor("!include:", _extends_constructor)

    if constructors:
        for name, constructor in constructors.items():
            loader.add_constructor("!" + name, constructor)

    try:
        return loader.get_single_data()
    finally:
        loader.dispose()