]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/lib/puppet/util/ini_file/section.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / lib / puppet / util / ini_file / section.rb
1 module Puppet
2 module Util
3 class IniFile
4   class Section
5     # Some implementation details:
6     #
7     #  * `name` will be set to the empty string for the 'global' section.
8     #  * there will always be a 'global' section, with a `start_line` of 0,
9     #    but if the file actually begins with a real section header on
10     #    the first line, then the 'global' section will have an
11     #    `end_line` of `nil`.
12     #  * `start_line` and `end_line` will be set to `nil` for a new non-global
13     #    section.
14     def initialize(name, start_line, end_line, settings, indentation)
15       @name = name
16       @start_line = start_line
17       @end_line = end_line
18       @existing_settings = settings.nil? ? {} : settings
19       @additional_settings = {}
20       @indentation = indentation
21     end
22
23     attr_reader :name, :start_line, :end_line, :additional_settings, :indentation
24
25     def is_global?()
26       @name == ''
27     end
28
29     def is_new_section?()
30       # a new section (global or named) will always have `end_line`
31       # set to `nil`
32       @end_line.nil?
33     end
34
35     def setting_names
36       @existing_settings.keys | @additional_settings.keys
37     end
38
39     def get_value(setting_name)
40       @existing_settings[setting_name] || @additional_settings[setting_name]
41     end
42
43     def has_existing_setting?(setting_name)
44       @existing_settings.has_key?(setting_name)
45     end
46
47     def update_existing_setting(setting_name, value)
48       @existing_settings[setting_name] = value
49     end
50
51     def remove_existing_setting(setting_name)
52       if (@existing_settings.delete(setting_name))
53         if @end_line
54           @end_line = @end_line - 1
55         end
56       end
57     end
58
59     # This is a hacky method; it's basically called when we need to insert
60     # a new setting but we don't want it to appear at the very end of the
61     # section.  Instead we hack it into the existing settings list and
62     # increment our end_line number--this assumes that the caller (`ini_file`)
63     # is doing some babysitting w/rt the other sections and the actual data
64     # of the lines.
65     def insert_inline_setting(setting_name, value)
66       @existing_settings[setting_name] = value
67       if @end_line
68         @end_line = @end_line + 1
69       end
70     end
71
72     def set_additional_setting(setting_name, value)
73       @additional_settings[setting_name] = value
74     end
75
76     # Decrement the start and end line numbers for the section (if they are
77     # defined); this is intended to be called when a setting is removed
78     # from a section that comes before this section in the ini file.
79     def decrement_line_nums()
80       if @start_line
81         @start_line = @start_line - 1
82       end
83       if @end_line
84         @end_line = @end_line - 1
85       end
86     end
87
88     # Increment the start and end line numbers for the section (if they are
89     # defined); this is intended to be called when an inline setting is added
90     # to a section that comes before this section in the ini file.
91     def increment_line_nums()
92       if @start_line
93         @start_line = @start_line + 1
94       end
95       if @end_line
96         @end_line = @end_line + 1
97       end
98     end
99
100   end
101 end
102 end
103 end