From 2dc4cb7c5b83d1cd2caefa0479d28611a5259f27 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 23 Nov 2012 10:06:18 +0000 Subject: Updated the API for the parser to split parse_file into parse_lines. --- lib/custodian/parser.rb | 57 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'lib') diff --git a/lib/custodian/parser.rb b/lib/custodian/parser.rb index cd53e1b..42eb855 100644 --- a/lib/custodian/parser.rb +++ b/lib/custodian/parser.rb @@ -63,16 +63,13 @@ module Custodian # # Constructor # - def initialize( filename ) + def initialize( ) @MACROS = Hash.new() @jobs = Array.new() - @file = filename @timeout = 60 - raise ArgumentError, "Missing configuration file!" if ( @file.nil? ) - raise ArgumentError, "File not found: #{@file}" unless ( File.exists?( @file) ) end @@ -327,7 +324,7 @@ module Custodian end end - ret + return ret else raise ArgumentError, "Unknown line: '#{line}'" end @@ -335,21 +332,16 @@ module Custodian - - # - # Parse the configuration file which was named in our constructor. # - # This updates our @jobs array with the tests - and optionally - # invokes our callback. + # Parse a text-snippet, with multiple lines. # - def parse_file( &callback ) + def parse_lines( text ) # - # Parse the configuration file on the command line + # Split on newline # - File.open( @file, "r").each_line do |line| - - ret = parse_line( line) + text.each do |line| + ret = parse_line( line ) # # The return value from the parse_line method @@ -367,22 +359,33 @@ module Custodian @jobs.push( probe ) end end - - # - # If there was an optional callback then invoke it - # with the newly added job/jobs. - # - if ( block_given? ) - if ( ret.kind_of?( Array ) ) - ret.each do |probe| - yield probe.to_s - end - end - end end end + + # + # Parse the configuration file specified. + # + # This updates our @jobs array with the tests. + # + def parse_file( filename ) + + raise ArgumentError, "Missing configuration file!" if ( filename.nil? ) + raise ArgumentError, "File not found: #{@file}" unless ( File.exists?( filename) ) + + # + # Read the configuration file. + # + out = File.open( filename, 'r') {|file| file.readlines.collect} + + # + # Parse it + # + parse_lines( out ) + end + + end -- cgit v1.2.1