confparser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/confparser.rb +104 -0
  2. metadata +64 -0
data/lib/confparser.rb ADDED
@@ -0,0 +1,104 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # Copyleft shura [shura1991@gmail.com]
6
+ #
7
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
8
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9
+ #
10
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
11
+ #++
12
+
13
+ autoload :StringIO, 'stringio'
14
+
15
+ class ConfParser < Hash
16
+ class Section < Hash
17
+ attr_reader :parent
18
+
19
+ def initialize (parent)
20
+ super()
21
+ @parent = parent
22
+ end
23
+
24
+ alias __get__ []
25
+ def [] (name)
26
+ __get__(name).tap {|x|
27
+ if x.is_a?(String)
28
+ x.gsub!(/\$\((.+?)\)/) {|n|
29
+ (self[$1] || parent[$1]).to_s
30
+ }
31
+ end
32
+ }
33
+ end
34
+
35
+ private :__get__
36
+ end
37
+
38
+ class << self
39
+ protected :new
40
+
41
+ def from_file (file)
42
+ return nil unless File.file?(file)
43
+ self.new(File.open(file))
44
+ end
45
+
46
+ def from_io (io)
47
+ return nil unless io.is_a?(IO)
48
+ self.new(io)
49
+ end
50
+
51
+ def from_string (str)
52
+ return nil unless str.is_a?(String)
53
+ self.new(StringIO.new(str))
54
+ end
55
+ end
56
+
57
+ def initialize (io)
58
+ section, key, lineno = nil, nil, 0
59
+
60
+ io.each_line {|line|
61
+ lineno += 1
62
+
63
+ case line
64
+ when /^\s*[;#]/ then next
65
+ when /^\s*(.+?)\s*[=:]\s*(.*)$/
66
+ if section
67
+ self[section] = Section.new(self) unless self[section]
68
+ key = $1
69
+ self[section][key] = $2
70
+ else
71
+ key = $1
72
+ self[key] = $2
73
+ end
74
+ when /^\s*\[(.+?)\]\s*$/
75
+ section = $1
76
+ else
77
+ if key
78
+ if section
79
+ self[section] = Section.new(self) unless self[section]
80
+ self[section][key] += "\n" + line
81
+ else
82
+ self[key] += "\n" + line
83
+ end
84
+ else
85
+ raise "Syntax error at line #{lineno}"
86
+ end
87
+ end
88
+ }
89
+ io.close
90
+ end
91
+
92
+ alias __get__ []
93
+ def [] (name)
94
+ __get__(name).tap {|x|
95
+ if x.is_a?(String)
96
+ x.gsub!(/\$\((.+?)\)/) {|n|
97
+ (self[$1]).to_s
98
+ }
99
+ end
100
+ }
101
+ end
102
+
103
+ private :__get__
104
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confparser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - shura
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-08-05 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: parses configuration files compatable with Python's ConfigParser, gitconfig, etc
22
+ email: shura1991@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/confparser.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/shurizzle/confparser
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.7
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: parses configuration files compatable with Python's ConfigParser, gitconfig, etc
63
+ test_files: []
64
+