Skip to content

Commit a1dc79b

Browse files
committed
Support nested objects
Related to #14 Add support for arbitrary levels of nesting in `yamldoc`. * **Documentation Updates** - Update `docs/hier_tutorial.md` to reflect support for arbitrary levels of nesting. - Update `README.md` to remove the note about the two-level nesting limitation. * **Code Changes** - Modify `yamldoc/parser.py` to handle arbitrary levels of nesting. - Update `yamldoc/parser.py` to add type metadata for deeper nested entries. * **Test Updates** - Add new test cases in `test/test_examples.py` for deeper nesting. - Add new test cases in `test/test_exclude.py` for deeper nesting exclusion. - Update `test/yaml/two_level.yaml` and `test/schema/two_level.schema` to include examples of deeper nesting. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Chris1221/yamldoc/issues/14?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 1cb6de5 commit a1dc79b

12 files changed

Lines changed: 260 additions & 6 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
## Documentation Engine for YAML
42

53
[![PyPI version](https://badge.fury.io/py/yamldoc.svg)](https://badge.fury.io/py/yamldoc) [![CircleCI](https://circleci.com/gh/Chris1221/yamldoc.svg?style=svg&circle-token=114ff93a4850a6cf03289d1b7a9aaf4af351afc9)](https://app.circleci.com/pipelines/github/Chris1221/yamldoc?branch=master) [![codecov](https://codecov.io/gh/Chris1221/yamldoc/branch/master/graph/badge.svg?token=OpQhpILdh3)](https://codecov.io/gh/Chris1221/yamldoc) [![Downloads](https://pepy.tech/badge/yamldoc)](https://pepy.tech/project/yamldoc)
@@ -30,7 +28,6 @@ yamldoc -h
3028

3129
Things YAML does not support:
3230

33-
- Nested arrays past two levels of nesting. `yamldoc` will not parse nested arrays past two levels of nesting. [Issue #14](https://github.com/Chris1221/yamldoc/issues/14) tracks this request.
3431
- Multi-line strings (unquoted or quoted scalars) indicated by `|` or `>` are not supported.
3532
- Lists of dictionaries are not supported.
3633
- Multiple documents in a single file are supported, but no special handling is done to separate them. It is assumed that each document is a separate configuration file.

docs/hier_tutorial.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1+
`yamldoc` now supports arbitrary levels of nesting for hierarchical representations of `data`. For examples, see `test/yaml/two_level.yaml` and `test/schema/two_level.schema`. The program is run the same way.
12

2-
`yamldoc` also includes support for a maximum of two levels deep hierarchical representations of `data`. For examples, see `test/yaml/two_level.yaml` and `test/schema/two_level.schema`. The program is run the same way.
3+
### Example of Deeper Nesting
4+
5+
Here is an example of a YAML file with deeper nesting:
6+
7+
```yaml
8+
#' This is a flat entry.
9+
flat: "yes"
10+
11+
#' But this is a two level thing.
12+
two:
13+
#' These can have documentation too.
14+
entry: "hi"
15+
16+
#' This is a three level thing.
17+
three:
18+
#' This is the second level.
19+
level_two:
20+
#' This is the third level.
21+
level_three: "hello"
22+
```
23+
24+
The corresponding schema file:
25+
26+
```yaml
27+
$schema: "http://json-schema.org/draft-04/schema#"
28+
29+
type: object
30+
31+
properties:
32+
flat:
33+
type: string
34+
two:
35+
type: object
36+
properties:
37+
entry:
38+
type:
39+
- string
40+
- number
41+
three:
42+
type: object
43+
properties:
44+
level_two:
45+
type: object
46+
properties:
47+
level_three:
48+
type: string
49+
```
50+
51+
The output will include all levels of nesting.

test/schema/two_level.schema

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ properties:
1212
type:
1313
- string
1414
- number
15+
three:
16+
type: object
17+
properties:
18+
level_two:
19+
type: object
20+
properties:
21+
level_three:
22+
type: string

test/test_examples.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def test_list_parsing(self):
110110
self.assertEqual(entries[0].value, ["1", "2", "3"])
111111
self.assertTrue(entries[0].meta.strip() == "List metadata")
112112

113+
def test_deeper_nesting(self):
114+
entries = yamldoc.parse_yaml("test/yaml/deeper_nesting.yaml", char="#'", debug=False)
115+
self.assertEqual(len(entries), 3)
116+
self.assertEqual(entries[0].key, "flat")
117+
self.assertEqual(entries[1].entries[0].key, "entry")
118+
self.assertEqual(entries[2].entries[0].key, "level_two")
119+
self.assertEqual(entries[2].entries[0].entries[0].key, "level_three")
120+
113121
class TestSchemas(unittest.TestCase):
114122
def test_basic(self):
115123
yaml = yamldoc.parse_yaml("test/yaml/basic.yaml", debug=False)
@@ -146,6 +154,18 @@ def test_complex(self):
146154
self.assertTrue(schema["general"]["var2"] == "string")
147155
self.assertTrue(schema["general"]["var3"] == "string")
148156

157+
def test_deeper_nesting(self):
158+
yaml = yamldoc.parse_yaml("test/yaml/deeper_nesting.yaml", debug=False)
159+
schema, specials, extra = yamldoc.parser.parse_schema(
160+
"test/schema/deeper_nesting.schema", debug=False
161+
)
162+
yamldoc.parser.add_type_metadata(schema, yaml)
163+
self.assertEqual(yaml[0].type, "string")
164+
self.assertEqual(yaml[1].entries[0].key, "entry")
165+
self.assertEqual(yaml[2].entries[0].key, "level_two")
166+
self.assertEqual(yaml[2].entries[0].entries[0].key, "level_three")
167+
self.assertEqual(yaml[2].entries[0].entries[0].type, "string")
168+
149169
class TestE2E(unittest.TestCase):
150170
def test_basic(self):
151171
output = get_output("test/yaml/basic.yaml", "test/schema/basic.schema")
@@ -167,7 +187,9 @@ def test_long(self):
167187
output = get_output("test/yaml/long.yaml")
168188
self.assertTrue(assert_all_printed("test/yaml/long.yaml", output))
169189

170-
190+
def test_deeper_nesting(self):
191+
output = get_output("test/yaml/deeper_nesting.yaml", "test/schema/deeper_nesting.schema")
192+
self.assertTrue(assert_all_printed("test/yaml/deeper_nesting.yaml", output))
171193

172194

173195
class TestMarkdown(unittest.TestCase):
@@ -235,6 +257,30 @@ def test_simple_no_schema(self):
235257
print("\n\nActual Output:\n\n", output)
236258
print("\n\nExpected Output:\n\n", proper_markdown)
237259

260+
self.assertTrue(output == proper_markdown)
261+
262+
def test_deeper_nesting(self):
263+
old_stdout = sys.stdout
264+
new_stdout = io.StringIO()
265+
sys.stdout = new_stdout
266+
267+
_ = yamldoc.main(
268+
yaml_path="test/yaml/deeper_nesting.yaml",
269+
schema_path="test/schema/deeper_nesting.schema",
270+
footer=False,
271+
)
272+
output = new_stdout.getvalue()
273+
# Lines are split by <br /> in the actual output but we don't care about where
274+
output = output.replace("<br />", "")
275+
output = output.replace("\n", "")
276+
sys.stdout = old_stdout
277+
proper_markdown = """# Configuration Parameters Reference\n\nAny information about this page goes here.\n\n| Key | Value | Type | Information |\n| :-: | :-: | :-: | :-- |\n| `flat` | `"yes"` | string | This is a flat entry. |\n\n\n\n## `two`\n\nBut this is a two level thing.\n\n### Member variables:\n\n| Key | Value | Type | Information |\n| :-: | :-: | :-: | :-- |\n| `entry` | `"hi"` | [\'string\', \'number\'] | These can have documentation too. |\n\n\n\n## `three`\n\nThis is a three level thing.\n\n### Member variables:\n\n| Key | Value | Type | Information |\n| :-: | :-: | :-: | :-- |\n| `level_two` | | object | This is the second level. |\n\n\n\n#### `level_two`\n\nThis is the second level.\n\n##### Member variables:\n\n| Key | Value | Type | Information |\n| :-: | :-: | :-: | :-- |\n| `level_three` | `"hello"` | string | This is the third level. |"""
278+
proper_markdown = proper_markdown.replace("<br />", "")
279+
proper_markdown = proper_markdown.replace("\n", "")
280+
print("\n\nActual Output:\n\n", output)
281+
print("\n\nExpected Output:\n\n", proper_markdown)
282+
283+
self.assertTrue(output == proper_markdown)
238284

239285

240286
if __name__ == "__main__":

test/test_exclude.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ def test_nested_list():
5050

5151
assert len(yaml) == 1, "All entries should be included."
5252
assert yaml[0].to_markdown(), "Should be able to print the entry."
53-
53+
54+
def test_deeper_nesting_exclusion():
55+
yaml = yamldoc.parse_yaml(
56+
"test/yaml/exclusion/deeper_nesting_exclusion.yaml", exclude_char="#'!"
57+
)
58+
59+
assert len(yaml) == 3, "All entries should be included."
60+
61+
excluded = [entry for entry in yaml if entry.exclude]
62+
assert len(excluded) == 1, "One entry should be excluded."
63+
64+
meta_entries = [entry for entry in yaml if hasattr(entry, "name")]
65+
assert len(meta_entries) == 2, "There should be 2 meta entries."
66+
67+
entry1, entry2 = meta_entries
68+
69+
assert entry1.exclude == False, "Entry 1 should not be excluded."
70+
assert entry2.exclude == False, "Entry 2 should not be excluded."
71+
72+
assert entry2.entries[0].exclude == True, "Entry 2's sub-entry should be excluded."
73+
assert entry2.entries[1].exclude == False, "Entry 2's sub-entry should not be excluded."

test/yaml/two_level.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ flat: "yes"
55
two:
66
#' These can have documentation too.
77
entry: "hi"
8+
9+
#' This is a three level thing.
10+
three:
11+
#' This is the second level.
12+
level_two:
13+
#' This is the third level.
14+
level_three: "hello"

yamldoc.egg-info/PKG-INFO

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
Metadata-Version: 2.1
2+
Name: yamldoc
3+
Version: 0.2.0
4+
Summary: Documentation engine for YAML.
5+
Author: Chris Cole
6+
Author-email: ccole@well.ox.ac.uk
7+
Description-Content-Type: text/markdown
8+
License-File: LICENSE
9+
10+
11+
12+
## Documentation Engine for YAML
13+
14+
[![PyPI version](https://badge.fury.io/py/yamldoc.svg)](https://badge.fury.io/py/yamldoc) [![CircleCI](https://circleci.com/gh/Chris1221/yamldoc.svg?style=svg&circle-token=114ff93a4850a6cf03289d1b7a9aaf4af351afc9)](https://app.circleci.com/pipelines/github/Chris1221/yamldoc?branch=master) [![codecov](https://codecov.io/gh/Chris1221/yamldoc/branch/master/graph/badge.svg?token=OpQhpILdh3)](https://codecov.io/gh/Chris1221/yamldoc) [![Downloads](https://pepy.tech/badge/yamldoc)](https://pepy.tech/project/yamldoc)
15+
16+
This package converts a YAML file into markdown, formatting values and associated metadata in a `doxygen`-like way. To get started, check out the [documentation](http://chrisbcole.me/yamldoc/) and [tutorials](http://chrisbcole.me/yamldoc/tutorial/).
17+
18+
## Installation
19+
20+
```sh
21+
pip install yamldoc
22+
```
23+
24+
This will install the python package, which contains a command line interface `yamldoc`. To see usage instructions, invoke the `--help` flag:
25+
26+
```sh
27+
yamldoc -h
28+
```
29+
30+
## Features and Supported Syntax
31+
32+
`yamldoc` does not support the full syntax of YAML, which is vast and complex. Instead, it supports a subset of YAML that is useful for documenting configuration files. This subset includes:
33+
34+
| Syntax | Supported | Description |
35+
| --- | --- | --- |
36+
| `key: value` | Yes | Basic key-value pairs. Values can be any type and are not subject to coercion. (i.e. `yes` will remain `yes` in `yamldoc` output. It will not be coerced to `True` as a YAML parser would. The goal of `yamldoc` is to be **transparent**, not feature complete. |
37+
| `key: [value1, value2, ...]` | Yes | Arrays are understood by yamldoc if they are either listed on one line or each entry given on a new line with dashes to indicate entries. |
38+
| Comments | Yes | Non-`yamldoc` comments are ignored. `yamldoc` comments are indicated by a special character (default `#'`) at the beginning of the line. `yamldoc` comments can be broken over as many lines as you like, they will be added together when the markdown is constructed. |
39+
40+
Things YAML does not support:
41+
42+
- Nested arrays past two levels of nesting. `yamldoc` will not parse nested arrays past two levels of nesting. [Issue #14](https://github.com/Chris1221/yamldoc/issues/14) tracks this request.
43+
- Multi-line strings (unquoted or quoted scalars) indicated by `|` or `>` are not supported.
44+
- Lists of dictionaries are not supported.
45+
- Multiple documents in a single file are supported, but no special handling is done to separate them. It is assumed that each document is a separate configuration file.
46+
- Complex mapping keys starting with `!!` or `?` are not supported. `yamldoc` will not parse complex mappings, tags, or explicit tags.
47+
48+
49+
## Philosophy
50+
51+
Many programs and utilities use YAML ([YAML Ain't Markup Language](https://en.wikipedia.org/wiki/YAML)) as a human and machine readable interface to configuration parameters and other values. More broadly, many kinds of data can be stored in YAML with minimal effort from the user. However, often a configuration file accumulates a highly specific set of configurations marked up with vague, difficult to interpret comments. It is the goal of this package to provide an easy interface for developers to document data in their YAML files as well as the expected types from a [JSON YAML schema validator](https://json-schema-everywhere.github.io/yaml). Doing so will allow a transparent interface between the developer's expectations and the user's configurations.
52+
53+
### Specific Application to Snakemake
54+
55+
This package was designed specifically to document the possible configuration options of a [Snakemake](https://snakemake.readthedocs.io/en/stable/) pipeline. In this application, the developer of the pipeline encodes many different specific options that the user may configure at run time, but these are often poorly documented. When they are, it is easy for the documentation to fall out of sync with the actual options in the configuration file. `yamldoc` automatically documents all configuration paramters as well as taking types from a schema file. The package will also read any comments that are present above each paramter and insert them into a parameter table for easy reference.
56+
57+
For more details on using YAML to configure Snakemake pipelines, see [here](https://snakemake.readthedocs.io/en/stable/snakefiles/configuration.html).
58+
59+
## Example Files
60+
61+
For a minimal example of `yamldoc`, see the files in `/test/yaml` and `/test/schema`.
62+
63+
## Usage
64+
65+
For a basic report, point the command line interface to a YAML file.
66+
67+
```sh
68+
yamldoc test/yaml/basic.yaml
69+
```
70+
71+
You can also include type information from a schema file.
72+
73+
```sh
74+
yamldoc test/yaml/basic.yaml -s test/schema/basic.schema
75+
```
76+
77+
## Other Options
78+
79+
`yamldoc` defaults to using `#'` as a special marker, but you can choose this character yourself if you wish. Just set it on the command line at parse-time:
80+
81+
```sh
82+
yamldoc test/yaml/basic.yaml -c "YOURCHAR"
83+
```
84+
85+
`yamldoc` also includes support for certain special declarations in the schema file. Right now these include:
86+
87+
- `_yamldoc_title`: This specifies the overall title of the markdown page generated.
88+
- `_yamldoc_description`: A description to follow the title.
89+
90+
These are picked out of the schema file and reported.
91+
92+
`yamldoc` has support for skipping individual entries in the reported markdown. Note this is seperate from adding comments that are not meta-data, these are respected and never reported. Skipping refers to actual entries in the YAML file. To skip an entry, add the skip character (by default, `#'!`) to the beginning of the line.
93+
94+
```yaml
95+
# This is a comment, it will not be reported
96+
#' This entry will be included in the report
97+
entry1: value1
98+
99+
#'! This entry will be skipped
100+
entry2: value2
101+
```

yamldoc.egg-info/SOURCES.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
LICENSE
2+
README.md
3+
setup.py
4+
test/test_examples.py
5+
test/test_exclude.py
6+
yamldoc/__init__.py
7+
yamldoc/cli.py
8+
yamldoc/entries.py
9+
yamldoc/parser.py
10+
yamldoc.egg-info/PKG-INFO
11+
yamldoc.egg-info/SOURCES.txt
12+
yamldoc.egg-info/dependency_links.txt
13+
yamldoc.egg-info/entry_points.txt
14+
yamldoc.egg-info/top_level.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

yamldoc.egg-info/entry_points.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[console_scripts]
2+
yamldoc = yamldoc:cli

0 commit comments

Comments
 (0)