diff --git a/README.md b/README.md index fbd1755..f394d7f 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ modified. ```python doc = yamltrip.loads("items:\n - a\n - b") +doc.root # {"items": ["a", "b"]} doc["items"] # ["a", "b"] doc["items", 0] # "a" ("items", 0) in doc # True diff --git a/src/yamltrip/document.py b/src/yamltrip/document.py index ed60c6c..045dac6 100644 --- a/src/yamltrip/document.py +++ b/src/yamltrip/document.py @@ -93,6 +93,11 @@ def source(self) -> str: """The current YAML source text.""" return self._source + @property + def root(self) -> Any: + """The entire document parsed as a Python object.""" + return self[()] + def __eq__(self, other: object) -> bool: """Compare documents by their source text.""" if not isinstance(other, Document): diff --git a/src/yamltrip/editor.py b/src/yamltrip/editor.py index 01f35b6..898755e 100644 --- a/src/yamltrip/editor.py +++ b/src/yamltrip/editor.py @@ -83,6 +83,11 @@ def document(self) -> Document: raise RuntimeError(msg) return self._document + @property + def root(self) -> Any: + """The entire document parsed as a Python object.""" + return self.document.root + def __getitem__(self, keys: object) -> Any: """Retrieve the parsed value at the given path.""" return self.document[keys]