Пример #1
0
def test_anchors_aliases():
    text = """
    left hand: &A
      name: The Bastard Sword of Eowyn
      weight: 30
    right hand: *A
    """
    with pytest.raises(yaml.YAMLError):
        yaml.load(text)
Пример #2
0
def parse_docs(docs, marks):
    """
    Parse YAML syntax content from docs

    If docs is None, return {}
    If docs has no YAML content, return {"$desc": docs}
    Else, parse YAML content, return {"$desc": docs, YAML}

    Args:
        docs (str): docs to be parsed
        marks (list): list of which indicate YAML content starts
    Returns:
        A dict contains information of docs
    """
    if docs is None:
        return {}
    indexs = []
    for mark in marks:
        i = docs.find(mark)
        if i >= 0:
            indexs.append(i)
    if not indexs:
        return {"$desc": textwrap.dedent(docs).strip()}
    start = min(indexs)
    start = docs.rfind("\n", 0, start)
    yamltext = textwrap.dedent(docs[start + 1:])
    meta = yaml.load(yamltext)
    meta["$desc"] = textwrap.dedent(docs[:start]).strip()
    return meta
Пример #3
0
def parse_docs(docs, marks):
    """
    Parse YAML syntax content from docs

    If docs is None, return {}
    If docs has no YAML content, return {"$desc": docs}
    Else, parse YAML content, return {"$desc": docs, YAML}

    Args:
        docs (str): docs to be parsed
        marks (list): list of which indicate YAML content starts
    Returns:
        A dict contains information of docs
    """
    if docs is None:
        return {}
    indexs = []
    for mark in marks:
        i = docs.find(mark)
        if i >= 0:
            indexs.append(i)
    if not indexs:
        return {"$desc": textwrap.dedent(docs).strip()}
    start = min(indexs)
    start = docs.rfind("\n", 0, start)
    yamltext = textwrap.dedent(docs[start + 1:])
    meta = yaml.load(yamltext)
    meta["$desc"] = textwrap.dedent(docs[:start]).strip()
    return meta
Пример #4
0
def _parse_doc(doc, mark):
    """
    Parse YAML syntax data from doc

    if doc is None, return ('', OrderedDict())
    if doc has no YAML data, return (doc, OrderedDict())
    else, parse YAML data, return (doc, data)

    Args:
        doc (str): doc to be parsed
        mark (Regex): which marks the start position of data
    Returns:
        tuple(desc, data): data is an OrderedDict contains information of doc
    """
    if doc is None:
        return '', OrderedDict()
    match = mark.search(doc)
    if not match:
        return textwrap.dedent(doc).strip(), OrderedDict()
    start = match.start()
    yamltext = textwrap.dedent(doc[start:])
    try:
        data = yaml.load(yamltext)
    except yaml.parser.ParserError as ex:
        raise SchemaError(str(ex)) from None
    return textwrap.dedent(doc[:start]).strip(), data
Пример #5
0
def test_order():
    yamltext = """
        a:
            x: 1
            y: 2
            z: 3
        b: 0
        c: 0
        d: 0
    """
    for i in range(100):
        data = yaml.load(yamltext)
        assert isinstance(data, OrderedDict)
        assert isinstance(data['a'], OrderedDict)
        assert list(data['a'].items()) == [('x', 1), ('y', 2), ('z', 3)]
        del data['a']
        assert list(data.items()) == [('b', 0), ('c', 0), ('d', 0)]
Пример #6
0
def main():
    if len(sys.argv) == 1:
        infile = sys.stdin
        outfile = sys.stdout
    elif len(sys.argv) == 2:
        infile = open(sys.argv[1], 'rb')
        outfile = sys.stdout
    elif len(sys.argv) == 3:
        infile = open(sys.argv[1], 'rb')
        outfile = open(sys.argv[2], 'wb')
    else:
        raise SystemExit(sys.argv[0] + " [infile [outfile]]")
    with infile:
        try:
            obj = yaml.load(infile)
        except ValueError as e:
            raise SystemExit(e)
    with outfile:
        json.dump(obj, outfile, sort_keys=True,
                  indent=4, separators=(',', ': '))
        outfile.write('\n')
Пример #7
0
def test_special_yaml_char(text):
    yaml.load(text)