parseconfig 0.4.3 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changelog +15 -0
- data/README +1 -4
- data/demo.conf +11 -6
- data/demo.rb +44 -10
- data/lib/parseconfig.rb +93 -11
- metadata +3 -3
data/Changelog
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
Mon Aug 31, 2009 - v0.5
|
2
|
+
- Added sub-groups feature per RubyForge tracker [#27019]. Config files
|
3
|
+
can now have [subgroups] whose values are added to a nested Hash object.
|
4
|
+
- Added the write() function per RubyForge tracker [#27019]. Will print
|
5
|
+
to STDOUT by default, or to a file object if passed.
|
6
|
+
- Added the add(), and add_to_group() functions to support new features
|
7
|
+
per RubyForge tracker [#27019].
|
8
|
+
- Thank you to Titouan Christophe for the submissions listed above!
|
9
|
+
- ParseConfig.get_params() returns all params including groups.
|
10
|
+
ParseConfig.get_groups() returns available sub-groups.
|
11
|
+
- See the demo.rb and demo.conf files which have been updated to reflect
|
12
|
+
this update.
|
13
|
+
- The methods override_value() and nil_value() are now deprecated and will
|
14
|
+
be removed in future versions.
|
15
|
+
|
1
16
|
Sat Mar 28, 2009 - v0.4.3
|
2
17
|
- Added the self.params member that is a Hash holding all parameter/values.
|
3
18
|
- Added the 'get_params' to return an array of all config parameters.
|
data/README
CHANGED
@@ -14,9 +14,6 @@ applications.
|
|
14
14
|
Parsing of simple configuration files (not Ruby specific).
|
15
15
|
Allows ruby applications as well as Bash Init scripts to use the same config.
|
16
16
|
|
17
|
-
Problem: Currently single quotes are stripped... therefore, values with a
|
18
|
-
single quote are not possible.
|
19
|
-
|
20
17
|
== SYNOPSIS:
|
21
18
|
|
22
19
|
require('rubygems')
|
@@ -37,7 +34,7 @@ single quote are not possible.
|
|
37
34
|
|
38
35
|
== LICENSE:
|
39
36
|
|
40
|
-
Copyright (C) 2006,
|
37
|
+
Copyright (C) 2006,2009 BJ Dierkes <wdierkes@5dollarwhitebox.org>
|
41
38
|
|
42
39
|
This program is free software; you can redistribute it and/or
|
43
40
|
modify it under the terms of the GNU General Public License
|
data/demo.conf
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
admin_email = root@localhost
|
2
|
+
listen_ip = 127.0.0.1
|
3
|
+
listen_port = 87345
|
4
|
+
|
5
|
+
[group1]
|
6
|
+
user_name = johnny
|
7
|
+
group_name = daemons
|
8
|
+
|
9
|
+
[group2]
|
10
|
+
user_name = rita
|
11
|
+
group_name = daemons
|
data/demo.rb
CHANGED
@@ -14,19 +14,53 @@ rescue Errno::EACCES
|
|
14
14
|
exit
|
15
15
|
end
|
16
16
|
|
17
|
-
puts
|
18
|
-
puts
|
19
|
-
puts
|
17
|
+
puts
|
18
|
+
puts 'Reading main config section and groups...'
|
19
|
+
puts '-' * 77
|
20
|
+
puts
|
21
|
+
c.write()
|
22
|
+
puts
|
23
|
+
puts 'Available params are...'
|
24
|
+
puts '-' * 77
|
25
|
+
puts
|
26
|
+
puts c.get_params()
|
27
|
+
puts
|
28
|
+
puts
|
20
29
|
|
30
|
+
puts 'Available sub-groups are...'
|
31
|
+
puts '-' * 77
|
32
|
+
puts
|
33
|
+
puts c.get_groups()
|
34
|
+
puts
|
35
|
+
puts
|
36
|
+
|
37
|
+
puts 'Accessing sub-group params...'
|
38
|
+
puts '-' * 77
|
39
|
+
puts
|
40
|
+
puts "group1 user name value is: #{c.params['group1']['user_name']}"
|
41
|
+
puts
|
42
|
+
puts
|
43
|
+
|
44
|
+
puts "Using params hash..."
|
45
|
+
puts '-' * 77
|
46
|
+
puts
|
47
|
+
puts "The admin email address is #{c.params['admin_email']}"
|
48
|
+
puts
|
49
|
+
puts
|
50
|
+
|
51
|
+
puts "Using get_value (kind of deprecated)..."
|
52
|
+
puts '-' * 77
|
53
|
+
puts
|
54
|
+
puts "The listen address is #{c.get_value('listen_ip')} and the user name " +
|
55
|
+
"is #{c.get_value('group1')['user_name']}"
|
21
56
|
puts
|
22
57
|
puts
|
23
|
-
puts "All the paramaters are..."
|
24
|
-
res = c.get_params()
|
25
|
-
res.each do |param|
|
26
|
-
puts param
|
27
|
-
end
|
28
58
|
|
59
|
+
puts "Writing the config out to a file"
|
60
|
+
puts '-' * 77
|
29
61
|
puts
|
62
|
+
f = open('/tmp/parseconfig_sample_config', 'w')
|
63
|
+
c.write(f)
|
64
|
+
f.close()
|
65
|
+
puts "Config written to /tmp/parseconfig_sample_config"
|
30
66
|
puts
|
31
|
-
puts "using params hash"
|
32
|
-
puts c.params['age']
|
data/lib/parseconfig.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# $Id: parseconfig.rb 37 2008-02-29 07:27:33Z wdierkes $
|
3
3
|
#
|
4
4
|
# Author:: BJ Dierkes <wdierkes@5dollarwhitebox.org>
|
5
|
-
# Copyright:: Copyright (c) 2006,
|
5
|
+
# Copyright:: Copyright (c) 2006,2009 5dollarwhitebox.org
|
6
6
|
# License:: GPL
|
7
7
|
# URL:: http://www.5dollarwhitebox.org
|
8
8
|
#
|
@@ -15,11 +15,14 @@
|
|
15
15
|
# as well as the ChangeLog and README files included.
|
16
16
|
#
|
17
17
|
|
18
|
+
# Note: A group is a set of parameters defined for a subpart of a
|
19
|
+
# config file
|
20
|
+
|
18
21
|
class ParseConfig
|
19
22
|
|
20
|
-
Version = '0.
|
23
|
+
Version = '0.5'
|
21
24
|
|
22
|
-
attr_accessor :config_file, :params
|
25
|
+
attr_accessor :config_file, :params, :groups
|
23
26
|
|
24
27
|
# Initialize the class with the path to the 'config_file'
|
25
28
|
# The class objects are dynamically generated by the
|
@@ -27,14 +30,32 @@ class ParseConfig
|
|
27
30
|
# the config file is 'param = value' then the itializer
|
28
31
|
# will eval "@param = value"
|
29
32
|
#
|
30
|
-
def initialize(config_file)
|
33
|
+
def initialize(config_file=nil)
|
31
34
|
@config_file = config_file
|
32
35
|
@params = {}
|
36
|
+
@groups = []
|
37
|
+
|
38
|
+
if(self.config_file)
|
39
|
+
self.validate_config()
|
40
|
+
self.import_config()
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Validate the config file, and contents
|
45
|
+
def validate_config()
|
33
46
|
if !File.readable?(self.config_file)
|
34
47
|
raise Errno::EACCES, "#{self.config_file} is not readable"
|
35
48
|
end
|
36
|
-
|
37
|
-
|
49
|
+
|
50
|
+
# FIX ME: need to validate contents/structure?
|
51
|
+
end
|
52
|
+
|
53
|
+
# Import data from the config to our config object.
|
54
|
+
def import_config()
|
55
|
+
# The config is top down.. anything after a [group] gets added as part
|
56
|
+
# of that group until a new [group] is found.
|
57
|
+
group = nil
|
58
|
+
open(self.config_file).each do |line|
|
38
59
|
line.strip!
|
39
60
|
unless (/^\#/.match(line))
|
40
61
|
if(/\s*=\s*/.match(line))
|
@@ -51,11 +72,21 @@ class ParseConfig
|
|
51
72
|
else
|
52
73
|
new_value = ''
|
53
74
|
end
|
54
|
-
|
55
|
-
|
75
|
+
|
76
|
+
if group
|
77
|
+
self.add_to_group(group, var_name, new_value)
|
78
|
+
else
|
79
|
+
self.add(var_name, new_value)
|
80
|
+
end
|
81
|
+
|
82
|
+
elsif(/^\[(.+)\]$/.match(line).to_a != [])
|
83
|
+
group = /^\[(.+)\]$/.match(line).to_a[1]
|
84
|
+
self.add(group, {})
|
85
|
+
self.groups.push(group)
|
86
|
+
|
56
87
|
end
|
57
88
|
end
|
58
|
-
|
89
|
+
end
|
59
90
|
end
|
60
91
|
|
61
92
|
# This method will provide the value held by the object "@param"
|
@@ -65,22 +96,73 @@ class ParseConfig
|
|
65
96
|
self.instance_variable_get("@#{param}")
|
66
97
|
end
|
67
98
|
|
99
|
+
# This method returns all parameters/groups defined in a config file.
|
100
|
+
def get_params()
|
101
|
+
return self.params.keys
|
102
|
+
end
|
103
|
+
|
104
|
+
# List available sub-groups of the config.
|
105
|
+
def get_groups()
|
106
|
+
return self.groups
|
107
|
+
end
|
108
|
+
|
68
109
|
# This method is simple. Should you need to override a value
|
69
110
|
# dynamically, use override_value(param, value) where 'param' is
|
70
111
|
# the name of the paramater in the config file.
|
71
112
|
#
|
113
|
+
# DEPRECATED - will be removed in future versions.
|
114
|
+
#
|
72
115
|
def override_value(param, value)
|
73
116
|
self.instance_variable_set("@#{param}", value)
|
74
117
|
end
|
75
118
|
|
76
119
|
# This method will set the value of '@param' to nil (not in the config
|
77
120
|
# file, only in the app).
|
121
|
+
#
|
122
|
+
# DEPRECATED - will be removed in future versions.
|
123
|
+
#
|
78
124
|
def nil_value(param)
|
79
125
|
self.instance_variable_set("@#{param}", nil)
|
80
126
|
end
|
81
127
|
|
82
|
-
|
83
|
-
|
128
|
+
# This method adds an element to the config object (not the config file)
|
129
|
+
# By adding a Hash, you create a new group
|
130
|
+
def add(param_name, value)
|
131
|
+
self.instance_variable_set("@#{param_name}", value)
|
132
|
+
self.params["#{param_name}"] = value
|
133
|
+
end
|
134
|
+
|
135
|
+
# Add parameters to a group. Note that parameters with the same name
|
136
|
+
# could be placed in different groups
|
137
|
+
def add_to_group(group, param_name, value)
|
138
|
+
self.instance_eval("@#{group}[\"#{param_name}\"] = \"#{value}\"")
|
139
|
+
self.params["#{group}"]["#{param_name}"] = value
|
84
140
|
end
|
85
141
|
|
142
|
+
# Writes out the config file to output_stream
|
143
|
+
def write(output_stream=STDOUT)
|
144
|
+
self.params.each do |name,value|
|
145
|
+
if value.class.to_s != 'Hash'
|
146
|
+
if value.scan(/\w+/).length > 1
|
147
|
+
output_stream.puts "#{name} = \"#{value}\""
|
148
|
+
else
|
149
|
+
output_stream.puts "#{name} = #{value}"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
output_stream.puts "\n"
|
154
|
+
|
155
|
+
self.groups.each do |group|
|
156
|
+
output_stream.puts "[#{group}]"
|
157
|
+
self.params[group].each do |param, value|
|
158
|
+
if value.scan(/\w+/).length > 1
|
159
|
+
output_stream.puts "#{param} = \"#{value}\""
|
160
|
+
else
|
161
|
+
output_stream.puts "#{param} = #{value}"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
output_stream.puts "\n"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
86
168
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parseconfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.5"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BJ Dierkes
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-21 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
requirements: []
|
52
52
|
|
53
53
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.
|
54
|
+
rubygems_version: 1.3.1
|
55
55
|
signing_key:
|
56
56
|
specification_version: 2
|
57
57
|
summary: ParseConfig provides simple parsing of standard *nix style config files.
|