parseconfig 1.0.8 → 1.1.0
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.
- checksums.yaml +5 -5
- data/Changelog +4 -0
- data/lib/parseconfig.rb +75 -78
- data/lib/version.rb +3 -0
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 79e32d975451b2b4cbf6ee3123e87b26a1fbf540066b3d44322c02d573a451af
|
4
|
+
data.tar.gz: 3a53181d9344b6b2ea790f968d7f33305709944bafd1c430a27601c411672fff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7b538e6e901837e6d6e479e072841ba6dd404ecc060cf417d98a40e714daabaf92fa54c3eeec137ff11d5df98e055f374c8f8f43917ce4d3d3ef4668a3d2efa
|
7
|
+
data.tar.gz: 634668d51266a54123d170652bb8bd03fcac5936c4a0deb0f388860ecad6cd84cd88c256b85be6d78a7155b766cbccbc2de0334d3b601e1b9304aaa8403118c9
|
data/Changelog
CHANGED
data/lib/parseconfig.rb
CHANGED
@@ -17,9 +17,6 @@
|
|
17
17
|
# config file
|
18
18
|
|
19
19
|
class ParseConfig
|
20
|
-
|
21
|
-
Version = '1.0.8'
|
22
|
-
|
23
20
|
attr_accessor :config_file, :params, :groups
|
24
21
|
|
25
22
|
# Initialize the class with the path to the 'config_file'
|
@@ -28,81 +25,83 @@ class ParseConfig
|
|
28
25
|
# the config file is 'param = value' then the itializer
|
29
26
|
# will eval "@param = value"
|
30
27
|
#
|
31
|
-
def initialize(config_file=nil, separator='=', comments=['#', ';'])
|
28
|
+
def initialize(config_file = nil, separator = '=', comments = ['#', ';'])
|
32
29
|
@config_file = config_file
|
33
30
|
@params = {}
|
34
31
|
@groups = []
|
35
|
-
@
|
32
|
+
@split_regex = '\s*' + separator + '\s*'
|
36
33
|
@comments = comments
|
37
34
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
35
|
+
return unless config_file
|
36
|
+
|
37
|
+
validate_config
|
38
|
+
import_config
|
42
39
|
end
|
43
40
|
|
44
41
|
# Validate the config file, and contents
|
45
|
-
def validate_config
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
def validate_config
|
43
|
+
return if File.readable?(config_file)
|
44
|
+
|
45
|
+
raise Errno::EACCES, "#{config_file} is not readable"
|
49
46
|
|
50
47
|
# FIX ME: need to validate contents/structure?
|
51
48
|
end
|
52
49
|
|
53
50
|
# Import data from the config to our config object.
|
54
|
-
def import_config
|
51
|
+
def import_config
|
55
52
|
# The config is top down.. anything after a [group] gets added as part
|
56
53
|
# of that group until a new [group] is found.
|
57
54
|
group = nil
|
58
|
-
open(
|
59
|
-
line
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
line.
|
55
|
+
open(config_file) do |f|
|
56
|
+
f.each_with_index do |line, i|
|
57
|
+
line.strip!
|
58
|
+
|
59
|
+
# force_encoding not available in all versions of ruby
|
60
|
+
begin
|
61
|
+
if i.eql? 0 && 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
65
|
end
|
66
|
-
rescue NoMethodError
|
67
|
-
end
|
68
66
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
67
|
+
is_comment = false
|
68
|
+
@comments.each do |comment|
|
69
|
+
if /^#{comment}/.match(line)
|
70
|
+
is_comment = true
|
71
|
+
break
|
72
|
+
end
|
74
73
|
end
|
75
|
-
end
|
76
74
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
75
|
+
unless is_comment
|
76
|
+
if /#{@split_regex}/.match(line)
|
77
|
+
param, value = line.split(/#{@split_regex}/, 2)
|
78
|
+
var_name = param.to_s.chomp.strip
|
79
|
+
value = value.chomp.strip
|
80
|
+
new_value = ''
|
81
|
+
if value
|
82
|
+
if value =~ /^['"](.*)['"]$/
|
83
|
+
new_value = Regexp.last_match(1)
|
84
|
+
else
|
85
|
+
new_value = value
|
86
|
+
end
|
86
87
|
else
|
87
|
-
new_value =
|
88
|
+
new_value = ''
|
88
89
|
end
|
89
|
-
else
|
90
|
-
new_value = ''
|
91
|
-
end
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
if group
|
92
|
+
add_to_group(group, var_name, new_value)
|
93
|
+
else
|
94
|
+
add(var_name, new_value)
|
95
|
+
end
|
96
|
+
elsif /^\[(.+)\]$/.match(line).to_a != []
|
97
|
+
group = /^\[(.+)\]$/.match(line).to_a[1]
|
98
|
+
add(group, {})
|
99
|
+
elsif /\w+/.match(line)
|
100
|
+
add(line.to_s.chomp.strip, true)
|
97
101
|
end
|
98
|
-
|
99
|
-
elsif(/^\[(.+)\]$/.match(line).to_a != [])
|
100
|
-
group = /^\[(.+)\]$/.match(line).to_a[1]
|
101
|
-
self.add(group, {})
|
102
|
-
|
103
102
|
end
|
104
103
|
end
|
105
|
-
end
|
104
|
+
end
|
106
105
|
end
|
107
106
|
|
108
107
|
# This method will provide the value held by the object "@param"
|
@@ -112,65 +111,63 @@ class ParseConfig
|
|
112
111
|
# DEPRECATED - will be removed in future versions
|
113
112
|
#
|
114
113
|
def get_value(param)
|
115
|
-
puts
|
114
|
+
puts 'ParseConfig Deprecation Warning: get_value() is deprecated. Use ' \
|
116
115
|
"config['param'] or config['group']['param'] instead."
|
117
|
-
|
116
|
+
params[param]
|
118
117
|
end
|
119
118
|
|
120
119
|
# This method is a shortcut to accessing the @params variable
|
121
120
|
def [](param)
|
122
|
-
|
121
|
+
params[param]
|
123
122
|
end
|
124
123
|
|
125
124
|
# This method returns all parameters/groups defined in a config file.
|
126
|
-
def get_params
|
127
|
-
|
125
|
+
def get_params
|
126
|
+
params.keys
|
128
127
|
end
|
129
128
|
|
130
129
|
# List available sub-groups of the config.
|
131
|
-
def get_groups
|
132
|
-
|
130
|
+
def get_groups
|
131
|
+
groups
|
133
132
|
end
|
134
133
|
|
135
134
|
# This method adds an element to the config object (not the config file)
|
136
135
|
# By adding a Hash, you create a new group
|
137
136
|
def add(param_name, value, override = false)
|
138
137
|
if value.class == Hash
|
139
|
-
if
|
140
|
-
if
|
138
|
+
if params.key? param_name
|
139
|
+
if params[param_name].class == Hash
|
141
140
|
if override
|
142
|
-
|
141
|
+
params[param_name] = value
|
143
142
|
else
|
144
|
-
|
143
|
+
params[param_name].merge!(value)
|
145
144
|
end
|
146
|
-
elsif
|
147
|
-
if
|
145
|
+
elsif params.key? param_name
|
146
|
+
if params[param_name].class != value.class
|
148
147
|
raise ArgumentError, "#{param_name} already exists, and is of different type!"
|
149
148
|
end
|
150
149
|
end
|
151
150
|
else
|
152
|
-
|
151
|
+
params[param_name] = value
|
153
152
|
end
|
154
|
-
|
155
|
-
|
153
|
+
unless groups.include?(param_name)
|
154
|
+
groups.push(param_name)
|
156
155
|
end
|
157
156
|
else
|
158
|
-
|
157
|
+
params[param_name] = value
|
159
158
|
end
|
160
159
|
end
|
161
160
|
|
162
161
|
# Add parameters to a group. Note that parameters with the same name
|
163
162
|
# could be placed in different groups
|
164
163
|
def add_to_group(group, param_name, value)
|
165
|
-
|
166
|
-
|
167
|
-
end
|
168
|
-
self.params[group][param_name] = value
|
164
|
+
add(group, {}) unless groups.include?(group)
|
165
|
+
params[group][param_name] = value
|
169
166
|
end
|
170
167
|
|
171
168
|
# Writes out the config file to output_stream
|
172
|
-
def write(output_stream=STDOUT, quoted=true)
|
173
|
-
|
169
|
+
def write(output_stream = STDOUT, quoted = true)
|
170
|
+
params.each do |name, value|
|
174
171
|
if value.class.to_s != 'Hash'
|
175
172
|
if quoted == true
|
176
173
|
output_stream.puts "#{name} = \"#{value}\""
|
@@ -181,9 +178,9 @@ class ParseConfig
|
|
181
178
|
end
|
182
179
|
output_stream.puts "\n"
|
183
180
|
|
184
|
-
|
181
|
+
groups.each do |group|
|
185
182
|
output_stream.puts "[#{group}]"
|
186
|
-
|
183
|
+
params[group].each do |param, value|
|
187
184
|
if quoted == true
|
188
185
|
output_stream.puts "#{param} = \"#{value}\""
|
189
186
|
else
|
@@ -202,7 +199,7 @@ class ParseConfig
|
|
202
199
|
# Returns true if ParseConfig are equivalent and false if they differ.
|
203
200
|
|
204
201
|
def eql?(other)
|
205
|
-
|
202
|
+
params == other.params && groups == other.groups
|
206
203
|
end
|
207
204
|
alias == eql?
|
208
205
|
end
|
data/lib/version.rb
ADDED
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parseconfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BJ Dierkes
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: ParseConfig provides simple parsing of
|
14
|
-
the form of 'param = value'.
|
13
|
+
description: ParseConfig provides simple parsing of standardconfiguration files in
|
14
|
+
the form of 'param = value'. It also supports nested [group] sections.
|
15
15
|
email: derks@datafolklabs.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
@@ -21,10 +21,11 @@ files:
|
|
21
21
|
- LICENSE
|
22
22
|
- README.md
|
23
23
|
- lib/parseconfig.rb
|
24
|
+
- lib/version.rb
|
24
25
|
homepage: http://github.com/datafolklabs/ruby-parseconfig/
|
25
26
|
licenses: []
|
26
27
|
metadata: {}
|
27
|
-
post_install_message:
|
28
|
+
post_install_message:
|
28
29
|
rdoc_options: []
|
29
30
|
require_paths:
|
30
31
|
- lib
|
@@ -39,10 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
40
|
- !ruby/object:Gem::Version
|
40
41
|
version: '0'
|
41
42
|
requirements: []
|
42
|
-
|
43
|
-
|
44
|
-
signing_key:
|
43
|
+
rubygems_version: 3.0.6
|
44
|
+
signing_key:
|
45
45
|
specification_version: 4
|
46
46
|
summary: Config File Parser for Standard Unix/Linux Type Config Files
|
47
47
|
test_files: []
|
48
|
-
has_rdoc:
|