Skip to content

Commit

Permalink
Minor changes for move to DFL
Browse files Browse the repository at this point in the history
  • Loading branch information
derks committed Dec 18, 2013
1 parent c8a9d84 commit aed9576
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 40 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
language: ruby
language: ruby
script: ./utils/run-tests.sh
install: sudo gem install rspec
install: sudo gem install rspec
rvm:
- 1.8.6
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0
- jruby-18mode
- jruby-19mode
- jruby-20mode
- ruby-head
- jruby-head
- ree
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

The MIT License:

Copyright (c) 2006-2012 BJ Dierkes
Copyright (c) 2006-2013 Data Folk Labs, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Ruby ParseConfig Library
ParseConfig provides simple parsing of standard configuration files in the
form of 'param = value'. It also supports nested [group] sections.

[![Continuous Integration Status](https://secure.travis-ci.org/derks/ruby-parseconfig.png)](http://travis-ci.org/derks/ruby-parseconfig)
[![Continuous Integration Status](https://secure.travis-ci.org/datafolklabs/ruby-parseconfig.png)](http://travis-ci.org/datafolklabs/ruby-parseconfig)

Installation
------------
Expand Down
60 changes: 32 additions & 28 deletions lib/parseconfig.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#
# Author:: BJ Dierkes <derks@bjdierkes.com>
#
# Author:: BJ Dierkes <derks@bjdierkes.com>
# Copyright:: Copyright (c) 2006,2012 BJ Dierkes
# License:: MIT
# License:: MIT
# URL:: https://github.com/derks/ruby-parseconfig
#
#

# This class was written to simplify the parsing of configuration
# files in the format of "param = value". Please review the
# files in the format of "param = value". Please review the
# demo files included with this package.
#
# For further information please refer to the './doc' directory
# as well as the ChangeLog and README files included.
#

# Note: A group is a set of parameters defined for a subpart of a
# Note: A group is a set of parameters defined for a subpart of a
# config file

class ParseConfig
Version = '1.0.2'

Version = '1.0.3'

attr_accessor :config_file, :params, :groups

Expand All @@ -32,37 +32,41 @@ def initialize(config_file=nil)
@config_file = config_file
@params = {}
@groups = []

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

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

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

# Import data from the config to our config object.
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.
# 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!

if i.eql? 0 and line.include?("\xef\xbb\xbf".force_encoding("UTF-8"))
line.delete!("\xef\xbb\xbf".force_encoding("UTF-8"))
# 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"))
end
rescue NoMethodError
end

unless (/^\#/.match(line))
if(/\s*=\s*/.match(line))
param, value = line.split(/\s*=\s*/, 2)
param, value = line.split(/\s*=\s*/, 2)
var_name = "#{param}".chomp.strip
value = value.chomp.strip
new_value = ''
Expand All @@ -74,26 +78,26 @@ def import_config()
end
else
new_value = ''
end
end

if group
self.add_to_group(group, var_name, new_value)
else
self.add(var_name, new_value)
end

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

end
end
end }
end

# This method will provide the value held by the object "@param"
# where "@param" is actually the name of the param in the config
# file.
# where "@param" is actually the name of the param in the config
# file.
#
# DEPRECATED - will be removed in future versions
#
Expand All @@ -112,7 +116,7 @@ def [](param)
def get_params()
return self.params.keys
end

# List available sub-groups of the config.
def get_groups()
return self.groups
Expand Down Expand Up @@ -152,7 +156,7 @@ def add_to_group(group, param_name, value)

# Writes out the config file to output_stream
def write(output_stream=STDOUT)
self.params.each do |name,value|
self.params.each do |name,value|
if value.class.to_s != 'Hash'
if value.scan(/\w+/).length > 1
output_stream.puts "#{name} = \"#{value}\""
Expand All @@ -162,10 +166,10 @@ def write(output_stream=STDOUT)
end
end
output_stream.puts "\n"

self.groups.each do |group|
output_stream.puts "[#{group}]"
self.params[group].each do |param, value|
self.params[group].each do |param, value|
if value.scan(/\w+/).length > 1
output_stream.puts "#{param} = \"#{value}\""
else
Expand Down
16 changes: 8 additions & 8 deletions utils/parseconfig.gemspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Gem::Specification.new do |s|
s.name = %q{parseconfig}
s.version = "1.0.2"
s.date = %q{2012-06-12}
s.version = "1.0.3"
s.date = %q{2013-12-19}
s.authors = ["BJ Dierkes"]
s.email = %q{derks@bjdierkes.com}
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/derks/ruby-parseconfig/}
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",
s.files = [ "README.md",
"Changelog",
"LICENSE",
"doc",
"lib/parseconfig.rb",
"tests"]

end

0 comments on commit aed9576

Please sign in to comment.