parseconfig 1.0.8 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +5 -5
  2. data/Changelog +4 -0
  3. data/lib/parseconfig.rb +75 -78
  4. data/lib/version.rb +3 -0
  5. metadata +9 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 56edd2517c03e43f3a23682fde2c9d75d347915a
4
- data.tar.gz: e3558aa93f8891394d6a2a16cb23eccd572ff7dd
2
+ SHA256:
3
+ metadata.gz: 79e32d975451b2b4cbf6ee3123e87b26a1fbf540066b3d44322c02d573a451af
4
+ data.tar.gz: 3a53181d9344b6b2ea790f968d7f33305709944bafd1c430a27601c411672fff
5
5
  SHA512:
6
- metadata.gz: 80853b4986b86ba31a242c340d72422719214e9e1955588fe63575d1afb74da7b3535efeb7ab9b27dfa5d58e47820f26d234e0cf1309224c12ae6b3a85f4e7c9
7
- data.tar.gz: 504a7f0c4d64ea7679850edecb18802087d0de0eb96195830af33284d5a747b1e3f108d2aa7f0cfbb006abee3abb0f47950f249adabacfde6e3c30f9b42d1918
6
+ metadata.gz: d7b538e6e901837e6d6e479e072841ba6dd404ecc060cf417d98a40e714daabaf92fa54c3eeec137ff11d5df98e055f374c8f8f43917ce4d3d3ef4668a3d2efa
7
+ data.tar.gz: 634668d51266a54123d170652bb8bd03fcac5936c4a0deb0f388860ecad6cd84cd88c256b85be6d78a7155b766cbccbc2de0334d3b601e1b9304aaa8403118c9
data/Changelog CHANGED
@@ -1,3 +1,7 @@
1
+ Mon Sep 28, 2020 - v1.1.0
2
+ - Add non-value option support.
3
+ Resolves Issue #11
4
+
1
5
  Mon Jan 25, 2016 - v1.0.8
2
6
  - Use backward compatible positional arguments (not named keyword arguments)
3
7
  Resolves Issue #31
@@ -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
- @splitRegex = '\s*' + separator + '\s*'
32
+ @split_regex = '\s*' + separator + '\s*'
36
33
  @comments = comments
37
34
 
38
- if(self.config_file)
39
- self.validate_config()
40
- self.import_config()
41
- end
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
- unless File.readable?(self.config_file)
47
- raise Errno::EACCES, "#{self.config_file} is not readable"
48
- end
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(self.config_file) { |f| f.each_with_index do |line, i|
59
- line.strip!
60
-
61
- # force_encoding not available in all versions of ruby
62
- begin
63
- if i.eql? 0 and line.include?("\xef\xbb\xbf".force_encoding("UTF-8"))
64
- line.delete!("\xef\xbb\xbf".force_encoding("UTF-8"))
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
- is_comment = false
70
- @comments.each do |comment|
71
- if (/^#{comment}/.match(line))
72
- is_comment = true
73
- break
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
- unless is_comment
78
- if(/#{@splitRegex}/.match(line))
79
- param, value = line.split(/#{@splitRegex}/, 2)
80
- var_name = "#{param}".chomp.strip
81
- value = value.chomp.strip
82
- new_value = ''
83
- if (value)
84
- if value =~ /^['"](.*)['"]$/
85
- new_value = $1
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 = value
88
+ new_value = ''
88
89
  end
89
- else
90
- new_value = ''
91
- end
92
90
 
93
- if group
94
- self.add_to_group(group, var_name, new_value)
95
- else
96
- self.add(var_name, new_value)
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 "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \
114
+ puts 'ParseConfig Deprecation Warning: get_value() is deprecated. Use ' \
116
115
  "config['param'] or config['group']['param'] instead."
117
- return self.params[param]
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
- return self.params[param]
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
- return self.params.keys
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
- return self.groups
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 self.params.has_key?(param_name)
140
- if self.params[param_name].class == Hash
138
+ if params.key? param_name
139
+ if params[param_name].class == Hash
141
140
  if override
142
- self.params[param_name] = value
141
+ params[param_name] = value
143
142
  else
144
- self.params[param_name].merge!(value)
143
+ params[param_name].merge!(value)
145
144
  end
146
- elsif self.params.has_key?(param_name)
147
- if self.params[param_name].class != value.class
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
- self.params[param_name] = value
151
+ params[param_name] = value
153
152
  end
154
- if ! self.groups.include?(param_name)
155
- self.groups.push(param_name)
153
+ unless groups.include?(param_name)
154
+ groups.push(param_name)
156
155
  end
157
156
  else
158
- self.params[param_name] = value
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
- if ! self.groups.include?(group)
166
- self.add(group, {})
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
- self.params.each do |name,value|
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
- self.groups.each do |group|
181
+ groups.each do |group|
185
182
  output_stream.puts "[#{group}]"
186
- self.params[group].each do |param, value|
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
- self.params == other.params && self.groups == other.groups
202
+ params == other.params && groups == other.groups
206
203
  end
207
204
  alias == eql?
208
205
  end
@@ -0,0 +1,3 @@
1
+ module ParseConfig
2
+ VERSION = '1.1.0'.freeze
3
+ end
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.8
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: 2016-01-25 00:00:00.000000000 Z
11
+ date: 2020-09-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: ParseConfig provides simple parsing of standard configuration files in
14
- the form of 'param = value'. It also supports nested [group] sections.
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
- rubyforge_project:
43
- rubygems_version: 2.4.1
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: