parseconfig 1.0.6 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +5 -5
  2. data/Changelog +19 -0
  3. data/LICENSE +1 -1
  4. data/README.md +74 -23
  5. data/lib/parseconfig.rb +86 -71
  6. data/lib/version.rb +3 -0
  7. metadata +10 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8d1e9b35305ee60ce0732906eee17091e8c3564e
4
- data.tar.gz: 20c5c7343345170d04a5f9009f55f9a31bf61cc2
2
+ SHA256:
3
+ metadata.gz: 6d3c3c0e6a2c362a4d7afb9c6ecef637497b94137e570b96c3eade26f07f8825
4
+ data.tar.gz: ae849249bce23a5988a51159e388237af2540883dc60de7d164e14501c3d484d
5
5
  SHA512:
6
- metadata.gz: 7db0455728e0e7a4cbb058db4766e28bf5029258b10f808c11bf5eca06a5d53dafb9499dac1339bbcf847306ac3f39c6f55cd9ed77dfbdb57134170dcd3ff413
7
- data.tar.gz: 31d19e90674de0732c023e644b90e9860c5845b8d0cabed89022786b45d8c43a4d16c81cd8b2f9f196da6548ada8a95741ee941fbe43bf1b250a5e822373a9c3
6
+ metadata.gz: 3f49ecd6162b3a6f6bb6140f7a44666f312e6682862e8e6957ce28792b4e5e1629b815e981b409ee0588a9309d2c4dbdcce595b3f42e2bee390f6439731b6c54
7
+ data.tar.gz: bfc2d1519921b14744d873bf71ad76b3da18068cc0929ce59baac5757e0dcb5948b59a86940e2141ee99530ef436787157f4a04726c8562762979f48c8708996
data/Changelog CHANGED
@@ -1,3 +1,22 @@
1
+
2
+ Wed Sep 29, 2021 - v1.1.1 (DEVELOPMENT - will be released as 1.1.2)
3
+ - Dev and test against recent versions of Ruby (2.6+, 3.0+)
4
+ - Docker development support
5
+ - Remove tests for deprecated 'get_value()' method
6
+ - Use Rspec 'expect' in place of 'should'
7
+
8
+ Mon Sep 28, 2020 - v1.1.0
9
+ - Add non-value option support.
10
+ Resolves Issue #11
11
+
12
+ Mon Jan 25, 2016 - v1.0.8
13
+ - Use backward compatible positional arguments (not named keyword arguments)
14
+ Resolves Issue #31
15
+
16
+ Mon Jan 25, 2016 - v1.0.7
17
+ - Support alternative comment types (default: ['#', ';']) (@voobscout)
18
+ Resolves Issue #30
19
+
1
20
  Mon Oct 06, 2014 - v1.0.6
2
21
  - Fix where extraneous double quotes were getting wrapped around parameters
