@@ -5,11 +5,14 @@ module Puppet
55module Util
66 class IniFile
77
8- def initialize ( path , key_val_separator = ' = ' )
8+ def initialize ( path , key_val_separator = ' = ' , section_prefix = '[' , section_suffix = ']' )
99
1010 k_v_s = key_val_separator . strip
1111
12- @@SECTION_REGEX = /^\s *\[ ([^\] ]*)\] \s *$/
12+ @section_prefix = section_prefix
13+ @section_suffix = section_suffix
14+
15+ @@SECTION_REGEX = section_regex
1316 @@SETTING_REGEX = /^(\s *)([^#;\s ]|[^#;\s ].*?[^\s #{ k_v_s } ])(\s *#{ k_v_s } \s *)(.*)\s *$/
1417 @@COMMENTED_SETTING_REGEX = /^(\s *)[#;]+(\s *)(.*?[^\s #{ k_v_s } ])(\s *#{ k_v_s } [ \t ]*)(.*)\s *$/
1518
@@ -22,6 +25,26 @@ def initialize(path, key_val_separator = ' = ')
2225 end
2326 end
2427
28+ def section_regex
29+ # Only put in prefix/suffix if they exist
30+ # Also, if the prefix is '', the negated
31+ # set match should be a match all instead.
32+ r_string = '^\s*'
33+ r_string += Regexp . escape ( @section_prefix )
34+ r_string += '('
35+ if @section_prefix != ''
36+ r_string += '[^'
37+ r_string += Regexp . escape ( @section_prefix )
38+ r_string += ']'
39+ else
40+ r_string += '.'
41+ end
42+ r_string += '*)'
43+ r_string += Regexp . escape ( @section_suffix )
44+ r_string += '\s*$'
45+ /#{ r_string } /
46+ end
47+
2548 def section_names
2649 @section_names
2750 end
@@ -107,7 +130,7 @@ def save
107130 whitespace_buffer = [ ]
108131
109132 if ( section . is_new_section? ) && ( ! section . is_global? )
110- fh . puts ( "\n [ #{ section . name } ] " )
133+ fh . puts ( "\n #{ @section_prefix } #{ section . name } #{ @section_suffix } " )
111134 end
112135
113136 if ! section . is_new_section?
0 commit comments