iniparser 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57828451b2152df706f149f08a0e163ab260eb83
4
- data.tar.gz: 94c5a6efa3dc99fb7f92d8ae1cea835de6190e8b
3
+ metadata.gz: 361522b3234615dbc73ae63ecd9abbb9ed1df79a
4
+ data.tar.gz: 9c7d1909eb8a8c7d5616d9be5d8d2f9021ee2d1b
5
5
  SHA512:
6
- metadata.gz: 1799b8c7c03b8b1470befcaa20239affcec38a840fd012c91b42c40fe14057c212ae355dd9d1b2e78f2ce6ce5c24fe3183185fdb210d3e21ca628491b6dc5f4f
7
- data.tar.gz: 82e6c8c1040b1399b5e7549800a9fa40ab219eee21b90f6d9ee819dc5217f9cf59545b2d8c084920cd52ec52b64e22be81ba8917aa036936fc997a61241b1031
6
+ metadata.gz: bd91875fcc709176b7a1da91264497c82aa954660debcc5b3a51083f5c521618cf728e382c66b1b09ddbc32b2bea11c304f1e815ea0995fc6ea4fc79577811c5
7
+ data.tar.gz: 34a3188526a16e62006c34d4a9406c2f5c256ea020ceea8def88c68d105fb14670782cf54a6d7bd8a47ac5c1db48342464b8f7de3e269e04f8d42b2e0e755983
@@ -3,6 +3,8 @@ Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
5
  lib/iniparser.rb
6
+ lib/iniparser/parser.rb
6
7
  lib/iniparser/version.rb
7
8
  test/helper.rb
9
+ test/test_parser.rb
8
10
  test/test_version.rb
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # iniparser
2
2
 
3
- iniparser gem - read INI configuration, setttings and data files into a hash
3
+ iniparser gem - read / parse INI configuration, setttings and data files into a hash
4
4
 
5
5
 
