Ejemplo n.º 1
0
 def test_variable_level_children(self):
     graph = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 body="Text",
                 children=[
                     g.Node(
                         section="## Subtitle",
                         body="Text",
                         children=[
                             g.Node(
                                 section="### Subtitle 2",
                                 body="Text",
                             )
                         ],
                     ),
                     g.Node(
                         section="## Subtitle 3",
                         body="Text",
                     ),
                 ],
             )
         ],
     )
     gold = "# Title\nText\n\n## Subtitle\nText\n\n### Subtitle 2\nText\n\n## Subtitle 3\nText"
     output = dumps(graph)
     self.assertEqual(gold, output)
Ejemplo n.º 2
0
 def test_increasingly_nested(self):
     graph = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 body="Text",
                 children=[
                     g.Node(
                         section="## Subtitle",
                         body="More text",
                         children=[
                             g.Node(
                                 section="### Sub-subtitle",
                                 body="even more text",
                             )
                         ],
                     )
                 ],
             )
         ],
     )
     gold = "# Title\nText\n\n## Subtitle\nMore text\n\n### Sub-subtitle\neven more text"
     output = dumps(graph)
     self.assertEqual(gold, output)
Ejemplo n.º 3
0
    def test_same_graph(self):
        graph1 = g.Node(section="# Title", body="Some text")
        graph2 = g.Node(section="# Title", body="Some text")
        gold = g.Node()

        output = update(graph1, graph2)
        self.assertEqual(gold, output)
Ejemplo n.º 4
0
 def test_increasingly_nested(self):
     readme = "# Title\nText\n\n## Subtitle\nMore text\n\n### Sub-subtitle\neven more text"
     gold = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 body="Text",
                 children=[
                     g.Node(
                         section="## Subtitle",
                         body="More text",
                         children=[
                             g.Node(
                                 section="### Sub-subtitle",
                                 body="even more text",
                             )
                         ],
                     )
                 ],
             )
         ],
     )
     output = readme_to_graph(readme)
     self.assertEqual(gold, output)
Ejemplo n.º 5
0
 def test_variable_level_children(self):
     readme = "# Title\nText\n\n## Subtitle\nText\n\n### Subtitle 2\nText\n\n## Subtitle 3\nText"
     gold = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 body="Text",
                 children=[
                     g.Node(
                         section="## Subtitle",
                         body="Text",
                         children=[
                             g.Node(
                                 section="### Subtitle 2",
                                 body="Text",
                             )
                         ],
                     ),
                     g.Node(
                         section="## Subtitle 3",
                         body="Text",
                     ),
                 ],
             )
         ],
     )
     output = readme_to_graph(readme)
     self.assertEqual(gold, output)
Ejemplo n.º 6
0
 def test_section_no_body_no_children(self):
     graph = g.Node(
         section="$root",
         children=[g.Node(section="# Title")],
     )
     gold = "# Title"
     output = dumps(graph)
     self.assertEqual(gold, output)
Ejemplo n.º 7
0
 def test_section_no_body_no_children(self):
     readme = "# Title"
     gold = g.Node(
         section="$root",
         children=[g.Node(section="# Title")],
     )
     output = readme_to_graph(readme)
     self.assertEqual(gold, output)
Ejemplo n.º 8
0
 def test_one_fnc_no_preamble(self):
     module = ONE_FNC_NO_PREAMBLE
     name = "module"
     level = 3
     gold = g.Node(
         section="### module",
         children=[g.Node(section="#### `test`", body="some text")],
     )
     output = ast_to_graph(module, name, level)
     self.assertEqual(gold, output)
Ejemplo n.º 9
0
    def test_remove_child_node(self):
        graph1 = g.Node(
            section="# Title",
            children=[g.Node(section="## Subtitle", body="text")],
        )
        graph2 = g.Node(section="## Subtitle")
        gold = g.Node(section="# Title")

        output = update(graph1, graph2)
        self.assertEqual(gold, output)
Ejemplo n.º 10
0
 def test_different_section_same_p_section(self):
     graph1 = g.Node(
         section="$root",
         children=[g.Node(section="## Subtitle", body="Some text")],
     )
     graph2 = g.Node(
         section="### Subtitle",
         body="Some different text",
     )
     gold = True
     output = contains(graph1, graph2)
     self.assertEqual(gold, output)
Ejemplo n.º 11
0
 def test_empty_module(self):
     module = EMPTY_MODULE
     name = "test"
     level = 3
     gold = g.Node()
     output = ast_to_graph(module, name, level)
     self.assertEqual(gold, output)
Ejemplo n.º 12
0
 def test_two_fnc_w_preamble(self):
     module = TWO_FNC_W_PREAMBLE
     name = "module"
     level = 3
     gold = g.Node(
         section="### module",
         body="this is an intro",
         children=[
             g.Node(section="#### `test`", body="some text"),
             g.Node(
                 section="#### `test2`",
                 body="some text2",
             ),
         ],
     )
     output = ast_to_graph(module, name, level)
     self.assertEqual(gold, output)
Ejemplo n.º 13
0
 def test_module_with_one_fnc_w_docstring_no_example(self, ):
     module = ONE_FNC_W_DOCSTRING_NO_EXAMPLE
     level = 4
     gold = g.Node(
         section="#### `fnc`",
         body="This fnc does things",
     )
     output = function_to_graph(module, level)
     self.assertEqual(gold, output)
