dotini 0.1.0
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.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +23 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +23 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +68 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dotini.gemspec +34 -0
- data/lib/dotini.rb +10 -0
- data/lib/dotini/ini_file.rb +91 -0
- data/lib/dotini/key_value_pair.rb +36 -0
- data/lib/dotini/section.rb +49 -0
- data/lib/dotini/version.rb +3 -0
- metadata +120 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9129f4261682214a224a4085a6feab009e1366713e5c3b138c2e0275605da26f
|
|
4
|
+
data.tar.gz: 30890b30a109b43e3a6d91100b74c304ba644e886147c8fa7b97fba08606b547
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: aefebdb8e20d507093eeae7943f517a64c08f4fff6f99aead685f2c545ade8410117bbe4d78f8ef9c7aeee539ba369ae838efcc4d8faaeabc8dadc3dc4570d1f
|
|
7
|
+
data.tar.gz: 88001949bead84ba98de29dc672387edcf24e67758e14794e073647558129445c0f8e737d5deb4f475da52dac5938dfabaecad8903d5899c2dc897a06cdea997
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
tests:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
strategy:
|
|
9
|
+
fail-fast: false
|
|
10
|
+
matrix:
|
|
11
|
+
ruby: [ '2.6.7', '2.7.3', '3.0.1']
|
|
12
|
+
name: Ruby ${{ matrix.ruby }}
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v2
|
|
15
|
+
- name: Set up Ruby
|
|
16
|
+
uses: ruby/setup-ruby@v1
|
|
17
|
+
with:
|
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
|
19
|
+
bundler-cache: true
|
|
20
|
+
- name: Install dependencies
|
|
21
|
+
run: bundle install --jobs 4 --retry 3
|
|
22
|
+
- name: Run tests
|
|
23
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require:
|
|
2
|
+
- rubocop-performance
|
|
3
|
+
- rubocop-rake
|
|
4
|
+
AllCops:
|
|
5
|
+
NewCops: enable
|
|
6
|
+
Layout/LineLength:
|
|
7
|
+
Max: 120
|
|
8
|
+
Metrics/AbcSize:
|
|
9
|
+
Enabled: false
|
|
10
|
+
Metrics/BlockLength:
|
|
11
|
+
inherit_mode:
|
|
12
|
+
merge:
|
|
13
|
+
- Exclude
|
|
14
|
+
Exclude:
|
|
15
|
+
- lib/tasks/**/*
|
|
16
|
+
- spec/**/*
|
|
17
|
+
Metrics/MethodLength:
|
|
18
|
+
inherit_mode:
|
|
19
|
+
merge:
|
|
20
|
+
- Exclude
|
|
21
|
+
Max: 30
|
|
22
|
+
Metrics/ModuleLength:
|
|
23
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at wendelscardua@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
dotini (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.2)
|
|
10
|
+
coderay (1.1.3)
|
|
11
|
+
diff-lcs (1.4.4)
|
|
12
|
+
method_source (1.0.0)
|
|
13
|
+
parallel (1.20.1)
|
|
14
|
+
parser (3.0.1.1)
|
|
15
|
+
ast (~> 2.4.1)
|
|
16
|
+
pry (0.14.1)
|
|
17
|
+
coderay (~> 1.1)
|
|
18
|
+
method_source (~> 1.0)
|
|
19
|
+
rainbow (3.0.0)
|
|
20
|
+
rake (12.3.3)
|
|
21
|
+
regexp_parser (2.1.1)
|
|
22
|
+
rexml (3.2.5)
|
|
23
|
+
rspec (3.9.0)
|
|
24
|
+
rspec-core (~> 3.9.0)
|
|
25
|
+
rspec-expectations (~> 3.9.0)
|
|
26
|
+
rspec-mocks (~> 3.9.0)
|
|
27
|
+
rspec-core (3.9.3)
|
|
28
|
+
rspec-support (~> 3.9.3)
|
|
29
|
+
rspec-expectations (3.9.2)
|
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
31
|
+
rspec-support (~> 3.9.0)
|
|
32
|
+
rspec-mocks (3.9.1)
|
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
34
|
+
rspec-support (~> 3.9.0)
|
|
35
|
+
rspec-support (3.9.3)
|
|
36
|
+
rubocop (1.15.0)
|
|
37
|
+
parallel (~> 1.10)
|
|
38
|
+
parser (>= 3.0.0.0)
|
|
39
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
40
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
41
|
+
rexml
|
|
42
|
+
rubocop-ast (>= 1.5.0, < 2.0)
|
|
43
|
+
ruby-progressbar (~> 1.7)
|
|
44
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
45
|
+
rubocop-ast (1.7.0)
|
|
46
|
+
parser (>= 3.0.1.1)
|
|
47
|
+
rubocop-performance (1.11.3)
|
|
48
|
+
rubocop (>= 1.7.0, < 2.0)
|
|
49
|
+
rubocop-ast (>= 0.4.0)
|
|
50
|
+
rubocop-rake (0.5.1)
|
|
51
|
+
rubocop
|
|
52
|
+
ruby-progressbar (1.11.0)
|
|
53
|
+
unicode-display_width (2.0.0)
|
|
54
|
+
|
|
55
|
+
PLATFORMS
|
|
56
|
+
ruby
|
|
57
|
+
|
|
58
|
+
DEPENDENCIES
|
|
59
|
+
dotini!
|
|
60
|
+
pry (~> 0.14)
|
|
61
|
+
rake (~> 12.3)
|
|
62
|
+
rspec (~> 3.0)
|
|
63
|
+
rubocop (~> 1.15)
|
|
64
|
+
rubocop-performance (~> 1.11)
|
|
65
|
+
rubocop-rake (~> 0.5)
|
|
66
|
+
|
|
67
|
+
BUNDLED WITH
|
|
68
|
+
2.1.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Wendel Scardua
|
|
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,105 @@
|
|
|
1
|
+
[](https://github.com/wendelscardua/dotini/actions?query=workflow%3ACI+branch%3Amain)
|
|
2
|
+
|
|
3
|
+
# Dotini
|
|
4
|
+
|
|
5
|
+
Dotini allows [INI files](https://en.wikipedia.org/wiki/INI_file) to be parsed, created,
|
|
6
|
+
modified and written, preserving existing comments.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'dotini'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
$ bundle install
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
|
|
22
|
+
$ gem install dotini
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Reading an INI file
|
|
27
|
+
|
|
28
|
+
Given this INI file:
|
|
29
|
+
|
|
30
|
+
```ini
|
|
31
|
+
[main]
|
|
32
|
+
username = foo
|
|
33
|
+
; this is a color
|
|
34
|
+
; the color is nice
|
|
35
|
+
color = red
|
|
36
|
+
|
|
37
|
+
[personal]
|
|
38
|
+
color = cyan
|
|
39
|
+
path = /tmp ; TODO: change later
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
It can be read with:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
ini_file = Dotini::IniFile.load(filename, options) # options are not required
|
|
46
|
+
|
|
47
|
+
ini_file['main']['color'].value # => 'red'
|
|
48
|
+
ini_file['main']['color'].prepended_comments # => ['; this is a color', '; the color is nice']
|
|
49
|
+
ini_file['personal']['path'].value # => '/tmp'
|
|
50
|
+
ini_file['personal']['path'].inline_comment # => '; TODO: change later'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
These are the available options:
|
|
54
|
+
|
|
55
|
+
- `comment_character` (default: `';'`)
|
|
56
|
+
- `key_pattern` (default: `Dotini::IniFile::DEFAULT_KEY_PATTERN`)
|
|
57
|
+
- `value_pattern` (default: `Dotini::IniFile::DEFAULT_VALUE_PATTERN`)
|
|
58
|
+
|
|
59
|
+
### Creating a new INI file
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
ini_file = Dotini::IniFile.new
|
|
63
|
+
ini_file['profile default']['color'] = 'blue'
|
|
64
|
+
ini_file['preferences']['width'] = 42
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Saving the INI file
|
|
68
|
+
|
|
69
|
+
Given the INI file above, it can be turned into a string:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
ini_file.to_s # => "[profile default]\ncolor = blue\n[preferences]\nwidth = 42\n"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
...or it can be written to a IO stream:
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
File.open('new-file.ini', 'wb') do |file|
|
|
79
|
+
ini_file.write(file)
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Converting the INI file to a hash
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
ini_file.to_h # => { 'profile default' => { 'color' => 'blue' }, 'preferences' => { 'width' => '42' } }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
92
|
+
|
|
93
|
+
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).
|
|
94
|
+
|
|
95
|
+
## Contributing
|
|
96
|
+
|
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wendelscardua/dotini. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/wendelscardua/dotini/blob/master/CODE_OF_CONDUCT.md).
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
102
|
+
|
|
103
|
+
## Code of Conduct
|
|
104
|
+
|
|
105
|
+
Everyone interacting in the Dotini project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dotini/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'dotini'
|
|
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(__FILE__)
|
data/bin/setup
ADDED
data/dotini.gemspec
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/dotini/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'dotini'
|
|
7
|
+
spec.version = Dotini::VERSION
|
|
8
|
+
spec.authors = ['Wendel Scardua']
|
|
9
|
+
spec.email = ['wendelscardua@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'Read and write INI files'
|
|
12
|
+
spec.description = 'Parser and generator of INI files'
|
|
13
|
+
spec.homepage = 'https://github.com/wendelscardua/dotini'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
|
16
|
+
|
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
|
19
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency 'pry', '~> 0.14'
|
|
22
|
+
spec.add_development_dependency 'rubocop', '~> 1.15'
|
|
23
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.11'
|
|
24
|
+
spec.add_development_dependency 'rubocop-rake', '~> 0.5'
|
|
25
|
+
|
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
28
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
30
|
+
end
|
|
31
|
+
spec.bindir = 'exe'
|
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
33
|
+
spec.require_paths = ['lib']
|
|
34
|
+
end
|
data/lib/dotini.rb
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dotini
|
|
4
|
+
# Representation of a INI file
|
|
5
|
+
class IniFile
|
|
6
|
+
DEFAULT_KEY_PATTERN = /[^=\s]+/.freeze
|
|
7
|
+
DEFAULT_VALUE_PATTERN = /(?:"[^"]*"|[^=\s]*)/.freeze
|
|
8
|
+
|
|
9
|
+
attr_accessor :sections
|
|
10
|
+
|
|
11
|
+
# Creates a new, empty INI file
|
|
12
|
+
def initialize
|
|
13
|
+
@sections = []
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Retrieves an existing section, or creates a new one
|
|
17
|
+
def [](name)
|
|
18
|
+
sections.find { |section| section.name == name } ||
|
|
19
|
+
Section.new(name).tap { |section| sections << section }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Represents the current INI file as a hash
|
|
23
|
+
def to_h
|
|
24
|
+
{}.tap do |hash|
|
|
25
|
+
sections.each do |section|
|
|
26
|
+
section.to_h.then do |section_hash|
|
|
27
|
+
next if section_hash.empty?
|
|
28
|
+
|
|
29
|
+
(hash[section.name] ||= {}).merge! section_hash
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Represents the current INI file as a string
|
|
36
|
+
def to_s
|
|
37
|
+
buffer = StringIO.new
|
|
38
|
+
sections.each do |section|
|
|
39
|
+
buffer << section.to_s
|
|
40
|
+
end
|
|
41
|
+
buffer.string
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def write(io_stream)
|
|
45
|
+
io_stream.write(to_s)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class << self
|
|
49
|
+
# Loads an INI file by name
|
|
50
|
+
# The options are:
|
|
51
|
+
# - comment_character: which character is used for comments
|
|
52
|
+
# - key_pattern: a regexp that matches the property keys
|
|
53
|
+
# - value_pattern: a regexp that matches the property values
|
|
54
|
+
def load(filename,
|
|
55
|
+
comment_character: ';',
|
|
56
|
+
key_pattern: DEFAULT_KEY_PATTERN,
|
|
57
|
+
value_pattern: DEFAULT_VALUE_PATTERN)
|
|
58
|
+
line_pattern = /\A(?<key>#{key_pattern})
|
|
59
|
+
\s*=\s*
|
|
60
|
+
(?<value>#{value_pattern})
|
|
61
|
+
(?:\s*(?<inline_comment>#{comment_character}.*))?\z/x
|
|
62
|
+
ini_file = IniFile.new
|
|
63
|
+
current_section = Section.new(nil)
|
|
64
|
+
current_key_value_pair = KeyValuePair.new
|
|
65
|
+
File.open(filename, 'r') do |f|
|
|
66
|
+
f.each_line(chomp: true) do |line|
|
|
67
|
+
line.strip!
|
|
68
|
+
if line.start_with?(comment_character)
|
|
69
|
+
current_key_value_pair.prepended_comments << line
|
|
70
|
+
elsif (match = line.match(/\A\[(?<section_name>[^\]]+)\]\z/))
|
|
71
|
+
current_section.key_value_pairs << current_key_value_pair
|
|
72
|
+
ini_file.sections << current_section
|
|
73
|
+
current_section = Section.new(match['section_name'])
|
|
74
|
+
current_key_value_pair = KeyValuePair.new
|
|
75
|
+
elsif (match = line.match(line_pattern))
|
|
76
|
+
current_key_value_pair.key = match['key']
|
|
77
|
+
current_key_value_pair.value = match['value']
|
|
78
|
+
current_key_value_pair.inline_comment = match['inline_comment']
|
|
79
|
+
current_section.key_value_pairs << current_key_value_pair
|
|
80
|
+
current_key_value_pair = KeyValuePair.new
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
current_section.key_value_pairs << current_key_value_pair
|
|
85
|
+
ini_file.sections << current_section
|
|
86
|
+
|
|
87
|
+
ini_file
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dotini
|
|
4
|
+
# Key/value pair, with optional prepended and inline comments
|
|
5
|
+
class KeyValuePair
|
|
6
|
+
attr_accessor :key, :value, :prepended_comments, :inline_comment
|
|
7
|
+
|
|
8
|
+
# Creates a new, undefined key/value pair with no comments
|
|
9
|
+
def initialize
|
|
10
|
+
@key = nil
|
|
11
|
+
@value = nil
|
|
12
|
+
@prepended_comments = []
|
|
13
|
+
@inline_comment = nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Represents the key/value pair as a string
|
|
17
|
+
def to_s
|
|
18
|
+
buffer = StringIO.new
|
|
19
|
+
prepended_comments.each do |line|
|
|
20
|
+
buffer << line << "\n"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
unless key.nil?
|
|
24
|
+
buffer << "#{key} = #{value}"
|
|
25
|
+
buffer <<
|
|
26
|
+
if inline_comment.nil?
|
|
27
|
+
"\n"
|
|
28
|
+
else
|
|
29
|
+
" #{inline_comment}\n"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
buffer.string
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dotini
|
|
4
|
+
# A single INI file section
|
|
5
|
+
class Section
|
|
6
|
+
attr_accessor :name, :key_value_pairs
|
|
7
|
+
|
|
8
|
+
# Creates a new, empty section
|
|
9
|
+
def initialize(name)
|
|
10
|
+
@name = name
|
|
11
|
+
@key_value_pairs = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Retrieves a KeyValuePair from the section, or creates one
|
|
15
|
+
def [](key)
|
|
16
|
+
key_value_pairs.find { |key_pair| key_pair.key == key } ||
|
|
17
|
+
KeyValuePair.new.tap do |pair|
|
|
18
|
+
pair.key = key
|
|
19
|
+
key_value_pairs << pair
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Sets a value for a key in this section
|
|
24
|
+
def []=(key, value)
|
|
25
|
+
self[key].value = value
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Represents the section as a hash
|
|
29
|
+
def to_h
|
|
30
|
+
{}.tap do |hash|
|
|
31
|
+
key_value_pairs.each do |pair|
|
|
32
|
+
next if pair.key.nil?
|
|
33
|
+
|
|
34
|
+
hash[pair.key] = pair.value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Represents the section as a string
|
|
40
|
+
def to_s
|
|
41
|
+
buffer = StringIO.new
|
|
42
|
+
buffer << "[#{name}]\n" unless name.nil?
|
|
43
|
+
key_value_pairs.each do |pair|
|
|
44
|
+
buffer << pair.to_s
|
|
45
|
+
end
|
|
46
|
+
buffer.string
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dotini
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wendel Scardua
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-05-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: pry
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.14'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.14'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rubocop
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.15'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.15'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop-performance
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.11'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.11'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rubocop-rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0.5'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0.5'
|
|
69
|
+
description: Parser and generator of INI files
|
|
70
|
+
email:
|
|
71
|
+
- wendelscardua@gmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".github/workflows/test.yml"
|
|
77
|
+
- ".gitignore"
|
|
78
|
+
- ".rspec"
|
|
79
|
+
- ".rubocop.yml"
|
|
80
|
+
- CODE_OF_CONDUCT.md
|
|
81
|
+
- Gemfile
|
|
82
|
+
- Gemfile.lock
|
|
83
|
+
- LICENSE.txt
|
|
84
|
+
- README.md
|
|
85
|
+
- Rakefile
|
|
86
|
+
- bin/console
|
|
87
|
+
- bin/setup
|
|
88
|
+
- dotini.gemspec
|
|
89
|
+
- lib/dotini.rb
|
|
90
|
+
- lib/dotini/ini_file.rb
|
|
91
|
+
- lib/dotini/key_value_pair.rb
|
|
92
|
+
- lib/dotini/section.rb
|
|
93
|
+
- lib/dotini/version.rb
|
|
94
|
+
homepage: https://github.com/wendelscardua/dotini
|
|
95
|
+
licenses:
|
|
96
|
+
- MIT
|
|
97
|
+
metadata:
|
|
98
|
+
homepage_uri: https://github.com/wendelscardua/dotini
|
|
99
|
+
source_code_uri: https://github.com/wendelscardua/dotini
|
|
100
|
+
changelog_uri: https://github.com/wendelscardua/dotini
|
|
101
|
+
post_install_message:
|
|
102
|
+
rdoc_options: []
|
|
103
|
+
require_paths:
|
|
104
|
+
- lib
|
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: 2.6.0
|
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
|
+
requirements:
|
|
112
|
+
- - ">="
|
|
113
|
+
- !ruby/object:Gem::Version
|
|
114
|
+
version: '0'
|
|
115
|
+
requirements: []
|
|
116
|
+
rubygems_version: 3.1.2
|
|
117
|
+
signing_key:
|
|
118
|
+
specification_version: 4
|
|
119
|
+
summary: Read and write INI files
|
|
120
|
+
test_files: []
|