3
22
  that have non-word characters (issue #19)
data/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  The MIT License:
3
3
 
4
- Copyright (c) 2006-2014 Data Folk Labs, LLC
4
+ Copyright (c) 2006 Data Folk Labs, LLC
5
5
 
6
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,42 +1,43 @@
1
- Ruby ParseConfig Library
2
- ========================
1
+ # Ruby ParseConfig Library
3
2
 
4
3
  ParseConfig provides simple parsing of standard configuration files in the
5
- form of 'param = value'. It also supports nested [group] sections.
4
+ form of `param = value`. It also supports nested `[group]` sections.
6
5
 
7
- [![Continuous Integration Status](https://secure.travis-ci.org/datafolklabs/ruby-parseconfig.png)](http://travis-ci.org/datafolklabs/ruby-parseconfig)
6
+ [![Continuous Integration Status](https://app.travis-ci.com/datafolklabs/ruby-parseconfig.svg?branch=master)](https://app.travis-ci.com/github/datafolklabs/ruby-parseconfig)
8
7
 
9
- Installation
10
- ------------
8
+ ## Installation
11
9
 
12
- $ sudo gem install parseconfig
10
+ ```
11
+ $ gem install parseconfig
12
+ ```
13
13
 
14
14
  Gemfile
15
15
 
16
- gem 'parseconfig'
16
+ ```
17
+ gem 'parseconfig'
18
+ ```
17
19
 
18
- Usage
19
- -----
20
+ ## Usage
20
21
 
21
22
  An example configuration file might look like:
22
23
 
23
- # Example Config
24
- param1 = value1
25
- param2 = value2
26
-
27
- [group1]
28
- group1_param1 = group1_value1
29
- group1_param2 = group1_value2
24
+ ```
25
+ # Example Config
26
+ param1 = value1
27
+ param2 = value2
30
28
 
31
- [group2]
32
- group2_param1 = group2_value1
33
- group2_param2 = group2_value2
29
+ [group1]
30
+ group1_param1 = group1_value1
31
+ group1_param2 = group1_value2
34
32
 
33
+ [group2]
34
+ group2_param1 = group2_value1
35
+ group2_param2 = group2_value2
36
+ ```
35
37
 
36
38
  Access it with ParseConfig:
37
39
 
38
40
  ```ruby
39
-
40
41
  >> require 'parseconfig'
41
42
  => true
42
43
 
@@ -74,15 +75,65 @@ Access it with ParseConfig:
74
75
 
75
76
  >> file = File.open('/path/to/config/file', 'w')
76
77
  => #<File:file>
78
+
77
79
  >> config.write(file)
78
80
  => []
81
+
79
82
  >> file.close
80
83
  => nil
81
84
 
82
85
  ```
83
86
 
84
- License
85
- -------
87
+ ## Development
88
+
89
+ ### Docker
90
+
91
+ This project includes a `docker-compose` configuration that sets up all required services, and dependencies for development and testing. This is the recommended path for local development, and is the only fully supported option.
92
+
93
+ The following creates all required docker containers, and launches a BASH shell within the `parseconfig` dev container for development.
94
+ ```
95
+ $ make dev
96
+
97
+ |> parseconfig <| src #
98
+ ```
99
+
100
+ The above is the equivalent of running:
101
+
102
+ ```
103
+ $ docker-compose up -d
104
+
105
+ $ docker-compose exec parseconfig /bin/bash
106
+ ```
107
+
108
+ **Testing Alternative Versions of Ruby**
109
+
110
+ The latest stable version of Ruby is the default, and target version accessible as the `parseconfig` container within Docker Compose. For testing against alternative versions of Ruby, additional containers are created (ex: `parseconfig-rb26`, `parseconfig-rb27`, etc). You can access these containers via:
111
+
112
+ ```
113
+ $ docker-compose ps
114
+ Name Command State Ports
115
+ ---------------------------------------------------------------
116
+ ruby-parseconfig_parseconfig-rb26_1 /bin/bash Up
117
+ ruby-parseconfig_parseconfig-rb27_1 /bin/bash Up
118
+ ruby-parseconfig_parseconfig-rb30_1 /bin/bash Up
119
+ ruby-parseconfig_parseconfig_1 /bin/bash Up
120
+
121
+
122
+ $ docker-compose exec parseconfig-rb26 /bin/bash
123
+
124
+ |> parseconfig-rb26 <| src #
125
+ ```
126
+
127
+ ### Running Unit Tests
128
+
129
+ ```
130
+ $ make test
131
+ ```
132
+
133
+
134
+ ## License
86
135
 
87
136
  The ParseConfig library is Open Source and distributed under the MIT license.
88
137
  Please see the LICENSE file included with this software.
138
+
139
+
data/lib/parseconfig.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: BJ Dierkes <derks@datafolklabs.com>
3
- # Copyright:: Copyright (c) 2006,2013 BJ Dierkes
3
+ # Copyright:: Copyright (c) 2006,2016 Data Folk Labs, LLC
4
4
  # License:: MIT
5
5
  # URL:: https://github.com/datafolklabs/ruby-parseconfig
6
6
  #
@@ -17,9 +17,6 @@
17
17
  # config file
18
18
 
19
19
  class ParseConfig
20
-
21
- Version = '1.0.6'
22
-
23
20
  attr_accessor :config_file, :params, :groups
24
21
 
25
22
  # Initialize the class with the path to the 'config_file'
@@ -28,71 +25,87 @@ 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)
28
+ def initialize(config_file = nil, separator = '=', comments = ['#', ';'])
32
29
  @config_file = config_file
33
30
  @params = {}
34
31
  @groups = []
32
+ @split_regex = '\s*' + separator + '\s*'
33
+ @comments = comments
35
34
 
36
- if(self.config_file)
37
- self.validate_config()
38
- self.import_config()
39
- end
35
+ return unless config_file
36
+
37
+ validate_config
38
+ import_config
40
39
  end
41
40
 
42
41
  # Validate the config file, and contents
43
- def validate_config()
44
- unless File.readable?(self.config_file)
45
- raise Errno::EACCES, "#{self.config_file} is not readable"
46
- end
42
+ def validate_config
43
+ return if File.readable?(config_file)
44
+
45
+ raise Errno::EACCES, "#{config_file} is not readable"
47
46
 
48
47
  # FIX ME: need to validate contents/structure?
49
48
  end
50
49
 
51
50
  # Import data from the config to our config object.
52
- def import_config()
51
+ def import_config
53
52
  # The config is top down.. anything after a [group] gets added as part
54
53
  # of that group until a new [group] is found.
55
54
  group = nil
56
- open(self.config_file) { |f| f.each_with_index do |line, i|
57
- line.strip!
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
+ end
58
66
 
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"))
67
+ is_comment = false
68
+ @comments.each do |comment|
69
+ if /^#{comment}/.match(line)
70
+ is_comment = true
71
+ break
72
+ end
63
73
  end
64
- rescue NoMethodError
65
- end
66
74
 
67
- unless (/^\#/.match(line))
68
- if(/\s*=\s*/.match(line))
69
- param, value = line.split(/\s*=\s*/, 2)
70
- var_name = "#{param}".chomp.strip
71
- value = value.chomp.strip
72
- new_value = ''
73
- if (value)
74
- if value =~ /^['"](.*)['"]$/
75
- 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
76
87
  else
77
- new_value = value
88
+ new_value = ''
78
89
  end
79
- else
80
- new_value = ''
81
- end
82
90
 
83
- if group
84
- self.add_to_group(group, var_name, new_value)
85
- else
86
- 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 /^\[(.+)\](\s*#{escaped_comment_regex}+.*)?$/.match(line).to_a != []
97
+ group = /^\[(.+)\](\s*#{escaped_comment_regex}+.*)?$/.match(line).to_a[1]
98
+ add(group, {})
99
+ elsif /\w+/.match(line)
100
+ add(line.to_s.chomp.strip, true)
87
101
  end
88
-
89
- elsif(/^\[(.+)\]$/.match(line).to_a != [])
90
- group = /^\[(.+)\]$/.match(line).to_a[1]
91
- self.add(group, {})
92
-
93
102
  end
94
103
  end
95
- end }
104
+ end
105
+ end
106
+
107
+ def escaped_comment_regex
108
+ /[#{Regexp.escape(@comments.join(''))}]/
96
109
  end
97
110
 
98
111
  # This method will provide the value held by the object "@param"
@@ -102,61 +115,63 @@ class ParseConfig
102
115
  # DEPRECATED - will be removed in future versions
103
116
  #
104
117
  def get_value(param)
105
- puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \
118
+ puts 'ParseConfig Deprecation Warning: get_value() is deprecated. Use ' \
106
119
  "config['param'] or config['group']['param'] instead."
107
- return self.params[param]
120
+ params[param]
108
121
  end
109
122
 
110
123
  # This method is a shortcut to accessing the @params variable
111
124
  def [](param)
112
- return self.params[param]
125
+ params[param]
113
126
  end
114
127
 
115
128
  # This method returns all parameters/groups defined in a config file.
116
- def get_params()
117
- return self.params.keys
129
+ def get_params
130
+ params.keys
118
131
  end
119
132
 
120
133
  # List available sub-groups of the config.
121
- def get_groups()
122
- return self.groups
134
+ def get_groups
135
+ groups
123
136
  end
124
137
 
125
138
  # This method adds an element to the config object (not the config file)
126
139
  # By adding a Hash, you create a new group
127
- def add(param_name, value)
140
+ def add(param_name, value, override = false)
128
141
  if value.class == Hash
129
- if self.params.has_key?(param_name)
130
- if self.params[param_name].class == Hash
131
- self.params[param_name].merge!(value)
132
- elsif self.params.has_key?(param_name)
133
- if self.params[param_name].class != value.class
142
+ if params.key? param_name
143
+ if params[param_name].class == Hash
144
+ if override
145
+ params[param_name] = value
146
+ else
147
+ params[param_name].merge!(value)
148
+ end
149
+ elsif params.key? param_name
150
+ if params[param_name].class != value.class
134
151
  raise ArgumentError, "#{param_name} already exists, and is of different type!"
135
152
  end
136
153
  end
137
154
  else
138
- self.params[param_name] = value
155
+ params[param_name] = value
139
156
  end
140
- if ! self.groups.include?(param_name)
141
- self.groups.push(param_name)
157
+ unless groups.include?(param_name)
158
+ groups.push(param_name)
142
159
  end
143
160
  else
144
- self.params[param_name] = value
161
+ params[param_name] = value
145
162
  end
146
163
  end
147
164
 
148
165
  # Add parameters to a group. Note that parameters with the same name
149
166
  # could be placed in different groups
150
167
  def add_to_group(group, param_name, value)
151
- if ! self.groups.include?(group)
152
- self.add(group, {})
153
- end
154
- self.params[group][param_name] = value
168
+ add(group, {}) unless groups.include?(group)
169
+ params[group][param_name] = value
155
170
  end
156
171
 
157
172
  # Writes out the config file to output_stream
158
- def write(output_stream=STDOUT, quoted=true)
159
- self.params.each do |name,value|
173
+ def write(output_stream = STDOUT, quoted = true)
174
+ params.each do |name, value|
160
175
  if value.class.to_s != 'Hash'
161
176
  if quoted == true
162
177
  output_stream.puts "#{name} = \"#{value}\""
@@ -167,9 +182,9 @@ class ParseConfig
167
182
  end
168
183
  output_stream.puts "\n"
169
184
 
170
- self.groups.each do |group|
185
+ groups.each do |group|
171
186
  output_stream.puts "[#{group}]"
172
- self.params[group].each do |param, value|
187
+ params[group].each do |param, value|
173
188
  if quoted == true
174
189
  output_stream.puts "#{param} = \"#{value}\""
175
190
  else
@@ -188,7 +203,7 @@ class ParseConfig
188
203
  # Returns true if ParseConfig are equivalent and false if they differ.
189
204
 
190
205
  def eql?(other)
191
- self.params == other.params && self.groups == other.groups
206
+ params == other.params && groups == other.groups
192
207
  end
193
208
  alias == eql?
194
209
  end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module ParseConfig
2
+ VERSION = '1.1.2'.freeze
3
+ end
metadata CHANGED
@@ -1,18 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parseconfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - BJ Dierkes
8
+ - Dmitry Shagin
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-10-06 00:00:00.000000000 Z
12
+ date: 2021-09-30 00:00:00.000000000 Z
12
13
  dependencies: []
13
- description: ParseConfig provides simple parsing of standard configuration files in
14
- the form of 'param = value'. It also supports nested [group] sections.
15
- email: derks@datafolklabs.com
14
+ description: ParseConfig provides simple parsing of standardconfiguration files in
15
+ the form of 'param = value'. It also supports nested [group] sections.
16
+ email: team@datafolklabs.com
16
17
  executables: []
17
18
  extensions: []
18
19
  extra_rdoc_files: []
@@ -21,8 +22,10 @@ files:
21
22
  - LICENSE
22
23
  - README.md
23
24
  - lib/parseconfig.rb
25
+ - lib/version.rb
24
26
  homepage: http://github.com/datafolklabs/ruby-parseconfig/
25
- licenses: []
27
+ licenses:
28
+ - MIT
26
29
  metadata: {}
27
30
  post_install_message:
28
31
  rdoc_options: []
@@ -39,8 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
42
  - !ruby/object:Gem::Version
40
43
  version: '0'
41
44
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.2.2
45
+ rubygems_version: 3.0.3
44
46
  signing_key:
45
47
  specification_version: 4
46
48
  summary: Config File Parser for Standard Unix/Linux Type Config Files