Skip to content

Commit

Permalink
Add non-value option support
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryshagin committed Jan 6, 2021
1 parent 0cc6309 commit 7be5a0a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 94 deletions.
4 changes: 4 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Mon Sep 28, 2020 - v1.0.9
- Add non-value option support.
Resolves Issue #11

Mon Jan 25, 2016 - v1.0.8
- Use backward compatible positional arguments (not named keyword arguments)
Resolves Issue #31
Expand Down
153 changes: 75 additions & 78 deletions lib/parseconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
# config file

class ParseConfig

Version = '1.0.8'

attr_accessor :config_file, :params, :groups

# Initialize the class with the path to the 'config_file'
Expand All @@ -28,81 +25,83 @@ class ParseConfig
# the config file is 'param = value' then the itializer
# will eval "@param = value"
#
def initialize(config_file=nil, separator='=', comments=['#', ';'])
def initialize(config_file = nil, separator = '=', comments = ['#', ';'])
@config_file = config_file
@params = {}
@groups = []
@splitRegex = '\s*' + separator + '\s*'
@split_regex = '\s*' + separator + '\s*'
@comments = comments

if(self.config_file)
self.validate_config()
self.import_config()
end
return unless config_file

validate_config
import_config
end

# Validate the config file, and contents
def validate_config()
unless File.readable?(self.config_file)
raise Errno::EACCES, "#{self.config_file} is not readable"
end
def validate_config
return if File.readable?(config_file)

raise Errno::EACCES, "#{config_file} is not readable"

# FIX ME: need to validate contents/structure?
end

