Path: | lib/puppet/util/fileparsing.rb |
Last Update: | Wed Dec 09 12:54:29 -0800 2009 |
A mini-language for parsing files. This is only used file the ParsedFile provider, but it makes more sense to split it out so it‘s easy to maintain in one place.
You can use this module to create simple parser/generator classes. For instance, the following parser should go most of the way to parsing /etc/passwd:
class Parser include Puppet::Util::FileParsing record_line :user, :fields => %w{name password uid gid gecos home shell}, :separator => ":" end
You would use it like this:
parser = Parser.new lines = parser.parse(File.read("/etc/passwd")) lines.each do |type, hash| # type will always be :user, since we only have one p hash end
Each line in this case would be a hash, with each field set appropriately. You could then call ‘parser.to_line(hash)’ on any of those hashes to generate the text line again.