Module: Puppet::Util::FileParsing
- Includes:
- Puppet::Util
- Included in:
- Provider::ParsedFile
- Defined in:
- lib/vendor/puppet/util/fileparsing.rb
Defined Under Namespace
Classes: FileRecord
Constant Summary
Constants included from Puppet::Util
AbsolutePathPosix, AbsolutePathWindows
Instance Attribute Summary collapse
- #line_separator ⇒ Object
-
#trailing_separator ⇒ Object
Whether to add a trailing separator to the file.
Instance Method Summary collapse
-
#clear_records ⇒ Object
Clear all existing record definitions.
- #fields(type) ⇒ Object
-
#handle_record_line(line, record) ⇒ Object
Try to match a record.
-
#handle_text_line(line, record) ⇒ Object
Try to match a specific text line.
-
#lines(text) ⇒ Object
Split text into separate lines using the record separator.
-
#parse(text) ⇒ Object
Split a bunch of text into lines and then parse them individually.
-
#parse_line(line) ⇒ Object
Handle parsing a single line.
-
#record_line(name, options, &block) ⇒ Object
Define a new type of record.
-
#records? ⇒ Boolean
Are there any record types defined?.
-
#text_line(name, options, &block) ⇒ Object
Define a new type of text record.
-
#to_file(records) ⇒ Object
Generate a file from a bunch of hash records.
-
#to_line(details) ⇒ Object
Convert our parsed record into a text record.
- #valid_attr?(type, attr) ⇒ Boolean
Methods included from Puppet::Util
absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask
Methods included from POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Instance Attribute Details
#line_separator ⇒ Object
205 206 207 208 209 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 205 def line_separator @line_separator ||= "\n" @line_separator end |
#trailing_separator ⇒ Object
Whether to add a trailing separator to the file. Defaults to true
332 333 334 335 336 337 338 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 332 def trailing_separator if defined?(@trailing_separator) return @trailing_separator else return true end end |
Instance Method Details
#clear_records ⇒ Object
Clear all existing record definitions. Only used for testing.
125 126 127 128 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 125 def clear_records @record_types.clear @record_order.clear end |
#fields(type) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 130 def fields(type) if record = record_type(type) record.fields.dup else nil end end |
#handle_record_line(line, record) ⇒ Object
Try to match a record.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 144 def handle_record_line(line, record) ret = nil if record.respond_to?(:process) if ret = record.send(:process, line.dup) unless ret.is_a?(Hash) raise Puppet::DevError, "Process record type #{record.name} returned non-hash" end else return nil end elsif regex = record.match # In this case, we try to match the whole line and then use the # match captures to get our fields. if match = regex.match(line) fields = [] ret = {} record.fields.zip(match.captures).each do |field, value| if value == record.absent ret[field] = :absent else ret[field] = value end end else nil end else ret = {} sep = record.separator # String "helpfully" replaces ' ' with /\s+/ in splitting, so we # have to work around it. if sep == " " sep = / / end line_fields = line.split(sep) record.fields.each do |param| value = line_fields.shift if value and value != record.absent ret[param] = value else ret[param] = :absent end end if record.rollup and ! line_fields.empty? last_field = record.fields[-1] val = ([ret[last_field]] + line_fields).join(record.joiner) ret[last_field] = val end end if ret ret[:record_type] = record.name return ret else return nil end end |
#handle_text_line(line, record) ⇒ Object
Try to match a specific text line.
139 140 141 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 139 def handle_text_line(line, record) line =~ record.match ? {:record_type => record.name, :line => line} : nil end |
#lines(text) ⇒ Object
Split text into separate lines using the record separator.
212 213 214 215 216 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 212 def lines(text) # Remove any trailing separators, and then split based on them # LAK:NOTE See http://snurl.com/21zf8 [groups_google_com] x = text.sub(/#{self.line_separator}\Q/,'').split(self.line_separator) end |
#parse(text) ⇒ Object
Split a bunch of text into lines and then parse them individually.
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 219 def parse(text) count = 1 lines(text).collect do |line| count += 1 if val = parse_line(line) val else error = Puppet::Error.new("Could not parse line #{line.inspect}") error.line = count raise error end end end |
#parse_line(line) ⇒ Object
Handle parsing a single line.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 234 def parse_line(line) raise Puppet::DevError, "No record types defined; cannot parse lines" unless records? @record_order.each do |record| # These are basically either text or record lines. method = "handle_#{record.type}_line" if respond_to?(method) if result = send(method, line, record) record.send(:post_parse, result) if record.respond_to?(:post_parse) return result end else raise Puppet::DevError, "Somehow got invalid line type #{record.type}" end end nil end |
#record_line(name, options, &block) ⇒ Object
Define a new type of record. These lines get split into hashes. Valid options are:
-
:absent
: What to use as value within a line, when a field is absent. Note that in the record object, the literal :absent symbol is used, and not this value. Defaults to “”. -
:fields
: The list of fields, as an array. By default, all fields are considered required. -
:joiner
: How to join fields together. Defaults to ‘t’. -
:optional
: Which fields are optional. If these are missing, you’ll just get the ‘absent’ value instead of an ArgumentError. -
:rts
: 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. -
:separator
: The record separator. Defaults to /s+/.
268 269 270 271 272 273 274 275 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 268 def record_line(name, , &block) raise ArgumentError, "Must include a list of fields" unless .include?(:fields) record = FileRecord.new(:record, , &block) record.name = name.intern new_line_type(record) end |
#records? ⇒ Boolean
Are there any record types defined?
278 279 280 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 278 def records? defined?(@record_types) and ! @record_types.empty? end |
#text_line(name, options, &block) ⇒ Object
Define a new type of text record.
283 284 285 286 287 288 289 290 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 283 def text_line(name, , &block) raise ArgumentError, "You must provide a :match regex for text lines" unless .include?(:match) record = FileRecord.new(:text, , &block) record.name = name.intern new_line_type(record) end |
#to_file(records) ⇒ Object
Generate a file from a bunch of hash records.
293 294 295 296 297 298 299 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 293 def to_file(records) text = records.collect { |record| to_line(record) }.join(line_separator) text += line_separator if trailing_separator text end |
#to_line(details) ⇒ Object
Convert our parsed record into a text record.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 302 def to_line(details) unless record = record_type(details[:record_type]) raise ArgumentError, "Invalid record type #{details[:record_type].inspect}" end if record.respond_to?(:pre_gen) details = details.dup record.send(:pre_gen, details) end case record.type when :text; return details[:line] else return record.to_line(details) if record.respond_to?(:to_line) line = record.join(details) if regex = record.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 |
#valid_attr?(type, attr) ⇒ Boolean
340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/vendor/puppet/util/fileparsing.rb', line 340 def valid_attr?(type, attr) type = type.intern if record = record_type(type) and record.fields.include?(attr.intern) return true else if attr.intern == :ensure return true else false end end end |