section.rb 3.1 KB

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