summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/fileparsing.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-13 03:28:47 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-13 03:28:47 +0000
commit064ddbc218c56de91318c9dfedc481e67ed60750 (patch)
treee93341b790ae6279c22138d05bd3f641d3620edd /lib/puppet/util/fileparsing.rb
parentbb80c1bb2c977fe462fa9d74031929547c2bbc40 (diff)
downloadpuppet-064ddbc218c56de91318c9dfedc481e67ed60750.tar.gz
puppet-064ddbc218c56de91318c9dfedc481e67ed60750.tar.xz
puppet-064ddbc218c56de91318c9dfedc481e67ed60750.zip
Hosts now work again, and it should be straightforward to create a netinfo provider, too.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1864 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util/fileparsing.rb')
-rw-r--r--lib/puppet/util/fileparsing.rb41
1 files changed, 36 insertions, 5 deletions
diff --git a/lib/puppet/util/fileparsing.rb b/lib/puppet/util/fileparsing.rb
index 4056f2fff..390648404 100644
--- a/lib/puppet/util/fileparsing.rb
+++ b/lib/puppet/util/fileparsing.rb
@@ -145,7 +145,18 @@ module Puppet::Util::FileParsing
return nil
end
- # Define a new type of record. These lines get split into hashes.
+ # Define a new type of record. These lines get split into hashes. Valid
+ # options are:
+ # * <tt>:absent</tt>: What to use when a field is absent. Defaults to "".
+ # * <tt>:fields</tt>: The list of fields, as an array. By default, all
+ # fields are considered required.
+ # * <tt>:joiner</tt>: How to join fields together. Defaults to '\t'.
+ # * <tt>:optional</tt>: Which fields are optional. If these are missing,
+ # you'll just get the 'absent' value instead of an ArgumentError.
+ # * <tt>:rts</tt>: Whether to remove trailing whitespace. Defaults to false.
+ # If true, whitespace will be removed; if a regex, then whatever matches
+ # the regex will be removed.
+ # * <tt>:separator</tt>: The record separator. Defaults to /\s+/.
def record_line(name, options, &block)
unless options.include?(:fields)
raise ArgumentError, "Must include a list of fields"
@@ -162,6 +173,12 @@ module Puppet::Util::FileParsing
options[:absent] ||= ""
+ if options[:optional]
+ options[:optional] = options[:optional].collect { |f| symbolize(f) }
+ else
+ options[:optional] = []
+ end
+
options[:separator] ||= /\s+/
# Unless they specified a string-based joiner, just use a single
@@ -218,14 +235,28 @@ module Puppet::Util::FileParsing
else
joinchar = type[:joiner] || type[:separator]
- return type[:fields].collect { |field|
+ line = type[:fields].collect { |field|
# If the field is marked absent, use the appropriate replacement
- if details[field] == :absent
- type[:absent]
+ if details[field] == :absent or details[field].nil?
+ if type[:optional].include?(field)
+ type[:absent]
+ else
+ raise ArgumentError, "Field %s is required" % field
+ end
else
details[field].to_s
end
- }.join(joinchar)
+ }.reject { |c| c.nil?}.join(joinchar)
+
+ if regex = type[:rts]
+ # If they say true, then use whitespace; else, use their regex.
+ if regex == true
+ regex = /\s+$/
+ end
+ return line.sub(regex,'')
+ else
+ return line
+ end
end
end