Class: ConfigParser

Inherits:
Hash
  • Object
show all
Defined in:
lib/configparser.rb

Overview

DESCRIPTION: parses configuration files compatible with Python’s ConfigParser

Instance Method Summary collapse

Constructor Details

#initialize(fname = nil) ⇒ ConfigParser

Returns a new instance of ConfigParser.



6
7
8
# File 'lib/configparser.rb', line 6

def initialize(fname = nil)
  self.parse(File.open(fname, "r").each_line) if fname
end

Instance Method Details

#parse(input_source) ⇒ Object



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
# File 'lib/configparser.rb', line 10

def parse(input_source)
	section = nil
	key = nil
	input_source.each do |line|
		next if (line =~ /^\s*(#|;)/)
		
		# parse out the lines of the config
		if line =~ /^\s*(.+?)\s*[=:]\s*(.*)$/ # handle key=value lines
			if section
				self[section] = {} unless self[section]
				key = $1
         if self[section][key]
           self[section][key] = [self[section][key]] unless self[section][key].is_a?(Array)
           self[section][key] << $2
         else
           self[section][key] = $2
         end
			else
				key = $1
         if self[key]
           self[key] = [self[key]] unless self[key].is_a?(Array)
           self[key] << $2
         else
           self[key] = $2
         end
			end
		elsif line =~ /^\s*\[(.+?)\]/ # handle new sections
			section = $1
       self[section] = {} unless self[section]
		elsif line =~ /^\s+(.+?)$/ # handle continued lines
			if section
         if self[section][key].is_a?(Array)
           self[section][key].last << " #{$1}";
         else
           self[section][key] << " #{$1}";
         end
			else
         if self[key].is_a?(Array)
           self[key].last << " #{$1}"
         else
           self[key] << " #{$1}"
         end
			end
		elsif line =~ /^([\w\d\_\-]+)$/
			if section
				self[section] = {} unless self[section]
				key = $1
         if self[section][key]
           self[section][key] = [self[section][key]] unless self[section][key].is_a?(Array)
           self[section][key] << true
         else
           self[section][key] = true
         end
			else
				key = $1
         if self[key]
           self[key] = [self[key]] unless self[key].is_a?(Array)
           self[key] << true
         else
           self[key] = true
         end
			end
		end
	end

	# handle substitutions (globals first)
	changes = true
	while changes do
		changes = false
		self.each_key do |k|
			next if self[k].is_a? Hash
			next unless self[k].is_a? String
			self[k].gsub!(/\$\((.+?)\)/) {|x|
				changes = true if self[$1]
				self[$1] || "$(#{$1})"
			}
		end
	end
	
	# handle substitutions within the sections
	changes = true
	while changes do
		changes = false
		self.each_key do |k|
			next unless self[k].is_a? Hash
			self[k].each_key do |j|
				next unless self[k][j].is_a? String
				self[k][j].gsub!(/\$\((.+?)\)/) {|x|
					changes = true if self[k][$1] || self[$1]
					self[k][$1] || self[$1] || "$(#{$1})"
				}
			end
		end
	end
end

#to_s(sep = ':') ⇒ Object



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
# File 'lib/configparser.rb', line 106

def to_s(sep=':')
	str = ""
	# print globals first
	self.keys.sort.each do |k|
		next if self[k].is_a? Hash
     if not self[k].is_a?(Array)
       self[k] = [self[k]]
     end
     self[k].each do |v|
		  if v === true
			  str << "#{k}\n"
       elsif v == ""
         str << "#{k}#{sep}\n"
		  else
			  str << "#{k}#{sep} #{v}\n"
		  end
     end
	end
	
	# now print the sections
	self.keys.sort.each do |k|
     next unless self[k].is_a? Hash
     str << "[#{k}]\n"
     self[k].sort.each do |j,v|
       if not v.is_a?(Array)
         v = [v]
       end
		  v.each do |v2|
			  if v2 === true
				  str << "#{j}\n"
         elsif v2 == ""
           str << "#{j}#{sep}\n"
			  else
			    str << "#{j}#{sep} #{v2}\n"
			  end
       end
		end
	end
	str
end