inistyle 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example.rb +5 -0
- data/inistyle.gemspec +27 -0
- data/lib/inistyle.rb +104 -0
- data/lib/inistyle/version.rb +3 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21de462433ac75043c67e027c999ce1776f86eba
|
4
|
+
data.tar.gz: 899950e7febb1f81f55cc3ae5629c7f255ecc44d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37062d715293b876f9da0d595cd8d2db8d04951c51550d557e347c1360a2f83adbd8d88c2f8c6c8013c2a20b79f7795679cdbb9ee566ad71edcaab5eaf2e5597
|
7
|
+
data.tar.gz: 088a25f4a02d53c7a4d07ad305093519d11ed9362baa670ec441f9b6cda6542d4cdb4caf55a32c059b2b4bfa4b49244dacb48653b55c56c0257396ede72c9877
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Geoff Williams
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Inistyle
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/inistyle`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'inistyle'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install inistyle
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/inistyle.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "inistyle"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/example.rb
ADDED
data/inistyle.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'inistyle/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "inistyle"
|
8
|
+
spec.version = Inistyle::VERSION
|
9
|
+
spec.authors = ["Geoff Williams"]
|
10
|
+
spec.email = ["geoff.williams@puppetlabs.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Parse and write INI sytle files.}
|
13
|
+
spec.description = %q{Simplistic parsing of INI style files. Data within sections can be handled as raw lines or as an array split on spaces.}
|
14
|
+
spec.homepage = "https://github.com/GeoffWilliams/inistyle"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
data/lib/inistyle.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require "inistyle/version"
|
2
|
+
|
3
|
+
#module IniStyle
|
4
|
+
class IniStyle
|
5
|
+
@@section_regexp = /\s*\[([^\]]+)\]\s*/
|
6
|
+
@@data_regexp = /([^#]*)#?.*/
|
7
|
+
|
8
|
+
def initialize(filename, data=false, split=false)
|
9
|
+
@filename = filename
|
10
|
+
@split = split
|
11
|
+
if data
|
12
|
+
@data = data
|
13
|
+
elsif File.exists?(filename)
|
14
|
+
parse
|
15
|
+
else
|
16
|
+
data = {}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse
|
21
|
+
current_section = '__GLOBAL__'
|
22
|
+
@data = {current_section => []}
|
23
|
+
File.open(@filename, "r").each_line do |line|
|
24
|
+
if line =~ @@section_regexp
|
25
|
+
# new section found
|
26
|
+
current_section = $1
|
27
|
+
@data[current_section] = []
|
28
|
+
else
|
29
|
+
# strip comments
|
30
|
+
if line =~ @@data_regexp
|
31
|
+
captured = $1
|
32
|
+
if captured !~ /^\s*$/
|
33
|
+
if @split
|
34
|
+
entry = captured.strip.split("\s+")
|
35
|
+
else
|
36
|
+
entry = captured.strip
|
37
|
+
end
|
38
|
+
@data[current_section].push(entry)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def save
|
46
|
+
open(@filename, 'w') { |f|
|
47
|
+
@data.keys { |key|
|
48
|
+
f.puts "[#{key}]"
|
49
|
+
@data[key].each { |entry|
|
50
|
+
if entry.kind_of?(Array)
|
51
|
+
line = entry.join(' ')
|
52
|
+
else
|
53
|
+
line = entry
|
54
|
+
end
|
55
|
+
f.puts line
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# Public: Get the section Hash by name. If the section does not exist, then
|
62
|
+
# it will be created.
|
63
|
+
#
|
64
|
+
# section - The section name as a String.
|
65
|
+
#
|
66
|
+
# Examples
|
67
|
+
#
|
68
|
+
# inifile['global']
|
69
|
+
# #=> global section Hash
|
70
|
+
#
|
71
|
+
# Returns the Hash of parameter/value pairs for this section.
|
72
|
+
def []( section )
|
73
|
+
return nil if section.nil?
|
74
|
+
@data[section.to_s]
|
75
|
+
end
|
76
|
+
|
77
|
+
# Public: Set the section to a hash of parameter/value pairs.
|
78
|
+
#
|
79
|
+
# section - The section name as a String.
|
80
|
+
# value - The Hash of parameter/value pairs.
|
81
|
+
#
|
82
|
+
# Examples
|
83
|
+
#
|
84
|
+
# inifile['tenderloin'] = { 'gritty' => 'yes' }
|
85
|
+
# #=> { 'gritty' => 'yes' }
|
86
|
+
#
|
87
|
+
# Returns the value Hash.
|
88
|
+
def []=( section, value )
|
89
|
+
@data[section.to_s] = value
|
90
|
+
end
|
91
|
+
|
92
|
+
def sections
|
93
|
+
@data.keys
|
94
|
+
end
|
95
|
+
|
96
|
+
def get_data
|
97
|
+
@data
|
98
|
+
end
|
99
|
+
|
100
|
+
def set_data(data)
|
101
|
+
@data = data
|
102
|
+
end
|
103
|
+
end
|
104
|
+
#end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inistyle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Geoff Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Simplistic parsing of INI style files. Data within sections can be handled
|
42
|
+
as raw lines or as an array split on spaces.
|
43
|
+
email:
|
44
|
+
- geoff.williams@puppetlabs.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- example.rb
|
57
|
+
- inistyle.gemspec
|
58
|
+
- lib/inistyle.rb
|
59
|
+
- lib/inistyle/version.rb
|
60
|
+
homepage: https://github.com/GeoffWilliams/inistyle
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.6
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Parse and write INI sytle files.
|
84
|
+
test_files: []
|
85
|
+
has_rdoc:
|