YAML as configuration format

Has anyone successfully used YAML as a configuration file format? I’m
sure it can be done–I’m not doubting that–but I’m curious as to the
recommended way of handling sequences. In XML, it’s pretty
straightforward, but in YAML it seems a little less natural. In other words:

can’t be done directly in YAML. Instead (as far as my limited YAML
knowledge goes) it seems like you have to do:

conf:
elements:

  • one: type
  • two: type
  • three: type

I’m not a big XML fan, so I’d love to find an alternative, but there are
times where YAML seems a little more awkward than XML. I’m chalking it
up to my lack of YAML experience.

On a related note, is there a site I can go to that details a kind of
"best practices" when using YAML? I’ve seen _why’s cookbook, and I’ve
looked at a few other YAML sites, but I’m curious as to how YAML is (and
should be) used in “real life”.

···


Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

ruby -h | ruby -e
’a=[];readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a <<
r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

Jamis Buck wrote:

Has anyone successfully used YAML as a configuration file format? I’m
sure it can be done–I’m not doubting that–but I’m curious as to the
recommended way of handling sequences. In XML, it’s pretty
straightforward, but in YAML it seems a little less natural. In other
words:

okay. yeah, let’s talk about this.

first of all, i’m not clear on the above example and i’d like to know
what you mean by the above.

my assumptions of the above would be:

  1. is a root-level element meant to enclose all tags pertinent to
    configuration.
  2. tags represent configuration settings. name' and type’
    are an equivalent of key/value pairs. you are mapping a setting to its
    value? name' would be a setting's name. honestly, i'm not sure what type’ means.
  3. these s are expected to retain their order.

are all three of these assumptions correct? consider #3 especially. is
it pertinent that these stay in order?

can’t be done directly in YAML. Instead (as far as my limited YAML
knowledge goes) it seems like you have to do:

conf:
elements:

  • one: type
  • two: type
  • three: type

well, how would you express these with ruby’s arrays and hashes? yaml’s
structures are no different.

i tend to use typing information to replace xml tags. to me it’s more
readable:

— !!conf

  • !!element {one: type}
  • !!element {two: type}
  • !!element {three: type}

the types above are private types. they are temporary types which are
temporary and could be replaced with domain types once you’ve got them
officially worked out.

i’m not suggesting that the above is a very good config file, but it’s
another way of representing xml.

why can’t you stick to a simple hash? you don’t need the conf' wrapper. and what are these elements’ that they need to be deeply
nested? if they need order, you could use an !omap.

— !omap

  • one: type
  • two: type
  • three: type

this will be loaded into ruby as a YAML::Omap object, which acts like an
ordered hash.

I’m not a big XML fan, so I’d love to find an alternative, but there are
times where YAML seems a little more awkward than XML. I’m chalking it
up to my lack of YAML experience.

i’m not sure what is so akward about even your example:

conf:
elements:
- one: type
- two: type
- three: type

it’s free of the angle brackets and quotes which plague your xml
equivalent. the white space further enforces the structure. and the
dashes reflect a forced ordering. really, i can’t imagine such a
structure expressed more elegantly.

if i’m missing the mark, i’d really appreciate some concrete scenarios
which could be fleshed out.

_why

I’m doing it, but the configuration files are pretty small and simple:

socket:
hostname: localhost
port: 11111
logging:
basedir: /usr/local/htdocs/enigo.com/log
minlevel: 0
maxsize: 10000000
maxage: 86400

THis is just a quick example, but none of the config files that I am
currently using YAML for are much larger or more complex than this.

Kirk Haines

···

On Mon, 15 Mar 2004, Jamis Buck wrote:

Has anyone successfully used YAML as a configuration file format? I’m

why the lucky stiff wrote:

first of all, i’m not clear on the above example and i’d like to know
what you mean by the above.

Sorry. I tried to condense a much-more complex real-life situation into
a bite-sized example and it lost a lot of its relevancy. Let me try
to just explain what I’m trying to do, and perhaps fish for some
suggestions without any of my own biased implementations tainting them.

I’m trying to implement an Apache Digester work-alike, in Ruby, using
YAML instead of XML. (Actually, my ultimate goal is to create a Hivemind
[1] work-alike in Ruby, using YAML for the configuration format, but the
relevant portions of Hivemind I’m trying to tackle right now are
concepts borrowed from the Digester project, etc, etc.) And I’m trying
to do it without really understanding how the Digester library actually
works. It’s more of a “gee-that-sounds-like-a-great-idea,
I-wonder-if-I-could-do-that-in-Ruby” kind of thing.

So, I’m under-educated, feeling cocky, and dangerous with a Ruby
interpreter. And I’m sitting here with a crazy idea that I should
probably leave to better qualified individuals, only I imagine they’re
all busy with worthier projects (like a poignant guide or something).
Thus, it falls to me. It’s a great responsibility, but I’m proud to
accept it, blah blah blah.

So, if you’re unfamiliar with the Digester, then you’re not much behind
my own level of familiarity with it. On the other hand, if you’re an
old hand at using the Digester libraries, just flash your battle scars
in my direction and kindly point out any gross errors I’m making in my
interpretation of it.

According to my rapid scan of the Digester [2] website, the digester is
for parsing XML configuration files. You define a schema of “rules”,
which map to rule objects, each of which has a well defined behavior.
The rules operate on a stack, etc, etc. As XML, the tag name defines
the name of the rule, with the attributes of the tag being rule-specific
and (naturally) corresponding to different parameters of the tag.

(I really, really hope I’m making sense, since this will be a pitiful
waste of bandwidth otherwise.)

So, you’ve got a sequence (ordered) of rules. Each rule is guaranteed
to have at least a name. It may have other attributes, which will be
rule-specific.

In YAML (and I’m going off half-cocked again, knowing only enough YAML
to be dangerous and incoherent):

rules:

  • { name: create-object, class: PatientData }
  • { name: set-attribute, name: ailment, value: hemorrhoids }
  • { name: invoke-parent, method: push }

The above psuedo-snippet would (given an arbitrary interpretation of the
Hivemind-like rules used) instantiate PatientData and push the new
object onto the stack. Then, it would set the “ailment” attribute of
the topmost stack item to “hemorrhoids”. Then, it would invoke the
“push” method of the next-to-topmost stack element, with the topmost
stack element as the parameter.

Now, I’ve probably answered my own question here; when I posted the
original message I didn’t know about the {…} shortcut for defining a
map, and was mildly sorry I couldn’t define an entire rule on a single
line (as above). That’s what I meant when I said that YAML felt a
little more awkward than XML.

The one-line map definition certainly feels nicer to me.

My question now is: is this an appropriate use of YAML? How could using
“types” (which I am unfamiliar with in YAML) make this
cleaner/better/easier? And lastly, is there a better way to represent
data like this in YAML?

I hope that was clearer than my original post. If it wasn’t, or if more
clarifications are needed anyway, I’ll do my best to comply.

Thanks for listening.

  • Jamis

[1] http://jakarta.apache.org/commons/sandbox/hivemind
[2] Digester - Commons

···


Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

ruby -h | ruby -e
‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a <<
r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

I have a bit more complicated example here:

http://savannah.nongnu.org/cgi-bin/viewcvs/samizdat/samizdat/config.yaml?rev=1.99

although it is not as scary as what Jamis seems to want.

···

On Mon, Mar 15, 2004 at 12:26:30PM +0900, Kirk Haines wrote:

Has anyone successfully used YAML as a configuration file format?
I’m doing it, but the configuration files are pretty small and simple


Dmitry Borodaenko