From 6e48627070ede8ab1f26a3d612749f4ea4053af8 Mon Sep 17 00:00:00 2001 From: weefuzzy Date: Thu, 10 Feb 2022 13:53:23 +0000 Subject: [PATCH 1/2] start of a common mechanism for parsing docutils afield_list`s to `dict`s --- flucoma/doc/rst/docutils.py | 19 +++++ flucoma/doc/test/test_fieldlist_parser.py | 88 +++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 flucoma/doc/test/test_fieldlist_parser.py diff --git a/flucoma/doc/rst/docutils.py b/flucoma/doc/rst/docutils.py index a3ae27df..5a553f7f 100644 --- a/flucoma/doc/rst/docutils.py +++ b/flucoma/doc/rst/docutils.py @@ -9,6 +9,25 @@ from docutils import nodes, utils from docutils.parsers.rst import roles +def parse_fieldlist(node): + """ + node should be the parent of a bunch of fields + """ + + res = {} + + f = node.next_node(nodes.field) + + while f is not None: + n = f.next_node(nodes.field_name) + v = f.next_node(nodes.field_body) + # print(n) + res[n.astext().strip()] = v.rawsource.strip() + f = f.next_node(nodes.field,siblings=True,descend=False) + + return (res if bool(res) else None) + + def fluid_object_role(role, rawtext, text, lineno, inliner, options={}, content=[]): """Create a link to a FluCoMa object diff --git a/flucoma/doc/test/test_fieldlist_parser.py b/flucoma/doc/test/test_fieldlist_parser.py new file mode 100644 index 00000000..b7120b55 --- /dev/null +++ b/flucoma/doc/test/test_fieldlist_parser.py @@ -0,0 +1,88 @@ +import unittest + +from docutils import nodes +from docutils.core import publish_doctree + +from flucoma.doc.rst.docutils import parse_fieldlist + +unnested = """\ + +:0: 0-stuff +:1: 1-stuff +:2: 2-stuff +""" + +nested_content="""\ +:0-0: nest0 +:0-1: nest1 +:0-2: nest2 +:0-3: + Let's put some more ``content`` **in** here + + * list0 + * list1 + * list2 + +:0-4: and relax\ +""" + +nested_nested_content="""\ +Let's put some more ``content`` **in** here + +* list0 +* list1 +* list2\ +""" + +nested1 = """\ +:0: + :0-0: nest0 + :0-1: nest1 + :0-2: nest2 + :0-3: + Let's put some more ``content`` **in** here + + * list0 + * list1 + * list2 + + :0-4: and relax + +:1: can we stop yet? +""" + +class TestFieldListParse(unittest.TestCase): + def test_unnested_parse(self): + tree = publish_doctree(source=unnested) + # print(tree.pformat()) + parsed = parse_fieldlist(tree) + + self.assertEqual(list(parsed.keys()),['0','1','2']) + self.assertEqual(list(parsed.values()),['0-stuff','1-stuff','2-stuff']) + + def test_nested_parse_top(self): + tree = publish_doctree(source=nested1) + parsed = parse_fieldlist(tree) + + self.assertEqual(list(parsed.keys()),['0','1']) + self.assertEqual(list(parsed.values()),[nested_content,'can we stop yet?']) + + def test_nested_parse_inner(self): + tree = publish_doctree(source=nested1) + + parent = tree.children[0] + + + for i,n in enumerate(parent.children): #top level fields + contains_subfields = n.next_node(nodes.field) + if i == 0: + self.assertTrue(contains_subfields) + else: + self.assertFalse(contains_subfields) + + if contains_subfields: + subfields = parse_fieldlist(n) + self.assertEqual(list(subfields.keys()),['0-0','0-1','0-2','0-3','0-4']) + self.assertEqual(list(subfields.values()),[ + 'nest0','nest1','nest2',nested_nested_content,'and relax' + ]) From 909056cf57a0e531eefa2a38824081215517e177 Mon Sep 17 00:00:00 2001 From: weefuzzy Date: Mon, 14 Feb 2022 16:14:47 +0000 Subject: [PATCH 2/2] ignore more variations on `build` --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c4f8df75..e3307eb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -build +build* json maxref maxref.zip