yamlook 0.3.5 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df1211ccca0c68e598391b78bd6328f09a1f67eb0948a926c5263bfa7206934a
4
- data.tar.gz: 496953bbe6969dc05ff4165b2ab4ac8a76eb9df68be265b126c2b6eacb7061f8
3
+ metadata.gz: 4ff790519841c559a55d2502b075037d0c6f8e9c7b204ddccc24a7940bd9c64c
4
+ data.tar.gz: c3de76314369e83a5bce053c194108987fbe3cb09884325552b3cdfcecec932a
5
5
  SHA512:
6
- metadata.gz: bdf2d00d030814fc65940d1ad4d3bed49dbad70a022945d8b2534f303316646a289670d1e99cf40a8f993870f96cfbd35f942c2b9cb105acccdd6c522272302c
7
- data.tar.gz: 7c68b6c0b71d2975014197ad68cde9f3e503213d5124ed993df1743530650e15e41ff450fd8b02d981ffde3a168fc567b31daf097823b786e7490fcd9596d06a
6
+ metadata.gz: c92be1c326364a5e48bfe927738e6fab7c1ebc7b1621f7a29388eb9addf9b8de75fe55c5a234f9a280d61b640f66890ffd11784d67ab6c00e20b4d38b82d266c
7
+ data.tar.gz: b7efd21a23b6bee5624b07a7eb4ca69c6f3096e8f5b0c626f8a962c6be81b48cc6910a105d4e956b58aa102e2618bae58f54e66a719d6fac0fae8562eacc6678
data/bin/yamlook CHANGED
@@ -7,4 +7,4 @@ require 'yamlook'
7
7
 
8
8
  cli_parser = Yamlook::Cli.new(ARGV)
9
9
 
10
- Yamlook::Search.perform(cli_parser.argument.split('.'))
10
+ Yamlook::Search.perform(cli_parser.argument.split(Yamlook::SEPARATOR))
data/lib/yamlook/file.rb CHANGED
@@ -10,32 +10,17 @@ module Yamlook
10
10
  end
11
11
 
12
12
  def search(keys)
13
- return unless yaml
13
+ file = ::File.read(filename)
14
+ handler = Handler.new(keys: keys, locales: LOCALES)
15
+ parser = ::Psych::Parser.new(handler)
14
16
 
15
- findings = Locale.key_combinations(keys).flat_map do |key_combination|
16
- root_node_list.search(key_combination).flatten.compact.map do |finding|
17
- finding.filename = filename
18
- finding
19
- end
17
+ findings = parser.parse(file).handler.found.map do |value, line, column|
18
+ "#{filename}:#{line}:#{column}\n#{value}"
20
19
  end
21
20
 
22
21
  findings if findings.any?
23
- end
24
-
25
- def yaml
26
- @yaml ||= parse_file(filename)
27
- end
28
-
29
- private
30
-
31
- def parse_file(filename)
32
- ::Psych.parse_file(filename)
33
22
  rescue ::Psych::Exception
34
23
  nil
35
24
  end
36
-
37
- def root_node_list
38
- NodeList.new(yaml.root.children)
39
- end
40
25
  end
41
26
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'psych'
4
+
5
+ module Yamlook
6
+ # Handler for Psych::Parser
7
+ class Handler < ::Psych::Handler
8
+ attr_reader :found
9
+
10
+ def initialize(keys:, locales: [])
11
+ super()
12
+ @keys = keys
13
+ @locales = locales
14
+ @found = []
15
+
16
+ @iterations = []
17
+ @current_iteration = Iteration.new(active: true)
18
+ end
19
+
20
+ def event_location(start_line, start_column, _end_line, _end_column)
21
+ @start_line = start_line
22
+ @start_column = start_column
23
+ end
24
+
25
+ def start_mapping(_anchor, _tag, _implicit, _style)
26
+ @iterations.push(@current_iteration.dup)
27
+ @current_iteration.reset!
28
+ end
29
+
30
+ def end_mapping
31
+ @iterations.pop
32
+ @current_iteration.reset!
33
+ end
34
+
35
+ def scalar(value, _anchor, _tag, _plain, _quoted, _style) # rubocop:disable Metrics/ParameterLists
36
+ @found << [value, @start_line.next, @start_column.next] if keys_out? && all_active?
37
+
38
+ refresh_current_interation!(value)
39
+ end
40
+
41
+ def refresh_current_interation!(value)
42
+ value_keys = value.split(SEPARATOR)
43
+
44
+ value_keys.shift if current_offset.zero? && LOCALES.include?(value_keys.first)
45
+
46
+ @current_iteration.offset = value_keys.count
47
+ @current_iteration.active = current_keys == value_keys
48
+ end
49
+
50
+ def current_offset
51
+ @iterations.sum(&:offset)
52
+ end
53
+
54
+ def current_keys
55
+ @keys.drop(current_offset).take(@current_iteration.offset)
56
+ end
57
+
58
+ def keys_out?
59
+ current_offset + @current_iteration.offset == @keys.size
60
+ end
61
+
62
+ def all_active?
63
+ @iterations.any? && @iterations.all?(&:active) && @current_iteration.active
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ # Holds information for iteration over Psych::Handler iterating through mappings
5
+ class Iteration
6
+ attr_accessor :active, :offset
7
+
8
+ def initialize(active: false, offset: 0)
9
+ @active = active
10
+ @offset = offset
11
+ end
12
+
13
+ def reset!
14
+ @active = false
15
+ @offset = 0
16
+ end
17
+ end
18
+ end
@@ -12,9 +12,9 @@ module Yamlook
12
12
  module_function
