From: Steve H. <sh...@zi...> - 2002-07-30 00:28:07
|
From: "Tom Harris" <To...@op...> > We use XML for serialization/configuration and its a pain, yaml looks much > more suitable. However, we typically use a few main files that include other > XML files as external entities, this allows the configuration for a > particular library to be kept apart from anything else, so we can update the > library and its configuration in the field with only a few files. There > appears to be no support for external-entity-like things in yaml (and I can > see why this is a good thing), how would I go about using yaml with files > including other yaml files? > There is a way to do it with the Python implementation, although YAML purists might consider it a little kludgy. You would mark nodes in your YAML that should act like "include" statements with a type indicator of "!include", and then when you go to parse the YAML, you would give the YAML library a type-resolving object that reads in the included YAML files. The Python library is reentrant, so the code below works as expected: ==== import yaml def writeFile(fn, data): f = open(fn, 'w') f.write(data) writeFile('toplevel.yml', """ - doc1: !!include doc1.yml - doc2: !!include doc2.yml """) writeFile('doc1.yml', "foo: bar") writeFile('doc2.yml', "more: stuff") class Includer: def resolveType(self, value, url): return yaml.loadFile(value).next() print yaml.loadFile('toplevel.yml', Includer()).next() ===== Here is the output: [{'doc1': {'foo': 'bar'}}, {'doc2': {'more': 'stuff'}}] If you're interested in the Python implementation specifically, let me know. I made a tiny patch to the library to make this code work, which will be in future tarballs, but which is not yet up on the website. There would be a solution for automatically emitting the YAML with the "!!include"s as well, but I haven't gotten there quite yet. For configuration files, you might be creating the files manually anyway. For pure serialization applications, there's probably no point in breaking out the files, although I haven't thought it through. BTW the notion of including files may eventually become part of core YAML, but we have been waiting for a use case. Sounds like you have one. :) -- Steve |