autoini 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd90d5011121b5b00414dc5da6ab3deaf07eeb8e
4
+ data.tar.gz: d4cc5d55c16b7ede2eda4d5f5dc857538479d1da
5
+ SHA512:
6
+ metadata.gz: 7665669b4bdeea08c253c598c459705fdb51ee6ac977938d8941c2d25cdddcf49461f96a82ad73df9348a0155fdb36f4af514dda526ff2a852b462ab0c971810
7
+ data.tar.gz: 5cbcb038d714bc5b182bf593a20e6de9405f407c6f52972a55e245b3e32e197ece6db096ed7e72530cb2dc8064a7038b48d5082625564a7ebda0032e6bc18cdc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at sam.boylett@meritec.co.uk. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autoini.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sam Boylett
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,89 @@
1
+ # Autoini
2
+
3
+ Autoini is a library for writing and parsing INI files. It supports key/value pairs, sections, blank lines and comments
4
+
5
+ It will escape control characters ([]=;#\r\n), so any data can be stored
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'autoini'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install autoini
22
+
23
+ ## Usage
24
+
25
+ To parse INI data:
26
+ ```ruby
27
+ ini = Autoini::Contents.parse(File.open('data.ini', 'rb', &:read))
28
+ ```
29
+
30
+ To generate INI data:
31
+ ```ruby
32
+ Autoini::Contents.new(
33
+ Autoini::Comment.new('some comment'),
34
+ Autoini::Pair.new('foo', "bar\nnew"),
35
+ Autoini::Pair.new('foo2', 'bar').tap{ |p| p.comment = 'a comment' },
36
+ Autoini::Section.new(
37
+ 'hello',
38
+ Autoini::Pair.new('[foo', 'bar'),
39
+ Autoini::Pair.new('fo;o2', 'bar').tap{ |p| p.comment = 'a comment' },
40
+ Autoini::BlankLine.new
41
+ ),
42
+ Autoini::Section.new(
43
+ 'world',
44
+ Autoini::Pair.new('foo', 'bar'),
45
+ Autoini::Pair.new('foo2', 'bar').tap{ |p| p.comment = 'a comment' },
46
+ ).tap{ |p| p.comment = 'cool section' },
47
+ ).to_s
48
+ ```
49
+ which will output:
50
+ ```
51
+ # some comment
52
+ foo=bar\nnew
53
+ foo2=bar # a comment
54
+ [hello]
55
+ \[foo=bar
56
+ fo\;o2=bar # a comment
57
+
58
+ [world] # cool section
59
+ foo=bar
60
+ foo2=bar # a comment
61
+ ```
62
+
63
+ ## Config
64
+
65
+ Autoini will always parse lines in a string by \n, but you can choose the new line string using:
66
+ ```ruby
67
+ Autoini.newline = "\r\n"
68
+ ```
69
+
70
+ Autoini will parse comments as begining with a ; or a #. You can choose which it uses to write comments by using:
71
+ ```ruby
72
+ Autoini.comment = ";"
73
+ ```
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ 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).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/automeow/autoini. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
84
+
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
89
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/autoini.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autoini/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autoini"
8
+ spec.version = Autoini::VERSION
9
+ spec.authors = ["Sam Boylett"]
10
+ spec.email = ["sam.boylett@alumni.york.ac.uk"]
11
+
12
+ spec.summary = %q{Parser / writer for INI files}
13
+ spec.homepage = "https://github.com/automeow/autoini"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "autoini"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/autoini.rb ADDED
@@ -0,0 +1,72 @@
1
+ require "autoini/element"
2
+ require "autoini/abstract_line"
3
+ require "autoini/comment"
4
+ require "autoini/inline_comment"
5
+ require "autoini/pair"
6
+ require "autoini/section"
7
+ require "autoini/blank_line"
8
+ require "autoini/contents"
9
+ require "autoini/version"
10
+
11
+ module Autoini
12
+ ESCAPE_CHAR = '\\'
13
+ COMMENTS = %w( # ; )
14
+ SPECIAL = %w( = [ ] ) | [ESCAPE_CHAR] | COMMENTS
15
+ MAP_CHARS = { "\r" => '\\r', "\n" => '\\n' }
16
+
17
+ @newline = "\n"
18
+ @comment = '#'
19
+
20
+ class << self
21
+ attr_accessor :newline, :comment
22
+
23
+ def escape(text)
24
+ ''.tap do |b|
25
+ text.each_char do |c|
26
+ b << MAP_CHARS[c] and next if MAP_CHARS[c]
27
+ b << '\\' if SPECIAL.include?(c)
28
+ b << c
29
+ end
30
+ end
31
+ end
32
+
33
+ def unescape_char(char)
34
+ return char if SPECIAL.include?(char)
35
+ MAP_CHARS.key("\\#{char}") ||
36
+ raise(ArgumentError, "#{char.inspect} is not an unescapable character")
37
+ end
38
+
39
+ def divide(text)
40
+ [].tap do |s|
41
+ buffer = ''
42
+ escaping = false
43
+ text.each_char do |c|
44
+ if escaping
45
+ buffer << unescape_char(c)
46
+ escaping = false
47
+ elsif c == ESCAPE_CHAR
48
+ escaping = true
49
+ elsif SPECIAL.include?(c)
50
+ s << buffer.strip unless buffer.strip.empty?
51
+ s << c
52
+ buffer = ''
53
+ else
54
+ buffer << c
55
+ end
56
+ end
57
+ s << buffer.strip unless buffer.strip.empty?
58
+ end
59
+ end
60
+
61
+ def wrap(array)
62
+ case array
63
+ when nil
64
+ []
65
+ when Array
66
+ array
67
+ else
68
+ [array]
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,11 @@
1
+ module Autoini
2
+ class AbstractLine < Element
3
+ def to_s
4
+ raise ArgumentError, "to_s must be overriden in the subclass"
5
+ end
6
+
7
+ def self.parse(line)
8
+ raise ArgumentError, "parse must be overriden in the subclass"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module Autoini
2
+ class BlankLine < AbstractLine
3
+ def to_s
4
+ ""
5
+ end
6
+
7
+ def ==(e)
8
+ e.is_a?(BlankLine)
9
+ end
10
+
11
+ def self.parse_with_comment(line)
12
+ parse(line)
13
+ end
14
+
15
+ def self.parse(line)
16
+ BlankLine.new if line.empty?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module Autoini
2
+ class Comment < AbstractLine
3
+ attr_accessor :comment
4
+
5
+ def initialize(comment)
6
+ @comment = comment
7
+ end
8
+
9
+ def to_s
10
+ "#{Autoini.comment} #{@comment}"
11
+ end
12
+
13
+ def ==(e)
14
+ e.is_a?(Comment) && comment == e.comment
15
+ end
16
+
17
+ def self.parse_with_comment(line)
18
+ parse(line)
19
+ end
20
+
21
+ def self.parse(line)
22
+ Comment.new(line[1..-1].join.strip) if line[0] == Autoini.comment
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,50 @@
1
+ module Autoini
2
+ class Contents
3
+ KLASSES = [Pair, Section, Comment, BlankLine]
4
+
5
+ attr_reader :lines
6
+
7
+ def self.parse(contents)
8
+ elements = []
9
+ section = nil
10
+ contents.split("\n").each do |l|
11
+ e = KLASSES.map{ |k| k.parse_with_comment(Autoini.divide(l.strip)) }
12
+ .select(&:itself)
13
+ .first || raise(ArgumentError, "couldn't parse line: #{l.inspect}")
14
+ if e.is_a?(Section)
15
+ section = e
16
+ elements << section
17
+ else
18
+ (section || elements) << e
19
+ end
20
+ end
21
+ new(*elements)
22
+ end
23
+
24
+ def initialize(*contents)
25
+ @lines = []
26
+ self << contents
27
+ end
28
+
29
+ def <<(contents)
30
+ Autoini.wrap(contents).each do |c|
31
+ unless c.is_a?(Element)
32
+ raise ArgumentError, "#{c.class.name} must extend Autoini::Element"
33
+ end
34
+ if !c.is_a?(Section) && lines.last.is_a?(Section)
35
+ raise ArgumentError, "Error on line #{c.inspect}: all elements after a section must be in a section"
36
+ end
37
+ lines << c
38
+ end
39
+ end
40
+
41
+ def to_s
42
+ lines.map(&:to_s).join(Autoini.newline)
43
+ end
44
+
45
+ def ==(c)
46
+ c.is_a?(Contents) && c.lines.length == lines.length &&
47
+ lines.map.with_index{ |l, i| c.lines[i] == l }.all?
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module Autoini
2
+ class Element
3
+ def ==(element)
4
+ raise ArgumentError, "== must be overriden in the subclass"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ module Autoini
2
+ module InlineComment
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ attr_accessor :comment
8
+
9
+ def line_comment(text)
10
+ return text unless comment
11
+ "#{text} #{Comment.new(comment).to_s}"
12
+ end
13
+
14
+ module ClassMethods
15
+ def parse_with_comment(line)
16
+ comment_index = COMMENTS.map{ |c| line.index(c) }.reject(&:nil?).min
17
+ return parse(line) unless comment_index
18
+ parse(line[0..(comment_index - 1)]).tap do |e|
19
+ e.comment = line[(comment_index + 1)..-1].join if e
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Autoini
2
+ class Pair < AbstractLine
3
+ include InlineComment
4
+
5
+ attr_accessor :key, :value
6
+
7
+ def initialize(key, value)
8
+ @key = key
9
+ @value = value
10
+ end
11
+
12
+ def to_s
13
+ line_comment("#{Autoini.escape(key)}=#{Autoini.escape(value)}")
14
+ end
15
+
16
+ def ==(e)
17
+ e.is_a?(Pair) && e.key == key && e.value == value && e.comment == comment
18
+ end
19
+
20
+ def self.parse(line)
21
+ Pair.new(line[0], line[2]) if line.length == 3 && line[1] == '='
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,37 @@
1
+ module Autoini
2
+ class Section < Element
3
+ include InlineComment
4
+
5
+ attr_accessor :title
6
+ attr_reader :lines
7
+
8
+ def initialize(title, *contents)
9
+ @title = title
10
+ @lines = []
11
+ self << contents
12
+ end
13
+
14
+ def <<(contents)
15
+ Autoini.wrap(contents).each do |c|
16
+ unless c.is_a?(AbstractLine)
17
+ raise ArgumentError, "#{c.class.name} must extend Autoini::AbstractLine"
18
+ end
19
+ @lines << c
20
+ end
21
+ end
22
+
23
+ def to_s
24
+ [line_comment("[#{title}]"), lines.map(&:to_s)].flatten.join(Autoini.newline)
25
+ end
26
+
27
+ def ==(e)
28
+ e.is_a?(Section) && e.title == title && e.comment == comment &&
29
+ e.lines.length == lines.length &&
30
+ lines.map.with_index{ |l, i| e.lines[i] == l }.all?
31
+ end
32
+
33
+ def self.parse(line)
34
+ Section.new(line[1]) if line.length == 3 && line[0] == '[' && line[2] == ']'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Autoini
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoini
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Boylett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-08 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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - sam.boylett@alumni.york.ac.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - autoini.gemspec
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/autoini.rb
73
+ - lib/autoini/abstract_line.rb
74
+ - lib/autoini/blank_line.rb
75
+ - lib/autoini/comment.rb
76
+ - lib/autoini/contents.rb
77
+ - lib/autoini/element.rb
78
+ - lib/autoini/inline_comment.rb
79
+ - lib/autoini/pair.rb
80
+ - lib/autoini/section.rb
81
+ - lib/autoini/version.rb
82
+ homepage: https://github.com/automeow/autoini
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Parser / writer for INI files
106
+ test_files: []