Sharuzzaman Ahmat Raslan
2012-Mar-16 02:07 UTC
[Puppet Users] Parsing Puppet YAML log in Python
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 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Thanks! I had just figured this out myself about a week ago :) I didn''t think to post to the group. On Thursday, March 15, 2012 10:07:44 PM UTC-4, Sharuzzaman Ahmat Raslan wrote:> > 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 >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/Wjjf6OncMkIJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.