13
13
 
14
14
  def perform(keys)
15
- raise NoArgumentsError, 'Nothing to seach for.' if keys.empty?
15
+ raise NoArgumentsError, "Nothing to search for.\n" if keys.empty?
16
16
 
17
- findings = Dir.glob(PATTERN).map do |filename|
17
+ findings = Dir.glob(PATTERN).flat_map do |filename|
18
18
  result = File.new(filename).search(keys)
19
19
  print_progress(result)
20
20
  result
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yamlook
4
- VERSION = '0.3.5'
4
+ VERSION = '0.4.1'
5
5
  end
data/lib/yamlook.rb CHANGED
@@ -2,9 +2,14 @@
2
2
 
3
3
  require 'psych'
4
4
 
5
- require 'yamlook/node'
6
- require 'yamlook/node_list'
7
- require 'yamlook/locale'
5
+ require 'yamlook/iteration'
6
+ require 'yamlook/handler'
8
7
  require 'yamlook/file'
9
8
  require 'yamlook/search'
10
9
  require 'yamlook/cli'
10
+
11
+ module Yamlook
12
+ SEPARATOR = '.'
13
+ LOCALES_FILEPATH = ::File.join(::File.expand_path('..', __dir__), 'locales.yaml')
14
+ LOCALES = Psych.load_file(LOCALES_FILEPATH)['locales'].freeze
15
+ end
data/locales.yaml CHANGED
@@ -1,176 +1,176 @@
1
1
  locales:
