Esempio n. 1
0
def test_lists(values):
    expected = [normalize_line_breaks(v) for v in values]

    # test normal dump
    assert nt.loads(nt.dumps(values), top=list) == expected

    # test dump with inlines
    content = nt.dumps(values, width=999)
    assert nt.loads(content, top=list) == expected

    # test dump with inlines and random spaces
    if content[0:] in ['[', '{']:
        content = add_spaces(content, '{}[],')
        assert nt.loads(content, top=list) == expected
Esempio n. 2
0
File: gui.py Progetto: jhb/sqldoc
def edit_post(_docid: str,
              request: Request,
              doc: str = Form(default=''),
              subpath: str = '',
              addschema: str = Form(default='')):
    error = ''
    try:
        try:
            doc = loads(doc, top='any')
            if not doc:
                doc = {}
            doc, errors = reg.convert(doc, copy_unknown=True)
            if errors:
                raise Exception(errors)
            if type(doc) is dict and _docid in doc:
                del (doc['_docid'])
        except NestedTextError:
            doc = doc

        if addschema:
            existing = reg.find_schemata(doc)
            if addschema in reg and addschema not in existing:
                for key in reg.collect_keys(addschema):
                    if key not in doc:
                        doc[key] = ''

        if _docid == 'new':
            if type(doc) == str:
                doc = dict(data=doc)
            doc['_schemata'] = reg.find_schemata(doc)
            doc = sqldoc.create_doc(doc)
        else:
            old_doc: dict = sqldoc.read_doc(_docid)
            if subpath:
                set_by_path(old_doc, doc, subpath, config.delimiter)
            else:
                old_doc = doc
            old_doc['_docid'] = _docid
            old_doc['_schemata'] = reg.find_schemata(old_doc)
            doc = sqldoc.update_doc(old_doc)
        sqldoc.commit()
        _docid = doc['_docid']
        return RedirectResponse(f'/gui/edit/{_docid}',
                                status_code=status.HTTP_302_FOUND)

    except Exception as error:

        if type(doc) in [list, tuple, dict]:
            _ = {k: v for k, v in doc.items() if k not in forbidden}
            docstring = dumps(_, indent=2)
        else:
            docstring = ''
        return dict(doc=doc,
                    docstring=docstring,
                    _docid=_docid,
                    error=error,
                    helpers=helpers,
                    request=request)
Esempio n. 3
0
def test_dicts(data):
    expected = {
        normalize_line_breaks(k): normalize_line_breaks(v)
        for k, v in data.items()
    }

    # test normal dump
    result = nt.loads(nt.dumps(data), top=dict)
    assert nt.loads(nt.dumps(data), top=dict) == expected

    # test dump with inlines
    content = nt.dumps(data, width=999)
    assert nt.loads(content, top=dict) == expected

    # test dump with inlines and random spaces
    if content[0:] in ['[', '{']:
        spacey_content = add_spaces(content, '{}[],:')
        assert nt.loads(spacey_content, top=dict) == expected
Esempio n. 4
0
def extra_schema(doc):
    try:
        if type(doc)==str:
            doc=loads(doc)
        existing = reg.find_schemata(doc)
        possible = reg.schemata
        extra = list(set(possible) - set(existing))
    except Exception:
        extra = []
    return extra
Esempio n. 5
0
 def loads_src(content, _):
     return lambda: nt.loads(content, 'SOURCE'), 'SOURCE'
Esempio n. 6
0
 def loads(content, _):
     return lambda: nt.loads(content), None
Esempio n. 7
0
        elif "out" in case.case["load"]:
            logger.info("Checking successful load")
            expected = case.case["load"]["out"]["data"]
            with open(load_in_path, "r", encoding="utf-8") as f:
                actual = nt.load(f)
            assert actual == expected

            # Debug info.
            logger.debug("Loaded %s", load_in_path)
            with open(load_in_path, "r", encoding="utf-8") as f:
                logger.debug("\n%s", f.read())
            logger.debug("%s", json.dumps(actual))

            # Check loads() function too.
            with open(load_in_path, "r", encoding="utf-8") as f:
                actual2 = nt.loads(f.read())
            assert actual2 == expected

        elif "err" in case.case["load"]:
            logger.info("Checking load error")
            with pytest.raises(nt.NestedtextError):
                with open(load_in_path, "r", encoding="utf-8") as f:
                    nt.load(f)
            # TODO: Proper error checking

    if "dump" in case.case:
        if case.id in skip_dump_testcases:
            logger.warning("Skipping dump check for %s: %s", case.id,
                           skip_dump_testcases[case.id])

        elif "out" in case.case["dump"]:
Esempio n. 8
0
def test_strings(s):
    assert nt.loads(nt.dumps(s), top=str) == normalize_line_breaks(s)
Esempio n. 9
0
#!/usr/bin/env python3

from pprint import pprint

import nestedtext as nt

nt_string = """
int: 1
float: 3.14
str: foo
multiline:
  > hello
  > world
list:
  - foo
  - bar
"""

pprint(nt.loads(nt_string))