Ejemplo n.º 14
0
def function_to_graph(
    obj: ast.FunctionDef, level: int
) -> dict:
    docstring = ast.get_docstring(obj)
    preamble = preamble_from_docstring(docstring)
    example = example_from_rst_docstring(docstring)
    section = obj.name
    section = "`" + section + "`"
    section = "#" * level + " " + section

    if not preamble and not example:
        return {}

    if preamble and not example:
        return g.Node(section=section, body=preamble)

    body = preamble + "\n" + example
    return g.Node(section=section, body=body)
Ejemplo n.º 15
0
 def test_module_with_one_fnc_w_docstring_w_example_and_args(self, ):
     module = ONE_FNC_W_DOCSTRING_W_EXAMPLE_AND_ARGS
     level = 4
     gold = g.Node(
         section="#### `fnc`",
         body=
         "This fnc does things\n```python\n>>> from foo import bar\n```",
     )
     output = function_to_graph(module, level)
     self.assertEqual(gold, output)
Ejemplo n.º 16
0
 def test_variable_children_of_root(self):
     graph = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 children=[
                     g.Node(
                         section="## Purpose",
                         body="test",
                     )
                 ],
             ),
             g.Node(
                 section="### temp",
                 body="Top level docstring",
                 children=[
                     g.Node(
                         section="#### bar",
                         body="this is bar",
                     ),
                     g.Node(
                         section="#### baz",
                         body="this is baz\n```python\n>>> from temp import baz\n```",
                     ),
                 ],
             ),
         ],
     )
     gold = "# Title\n\n## Purpose\ntest\n\n### temp\nTop level docstring\n\n#### bar\nthis is bar\n\n#### baz\nthis is baz\n```python\n>>> from temp import baz\n```"
     output = dumps(graph)
     self.assertEqual(gold, output)
Ejemplo n.º 17
0
def ast_to_graph(
    module: ast.Module, name: str, level: int
) -> dict:
    section = "#" * level + " " + name

    children = [
        graph
        for graph in convert_objects(module, level + 1)
        if graph
    ]

    body = module_preamble(module)

    if not children and not body:
        return {}

    if not body:
        return g.Node(section=section, children=children)

    return g.Node(
        section=section, body=body, children=children
    )
Ejemplo n.º 18
0
 def test_mult_same_level_children(self):
     graph = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 body="Text",
                 children=[
                     g.Node(
                         section="## Subtitle",
                         body="More text",
                     ),
                     g.Node(
                         section="## Subtitle 2",
                         body="even more text",
                     ),
                 ],
             )
         ],
     )
     gold = "# Title\nText\n\n## Subtitle\nMore text\n\n## Subtitle 2\neven more text"
     output = dumps(graph)
     self.assertEqual(gold, output)
Ejemplo n.º 19
0
 def test_mult_same_level_children(self):
     readme = "# Title\nText\n\n## Subtitle\nMore text\n\n## Subtitle 2\neven more text"
     gold = g.Node(
         section="$root",
         children=[
             g.Node(
                 section="# Title",
                 body="Text",
                 children=[
                     g.Node(
                         section="## Subtitle",
                         body="More text",
                     ),
                     g.Node(
                         section="## Subtitle 2",
                         body="even more text",
                     ),
                 ],
             )
         ],
     )
     output = readme_to_graph(readme)
     self.assertEqual(gold, output)
Ejemplo n.º 20
0
def python_to_graph(path: str, level: int) -> dict:
    if isdir(path):
        section = splitext(basename(path))[0]
        section = "#" * level + " " + section

        children = [
            python_to_graph(child, level + 1)
            for child in abs_listdir(path)
        ]
        children = [child for child in children if child]

        if children:
            return g.Node(
                section=section, children=children
            )

    if isfile(path):
        return module_to_graph(path, level)
Ejemplo n.º 21
0
def update(graph1: dict, graph2: dict) -> dict:
    if graph1 == graph2:
        return {}

    graph1 = deepcopy(graph1)

    if not contains(graph1, graph2):
        children = g.node_children(graph1)
        children.append(graph2)

        return g.Node(
            section=g.node_section(graph1),
            body=g.node_body(graph1),
            children=children,
        )

    if not g.truth_value(graph2):
        return remove_node(graph1, graph2)

    return update_node(graph1, graph2)
Ejemplo n.º 22
0
def _modify(
    mod_graph: dict, mod_with: dict, on_eq: dict
) -> dict:
    if g.node_p_section(mod_graph) == g.node_p_section(
        mod_with
    ):
        return on_eq

    children = g.node_children(mod_graph)
    new_children = []

    for child in children:
        res = _modify(child, mod_with, on_eq)
        if res:
            new_children.append(res)

    return g.Node(
        section=g.node_section(mod_graph),
        body=g.node_body(mod_graph),
        children=new_children,
    )
Ejemplo n.º 23
0
def root_node() -> dict:
    return g.Node(section="$root")
Ejemplo n.º 24
0
def groups_to_nodes(groups: List[Tuple[str]], ) -> List[dict]:
    # TODO: handle pretty_section
    return [g.Node(section=group[0], body=group[1]) for group in groups]
Ejemplo n.º 25
0
 def test_null_graph(self):
     graph = g.Node()
     gold = ""
     output = dumps(graph)
     self.assertEqual(gold, output)
Ejemplo n.º 26
0
 def test_null_readme(self):
     readme = ""
     gold = g.Node(section="$root")
     output = readme_to_graph(readme)
     self.assertEqual(gold, output)
Ejemplo n.º 27
0
 def test_module_with_one_fnc_no_docstrings(self):
     module = ONE_FNC_NO_DOCSTRING
     level = 4
     gold = g.Node()
     output = function_to_graph(module, level)
     self.assertEqual(gold, output)