Parsing Puppet YAML log in Python

268 views
Skip to first unread message

Sharuzzaman Ahmat Raslan

unread,
Mar 15, 2012, 10:07:44 PM3/15/12
to puppet...@googlegroups.com
Hi all,

Yesterday I emailed Kirill Simonov, the creator of PyYAML, to get help to parse Puppet YAML file.

He gladly helped with the following code. This code is for parsing Puppet log, but I'm sure you can modify it to parse other Puppet YAML file.

The idea is to create the correct loader for the Ruby object, then PyYAML can read the data after that.

Here goes the code:


#!/usr/bin/env python

import yaml

def construct_ruby_object(loader, suffix, node):
   
return loader.construct_yaml_map(node)

def construct_ruby_sym(loader, node):
   
return loader.construct_yaml_str(node)

yaml
.add_multi_constructor(u"!ruby/object:", construct_ruby_object)
yaml
.add_constructor(u"!ruby/sym", construct_ruby_sym)


stream
= file('201203130939.yaml','r')
mydata
= yaml.load(stream)
print mydata


--
Sharuzzaman Ahmat Raslan

EmmEff

unread,
Mar 18, 2012, 9:17:08 PM3/18/12
to puppet...@googlegroups.com
Thanks!  I had just figured this out myself about a week ago :)  I didn't think to post to the group.
Reply all
Reply to author
Forward
0 new messages