def test_stringsource():
    expect = b"hinz & kunz"
    source = parse.String(expect)
    for i in range(3):
        parsed = b"".join(data for (evtype, data) in source
                          if evtype == "bytes")
        assert parsed == expect
def test_base():
    e = parse.tree(parse.String(
        b'<a xmlns="http://www.w3.org/1999/xhtml" href="gurk.html"/>',
        'http://www.gurk.de/'),
                   parse.Expat(ns=True),
                   parse.Node(pool=xsc.Pool(html)),
                   validate=True)
    assert str(e[0].attrs.href) == "http://www.gurk.de/gurk.html"
def test_parsestringurl():
    # Base URLs should end up in the location info of the resulting XML tree
    node = parse.tree(b"gurk",
                      parse.SGMLOP(),
                      parse.NS(),
                      parse.Node(),
                      validate=True)
    assert str(node[0].startloc.url) == "STRING"

    node = parse.tree(parse.String(b"gurk", url="root:gurk.xmlxsc"),
                      parse.SGMLOP(), parse.NS(), parse.Node())
    assert str(node[0].startloc.url) == "root:gurk.xmlxsc"
Example #4
0
def getdoc(thing, format):
    if thing.__doc__ is None:
        return xsc.Null

    # Remove indentation
    lines = textwrap.dedent(thing.__doc__).split("\n")

    # remove empty lines
    while lines and not lines[0]:
        del lines[0]
    while lines and not lines[-1]:
        del lines[-1]

    text = "\n".join(lines)

    modulename = _getmodulename(thing)
    if inspect.ismethod(thing):
        base = f"METHOD-DOCSTRING({modulename}.{thing.__class__.__name__}.{thing.__qualname__})"
    elif isinstance(thing, property):
        base = f"PROPERTY-DOCSTRING({modulename}.{thing})"
    elif inspect.isfunction(thing):
        base = f"FUNCTION-DOCSTRING({modulename}.{thing.__qualname__})"
    elif inspect.isclass(thing):
        base = f"CLASS-DOCSTRING({modulename}.{thing.__qualname__})"
    elif inspect.ismodule(thing):
        base = f"MODULE-DOCSTRING({modulename})"
    else:
        base = "DOCSTRING"

    lformat = format.lower()
    if lformat == "plaintext":
        return xsc.Text(text)
    elif lformat == "restructuredtext":
        from ll.xist.ns import rest, doc
        return rest.fromstring(text, base=base).conv(target=doc)
    elif lformat == "xist":
        from ll.xist.ns import doc
        node = parse.tree(parse.String(text), parse.SGMLOP(), parse.NS(doc),
                          parse.Node(pool=xsc.docpool(), base=base))
        if not node[
                p]:  # optimization: one paragraph docstrings don't need a <p> element.
            node = p(node)

        if inspect.ismethod(thing):
            # Use the original method instead of the decorator
            realthing = thing
            while hasattr(realthing, "__wrapped__"):
                realthing = realthing.__wrapped__
            for ref in node.walknodes(pyref):
                if "module" not in ref.attrs:
                    ref["module"] = _getmodulename(realthing)
                    if "class_" not in ref.attrs:
                        ref["class_"] = thing.__self__.__class__.__name__
                        if "method" not in ref.attrs:
                            ref["method"] = thing.__name__
        elif inspect.isfunction(thing):
            # Use the original method instead of the decorator
            while hasattr(thing, "__wrapped__"):
                thing = thing.__wrapped__
            for ref in node.walknodes(pyref):
                if "module" not in ref.attrs:
                    ref["module"] = _getmodulename(thing)
        elif inspect.isclass(thing):
            for ref in node.walknodes(pyref):
                if "module" not in ref.attrs:
                    ref["module"] = _getmodulename(thing)
                    if "class_" not in ref.attrs:
                        ref["class_"] = thing.__name__
        elif inspect.ismodule(thing):
            for ref in node.walknodes(pyref):
                if "module" not in ref.attrs:
                    ref["module"] = thing.__name__
        return node
    else:
        raise ValueError(f"unsupported __docformat__ {format!r}")