configparser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +3 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/lib/configparser.rb +1 -0
- data/lib/configparser/base.rb +84 -0
- data/test/helper.rb +18 -0
- data/test/test_configparser.rb +37 -0
- metadata +155 -0
- metadata.gz.sig +1 -0
data.tar.gz.sig
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Chris Lee, PhD
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= configparser
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to configparser
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Chris Lee, PhD. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/lib/configparser.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'configparser/base'
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# DESCRIPTION: parses configuration files compatable with Python's ConfigParser
|
3
|
+
|
4
|
+
class ConfigParser < Hash
|
5
|
+
def initialize(fname)
|
6
|
+
section = nil
|
7
|
+
key = nil
|
8
|
+
File.open(fname,"r").each_line do |line|
|
9
|
+
next if (line =~ /^(#|;)/)
|
10
|
+
|
11
|
+
# parse out the lines of the config
|
12
|
+
if line =~ /^(.+?)\s*[=:]\s*(.+)$/ # handle key=value lines
|
13
|
+
if section
|
14
|
+
self[section] = {} unless self[section]
|
15
|
+
key = $1
|
16
|
+
self[section][key] = $2
|
17
|
+
else
|
18
|
+
key = $1
|
19
|
+
self[key] = $2
|
20
|
+
end
|
21
|
+
elsif line =~ /^\[(.+?)\]/ # handle new sections
|
22
|
+
section = $1
|
23
|
+
elsif line =~ /^\s+(.+?)$/ # handle continued lines
|
24
|
+
if section
|
25
|
+
self[section][key] += " #{$1}";
|
26
|
+
else
|
27
|
+
self[key] += " #{$1}"
|
28
|
+
end
|
29
|
+
elsif line =~ /^([\w\d\_\-]+)$/
|
30
|
+
if section
|
31
|
+
self[section] = {} unless self[section]
|
32
|
+
key = $1
|
33
|
+
self[section][key] = true
|
34
|
+
else
|
35
|
+
key = $1
|
36
|
+
self[key] = true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# handle substitutions (globals first)
|
42
|
+
self.each_key do |k|
|
43
|
+
next if self[k].is_a? Hash
|
44
|
+
next unless self[k].is_a? String
|
45
|
+
self[k].gsub!(/\$\((.+?)\)/) {|x| self[$1] || "$(#{$1})"}
|
46
|
+
end
|
47
|
+
|
48
|
+
# handle substitutions within the sections
|
49
|
+
self.each_key do |k|
|
50
|
+
next unless self[k].is_a? Hash
|
51
|
+
self[k].each_key do |j|
|
52
|
+
next unless self[k][j].is_a? String
|
53
|
+
self[k][j].gsub!(/\$\((.+?)\)/) {|x| self[k][$1] || self[$1] || "$(#{$1})"}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_s
|
59
|
+
str = ""
|
60
|
+
# print globals first
|
61
|
+
self.keys.sort.each do |k|
|
62
|
+
next if self[k].is_a? Hash
|
63
|
+
if self[k] === true
|
64
|
+
str << "#{k}\n"
|
65
|
+
else
|
66
|
+
str << "#{k}: #{self[k]}\n"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# now print the sections
|
71
|
+
self.keys.sort.each do |k|
|
72
|
+
next unless self[k].is_a? Hash
|
73
|
+
str << "[#{k}]\n"
|
74
|
+
self[k].keys.sort.each do |j|
|
75
|
+
if self[k][j] === true
|
76
|
+
str << "#{j}\n"
|
77
|
+
else
|
78
|
+
str << "#{j}: #{self[k][j]}\n"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
str
|
83
|
+
end
|
84
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'configparser'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestConfigparser < Test::Unit::TestCase
|
4
|
+
should "parse a simple config" do
|
5
|
+
cp = ConfigParser.new('test/simple.cfg')
|
6
|
+
assert_not_nil(cp)
|
7
|
+
assert_equal('hi',cp['test1'])
|
8
|
+
assert_equal('hello',cp['test2'])
|
9
|
+
assert_equal('55',cp['first_section']['mytest'])
|
10
|
+
assert_equal('99',cp['first_section']['yourtest'])
|
11
|
+
assert_nil(cp['first_section']['nothere'])
|
12
|
+
assert_equal('or the highway',cp['second section']['myway'])
|
13
|
+
end
|
14
|
+
|
15
|
+
should "convert a simple config to a string" do
|
16
|
+
cp = ConfigParser.new('test/simple.cfg')
|
17
|
+
doc = "test1: hi
|
18
|
+
test2: hello
|
19
|
+
[first_section]
|
20
|
+
myboolean
|
21
|
+
mytest: 55
|
22
|
+
yourtest: 99
|
23
|
+
[second section]
|
24
|
+
myway: or the highway
|
25
|
+
"
|
26
|
+
assert_equal(doc,cp.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
should "parse a config with substitutions" do
|
30
|
+
cp = ConfigParser.new('test/complex.cfg')
|
31
|
+
assert_not_nil(cp)
|
32
|
+
assert_equal('strange-default-whatever',cp['global2'])
|
33
|
+
assert_equal('strange-default-whatever-yodel-local',cp['section1']['local1'])
|
34
|
+
assert_equal('recent hotel',cp['section2']['local2'])
|
35
|
+
assert_equal('un$(resolvable)',cp['section2']['local3'])
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: configparser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris Lee
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain:
|
17
|
+
- |
|
18
|
+
-----BEGIN CERTIFICATE-----
|
19
|
+
MIIDYjCCAkqgAwIBAgIBADANBgkqhkiG9w0BAQUFADBXMREwDwYDVQQDDAhydWJ5
|
20
|
+
Z2VtczEYMBYGCgmSJomT8ixkARkWCGNocmlzbGVlMRMwEQYKCZImiZPyLGQBGRYD
|
21
|
+
ZGhzMRMwEQYKCZImiZPyLGQBGRYDb3JnMB4XDTExMDIyNzE1MzAxOVoXDTEyMDIy
|
22
|
+
NzE1MzAxOVowVzERMA8GA1UEAwwIcnVieWdlbXMxGDAWBgoJkiaJk/IsZAEZFghj
|
23
|
+
aHJpc2xlZTETMBEGCgmSJomT8ixkARkWA2RoczETMBEGCgmSJomT8ixkARkWA29y
|
24
|
+
ZzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALNM1Hjs6q58sf7Jp64A
|
25
|
+
vEY2cnRWDdFpD8UWpwaJK5kgSHOVgs+0mtszn+YlYjmx8kpmuYpyU4g9mNMImMQe
|
26
|
+
ow8pVsL4QBBK/1Ozgdxrsptk3IiTozMYA+g2I/+WvZSEDu9uHkKe8pvMBEMrg7RJ
|
27
|
+
IN7+jWaPnSzg3DbFwxwOdi+QRw33DjK7oFWcOaaBqWTUpI4epdi/c/FE1I6UWULJ
|
28
|
+
ZF/Uso0Sc2Pp/YuVhuMHGrUbn7zrWWo76nnK4DTLfXFDbZF5lIXT1w6BtIiN6Ho9
|
29
|
+
Rdr/W6663hYUo3WMsUSa3I5+PJXEBKmGHIZ2TNFnoFIRHha2fmm1HC9+BTaKwcO9
|
30
|
+
PLcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQURzsNkZo2rv86Ftc+hVww
|
31
|
+
RNICMrwwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQBRRw/iNA/PdnvW
|
32
|
+
OBoNCSr/IiHOGZqMHgPJwyWs68FhThnLc2EyIkuLTQf98ms1/D3p0XX9JsxazvKT
|
33
|
+
W/in8Mm/R2fkVziSdzqChtw/4Z4bW3c+RF7TgX6SP5cKxNAfKmAPuItcs2Y+7bdS
|
34
|
+
hr/FktVtT2iAmISRnlEbdaTpfl6N2ZWNT83khV6iOs5xRkX/+0e+GgAv9mE6nqr1
|
35
|
+
AkuDXMhposxcnFZUrZ3UtMPEe/JnyP7Vv6pvr3qtZm8FidFZU91+rX/fwdyBU8RP
|
36
|
+
/5l8uLWXXNt1wEbtu4N1I66LwTK2iRrQZE8XtlgZGbxYDFUkiurq3OafF2YwRs6W
|
37
|
+
6yhklP75
|
38
|
+
-----END CERTIFICATE-----
|
39
|
+
|
40
|
+
date: 2011-03-11 00:00:00 -05:00
|
41
|
+
default_executable:
|
42
|
+
dependencies:
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
requirement: *id001
|
54
|
+
prerelease: false
|
55
|
+
name: shoulda
|
56
|
+
type: :development
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 23
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 0
|
67
|
+
- 0
|
68
|
+
version: 1.0.0
|
69
|
+
requirement: *id002
|
70
|
+
prerelease: false
|
71
|
+
name: bundler
|
72
|
+
type: :development
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ~>
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 7
|
80
|
+
segments:
|
81
|
+
- 1
|
82
|
+
- 5
|
83
|
+
- 2
|
84
|
+
version: 1.5.2
|
85
|
+
requirement: *id003
|
86
|
+
prerelease: false
|
87
|
+
name: jeweler
|
88
|
+
type: :development
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
requirement: *id004
|
100
|
+
prerelease: false
|
101
|
+
name: rcov
|
102
|
+
type: :development
|
103
|
+
description: parses configuration files compatable with Python's ConfigParser
|
104
|
+
email: rubygems@chrislee.dhs.org
|
105
|
+
executables: []
|
106
|
+
|
107
|
+
extensions: []
|
108
|
+
|
109
|
+
extra_rdoc_files:
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.rdoc
|
112
|
+
files:
|
113
|
+
- lib/configparser.rb
|
114
|
+
- lib/configparser/base.rb
|
115
|
+
- LICENSE.txt
|
116
|
+
- README.rdoc
|
117
|
+
- test/helper.rb
|
118
|
+
- test/test_configparser.rb
|
119
|
+
has_rdoc: true
|
120
|
+
homepage: https://rubygems.org/gems/configparser
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
hash: 3
|
134
|
+
segments:
|
135
|
+
- 0
|
136
|
+
version: "0"
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
hash: 3
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
version: "0"
|
146
|
+
requirements: []
|
147
|
+
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 1.6.1
|
150
|
+
signing_key:
|
151
|
+
specification_version: 3
|
152
|
+
summary: parses configuration files compatable with Python's ConfigParser
|
153
|
+
test_files:
|
154
|
+
- test/helper.rb
|
155
|
+
- test/test_configparser.rb
|
metadata.gz.sig
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1R6�Cܮz��} �dk�]���tX�*����������t�C9�g,� @�J3���:k�>i�C�V�X��=7����܅ֿ��'�3�½>�"��%�/���*JV9}�e�#��k�����s�D�>�3q\�(VK�3T����LWS��OPJ��������GMt�R$�?��ߍIu=o�)2�|�c������O2����L�X4N��F��^qU �9��� �r��!n{�c[���U\`���
|