2
- - af
3
- - sq
4
- - am
5
- - ar
6
- - ar-dz
7
- - ar-bh
8
- - ar-eg
9
- - ar-iq
10
- - ar-jo
11
- - ar-kw
12
- - ar-lb
13
- - ar-ly
14
- - ar-ma
15
- - ar-om
16
- - ar-qa
17
- - ar-sa
18
- - ar-sy
19
- - ar-tn
20
- - ar-ae
21
- - ar-ye
22
- - hy
23
- - as
24
- - az
25
- - az-az
26
- - eu
27
- - be
28
- - bn
29
- - bs
30
- - bg
31
- - my
32
- - ca
33
- - zh
34
- - zh-cn
35
- - zh-hk
36
- - zh-mo
37
- - zh-sg
38
- - zh-tw
39
- - hr
40
- - cs
41
- - da
42
- - dv
43
- - nl
44
- - nl-be
45
- - nl-nl
46
- - en
47
- - en-au
48
- - en-bz
49
- - en-ca
50
- - en-cb
51
- - en-gb
52
- - en-in
53
- - en-ie
54
- - en-jm
55
- - en-nz
56
- - en-ph
57
- - en-za
58
- - en-tt
59
- - en-us
60
- - et
61
- - mk
62
- - fo
63
- - fa
64
- - fi
65
- - fr
66
- - fr-be
67
- - fr-ca
68
- - fr-fr
69
- - fr-lu
70
- - fr-ch
71
- - gd
72
- - gd-ie
73
- - gl
74
- - ka
75
- - de
76
- - de-at
77
- - de-de
78
- - de-li
79
- - de-lu
80
- - de-ch
81
- - el
82
- - gn
83
- - gu
84
- - he
85
- - hi
86
- - hu
87
- - is
88
- - id
89
- - it
90
- - it-it
91
- - it-ch
92
- - ja
93
- - kn
94
- - ks
95
- - kk
96
- - km
97
- - ko
98
- - lo
99
- - la
100
- - lv
101
- - lt
102
- - ms
103
- - ms-bn
104
- - ms-my
105
- - ml
106
- - mt
107
- - mi
108
- - mr
109
- - mn
110
- - ne
111
- - nb
112
- - no-no
113
- - nn
114
- - or
115
- - pl
116
- - pt
117
- - pt-br
118
- - pt-pt
119
- - pa
120
- - rm
121
- - ro
122
- - ro-mo
123
- - ru
124
- - ru-mo
125
- - sa
126
- - sr
127
- - sr-sp
128
- - tn
129
- - sd
130
- - si
131
- - sk
132
- - sl
133
- - so
134
- - sb
135
- - es
136
- - es-ar
137
- - es-bo
138
- - es-cl
139
- - es-co
140
- - es-cr
141
- - es-do
142
- - es-ec
143
- - es-sv
144
- - es-gt
145
- - es-hn
146
- - es-mx
147
- - es-ni
148
- - es-pa
149
- - es-py
150
- - es-pe
151
- - es-pr
152
- - es-es
153
- - es-uy
154
- - es-ve
155
- - sw
156
- - sv
157
- - sv-fi
158
- - sv-se
159
- - tg
160
- - ta
161
- - tt
162
- - te
163
- - th
164
- - bo
165
- - ts
166
- - tr
167
- - tk
168
- - uk
169
- - ur
170
- - uz
171
- - uz-uz
172
- - vi
173
- - cy
174
- - xh
175
- - yi
176
- - zu
2
+ - af
3
+ - sq
4
+ - am
5
+ - ar
6
+ - ar-dz
7
+ - ar-bh
8
+ - ar-eg
9
+ - ar-iq
10
+ - ar-jo
11
+ - ar-kw
12
+ - ar-lb
13
+ - ar-ly
14
+ - ar-ma
15
+ - ar-om
16
+ - ar-qa
17
+ - ar-sa
18
+ - ar-sy
19
+ - ar-tn
20
+ - ar-ae
21
+ - ar-ye
22
+ - hy
23
+ - as
24
+ - az
25
+ - az-az
26
+ - eu
27
+ - be
28
+ - bn
29
+ - bs
30
+ - bg
31
+ - my
32
+ - ca
33
+ - zh
34
+ - zh-cn
35
+ - zh-hk
36
+ - zh-mo
37
+ - zh-sg
38
+ - zh-tw
39
+ - hr
40
+ - cs
41
+ - da
42
+ - dv
43
+ - nl
44
+ - nl-be
45
+ - nl-nl
46
+ - en
47
+ - en-au
48
+ - en-bz
49
+ - en-ca
50
+ - en-cb
51
+ - en-gb
52
+ - en-in
53
+ - en-ie
54
+ - en-jm
55
+ - en-nz
56
+ - en-ph
57
+ - en-za
58
+ - en-tt
59
+ - en-us
60
+ - et
61
+ - mk
62
+ - fo
63
+ - fa
64
+ - fi
65
+ - fr
66
+ - fr-be
67
+ - fr-ca
68
+ - fr-fr
69
+ - fr-lu
70
+ - fr-ch
71
+ - gd
72
+ - gd-ie
73
+ - gl
74
+ - ka
75
+ - de
76
+ - de-at
77
+ - de-de
78
+ - de-li
79
+ - de-lu
80
+ - de-ch
81
+ - el
82
+ - gn
83
+ - gu
84
+ - he
85
+ - hi
86
+ - hu
87
+ - is
88
+ - id
89
+ - it
90
+ - it-it
91
+ - it-ch
92
+ - ja
93
+ - kn
94
+ - ks
95
+ - kk
96
+ - km
97
+ - ko
98
+ - lo
99
+ - la
100
+ - lv
101
+ - lt
102
+ - ms
103
+ - ms-bn
104
+ - ms-my
105
+ - ml
106
+ - mt
107
+ - mi
108
+ - mr
109
+ - mn
110
+ - ne
111
+ - nb
112
+ - no-no
113
+ - nn
114
+ - or
115
+ - pl
116
+ - pt
117
+ - pt-br
118
+ - pt-pt
119
+ - pa
120
+ - rm
121
+ - ro
122
+ - ro-mo
123
+ - ru
124
+ - ru-mo
125
+ - sa
126
+ - sr
127
+ - sr-sp
128
+ - tn
129
+ - sd
130
+ - si
131
+ - sk
132
+ - sl
133
+ - so
134
+ - sb
135
+ - es
136
+ - es-ar
137
+ - es-bo
138
+ - es-cl
139
+ - es-co
140
+ - es-cr
141
+ - es-do
142
+ - es-ec
143
+ - es-sv
144
+ - es-gt
145
+ - es-hn
146
+ - es-mx
147
+ - es-ni
148
+ - es-pa
149
+ - es-py
150
+ - es-pe
151
+ - es-pr
152
+ - es-es
153
+ - es-uy
154
+ - es-ve
155
+ - sw
156
+ - sv
157
+ - sv-fi
158
+ - sv-se
159
+ - tg
160
+ - ta
161
+ - tt
162
+ - te
163
+ - th
164
+ - bo
165
+ - ts
166
+ - tr
167
+ - tk
168
+ - uk
169
+ - ur
170
+ - uz
171
+ - uz-uz
172
+ - vi
173
+ - cy
174
+ - xh
175
+ - yi
176
+ - zu
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yamlook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viacheslav Mefodin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-15 00:00:00.000000000 Z
11
+ date: 2021-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psych
@@ -74,9 +74,8 @@ files:
74
74
  - lib/yamlook.rb
