/usr/lib/ruby/vendor_ruby/iniparse/lines.rb is in ruby-iniparse 1.4.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | module IniParse
module Lines
# A base class from which other line types should inherit.
module Line
# ==== Parameters
# opts<Hash>:: Extra options for the line.
#
def initialize(opts = {})
@comment = opts.fetch(:comment, nil)
@comment_sep = opts.fetch(:comment_sep, ';')
@comment_prefix = opts.fetch(:comment_prefix, ' ')
@comment_offset = opts.fetch(:comment_offset, 0)
@indent = opts.fetch(:indent, '')
end
# Returns if this line has an inline comment.
def has_comment?
not @comment.nil?
end
# Returns this line as a string as it would be represented in an INI
# document.
def to_ini
[*line_contents].map { |ini|
if has_comment?
ini += ' ' if ini =~ /\S/ # not blank
ini = ini.ljust(@comment_offset)
ini += comment
end
@indent + ini
}.join "\n"
end
# Returns the contents for this line.
def line_contents
''
end
# Returns the inline comment for this line. Includes the comment
# separator at the beginning of the string.
def comment
"#{ @comment_sep }#{ @comment_prefix }#{ @comment }"
end
# Returns whether this is a line which has no data.
def blank?
false
end
# Returns the options used to create the line
def options
{
comment: @comment,
comment_sep: @comment_sep,
comment_prefix: @comment_prefix,
comment_offset: @comment_offset,
indent: @indent
}
end
end
# Represents a section header in an INI document. Section headers consist
# of a string of characters wrapped in square brackets.
#
# [section]
# key=value
# etc
# ...
#
class Section
include Line
@regex = /^\[ # Opening bracket
([^\]]+) # Section name
\]$ # Closing bracket
/x
attr_accessor :key
attr_reader :lines
include Enumerable
# ==== Parameters
# key<String>:: The section name.
# opts<Hash>:: Extra options for the line.
#
def initialize(key, opts = {})
super(opts)
@key = key.to_s
@lines = IniParse::OptionCollection.new
end
def self.parse(line, opts)
if m = @regex.match(line)
[:section, m[1], opts]
end
end
# Returns this line as a string as it would be represented in an INI
# document. Includes options, comments and blanks.
def to_ini
coll = lines.to_a
if coll.any?
[*super,coll.to_a.map do |line|
if line.kind_of?(Array)
line.map { |dup_line| dup_line.to_ini }.join($/)
else
line.to_ini
end
end].join($/)
else
super
end
end
# Enumerates through each Option in this section.
#
# Does not yield blank and comment lines by default; if you want _all_
# lines to be yielded, pass true.
#
# ==== Parameters
# include_blank<Boolean>:: Include blank/comment lines?
#
def each(*args, &blk)
@lines.each(*args, &blk)
end
# Adds a new option to this section, or updates an existing one.
#
# Note that +[]=+ has no knowledge of duplicate options and will happily
# overwrite duplicate options with your new value.
#
# section['an_option']
# # => ['duplicate one', 'duplicate two', ...]
# section['an_option'] = 'new value'
# section['an_option]
# # => 'new value'
#
# If you do not wish to overwrite duplicates, but wish instead for your
# new option to be considered a duplicate, use +add_option+ instead.
#
def []=(key, value)
line = @lines[key.to_s]
opts = {}
if line.kind_of?(Array)
opts = line.first.options
elsif line.respond_to? :options
opts = line.options
end
@lines[key.to_s] = IniParse::Lines::Option.new(key.to_s, value, opts)
end
# Returns the value of an option identified by +key+.
#
# Returns nil if there is no corresponding option. If the key provided
# matches a set of duplicate options, an array will be returned containing
# the value of each option.
#
def [](key)
key = key.to_s
if @lines.has_key?(key)
if (match = @lines[key]).kind_of?(Array)
match.map { |line| line.value }
else
match.value
end
end
end
# Deletes the option identified by +key+.
#
# Returns the section.
#
def delete(*args)
@lines.delete(*args)
self
end
# Like [], except instead of returning just the option value, it returns
# the matching line instance.
#
# Will return an array of lines if the key matches a set of duplicates.
#
def option(key)
@lines[key.to_s]
end
# Returns true if an option with the given +key+ exists in this section.
def has_option?(key)
@lines.has_key?(key.to_s)
end
# Merges section +other+ into this one. If the section being merged into
# this one contains options with the same key, they will be handled as
# duplicates.
#
# ==== Parameters
# other<IniParse::Section>:: The section to merge into this one.
#
def merge!(other)
other.lines.each(true) do |line|
if line.kind_of?(Array)
line.each { |duplicate| @lines << duplicate }
else
@lines << line
end
end
end
#######
private
#######
def line_contents
'[%s]' % key
end
end
# Stores options which appear at the beginning of a file, without a
# preceding section.
class AnonymousSection < Section
def initialize
super('__anonymous__')
end
def to_ini
# Remove the leading space which is added by joining the blank line
# content with the options.
super.gsub(/\A\n/, '')
end
#######
private
#######
def line_contents
''
end
end
# Represents probably the most common type of line in an INI document:
# an option. Consists of a key and value, usually separated with an =.
#
# key = value
#
class Option
include Line
@regex = /^\s*([^=]+) # Option
=
(.*?)$ # Value
/x
attr_accessor :key, :value
# ==== Parameters
# key<String>:: The option key.
# value<String>:: The value for this option.
# opts<Hash>:: Extra options for the line.
#
def initialize(key, value, opts = {})
super(opts)
@key, @value = key.to_s, value
end
def self.parse(line, opts)
if m = @regex.match(line)
[:option, m[1].strip, typecast(m[2].strip), opts]
end
end
# Attempts to typecast values.
def self.typecast(value)
case value
when /^\s*$/ then nil
when /^-?(?:\d|[1-9]\d+)$/ then Integer(value)
when /^-?(?:\d|[1-9]\d+)(?:\.\d+)?(?:e[+-]?\d+)?$/i then Float(value)
when /^true$/i then true
when /^false$/i then false
else value
end
end
#######
private
#######
# returns an array to support multiple lines or a single one at once
# because of options key duplication
def line_contents
if value.kind_of?(Array)
value.map { |v, i| "#{key} = #{v}" }
else
"#{key} = #{value}"
end
end
end
# Represents a blank line. Used so that we can preserve blank lines when
# writing back to the file.
class Blank
include Line
def blank?
true
end
def self.parse(line, opts)
if line !~ /\S/ # blank
if opts[:comment].nil?
[:blank]
else
[:comment, opts[:comment], opts]
end
end
end
end
# Represents a comment. Comment lines begin with a semi-colon or hash.
#
# ; this is a comment
# # also a comment
#
class Comment < Blank
# Returns if this line has an inline comment.
#
# Being a Comment this will always return true, even if the comment
# is nil. This would be the case if the line starts with a comment
# seperator, but has no comment text. See spec/fixtures/smb.ini for a
# real-world example.
#
def has_comment?
true
end
# Returns the inline comment for this line. Includes the comment
# separator at the beginning of the string.
#
# In rare cases where a comment seperator appeared in the original file,
# but without a comment, just the seperator will be returned.
#
def comment
@comment !~ /\S/ ? @comment_sep : super
end
end
end # Lines
end # IniParse
|