parseconfig-1.0.8/0000755000004100000410000000000013364046174014060 5ustar www-datawww-dataparseconfig-1.0.8/README.md0000644000004100000410000000335513364046174015345 0ustar www-datawww-data# 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/datafolklabs/ruby-parseconfig.png)](http://travis-ci.org/datafolklabs/ruby-parseconfig) ## Installation ``` $ gem install parseconfig ``` Gemfile ``` gem 'parseconfig' ``` ## Usage An example configuration file might look like: ``` # Example Config param1 = value1 param2 = value2 [group1] group1_param1 = group1_value1 group1_param2 = group1_value2 [group2] group2_param1 = group2_value1 group2_param2 = group2_value2 ``` Access it with ParseConfig: ```ruby >> require 'parseconfig' => true >> config = ParseConfig.new('/path/to/config/example.conf') => #"value1" "param2"=>"value2", "group1"=>{ "param1"=>"value1" "param2"=>"value2", }, "group2"=>{ "param1"=>"value1" "param2"=>"value2", }, }> >> config.get_params => ["param1", "param2", "group1", "group2"] >> config['param1'] => "value1" >> config.get_groups => ["group1", "group2"] >> config['group1'] => {"group1_param1"=>"group1_value1", "group1_param2"=>"group1_value2"} >> config['group1']['group1_param1'] => "group1_value1" >> file = File.open('/path/to/config/file', 'w') => # >> config.write(file) => [] >> file.close => nil ``` ## License The ParseConfig library is Open Source and distributed under the MIT license. Please see the LICENSE file included with this software. parseconfig-1.0.8/parseconfig.gemspec0000644000004100000410000000202413364046174017723 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: parseconfig 1.0.8 ruby lib Gem::Specification.new do |s| s.name = "parseconfig".freeze s.version = "1.0.8" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["BJ Dierkes".freeze] s.date = "2016-01-25" s.description = "ParseConfig provides simple parsing of standard configuration files in the form of 'param = value'. It also supports nested [group] sections.".freeze s.email = "derks@datafolklabs.com".freeze s.files = ["Changelog".freeze, "LICENSE".freeze, "README.md".freeze, "lib/parseconfig.rb".freeze] s.homepage = "http://github.com/datafolklabs/ruby-parseconfig/".freeze s.rubygems_version = "2.5.2.1".freeze s.summary = "Config File Parser for Standard Unix/Linux Type Config Files".freeze end parseconfig-1.0.8/Changelog0000644000004100000410000000711013364046174015671 0ustar www-datawww-dataMon Jan 25, 2016 - v1.0.8 - Use backward compatible positional arguments (not named keyword arguments) Resolves Issue #31 Mon Jan 25, 2016 - v1.0.7 - Support alternative comment types (default: ['#', ';']) (@voobscout) Resolves Issue #30 Mon Oct 06, 2014 - v1.0.6 - Fix where extraneous double quotes were getting wrapped around parameters that have non-word characters (issue #19) Thu Dec 19, 2013 - v1.0.4 - Add fix for config files with Byte Order Marker (BOM) - Add support for .eql? function. Tue Jun 12, 2012 - v1.0.2 - Re-enable get_value() as it seems some projects are still using it. That said, get_value() *will* be removed in the future at some point. Tue Jun 12, 2012 - v1.0.0 - Resolved Issue #3, Config files not closed properly. - Resolved Issue #7, Added basic rspec testing - Resolved Issue #5, Readded support for array like access - Removed deprecated function get_value() Sat Feb 27, 2010 - v0.5.2 - Re-releasing under MIT License. - Fixed issue with the add() method where if you added a group outside of the import_config() method, the group would not be properly added to self.groups[]. - Added feature that if you attempt to add a group that already exists, the groups will be merged together. Any existing params in the original group will be overwritten by the new ones. - Fully deprecated override_value() and nil_value(). Anything using 'instance_variable*' has been removed as well as it wasn't working properly anyway. - If you attempt to add_to_group() to a group that doesn't exist, the group is added automatically. Mon Aug 31, 2009 - v0.5 - Added sub-groups feature per RubyForge tracker [#27019]. Config files can now have [subgroups] whose values are added to a nested Hash object. - Added the write() function per RubyForge tracker [#27019]. Will print to STDOUT by default, or to a file object if passed. - Added the add(), and add_to_group() functions to support new features per RubyForge tracker [#27019]. - Thank you to Titouan Christophe for the submissions listed above! - ParseConfig.get_params() returns all params including groups. ParseConfig.get_groups() returns available sub-groups. - See the demo.rb and demo.conf files which have been updated to reflect this update. - The methods override_value() and nil_value() are now deprecated and will be removed in future versions. Sat Mar 28, 2009 - v0.4.3 - Added the self.params member that is a Hash holding all parameter/values. - Added the 'get_params' to return an array of all config parameters. Thu Feb 28, 2008 - v0.4.2 - Fixed bug where if the value contains a '=' then the parameter is not properly set. [bjd] - Fixed bug #13680 Unable to parse config options that contain single quotes. [bjd] Sun Sep 03, 2007 - v0.4.1 - Now using 'instance_variable_set' and 'instance_variable_get' instead of 'eval' (yeah.. that was dirty). [bjd] Sat Aug 11, 2007 - v0.3.2 - reorganizing files a bit. [bjd] - renamed methods .... no more uglyStyle... now new_style. [bjd] Fri Feb 23, 2007 - v0.3.1 - Added a bit of code to remove 'single' quotes... so, until I figure a better option, values can't contain single quotes. [bjd] Wed Feb 07, 2007 - v0.2.1 - Renamed ParseConfig.class.rb to ParseConfig.rb. [bjd] - Add 'strip' to remove trailing white spaces from config file. [bjd] Wed Dec 13, 2006 - v0.1.3 - Added error check to ensure configFile is readable. [bjd] Sat Nov 25, 2006 - v0.1.2 - Added regex to only read lines matching /\s*=\s*/ as The class would bork if you had say, and empty line. [bjd] Fri Nov 24 2006 - v0.1.1 - Built class, which is functional and fully usable. [bjd] parseconfig-1.0.8/LICENSE0000644000004100000410000000210013364046174015056 0ustar www-datawww-data The MIT License: Copyright (c) 2006-2016 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 in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. parseconfig-1.0.8/lib/0000755000004100000410000000000013364046174014626 5ustar www-datawww-dataparseconfig-1.0.8/lib/parseconfig.rb0000644000004100000410000001345613364046174017464 0ustar www-datawww-data# # Author:: BJ Dierkes # Copyright:: Copyright (c) 2006,2016 Data Folk Labs, LLC # License:: MIT # URL:: https://github.com/datafolklabs/ruby-parseconfig # # This class was written to simplify the parsing of configuration # 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 # config file class ParseConfig Version = '1.0.8' attr_accessor :config_file, :params, :groups # Initialize the class with the path to the 'config_file' # The class objects are dynamically generated by the # name of the 'param' in the config file. Therefore, if # the config file is 'param = value' then the itializer # will eval "@param = value" # def initialize(config_file=nil, separator='=', comments=['#', ';']) @config_file = config_file @params = {} @groups = [] @splitRegex = '\s*' + separator + '\s*' @comments = comments if(self.config_file) self.validate_config() self.import_config() end 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 # FIX ME: need to validate contents/structure? 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. 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")) end rescue NoMethodError end is_comment = false @comments.each do |comment| if (/^#{comment}/.match(line)) is_comment = true break 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 else new_value = value end else new_value = '' 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. # # DEPRECATED - will be removed in future versions # def get_value(param) puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \ "config['param'] or config['group']['param'] instead." return self.params[param] end # This method is a shortcut to accessing the @params variable def [](param) return self.params[param] end # This method returns all parameters/groups defined in a config file. def get_params() return self.params.keys end # List available sub-groups of the config. def get_groups() return self.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 override self.params[param_name] = value else self.params[param_name].merge!(value) end elsif self.params.has_key?(param_name) if self.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 end if ! self.groups.include?(param_name) self.groups.push(param_name) end else self.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 end # Writes out the config file to output_stream def write(output_stream=STDOUT, quoted=true) self.params.each do |name,value| if value.class.to_s != 'Hash' if quoted == true output_stream.puts "#{name} = \"#{value}\"" else output_stream.puts "#{name} = #{value}" end end end output_stream.puts "\n" self.groups.each do |group| output_stream.puts "[#{group}]" self.params[group].each do |param, value| if quoted == true output_stream.puts "#{param} = \"#{value}\"" else output_stream.puts "#{param} = #{value}" end end output_stream.puts "\n" end end # Public: Compare this ParseConfig to some other ParseConfig. For two config to # be equivalent, they must have the same sections with the same parameters # # other - The other ParseConfig. # # Returns true if ParseConfig are equivalent and false if they differ. def eql?(other) self.params == other.params && self.groups == other.groups end alias == eql? end