75
75
  - lib/yamlook/cli.rb
76
76
  - lib/yamlook/file.rb
77
- - lib/yamlook/locale.rb
78
- - lib/yamlook/node.rb
79
- - lib/yamlook/node_list.rb
77
+ - lib/yamlook/handler.rb
78
+ - lib/yamlook/iteration.rb
80
79
  - lib/yamlook/search.rb
81
80
  - lib/yamlook/version.rb
82
81
  - locales.yaml
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Yamlook
4
- # Possible locale keys handling
5
- module Locale
6
- LOCALES_FILEPATH = File.join(File.expand_path('../..', __dir__), 'locales.yaml')
7
- LOCALES = Psych.load_file(LOCALES_FILEPATH)['locales'].freeze
8
-
9
- module_function
10
-
11
- def key_combinations(keys)
12
- localized_keys = LOCALES.map { |locale| [locale, *keys] }
13
-
14
- [keys, *localized_keys]
15
- end
16
- end
17
- end
data/lib/yamlook/node.rb DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Yamlook
4
- # Result of search - scalar node
5
- class Node
6
- attr_reader :value, :line, :column
7
- attr_accessor :filename
8
-
9
- def self.from_scalar(scalar)
10
- return unless scalar
11
-
12
- new(
13
- value: scalar.value,
14
- line: scalar.start_line + 1,
15
- column: scalar.start_column + 1
16
- )
17
- end
18
-
19
- def initialize(value:, line:, column:, filename: nil)
20
- @value = value
21
- @line = line
22
- @column = column
23
- @filename = filename
24
- end
25
-
26
- def to_s
27
- "#{filename}:#{line}:#{column}\n#{value}"
28
- end
29
- end
30
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Yamlook
4
- Mapping = ::Psych::Nodes::Mapping
5
- Scalar = ::Psych::Nodes::Scalar
6
-
7
- # List of yaml nodes
8
- class NodeList
9
- attr_reader :nodes
10
-
11
- def self.from_mapping(mapping)
12
- new(mapping.children)
13
- end
14
-
15
- def initialize(nodes)
16
- @nodes = nodes
17
- end
18
-
19
- def search(keys)
20
- return [] unless nodes
21
-
22
- keys.each_index.map do |index|
23
- key = keys[0..index].join('.')
24
- rest_keys = keys[index + 1..-1]
25
- result = find(key)
26
-
27
- case result
28
- when Mapping then NodeList.from_mapping(result).search(rest_keys)
29
- when Scalar then Node.from_scalar(result) if rest_keys.empty?
30
- end
31
- end
32
- end
33
-
34
- private
35
-
36
- def find(key)
37
- key_node_index = find_key_node_index(key)
38
- nodes[key_node_index + 1] if key_node_index
39
- end
40
-
41
- def find_key_node_index(key)
42
- nodes.find_index { |node| node.is_a?(Scalar) && node.value == key }
43
- end
44
- end
45
- end