parseconfig 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +25 -21
- data/LICENSE +1 -1
- data/README.md +24 -17
- data/lib/parseconfig.rb +51 -30
- metadata +25 -41
data/Changelog
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
Thu Dec 19, 2013 - v1.0.4
|
2
|
+
- Add fix for config files with Byte Order Marker (BOM)
|
3
|
+
- Add support for .eql? function.
|
4
|
+
|
1
5
|
Tue Jun 12, 2012 - v1.0.2
|
2
|
-
- Re-enable get_value() as it seems some projects are still using it.
|
6
|
+
- Re-enable get_value() as it seems some projects are still using it.
|
3
7
|
That said, get_value() *will* be removed in the future at some point.
|
4
|
-
|
8
|
+
|
5
9
|
Tue Jun 12, 2012 - v1.0.0
|
6
10
|
- Resolved Issue #3, Config files not closed properly.
|
7
11
|
- Resolved Issue #7, Added basic rspec testing
|
@@ -11,26 +15,26 @@ Tue Jun 12, 2012 - v1.0.0
|
|
11
15
|
Sat Feb 27, 2010 - v0.5.2
|
12
16
|
- Re-releasing under MIT License.
|
13
17
|
- Fixed issue with the add() method where if you added a group outside
|
14
|
-
of the import_config() method, the group would not be properly added to
|
18
|
+
of the import_config() method, the group would not be properly added to
|
15
19
|
self.groups[].
|
16
|
-
- Added feature that if you attempt to add a group that already exists, the
|
20
|
+
- Added feature that if you attempt to add a group that already exists, the
|
17
21
|
groups will be merged together. Any existing params in the original
|
18
22
|
group will be overwritten by the new ones.
|
19
23
|
- Fully deprecated override_value() and nil_value(). Anything using
|
20
24
|
'instance_variable*' has been removed as well as it wasn't working properly
|
21
25
|
anyway.
|
22
|
-
- If you attempt to add_to_group() to a group that doesn't exist, the group
|
26
|
+
- If you attempt to add_to_group() to a group that doesn't exist, the group
|
23
27
|
is added automatically.
|
24
|
-
|
28
|
+
|
25
29
|
Mon Aug 31, 2009 - v0.5
|
26
30
|
- Added sub-groups feature per RubyForge tracker [#27019]. Config files
|
27
31
|
can now have [subgroups] whose values are added to a nested Hash object.
|
28
|
-
- Added the write() function per RubyForge tracker [#27019]. Will print
|
32
|
+
- Added the write() function per RubyForge tracker [#27019]. Will print
|
29
33
|
to STDOUT by default, or to a file object if passed.
|
30
34
|
- Added the add(), and add_to_group() functions to support new features
|
31
35
|
per RubyForge tracker [#27019].
|
32
36
|
- Thank you to Titouan Christophe for the submissions listed above!
|
33
|
-
- ParseConfig.get_params() returns all params including groups.
|
37
|
+
- ParseConfig.get_params() returns all params including groups.
|
34
38
|
ParseConfig.get_groups() returns available sub-groups.
|
35
39
|
- See the demo.rb and demo.conf files which have been updated to reflect
|
36
40
|
this update.
|
@@ -39,44 +43,44 @@ Mon Aug 31, 2009 - v0.5
|
|
39
43
|
|
40
44
|
Sat Mar 28, 2009 - v0.4.3
|
41
45
|
- Added the self.params member that is a Hash holding all parameter/values.
|
42
|
-
- Added the 'get_params' to return an array of all config parameters.
|
46
|
+
- Added the 'get_params' to return an array of all config parameters.
|
43
47
|
|
44
48
|
Thu Feb 28, 2008 - v0.4.2
|
45
|
-
- Fixed bug where if the value contains a '=' then the parameter
|
49
|
+
- Fixed bug where if the value contains a '=' then the parameter
|
46
50
|
is not properly set. [bjd]
|
47
|
-
- Fixed bug #13680 Unable to parse config options that contain
|
48
|
-
single quotes. [bjd]
|
51
|
+
- Fixed bug #13680 Unable to parse config options that contain
|
52
|
+
single quotes. [bjd]
|
49
53
|
|
50
54
|
Sun Sep 03, 2007 - v0.4.1
|
51
55
|
- Now using 'instance_variable_set' and 'instance_variable_get'
|
52
|
-
instead of 'eval' (yeah.. that was dirty). [bjd]
|
56
|
+
instead of 'eval' (yeah.. that was dirty). [bjd]
|
53
57
|
|
54
58
|
|
55
59
|
Sat Aug 11, 2007 - v0.3.2
|
56
|
-
- reorganizing files a bit. [bjd]
|
57
|
-
- renamed methods .... no more uglyStyle... now new_style. [bjd]
|
60
|
+
- reorganizing files a bit. [bjd]
|
61
|
+
- renamed methods .... no more uglyStyle... now new_style. [bjd]
|
58
62
|
|
59
63
|
|
60
64
|
Fri Feb 23, 2007 - v0.3.1
|
61
65
|
- Added a bit of code to remove 'single' quotes... so, until I figure
|
62
|
-
a better option, values can't contain single quotes. [bjd]
|
66
|
+
a better option, values can't contain single quotes. [bjd]
|
63
67
|
|
64
68
|
|
65
69
|
Wed Feb 07, 2007 - v0.2.1
|
66
|
-
- Renamed ParseConfig.class.rb to ParseConfig.rb. [bjd]
|
67
|
-
- Add 'strip' to remove trailing white spaces from config file. [bjd]
|
70
|
+
- Renamed ParseConfig.class.rb to ParseConfig.rb. [bjd]
|
71
|
+
- Add 'strip' to remove trailing white spaces from config file. [bjd]
|
68
72
|
|
69
73
|
|
70
74
|
Wed Dec 13, 2006 - v0.1.3
|
71
|
-
- Added error check to ensure configFile is readable. [bjd]
|
75
|
+
- Added error check to ensure configFile is readable. [bjd]
|
72
76
|
|
73
77
|
|
74
78
|
Sat Nov 25, 2006 - v0.1.2
|
75
79
|
- Added regex to only read lines matching /\s*=\s*/ as
|
76
|
-
The class would bork if you had say, and empty line. [bjd]
|
80
|
+
The class would bork if you had say, and empty line. [bjd]
|
77
81
|
|
78
82
|
|
79
83
|
Fri Nov 24 2006 - v0.1.1
|
80
|
-
- Built class, which is functional and fully usable. [bjd]
|
84
|
+
- Built class, which is functional and fully usable. [bjd]
|
81
85
|
|
82
86
|
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
Ruby ParseConfig Library
|
2
2
|
========================
|
3
3
|
|
4
|
-
ParseConfig provides simple parsing of standard configuration files in the
|
4
|
+
ParseConfig provides simple parsing of standard configuration files in the
|
5
5
|
form of 'param = value'. It also supports nested [group] sections.
|
6
6
|
|
7
|
-
[![Continuous Integration Status](https://secure.travis-ci.org/
|
7
|
+
[![Continuous Integration Status](https://secure.travis-ci.org/datafolklabs/ruby-parseconfig.png)](http://travis-ci.org/datafolklabs/ruby-parseconfig)
|
8
8
|
|
9
9
|
Installation
|
10
10
|
------------
|
@@ -19,54 +19,61 @@ An example configuration file might look like:
|
|
19
19
|
# Example Config
|
20
20
|
param1 = value1
|
21
21
|
param2 = value2
|
22
|
-
|
23
|
-
[group1]
|
22
|
+
|
23
|
+
[group1]
|
24
24
|
group1_param1 = group1_value1
|
25
25
|
group1_param2 = group1_value2
|
26
|
-
|
26
|
+
|
27
27
|
[group2]
|
28
28
|
group2_param1 = group2_value1
|
29
29
|
group2_param2 = group2_value2
|
30
|
-
|
30
|
+
|
31
31
|
|
32
32
|
Access it with ParseConfig:
|
33
33
|
|
34
34
|
>> require('parseconfig.rb')
|
35
35
|
=> true
|
36
|
-
|
36
|
+
|
37
37
|
>> config = ParseConfig.new('/path/to/config/file')
|
38
|
-
=> #<ParseConfig:0x102410908
|
39
|
-
@config_file="example.conf",
|
40
|
-
@groups=["group1", "group2"],
|
38
|
+
=> #<ParseConfig:0x102410908
|
39
|
+
@config_file="example.conf",
|
40
|
+
@groups=["group1", "group2"],
|
41
41
|
@params={
|
42
42
|
"param1"=>"value1"
|
43
43
|
"param2"=>"value2",
|
44
44
|
"group1"=>{
|
45
45
|
"param1"=>"value1"
|
46
|
-
"param2"=>"value2",
|
47
|
-
},
|
46
|
+
"param2"=>"value2",
|
47
|
+
},
|
48
48
|
"group2"=>{
|
49
49
|
"param1"=>"value1"
|
50
|
-
"param2"=>"value2",
|
51
|
-
},
|
50
|
+
"param2"=>"value2",
|
51
|
+
},
|
52
52
|
}
|
53
53
|
>
|
54
|
-
|
54
|
+
|
55
55
|
>> config.get_params()
|
56
56
|
=> ["param1", "param2", "group1", "group2"]
|
57
57
|
|
58
58
|
>> config['param1']
|
59
59
|
=> "value1"
|
60
|
-
|
60
|
+
|
61
61
|
>> config.get_groups()
|
62
62
|
=> ["group1", "group2"]
|
63
|
-
|
63
|
+
|
64
64
|
>> config['group1']
|
65
65
|
=> {"group1_param1"=>"group1_value1", "group1_param2"=>"group1_value2"}
|
66
66
|
|
67
67
|
>> config['group1']['group1_param1']
|
68
68
|
=> "group1_value1"
|
69
69
|
|
70
|
+
>> file = File.open('/path/to/config/file', 'w')
|
71
|
+
=> #<File:file>
|
72
|
+
>> config.write(file)
|
73
|
+
=> []
|
74
|
+
>> file.close
|
75
|
+
=> nil
|
76
|
+
|
70
77
|
|
71
78
|
License
|
72
79
|
-------
|
data/lib/parseconfig.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
#
|
2
|
-
# Author:: BJ Dierkes <derks@
|
3
|
-
# Copyright:: Copyright (c) 2006,
|
4
|
-
# License:: MIT
|
5
|
-
# URL:: https://github.com/
|
6
|
-
#
|
1
|
+
#
|
2
|
+
# Author:: BJ Dierkes <derks@datafolklabs.com>
|
3
|
+
# Copyright:: Copyright (c) 2006,2013 BJ Dierkes
|
4
|
+
# License:: MIT
|
5
|
+
# URL:: https://github.com/datafolklabs/ruby-parseconfig
|
6
|
+
#
|
7
7
|
|
8
8
|
# This class was written to simplify the parsing of configuration
|
9
|
-
# files in the format of "param = value". Please review the
|
9
|
+
# files in the format of "param = value". Please review the
|
10
10
|
# demo files included with this package.
|
11
11
|
#
|
12
12
|
# For further information please refer to the './doc' directory
|
13
13
|
# as well as the ChangeLog and README files included.
|
14
14
|
#
|
15
15
|
|
16
|
-
# Note: A group is a set of parameters defined for a subpart of a
|
16
|
+
# Note: A group is a set of parameters defined for a subpart of a
|
17
17
|
# config file
|
18
18
|
|
19
19
|
class ParseConfig
|
20
|
-
|
21
|
-
Version = '1.0.
|
20
|
+
|
21
|
+
Version = '1.0.4'
|
22
22
|
|
23
23
|
attr_accessor :config_file, :params, :groups
|
24
24
|
|
@@ -32,32 +32,41 @@ class ParseConfig
|
|
32
32
|
@config_file = config_file
|
33
33
|
@params = {}
|
34
34
|
@groups = []
|
35
|
-
|
35
|
+
|
36
36
|
if(self.config_file)
|
37
37
|
self.validate_config()
|
38
38
|
self.import_config()
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
# Validate the config file, and contents
|
43
|
-
def validate_config()
|
44
|
-
|
45
|
-
raise Errno::EACCES, "#{self.config_file} is not readable"
|
43
|
+
def validate_config()
|
44
|
+
unless File.readable?(self.config_file)
|
45
|
+
raise Errno::EACCES, "#{self.config_file} is not readable"
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
# FIX ME: need to validate contents/structure?
|
49
|
-
end
|
50
|
-
|
49
|
+
end
|
50
|
+
|
51
51
|
# Import data from the config to our config object.
|
52
52
|
def import_config()
|
53
53
|
# The config is top down.. anything after a [group] gets added as part
|
54
|
-
# of that group until a new [group] is found.
|
54
|
+
# of that group until a new [group] is found.
|
55
55
|
group = nil
|
56
|
-
open(self.config_file) { |f| f.
|
56
|
+
open(self.config_file) { |f| f.each_with_index do |line, i|
|
57
57
|
line.strip!
|
58
|
+
|
59
|
+
# force_encoding not available in all versions of ruby
|
60
|
+
begin
|
61
|
+
if i.eql? 0 and line.include?("\xef\xbb\xbf".force_encoding("UTF-8"))
|
62
|
+
line.delete!("\xef\xbb\xbf".force_encoding("UTF-8"))
|
63
|
+
end
|
64
|
+
rescue NoMethodError
|
65
|
+
end
|
66
|
+
|
58
67
|
unless (/^\#/.match(line))
|
59
68
|
if(/\s*=\s*/.match(line))
|
60
|
-
param, value = line.split(/\s*=\s*/, 2)
|
69
|
+
param, value = line.split(/\s*=\s*/, 2)
|
61
70
|
var_name = "#{param}".chomp.strip
|
62
71
|
value = value.chomp.strip
|
63
72
|
new_value = ''
|
@@ -69,26 +78,26 @@ class ParseConfig
|
|
69
78
|
end
|
70
79
|
else
|
71
80
|
new_value = ''
|
72
|
-
end
|
81
|
+
end
|
73
82
|
|
74
83
|
if group
|
75
84
|
self.add_to_group(group, var_name, new_value)
|
76
85
|
else
|
77
86
|
self.add(var_name, new_value)
|
78
87
|
end
|
79
|
-
|
88
|
+
|
80
89
|
elsif(/^\[(.+)\]$/.match(line).to_a != [])
|
81
90
|
group = /^\[(.+)\]$/.match(line).to_a[1]
|
82
91
|
self.add(group, {})
|
83
|
-
|
92
|
+
|
84
93
|
end
|
85
94
|
end
|
86
95
|
end }
|
87
96
|
end
|
88
97
|
|
89
98
|
# This method will provide the value held by the object "@param"
|
90
|
-
# where "@param" is actually the name of the param in the config
|
91
|
-
# file.
|
99
|
+
# where "@param" is actually the name of the param in the config
|
100
|
+
# file.
|
92
101
|
#
|
93
102
|
# DEPRECATED - will be removed in future versions
|
94
103
|
#
|
@@ -107,7 +116,7 @@ class ParseConfig
|
|
107
116
|
def get_params()
|
108
117
|
return self.params.keys
|
109
118
|
end
|
110
|
-
|
119
|
+
|
111
120
|
# List available sub-groups of the config.
|
112
121
|
def get_groups()
|
113
122
|
return self.groups
|
@@ -147,7 +156,7 @@ class ParseConfig
|
|
147
156
|
|
148
157
|
# Writes out the config file to output_stream
|
149
158
|
def write(output_stream=STDOUT)
|
150
|
-
self.params.each do |name,value|
|
159
|
+
self.params.each do |name,value|
|
151
160
|
if value.class.to_s != 'Hash'
|
152
161
|
if value.scan(/\w+/).length > 1
|
153
162
|
output_stream.puts "#{name} = \"#{value}\""
|
@@ -157,10 +166,10 @@ class ParseConfig
|
|
157
166
|
end
|
158
167
|
end
|
159
168
|
output_stream.puts "\n"
|
160
|
-
|
169
|
+
|
161
170
|
self.groups.each do |group|
|
162
171
|
output_stream.puts "[#{group}]"
|
163
|
-
self.params[group].each do |param, value|
|
172
|
+
self.params[group].each do |param, value|
|
164
173
|
if value.scan(/\w+/).length > 1
|
165
174
|
output_stream.puts "#{param} = \"#{value}\""
|
166
175
|
else
|
@@ -170,4 +179,16 @@ class ParseConfig
|
|
170
179
|
output_stream.puts "\n"
|
171
180
|
end
|
172
181
|
end
|
182
|
+
|
183
|
+
# Public: Compare this ParseConfig to some other ParseConfig. For two config to
|
184
|
+
# be equivalent, they must have the same sections with the same parameters
|
185
|
+
#
|
186
|
+
# other - The other ParseConfig.
|
187
|
+
#
|
188
|
+
# Returns true if ParseConfig are equivalent and false if they differ.
|
189
|
+
|
190
|
+
def eql?(other)
|
191
|
+
self.params == other.params && self.groups == other.groups
|
192
|
+
end
|
193
|
+
alias == eql?
|
173
194
|
end
|
metadata
CHANGED
@@ -1,65 +1,49 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: parseconfig
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: 1.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- BJ Dierkes
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2012-06-12 00:00:00 -05:00
|
18
|
-
default_executable:
|
12
|
+
date: 2013-12-19 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
|
-
|
22
|
-
email: derks@
|
14
|
+
description: ParseConfig provides simple parsing of standard configuration files in
|
15
|
+
the form of 'param = value'. It also supports nested [group] sections.
|
16
|
+
email: derks@datafolklabs.com
|
23
17
|
executables: []
|
24
|
-
|
25
18
|
extensions: []
|
26
|
-
|
27
19
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
files:
|
20
|
+
files:
|
30
21
|
- README.md
|
31
22
|
- Changelog
|
32
23
|
- LICENSE
|
33
24
|
- lib/parseconfig.rb
|
34
|
-
|
35
|
-
homepage: http://github.com/derks/ruby-parseconfig/
|
25
|
+
homepage: http://github.com/datafolklabs/ruby-parseconfig/
|
36
26
|
licenses: []
|
37
|
-
|
38
27
|
post_install_message:
|
39
28
|
rdoc_options: []
|
40
|
-
|
41
|
-
require_paths:
|
29
|
+
require_paths:
|
42
30
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
57
43
|
requirements: []
|
58
|
-
|
59
44
|
rubyforge_project:
|
60
|
-
rubygems_version: 1.
|
45
|
+
rubygems_version: 1.8.23
|
61
46
|
signing_key:
|
62
47
|
specification_version: 3
|
63
48
|
summary: Config File Parser for Standard Unix/Linux Type Config Files
|
64
49
|
test_files: []
|
65
|
-
|