Round-trip parsing of data structure format (YAML or whatnot) preserving comments, for writing configuration
If you are using block structured YAML and Python is acceptable, youcan use the Python package¹ ruamel.yaml which is a derivative ofPyYAML and supports round trip preservation of comments:
import sysimport ruamel.yamlinp = """\# examplename: # details family: Smith # very common given: Alice # one of the siblings"""yaml = ruamel.yaml.YAML()code = yaml.load(inp)code['name']['given'] = 'Bob'yaml.dump(code, sys.stdout)
with result:
# examplename: # details family: Smith # very common given: Bob # one of the siblings
Note that the end-of-line comments are still aligned.
Instead of normal list
and dict
objects the code
consists ofwrapped versions² on which the comments attached.
¹ Install with pip install ruamel.yaml
. Works on Python 2.6/2.7/3.3+.Disclaimer: I am the author of that package.
² ordereddict
is used in case of a mapping, to preserve ordering
Yeah, you and everyone who thought wow, yaml sounds cool, simply put, it doesn't exist, yet
update: you probably want to use Config::General, its apache config format (xmlish)
No, PPI is not general purpose tool, if you want BNF-ness, you want to use Marpa
Of all INI/JSON/YAML/XML, XML probably has the best editor support for non-programmers (sounds crazy)