6
6
  * home :: [github.com/datatxt/iniparser](https://github.com/datatxt/iniparser)
@@ -9,12 +9,85 @@ iniparser gem - read INI configuration, setttings and data files into a hash
9
9
  * rdoc :: [rubydoc.info/gems/iniparser](http://rubydoc.info/gems/iniparser)
10
10
 
11
11
 
12
- ## Usage
13
12
 
14
- TBD
13
+ ## Usage - `INI.load`, `INI.load_file`
14
+
15
+ Use `INI.load` or `INI.load_file`. Example:
16
+
17
+
18
+ ``` ruby
19
+ require 'iniparser'
20
+
21
+ text = <<TXT
22
+ # comment
23
+ ; another comment
24
+
25
+ key1 = hello
26
+ key2 : hi!
27
+
28
+ [section1]
29
+ key3 = salut # end of line comment here
30
+
31
+ [section2]
32
+ key4: hola
33
+ blank =
34
+ blank2:
35
+
36
+ [ http://example.com ]
37
+ title = A rose is a rose is a rose, eh?
38
+ title2: A rose is a rose is a rose, eh? # comment here
39
+ ; another one here
40
+ title3 = A rose is a rose is a rose, eh?
41
+ TXT
42
+
43
+ hash = INI.load( text )
44
+ pp hash
45
+ ```
46
+
47
+ resulting in:
48
+
49
+ ``` ruby
50
+ {"key1"=>"hello",
51
+ "key2"=>"hi!",
52
+ "section1"=>{"key3"=>"salut"},
53
+ "section2"=>{"key4"=>"hola", "blank"=>"", "blank2"=>""},
54
+ "http://example.com"=>
55
+ {"title"=>"A rose is a rose is a rose, eh?",
56
+ "title2"=>"A rose is a rose is a rose, eh?",
57
+ "title3"=>"A rose is a rose is a rose, eh?"}}
58
+ ```
59
+
60
+ to access use:
61
+
62
+ ``` ruby
63
+ puts hash['key1']
64
+ # => 'hello'
65
+ puts hash['key2']
66
+ # => 'hi!'
67
+ puts hash['section1']['key3']
68
+ # => 'salut'
69
+ puts hash['section2']['key4']
70
+ # => 'hola'
71
+ puts hash['section2']['blank']
72
+ # => ''
73
+ puts hash['section2']['blank2']
74
+ # => ''
75
+ puts hash['http://example.com']['title']
76
+ # => 'A rose is a rose is a rose, eh?'
77
+ puts hash['http://example.com']['title2']
78
+ # => 'A rose is a rose is a rose, eh?'
79
+ puts hash['http://example.com']['title3']
80
+ # => 'A rose is a rose is a rose, eh?'
81
+ ```
15
82
 
16
83
 
17
84
  ## License
18
85
 
86
+ ![](https://publicdomainworks.github.io/buttons/zero88x31.png)
87
+
19
88
  The `iniparser` scripts are dedicated to the public domain.
20
89
  Use it as you please with no restrictions whatsoever.
90
+
91
+ ## Questions? Comments?
92
+
93
+ Post them to the [wwwmake forum](http://groups.google.com/group/wwwmake). Thanks!
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ Hoe.spec 'iniparser' do
5
5
 
6
6
  self.version = IniParser::VERSION
7
7
 
8
- self.summary = 'iniparser - read INI configuration, setttings and data files into a hash'
8
+ self.summary = 'iniparser - read / parse INI configuration, setttings and data files into a hash'
9
9
  self.description = summary
10
10
 
11
11
  self.urls = ['https://github.com/datatxt/iniparser']
@@ -1,9 +1,23 @@
1
1
  # encoding: utf-8
2
2
 
3
3
 
4
+ require 'pp'
4
5
 
5
6
  # our own code
6
7
  require 'iniparser/version' # note: let version always go first
8
+ require 'iniparser/parser'
9
+
10
+
11
+
12
+ module INI
13
+
14
+ # returns a nested hash
15
+ # (compatible structure - works like YAML.load_file)
16
+
17
+ def self.load_file( path ) IniParser::IniParser.load_file( path ); end
18
+ def self.load( text ) IniParser::IniParser.load( text ); end
19
+
20
+ end # module INI
7
21
 
8
22
 
9
23
 
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ module IniParser
5
+
6
+
7
+ class IniParser
8
+
9
+ # returns a nested hash
10
+ # (compatible structure - works like YAML.load_file)
11
+
12
+ def self.load_file( path )
13
+ text = File.open( path, 'r:bom|utf-8' ).read
14
+ self.load( text )
15
+ end
16
+
17
+ def self.load( text )
18
+ self.new( text ).parse
19
+ end
20
+
21
+
22
+ def initialize( text )
23
+ @text = text
24
+ end
25
+
26
+ def parse
27
+ hash = top_hash = Hash.new
28
+
29
+ text = @text
30
+ text = text.gsub( "\t", ' ' ) # replace all tabs w/ spaces
31
+
32
+ text.each_line do |line|
33
+
34
+ ### skip comments
35
+ # e.g. # this is a comment line
36
+ # or ; this too
37
+ # or -- haskell style
38
+ # or % text style
39
+
40
+ if line =~ /^\s*#/ || line =~ /^\s*;/ || line =~ /^\s*--/ || line =~ /^\s*%/
41
+ ## logger.debug 'skipping comment line'
42
+ next
43
+ end
44
+
45
+ ### skip blank lines
46
+ if line =~ /^\s*$/
47
+ ## logger.debug 'skipping blank line'
48
+ next
49
+ end
50
+
51
+ # pass 1) remove possible trailing eol comment
52
+ ## e.g -> New York # Sample EOL Comment Here (with or without commas,,,,)
53
+ ## becomes -> New York
54
+
55
+ line = line.sub( /\s+#.*$/, '' )
56
+
57
+ # pass 2) remove leading and trailing whitespace
58
+
59
+ line = line.strip
60
+
61
+ ## check for new section e.g. [planet012-xxx_bc]
62
+
63
+ ### todo: allow _ or - in strict section key? why? why not??
64
+ ### allow _ or - in value key? why why not??
65
+ if line =~ /^\s*\[\s*([a-z0-9_\-]+)\s*\]\s*$/ # strict section
66
+ key = $1.to_s.dup
67
+ hash = top_hash[ key ] = Hash.new
68
+ elsif line =~ /^\s*\[\s*([^ \]]+)\s*\]\s*$/ # liberal section; allow everything in key
69
+ key = $1.to_s.dup
70
+ hash = top_hash[ key ] = Hash.new
71
+ elsif line =~ /^\s*([a-z0-9_\-]+)\s*[:=](.*)$/
72
+ key = $1.to_s.dup
73
+ value = $2.to_s.strip.dup # check if it can be nil? if yes use blank string e.g. ''
74
+ ### todo: strip quotes from value??? why? why not?
75
+ hash[ key ] = value
76
+ else
77
+ puts "*** warn: skipping unknown line type in ini >#{line}<"
78
+ end
79
+ end # each lines
80
+
81
+ top_hash
82
+ end
83
+
84
+ end # class IniParser
85
+
86
+ end # module IniParser
@@ -2,8 +2,8 @@
2
2
 
3
3
  module IniParser
4
4
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
5
- MINOR = 0
6
- PATCH = 1
5
+ MINOR = 1
6
+ PATCH = 0
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
9
9
  def self.version
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_parser.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ class TestParser < MiniTest::Test
12
+
13
+ def test_load
14
+
15
+ text = <<TXT
16
+ # comment
17
+ ; another comment
18
+
19
+ key1 = hello
20
+ key2 : hi!
21
+
22
+ [section1]
23
+ key3 = salut # end of line comment here
24
+
25
+ [section2]
26
+ key4: hola
27
+ blank =
28
+ blank2:
29
+
30
+ [ http://example.com ]
31
+ title = A rose is a rose is a rose, eh?
32
+ title2: A rose is a rose is a rose, eh? # comment here
33
+ ; another one here
34
+ title3 = A rose is a rose is a rose, eh?
35
+ TXT
36
+
37
+ hash = INI.load( text )
38
+ pp hash
39
+
40
+ assert_equal 'hello', hash['key1']
41
+ assert_equal 'hi!', hash['key2']
42
+ assert_equal 'salut', hash['section1']['key3']
43
+ assert_equal 'hola', hash['section2']['key4']
44
+ assert_equal '', hash['section2']['blank']
45
+ assert_equal '', hash['section2']['blank2']
46
+ assert_equal 'A rose is a rose is a rose, eh?', hash['http://example.com']['title']
47
+ assert_equal 'A rose is a rose is a rose, eh?', hash['http://example.com']['title2']
48
+ assert_equal 'A rose is a rose is a rose, eh?', hash['http://example.com']['title3']
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iniparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.15'
41
- description: iniparser - read INI configuration, setttings and data files into a hash
41
+ description: iniparser - read / parse INI configuration, setttings and data files
42
+ into a hash
42
43
  email: ruby-talk@ruby-lang.org
43
44
  executables: []
44
45
  extensions: []
@@ -52,8 +53,10 @@ files:
52
53
  - README.md
53
54
  - Rakefile
54
55
  - lib/iniparser.rb
56
+ - lib/iniparser/parser.rb
55
57
  - lib/iniparser/version.rb
56
58
  - test/helper.rb
59
+ - test/test_parser.rb
57
60
  - test/test_version.rb
58
61
  homepage: https://github.com/datatxt/iniparser
59
62
  licenses:
@@ -80,5 +83,6 @@ rubyforge_project:
80
83
  rubygems_version: 2.6.7
81
84
  signing_key:
82
85
  specification_version: 4
83
- summary: iniparser - read INI configuration, setttings and data files into a hash
86
+ summary: iniparser - read / parse INI configuration, setttings and data files into
87
+ a hash
84
88
  test_files: []