# Import data from the config to our config object.
def import_config()
def import_config
# The config is top down.. anything after a [group] gets added as part
# of that group until a new [group] is found.
group = nil
open(self.config_file) { |f| f.each_with_index do |line, i|
line.strip!

# force_encoding not available in all versions of ruby
begin
if i.eql? 0 and line.include?("\xef\xbb\xbf".force_encoding("UTF-8"))
line.delete!("\xef\xbb\xbf".force_encoding("UTF-8"))
open(config_file) do |f|
f.each_with_index do |line, i|
line.strip!

# force_encoding not available in all versions of ruby
begin
if i.eql? 0 && line.include?("\xef\xbb\xbf".force_encoding('UTF-8'))
line.delete!("\xef\xbb\xbf".force_encoding('UTF-8'))
end
rescue NoMethodError
end
rescue NoMethodError
end

is_comment = false
@comments.each do |comment|
if (/^#{comment}/.match(line))
is_comment = true
break
is_comment = false
@comments.each do |comment|
if /^#{comment}/.match(line)
is_comment = true
break
end
end
end

unless is_comment
if(/#{@splitRegex}/.match(line))
param, value = line.split(/#{@splitRegex}/, 2)
var_name = "#{param}".chomp.strip
value = value.chomp.strip
new_value = ''
if (value)
if value =~ /^['"](.*)['"]$/
new_value = $1
unless is_comment
if /#{@split_regex}/.match(line)
param, value = line.split(/#{@split_regex}/, 2)
var_name = param.to_s.chomp.strip
value = value.chomp.strip
new_value = ''
if value
if value =~ /^['"](.*)['"]$/
new_value = Regexp.last_match(1)
else
new_value = value
end
else
new_value = value
new_value = ''
end
else
new_value = ''
end

if group
self.add_to_group(group, var_name, new_value)
else
self.add(var_name, new_value)
if group
add_to_group(group, var_name, new_value)
else
add(var_name, new_value)
end
elsif /^\[(.+)\]$/.match(line).to_a != []
group = /^\[(.+)\]$/.match(line).to_a[1]
add(group, {})
elsif /\w+/.match(line)
add(line.to_s.chomp.strip, true)
end

elsif(/^\[(.+)\]$/.match(line).to_a != [])
group = /^\[(.+)\]$/.match(line).to_a[1]
self.add(group, {})

end
end
end }
end
end

# This method will provide the value held by the object "@param"
Expand All @@ -112,65 +111,63 @@ def import_config()
# DEPRECATED - will be removed in future versions
#
def get_value(param)
puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \
puts 'ParseConfig Deprecation Warning: get_value() is deprecated. Use ' \
"config['param'] or config['group']['param'] instead."
return self.params[param]
params[param]
end

# This method is a shortcut to accessing the @params variable
def [](param)
return self.params[param]
params[param]
end

# This method returns all parameters/groups defined in a config file.
def get_params()
return self.params.keys
def get_params
params.keys
end

# List available sub-groups of the config.
def get_groups()
return self.groups
def get_groups
groups
end

# This method adds an element to the config object (not the config file)
# By adding a Hash, you create a new group
def add(param_name, value, override = false)
if value.class == Hash
if self.params.has_key?(param_name)
if self.params[param_name].class == Hash
if params.key? param_name
if params[param_name].class == Hash
if override
self.params[param_name] = value
params[param_name] = value
else
self.params[param_name].merge!(value)
params[param_name].merge!(value)
end
elsif self.params.has_key?(param_name)
if self.params[param_name].class != value.class
elsif params.key? param_name
if params[param_name].class != value.class
raise ArgumentError, "#{param_name} already exists, and is of different type!"
end
end
else
self.params[param_name] = value
params[param_name] = value
end
if ! self.groups.include?(param_name)
self.groups.push(param_name)
unless groups.include?(param_name)
groups.push(param_name)
end
else
self.params[param_name] = value
params[param_name] = value
end
end

# Add parameters to a group. Note that parameters with the same name
# could be placed in different groups
def add_to_group(group, param_name, value)
if ! self.groups.include?(group)
self.add(group, {})
end
self.params[group][param_name] = value
add(group, {}) unless groups.include?(group)
params[group][param_name] = value
end

# Writes out the config file to output_stream
def write(output_stream=STDOUT, quoted=true)
self.params.each do |name,value|
def write(output_stream = STDOUT, quoted = true)
params.each do |name, value|
if value.class.to_s != 'Hash'
if quoted == true
output_stream.puts "#{name} = \"#{value}\""
Expand All @@ -181,9 +178,9 @@ def write(output_stream=STDOUT, quoted=true)
end
output_stream.puts "\n"

self.groups.each do |group|
groups.each do |group|
output_stream.puts "[#{group}]"
self.params[group].each do |param, value|
params[group].each do |param, value|
if quoted == true
output_stream.puts "#{param} = \"#{value}\""
else
Expand All @@ -202,7 +199,7 @@ def write(output_stream=STDOUT, quoted=true)
# Returns true if ParseConfig are equivalent and false if they differ.

def eql?(other)
self.params == other.params && self.groups == other.groups
params == other.params && groups == other.groups
end
alias == eql?
end
3 changes: 3 additions & 0 deletions lib/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ParseConfig
VERSION = '1.0.9'.freeze
end
36 changes: 21 additions & 15 deletions parseconfig.gemspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
Gem::Specification.new do |s|
s.name = %q{parseconfig}
s.version = "1.0.8"
s.date = %q{2016-01-25}
s.authors = ["BJ Dierkes"]
s.email = %q{derks@datafolklabs.com}
s.summary = %q{Config File Parser for Standard Unix/Linux Type Config Files}
s.homepage = %q{http://github.com/datafolklabs/ruby-parseconfig/}
s.description = %q{ParseConfig provides simple parsing of standard configuration files in the form of 'param = value'. It also supports nested [group] sections.}
s.files = [ "README.md",
"Changelog",
"LICENSE",
"doc",
"lib/parseconfig.rb",
"tests"]
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'version'

Gem::Specification.new do |s|
s.name = 'parseconfig'
s.version = ParseConfig::VERSION
s.date = '2020-09-28'
s.authors = ['BJ Dierkes']
s.email = 'derks@datafolklabs.com'
s.summary = 'Config File Parser for Standard Unix/Linux Type Config Files'
s.homepage = 'http://github.com/datafolklabs/ruby-parseconfig/'
s.description = 'ParseConfig provides simple parsing of standard' \
"configuration files in the form of 'param = value'. " \
'It also supports nested [group] sections.'
s.files = ['README.md',
'Changelog',
'LICENSE',
'doc/',
'lib/parseconfig.rb',
'lib/version.rb',
'tests']
end
1 change: 1 addition & 0 deletions tests/files/demo.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
admin_email = root@localhost
listen_ip = 127.0.0.1
listen_port = 87345
no_value_option

[group1]
user_name = johnny
Expand Down
3 changes: 2 additions & 1 deletion tests/files/demo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"group2" => {
"user_name" => "rita",
"group_name" => "daemons"
}
},
"no_value_option" => true
}

0 comments on commit 7be5a0a

Please sign in to comment.