def schema2rst(data: dict, excluded_key: str, tree_name: str): """Mimics parser.schema2rst method with string input parameters""" tree = TreeNode(tree_name) rst = RST_DIRECTIVES TreeNode.dict2tree(data, tree, excluded_key) rst += _traverse_bfs(tree, _node2rst) return rst
def test_dict2tree_simple_dict(): expected = TreeNode('Root') child_1 = TreeNode('value1: foo', expected) child_2 = TreeNode('value2: bar', expected) dictionary = {'value1': 'foo', 'value2': 'bar'} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_simple_dict_with_integers(): expected = TreeNode('Root') child_1 = TreeNode('value1: 1', expected) child_2 = TreeNode('value2: 2', expected) dictionary = {'value1': 1, 'value2': 2} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_with_none_input_on_two_entries_gives_tree_with_two_child(): expected = TreeNode('Root') child_1 = TreeNode('value1: foo', expected) child_2 = TreeNode('value2: bar', expected) dictionary = {'value1': 'foo', 'value2': 'bar'} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_dict_with_dict(): expected = TreeNode('Root') child_1 = TreeNode('dict1', expected) child_2 = TreeNode('dict2', child_1) child_3 = TreeNode('value: foo', child_2) dictionary = {'dict1': {'dict2': {'value': 'foo'}}} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_list_of_list(): expected = TreeNode('Root') child_1 = TreeNode('list', expected) child_intermediate = TreeNode(0, child_1) child_1_1 = TreeNode('value: foo', child_intermediate) dictionary = {'list': [[{'value': 'foo'}]]} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_two_entries_appends_two_child_to_given_tree(): expected = TreeNode() child_1 = TreeNode('value1: foo', expected) child_2 = TreeNode('value2: bar', expected) dictionary = {'value1': 'foo', 'value2': 'bar'} result = TreeNode() result = TreeNode.dict2tree(dictionary, result) assert result == expected
def schema2rst(schema_file, excluded_key): """ Parse a json/yaml schema file into RST text. Example: with open("schema.json") as schema: print(jsonschema2rst(schema)) Args: schema_file(file): a json or yaml schema file descriptor. excluded_key(string): csv containing schema's keywords to ignore Returns: string: a restructured-text string representing ``schema_file`` """ tree = TreeNode( os.path.basename(change_extension(schema_file.name, JSON_EXTENSION))) rst = RST_DIRECTIVES TreeNode.dict2tree(yaml.load(schema_file), tree, excluded_key) rst += _traverse_bfs(tree, _node2rst) return rst
def test_dict2tree_none_parent_gives_tree_with_standard_root_value(): expected = TreeNode('Root') result = TreeNode.dict2tree({}, None) assert result == expected
def test_dict2tree_none_dict_gives_just_a_node(): expected = TreeNode('Root') result = TreeNode.dict2tree(None, None) assert result == expected