figtree 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +19 -6
- data/lib/figtree.rb +0 -36
- data/lib/figtree/ini_config.rb +39 -0
- data/lib/figtree/transformer.rb +1 -1
- data/lib/figtree/version.rb +1 -1
- data/spec/figtree/transformer_spec.rb +1 -1
- data/spec/figtree_spec.rb +6 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 011a21ea2c8dfbe34b870ef195349235aa1effa3
|
4
|
+
data.tar.gz: 0835b757e33b0b33b30640fe95de3deaedae8f9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38e21385476ad11e06a20553893c11271086056959d4613022b123bc64ecdf44653f79824ff611305c89655a75e3cbf1d1ac83b3017fb962823db7c9ded00a80
|
7
|
+
data.tar.gz: 86199767fdb013c40f4e9c769e394e76c98234b5e32fec12800507fcbee22ab6fd2e0e093bdfdb774ea78866274140fc31ea556021c2ba29118e7f2122c64e89
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,18 +1,32 @@
|
|
1
|
-
|
1
|
+
%%%,%%%%%%%
|
2
|
+
,'%% \\-*%%%%%%%
|
3
|
+
;%%%%%*% _%%%%"
|
4
|
+
,%%% \(_.*%%%%.
|
5
|
+
% *%%, ,%%%%*( '
|
6
|
+
%^ ,*%%% )\|,%%*%,_
|
7
|
+
*% \/ #).-"*%%*
|
8
|
+
_.) ,/ *%,
|
9
|
+
_________/)#(_____________
|
10
|
+
#### art by [b'ger](http://ascii.co.uk/art/tree)
|
11
|
+
|
12
|
+
# Figtree
|
2
13
|
## about
|
3
14
|
A parser and transformer for loading `.ini` files into Ruby dot notation accessible objects. `.ini` is not a standardized format. But the parser and transformer are easy to extend, unlike regex. :) And it's at 100% LOC coverage.
|
4
15
|
|
5
16
|
If the `.ini` file is invalid, an error will be raised, with the line and char position of the error. If you extend this gem to have more rules, and one of those rules fails to transform, you will have an error raised.
|
6
17
|
|
18
|
+
## disambiguation
|
19
|
+
Looking for the graphical viewer of phyllogenic trees? You want this other [Figtree](http://tree.bio.ed.ac.uk/software/figtree/).
|
20
|
+
|
7
21
|
## performance
|
8
|
-
A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed, and loaded.
|
22
|
+
A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed, and loaded. Currently, the whole `.ini` file is read into memory at once. The assumption being these files should not typically be too big. But future minor versions might move to line by line ingestion.
|
9
23
|
|
10
24
|
## installation
|
11
25
|
`gem install figtree`
|
12
26
|
|
13
27
|
## usage
|
14
28
|
require 'figtree'
|
15
|
-
config = Figtree.
|
29
|
+
config = Figtree::IniConfig.new('spec/support/settings.conf')
|
16
30
|
config.common.basic_size_limit
|
17
31
|
=> 26214400
|
18
32
|
# also good
|
@@ -22,7 +36,7 @@ A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed,
|
|
22
36
|
config.common[:paid_users_size_limit]
|
23
37
|
=> 2147483648
|
24
38
|
# and overrides? we got overrides
|
25
|
-
overridden_config = Figtree.
|
39
|
+
overridden_config = Figtree::IniConfig.new('spec/support/settings.conf', :production)
|
26
40
|
config.ftp.path
|
27
41
|
=> "/tmp/"
|
28
42
|
overridden_config.ftp.path
|
@@ -36,7 +50,6 @@ A typical `.ini` file takes slightly less than 0.02s to be parsed, transformed,
|
|
36
50
|
`rspec spec/`
|
37
51
|
|
38
52
|
#### TODO
|
53
|
+
- move way from needing to parse whole file at once? (move to `IO.foreach` ?)
|
39
54
|
- give char/line position of transformer failures
|
40
|
-
- change method signature from Module.class_method to just IniConfig.new(IOObject) ?
|
41
|
-
- change override to be single symbol rather than array (do we ever need multiples?)
|
42
55
|
- refactor marked TODO listings in files (mostly refactoring to generic in Transformer)
|
data/lib/figtree.rb
CHANGED
@@ -1,39 +1,3 @@
|
|
1
1
|
require 'figtree/parser'
|
2
2
|
require 'figtree/transformer'
|
3
3
|
require 'figtree/ini_config'
|
4
|
-
|
5
|
-
module Figtree
|
6
|
-
def self.load_config(file_path, overrides=[])
|
7
|
-
parsed_subgroups = figgy_transform(
|
8
|
-
figgy_parse(
|
9
|
-
File.read(file_path)
|
10
|
-
),
|
11
|
-
overrides
|
12
|
-
)
|
13
|
-
IniConfig.new(
|
14
|
-
parsed_subgroups.reduce({}, :merge!)
|
15
|
-
)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
def self.figgy_parse(str)
|
20
|
-
Parser.new.parse(str)
|
21
|
-
rescue Parslet::ParseFailed => failure
|
22
|
-
STDERR.puts "\nInvalid ini file.\n" +
|
23
|
-
"Error: #{failure.cause.ascii_tree}" +
|
24
|
-
"Please correct the file and retry."
|
25
|
-
raise
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.figgy_transform(tree, overrides = [])
|
29
|
-
Transformer.new.apply(tree, overrides: overrides)
|
30
|
-
rescue => e
|
31
|
-
STDERR.puts "\nInvalid transformation rule.\n" +
|
32
|
-
"Error: #{e}" +
|
33
|
-
"Please correct your transformer rule and retry."
|
34
|
-
raise TransformFailed
|
35
|
-
end
|
36
|
-
|
37
|
-
class TransformFailed < Exception
|
38
|
-
end
|
39
|
-
end
|
data/lib/figtree/ini_config.rb
CHANGED
@@ -1,7 +1,46 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
module Figtree
|
3
3
|
class IniConfig < OpenStruct
|
4
|
+
def initialize(ini, override = :none)
|
5
|
+
# cheat to allow a parsed hash in
|
6
|
+
if ini.is_a?(Hash)
|
7
|
+
parsed_subgroups = ini
|
8
|
+
else
|
9
|
+
parsed_subgroups = figgy_transform(
|
10
|
+
figgy_parse(
|
11
|
+
File.read(ini)
|
12
|
+
),
|
13
|
+
override
|
14
|
+
).reduce({}, :merge!)
|
15
|
+
end
|
16
|
+
super(
|
17
|
+
parsed_subgroups
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def figgy_parse(str)
|
23
|
+
Parser.new.parse(str)
|
24
|
+
rescue Parslet::ParseFailed => failure
|
25
|
+
STDERR.puts "\nInvalid ini file.\n" +
|
26
|
+
"Error: #{failure.cause.ascii_tree}" +
|
27
|
+
"Please correct the file and retry."
|
28
|
+
raise
|
29
|
+
end
|
30
|
+
|
31
|
+
def figgy_transform(tree, override)
|
32
|
+
Transformer.new.apply(tree, override: override)
|
33
|
+
rescue => e
|
34
|
+
STDERR.puts "\nInvalid transformation rule.\n" +
|
35
|
+
"Error: #{e}" +
|
36
|
+
"Please correct your transformer rule and retry."
|
37
|
+
raise TransformFailed
|
38
|
+
end
|
4
39
|
end
|
40
|
+
|
5
41
|
class Subgroup < OpenStruct
|
6
42
|
end
|
43
|
+
|
44
|
+
class TransformFailed < Exception
|
45
|
+
end
|
7
46
|
end
|
data/lib/figtree/transformer.rb
CHANGED
@@ -53,7 +53,7 @@ module Figtree
|
|
53
53
|
:optional_key => subtree(:overriding_key),
|
54
54
|
:file_path => subtree(:new_file_path),
|
55
55
|
) do
|
56
|
-
if
|
56
|
+
if override.to_sym == overriding_key[:snake_case_key].to_sym
|
57
57
|
{
|
58
58
|
overridden_key[:snake_case_key] => String(new_file_path)
|
59
59
|
}
|
data/lib/figtree/version.rb
CHANGED
@@ -45,7 +45,7 @@ module Figtree
|
|
45
45
|
Parser.new.parse("[http]\npath = /srv/\npath<production> = /srv/var/tmp/\n")
|
46
46
|
end
|
47
47
|
it 'can apply an override' do
|
48
|
-
expect(Transformer.new.apply(override_tree,
|
48
|
+
expect(Transformer.new.apply(override_tree, override: :production)).to eq(
|
49
49
|
[
|
50
50
|
{
|
51
51
|
http: Subgroup.new(path: '/srv/var/tmp/')
|
data/spec/figtree_spec.rb
CHANGED
@@ -59,21 +59,21 @@ describe Figtree do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'can parse a group and provide dot notation access' do
|
62
|
-
expect(Figtree.
|
62
|
+
expect(Figtree::IniConfig.new(settings_path).common).to eq(common)
|
63
63
|
end
|
64
64
|
it 'can parse the overrides correctly' do
|
65
|
-
expect(Figtree.
|
65
|
+
expect(Figtree::IniConfig.new(settings_path, :itscript).common).
|
66
66
|
to eq(common_with_override)
|
67
67
|
end
|
68
68
|
it 'can parse the whole Kebab without any misunderstandings' do
|
69
|
-
expect(Figtree.
|
69
|
+
expect(Figtree::IniConfig.new(settings_path)).to eq(the_whole_kebab)
|
70
70
|
end
|
71
71
|
|
72
72
|
context "performance" do
|
73
73
|
it 'can parse the whole ini file quickly' do
|
74
74
|
expect(
|
75
75
|
Benchmark.realtime do
|
76
|
-
Figtree.
|
76
|
+
Figtree::IniConfig.new(settings_path)
|
77
77
|
end
|
78
78
|
).to be < 0.014
|
79
79
|
end
|
@@ -83,13 +83,13 @@ describe Figtree do
|
|
83
83
|
let(:unparseable_config) { 'spec/support/unparseable_settings.conf' }
|
84
84
|
let(:untransformable_config) { 'spec/support/untransformable_settings.conf' }
|
85
85
|
it 'throws ParseFailed if unparseable' do
|
86
|
-
expect { Figtree.
|
86
|
+
expect { Figtree::IniConfig.new(unparseable_config) }.
|
87
87
|
to raise_error(Parslet::ParseFailed)
|
88
88
|
end
|
89
89
|
it 'throws TransformFailed if untransformable' do
|
90
90
|
allow_any_instance_of(String).to receive(:to_b).
|
91
91
|
and_raise(StandardError)
|
92
|
-
expect { Figtree.
|
92
|
+
expect { Figtree::IniConfig.new(untransformable_config) }.
|
93
93
|
to raise_error(Figtree::TransformFailed)
|
94
94
|
end
|
95
95
|
end
|