filex 0.1.4 → 0.1.5

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: 79be0fdcfe0f3da7f8fc16d5b300875a01d7c64668775cca92c9b2a659e93ea1
4
- data.tar.gz: 07de986587dcc1d22588964d84af13613c25b29fec804ea43db44639f29095db
3
+ metadata.gz: '07842dde93309500dc3c9462ba33e530443a84376a4c6e5cf7edce3d5b9843a7'
4
+ data.tar.gz: 73b1b04a6b0c0e42693e8ee11d523647b1aeaa57e31238f57ef3aecab64fc6b8
5
5
  SHA512:
6
- metadata.gz: e2c227e6164b81babb75a5d814b8504ce98ea198fc013430f4a492b34798727762fb9ff9b680a5887037b3c88f34b8f057fa89acb104f9226ed6ff415c630872
7
- data.tar.gz: 1d2d6f933234c15062e191d21617fa21f105da96c58e44a58a2c575659146c024de8bdb3fbc51da2605b9eb3d6c4e6ea896905468ee3a40a868cde19f796e608
6
+ metadata.gz: da6c1f283a4578ef8e4bc7377fa5da990363db1c3c213783e98aa09f0564f7746999bf76750c4347f3ea10938d3b2414edadfe557c34ad0ab71b61e63e1fe87c
7
+ data.tar.gz: 6526c843d4e043e4b996b73640323a0dcc0850b37370a6ad23ed50ed78037b0a10d586cd396cabc210d7e24fd7fa6a206372c77023d0de8d9795f4c41b18ec91
data/.gitignore CHANGED
File without changes
data/.rspec CHANGED
File without changes
@@ -0,0 +1,115 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.5
5
+ DisplayCopNames: true
6
+ #
7
+ StyleGuideBaseURL: https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md
8
+ #
9
+ Exclude:
10
+ - "Rakefile"
11
+ - "*.gemspec"
12
+ - "vendor/**/*"
13
+ - "spec/*.rb"
14
+ - 'bin/console'
15
+ - 'bin/setup'
16
+
17
+ Layout/ExtraSpacing:
18
+ AllowForAlignment: true
19
+ Layout/SpaceInsideBlockBraces:
20
+ EnforcedStyle: space
21
+ SpaceBeforeBlockParameters: false
22
+ Layout/MultilineMethodCallIndentation:
23
+ EnforcedStyle: indented
24
+ Layout/EmptyLineAfterGuardClause:
25
+ Enabled: false
26
+ Layout/MultilineOperationIndentation:
27
+ EnforcedStyle: indented
28
+
29
+ Lint/UnderscorePrefixedVariableName:
30
+ Enabled: false
31
+ Lint/UnusedMethodArgument:
32
+ Enabled: false
33
+
34
+ Metrics/LineLength:
35
+ Enabled: false
36
+ Metrics/ClassLength:
37
+ Exclude:
38
+ # - "test/**/*.rb"
39
+ Metrics/ParameterLists:
40
+ Max: 7
41
+ Metrics/MethodLength:
42
+ Max: 30
43
+
44
+ Naming/FileName:
45
+ Enabled: false
46
+ Naming/UncommunicativeMethodParamName:
47
+ AllowedNames:
48
+ - z
49
+
50
+ Style/AsciiComments:
51
+ Enabled: false
52
+ Style/HashSyntax:
53
+ Exclude:
54
+ - "**/*.rake"
55
+ - "Rakefile"
56
+ Style/EmptyMethod:
57
+ Enabled: false
58
+ Style/FrozenStringLiteralComment:
59
+ Enabled: false
60
+ Style/NumericLiterals:
61
+ Enabled: false
62
+ Style/StringLiterals:
63
+ EnforcedStyle: double_quotes
64
+ Style/TrailingCommaInArrayLiteral:
65
+ Enabled: false
66
+ Style/TrailingCommaInHashLiteral:
67
+ Enabled: false
68
+ Style/TrailingCommaInArguments:
69
+ Enabled: false
70
+ Style/Documentation:
71
+ Enabled: false
72
+ Style/WordArray:
73
+ Enabled: false
74
+ Style/BarePercentLiterals:
75
+ EnforcedStyle: percent_q
76
+ Style/BracesAroundHashParameters:
77
+ Enabled: false
78
+ Style/PercentQLiterals:
79
+ Enabled: false
80
+ Style/UnneededPercentQ:
81
+ Enabled: false
82
+ Style/IfUnlessModifier:
83
+ Enabled: false
84
+ Style/NumericPredicate:
85
+ Enabled: false
86
+ Style/MutableConstant:
87
+ Enabled: false
88
+ Style/SymbolArray:
89
+ Enabled: false
90
+ Style/FormatString:
91
+ Enabled: false
92
+ Style/ConditionalAssignment:
93
+ Enabled: false
94
+ Style/WhileUntilModifier:
95
+ Enabled: false
96
+ Style/RedundantBegin:
97
+ Enabled: false
98
+ Style/YodaCondition:
99
+ EnforcedStyle: forbid_for_equality_operators_only
100
+ Style/TernaryParentheses:
101
+ EnforcedStyle: require_parentheses_when_complex
102
+ Style/MethodCallWithArgsParentheses:
103
+ Exclude:
104
+ - "**/*.rake"
105
+ - "Rakefile"
106
+ - "Gemfile"
107
+ Enabled: true
108
+ IgnoredMethods:
109
+ - p
110
+ - pp
111
+ - puts
112
+ - print
113
+ - printf
114
+ - raise
115
+ - require
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE CHANGED
File without changes
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
File without changes
@@ -1,15 +1,30 @@
1
- require "filex/version"
1
+ # frozen_string_literal: true
2
2
 
3
+ #
4
+ # ファイル操作用モジュール
5
+ #
3
6
  module Filex
4
7
  require "digest"
5
8
  require "pp"
6
9
  require "erubis"
7
10
  require "yaml"
8
11
  require "messagex"
12
+ require "filex/version"
9
13
 
14
+ #
15
+ # ファイル操作用モジュールのエラークラス
16
+ #
10
17
  class Error < StandardError; end
11
18
 
12
- class Filex
19
+ #
20
+ # ファイル操作用クラス
21
+ #
22
+ class Filex # rubocop:disable Metrics/ClassLength
23
+ #
24
+ # Filexクラスで利用する終了ステータスの登録
25
+ #
26
+ # @param mes [Messagex] Messagexクラスのインスタンス
27
+ # @return [void]
13
28
  def self.setup(mes)
14
29
  mes.add_exitcode("EXIT_CODE_CANNOT_ANALYZE_YAMLFILE")
15
30
  mes.add_exitcode("EXIT_CODE_NAME_ERROR_EXCEPTION_IN_ERUBY")
@@ -17,6 +32,11 @@ module Filex
17
32
  mes.add_exitcode("EXIT_CODE_FILE_IS_EMPTY")
18
33
  end
19
34
 
35
+ #
36
+ # YAML形式文字列をRubyのオブジェクトに変換
37
+ # @param str [String] YAML形式の文字列
38
+ # @param mes [Messagex] Messagexクラスのインスタンス
39
+ # @return [Hash] YAMLの変換結果
20
40
  def self.load_yaml(str, mes)
21
41
  yamlhs = {}
22
42
  begin
@@ -30,11 +50,24 @@ module Filex
30
50
  yamlhs
31
51
  end
32
52
 
53
+ #
54
+ # YAML形式ファイルをRubyのオブジェクトに変換
55
+ #
56
+ # @param yamlfname [String] yamlファイル名
57
+ # @param mes [Messagex] Messagexクラスのインスタンス
58
+ # @return [Hash] YAMLの変換結果
33
59
  def self.check_and_load_yamlfile(yamlfname, mes)
34
60
  str = Filex.check_and_load_file(yamlfname, mes)
35
61
  load_yaml(str, mes)
36
62
  end
37
63
 
64
+ #
65
+ # YAML形式ファイルを存在チェック、(eRubyスクリプトとしての)YAMLファイルをハッシュを用いて置換した後にRubyのオブジェクトに変換
66
+ #
67
+ # @param yamlfname [String] yamlファイル名(eRubyスクリプトでもある)
68
+ # @param objx [Hash] eRubyスクリプト置換用ハッシュ
69
+ # @param mes [Messagex] Messagexクラスのインスタンス
70
+ # @return [Hash] YAMLの変換結果
38
71
  def self.check_and_expand_yamlfile(yamlfname, objx, mes)
39
72
  lines = Filex.check_and_expand_file_lines(yamlfname, objx, mes)
40
73
  str = escape_by_single_quote_with_lines_in_yamlformat(lines, mes).join("\n")
@@ -43,10 +76,23 @@ module Filex
43
76
  load_yaml(str, mes)
44
77
  end
45
78
 
79
+ #
80
+ # eRubyスクリプトの存在チェック、ハッシュを用いて置換したものを行の配列として得る
81
+ #
82
+ # @param fname [String] eRubyスクリプト名
83
+ # @param data [Hash] eRubyスクリプト置換用ハッシュ
84
+ # @param mes [Messagex] Messagexクラスのインスタンス
85
+ # @return [Array<String>] eRubyスクリプトの変換結果
46
86
  def self.check_and_expand_file_lines(fname, data, mes)
47
87
  check_and_expand_file(fname, data, mes).split("\n")
48
88
  end
49
89
 
90
+ #
91
+ # テキストファイルの存在チェック、ファイルの内容を1個の文字列に変換
92
+ #
93
+ # @param fname [String] ファイル名
94
+ # @param mes [Messagex] Messagexクラスのインスタンス
95
+ # @return [String] ファイルの内容
50
96
  def self.check_and_load_file(fname, mes)
51
97
  size = File.size?(fname)
52
98
  if size && (size > 0)
@@ -81,6 +127,14 @@ module Filex
81
127
  strdata
82
128
  end
83
129
 
130
+ #
131
+ # eRubyスクリプト文字列を、ハッシュを用いて置換した内容を1個の文字列に変換
132
+ #
133
+ # @param eruby_str [String] eRubyスクリプト文字列
134
+ # @param data [Hash] eRubyスクリプト置換用ハッシュ
135
+ # @param mes [Messagex] Messagexクラスのインスタンス
136
+ # @param fnames [Hash] 入力ファイル名群
137
+ # @return [String] eRubyスクリプトの変換結果
84
138
  def self.expand_str(eruby_str, data, mes, fnames={})
85
139
  begin
86
140
  mes.output_info("eruby_str=|#{eruby_str}|")
@@ -98,139 +152,209 @@ module Filex
98
152
  strdata
99
153
  end
100
154
 
155
+ #
156
+ # eRubyスクリプトファイルの存在チェック、ハッシュを用いてを置換後に1個の文字列に変換
157
+ #
158
+ # @param fname [String] eRubyスクリプトファイル
159
+ # @param objx [Hash] eRubyスクリプト置換用ハッシュ
160
+ # @param mes [Messagex] Messagexクラスのインスタンス
161
+ # @return [String] eRubyスクリプトファイルの変換結果
101
162
  def self.check_and_expand_file(fname, objx, mes)
102
163
  strdata = check_and_load_file(fname, mes)
103
164
  mes.output_info("fname=#{fname}")
104
165
  mes.output_info("strdata=#{strdata}")
105
166
  mes.output_info("objx=#{objx}")
106
- strdata2 = expand_str(strdata, objx, mes, { fname: fname })
167
+ strdata2 = expand_str(strdata, objx, mes, fname: fname)
107
168
  strdata2
108
169
  end
109
170
 
171
+ #
172
+ # 最初に現れる「:」と空白文字の組み合わせを区切り文字列として、文字列を2つに分割する
173
+ #
174
+ # @param str [String] 分割対象文字列
175
+ # @return [Array<String>] 第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分
110
176
  def self.colon_space(str)
111
177
  if (m = /^(\s*([^\s]+):\s)(.*)$/.match(str))
112
- key = m[1]
113
- value = m[3]
178
+ left = m[1]
179
+ right = m[3]
114
180
  end
115
181
 
116
- [key, value]
182
+ [left, right]
117
183
  end
118
184
 
185
+ #
186
+ # 最初に現れる「:」と空白文字以外の文字の組み合わせを区切り文字列として、文字列を2つに分割する
187
+ #
188
+ # @param str [String] 分割対象文字列
189
+ # @return [Array<String>] 第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分
119
190
  def self.colon_not_space(str)
120
191
  if (m = /^(\s*([^\s]+):[^\s])(.*)$/.match(str))
121
- key = m[1]
122
- value = m[3]
192
+ left = m[1]
193
+ right = m[3]
123
194
  end
124
195
 
125
- [key, value]
196
+ [left, right]
126
197
  end
127
198
 
199
+ #
200
+ # 最初に現れる「:」を区切り文字として、文字列を2つに分割する
201
+ #
202
+ # @param str [String] 分割対象文字列
203
+ # @return [Array<String>] 第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分
128
204
  def self.colon(str)
129
205
  if (m = /^(\s*([^\s]+):)(.*)$/.match(str))
130
- key = m[1]
131
- value = m[3]
206
+ left = m[1]
207
+ right = m[3]
132
208
  end
133
209
 
134
- [key, value]
210
+ [left, right]
135
211
  end
136
212
 
213
+ #
214
+ # 最初に現れる「-」と空白文字の組み合わせを区切り文字列として、文字列を2つに分割する
215
+ #
216
+ # @param str [String] 分割対象文字列
217
+ # @return [Array<String>] 第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分
137
218
  def self.hyphen_space(str)
138
219
  if (m = /^(\s*((\-\s+)+))(.+)$/.match(str))
139
- key = m[1]
140
- value = m[4]
220
+ left = m[1]
221
+ right = m[4]
141
222
  end
142
223
 
143
- [key, value]
224
+ [left, right]
144
225
  end
145
226
 
227
+ #
228
+ # YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第1段階)
229
+ #
230
+ # @param line [String] 対象文字列
231
+ # @param state [Hash] 状態
232
+ # @return [Array<String>] 第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分
146
233
  def self.escape_single_quote_yaml_first(line, state)
147
- k, v = colon_space(line)
148
- state[:mes].output_info("1|k=#{k}")
149
- k, v = colon(line) unless k
150
- state[:mes].output_info("2|k=#{k}")
151
-
152
- if v&.index("-")
153
- k, v = hyphen_space(line)
154
- state[:mes].output_info("3|k=#{k}")
155
- if k
234
+ # lineに対して": "での分割を試みる
235
+ left, right = colon_space(line)
236
+ state[:mes].output_info("1|left=#{left}")
237
+ left, right = colon(line) unless left
238
+ state[:mes].output_info("2|left=#{left}")
239
+
240
+ if right&.index("-")
241
+ left, right = hyphen_space(line)
242
+ state[:mes].output_info("3|left=#{left}")
243
+ if left
156
244
  state[:need_quoto] = true
157
245
  state[:mes].output_info("NQ|1|need_quoto=#{state[:need_quoto]}")
158
246
  end
159
247
  end
160
248
 
161
- unless k
162
- k, v = hyphen_space(line)
249
+ unless left
250
+ left, right = hyphen_space(line)
163
251
  end
164
252
 
165
- [k, v]
253
+ [left, right]
166
254
  end
167
255
 
168
- def self.escape_single_quote_yaml_second(line, state, key, value)
169
- state[:mes].output_info("4|k=#{key}|v=#{value}")
256
+ #
257
+ # YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第2段階)
258
+ #
259
+ # @param line [String] 対象文字列
260
+ # @param right [String] 対象文字列の分割右側部分
261
+ # @param state [Hash] 状態
262
+ # @option state [Messagex] :mes Messagexクラスのインスタンス
263
+ # @option state [Booleand] :has_quoto true: 対象文字列がエスケープされている false: 対象文字列はエスケープされていない
264
+ # @return [Array<String>] 第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分
265
+ def self.check_colon_in_right(right, state)
266
+ left3, right3 = colon_not_space(right)
267
+ state[:mes].output_info("52|left3=#{left3}|right3=#{right3}")
268
+ state[:need_quoto] = true
269
+ return [nil, nil] if left3
270
+
271
+ left2, right2 = colon(right)
272
+ state[:mes].output_info("53|left2=#{left2}|right2=#{right2}")
273
+ state[:need_quoto] = true
274
+ [left2, right2]
275
+ end
170
276
 
171
- return [key, value] if value.nil? || value.strip.empty?
277
+ #
278
+ # YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第2段階)
279
+ #
280
+ # @param line [String] 対象文字列
281
+ # @param state [Hash] 状態
282
+ # @option state [Messagex] :mes Messagexクラスのインスタンス
283
+ # @option state [Booleand] :has_quoto true: 対象文字列がエスケープされている false: 対象文字列はエスケープされていない
284
+ # @param left [String] 対象文字列の分割左側部分
285
+ # @param right [String] 対象文字列の分割右側部分
286
+ # @return [Array<String>] 第0要素 分割された文字列の左側部分、第1要素 分割された文字列の右側部分
287
+ def self.escape_single_quote_yaml_second(line, state, left, right)
288
+ state[:mes].output_info("4|left=#{left}|right=#{right}")
172
289
 
173
- state[:has_quoto] = true if value.index("'")
290
+ return [left, right] if right.nil? || right.strip.empty?
174
291
 
175
- return [key, value] if value.index(":").nil?
292
+ state[:has_quoto] = true if right.index("'")
176
293
 
177
- return([key, value]) if /\d:/.match?(value)
294
+ return [left, right] if right.index(":").nil?
178
295
 
179
- k2, v2 = colon_space(value)
180
- state[:mes].output_info("51|k2=#{k2}|v2=#{v2}")
296
+ return([left, right]) if /\d:/.match?(right)
181
297
 
182
- unless k2
183
- k3, v3 = colon_not_space(value)
184
- state[:mes].output_info("52|k3=#{k3}|v3=#{v3}")
185
- state[:need_quoto] = true
298
+ left2, right2 = colon_space(right)
299
+ state[:mes].output_info("51|left2=#{left2}|right2=#{right2}")
186
300
 
187
- unless k3
188
- k2, v2 = colon(value)
189
- state[:mes].output_info("53|k2=#{k2}|v2=#{v2}")
190
- state[:need_quoto] = true
191
- end
301
+ unless left2
302
+ left2, right2 = check_colon_in_right(right, state)
192
303
  end
193
304
 
194
- if k2
195
- key += k2
196
- value = v2
197
- state[:mes].output_info("6|k=#{key}|v=#{value}")
305
+ if left2
306
+ left += left2
307
+ right = right2
308
+ state[:mes].output_info("6|left=#{left}|right=#{right}")
198
309
  end
199
- [key, value]
310
+ [left, right]
200
311
  end
201
312
 
202
- def self.escape_single_quote_yaml_third(line, state, key, value)
203
- return line if value.nil? || value.strip.empty?
204
-
205
- unless state[:need_quoto]
206
- if value.index(":") || value.index("*")
207
- state[:mes].output_info("1 not need_quoto")
208
- unless value.index("-")
209
- state[:need_quoto] = true
210
- state[:mes].output_info("NQ|2|need_quoto=#{state[:need_quoto]}")
211
- end
212
- state[:mes].output_info("1A need_quoto=#{state[:need_quoto]}")
213
- end
214
- end
215
-
216
- if state[:need_quoto] && !state[:has_quoto]
217
- state[:mes].output_info("2 need_quoto")
218
- key + %q(') + value + %q(')
219
- else
220
- line
313
+ #
314
+ # YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第3段階)
315
+ #
316
+ # @param line [String] 対象文字列
317
+ # @param state [Hash] 状態
318
+ # @option state [Messagex] :mes Messagexクラスのインスタンス
319
+ # @option state [Booleand] :has_quoto true: 対象文字列がエスケープされている false: 対象文字列はエスケープされていない
320
+ # @param left [String] 対象文字列の分割左側部分
321
+ # @param right [String] 対象文字列の分割右側部分
322
+ # @return [void]
323
+ def self.escape_single_quote_yaml_third(line, state, left, right)
324
+ return if right.nil? || right.strip.empty?
325
+
326
+ return if state[:need_quoto]
327
+
328
+ return unless right.index(":") && right.index("*")
329
+
330
+ state[:mes].output_info("1 not need_quoto")
331
+ unless right.index("-")
332
+ state[:need_quoto] = true
333
+ state[:mes].output_info("NQ|2|need_quoto=#{state[:need_quoto]}")
221
334
  end
335
+ state[:mes].output_info("1A need_quoto=#{state[:need_quoto]}")
222
336
  end
223
337
 
338
+ #
339
+ # YAML形式の文字列の配列に(必要であれば)シングルクォーテーションでのエスケープを追加
340
+ #
341
+ # @param lines [Array<String>] 対象行の配列
342
+ # @param mes [Messagex] :mes Messagexクラスのインスタンス
343
+ # @return [Array<String>] 必要なエスケープがされた対象文字列
224
344
  def self.escape_by_single_quote_with_lines_in_yamlformat(lines, mes)
225
345
  state = { mes: mes }
226
346
  lines.map do |line|
227
347
  state[:need_quoto] = false
228
348
  state[:has_quoto] = false
229
349
 
230
- k, v = escape_single_quote_yaml_first(line, state)
231
- k, v = escape_single_quote_yaml_second(line, state, k, v)
232
- if k
233
- escape_single_quote_yaml_third(line, state, k, v)
350
+ left, right = escape_single_quote_yaml_first(line, state)
351
+ left, right = escape_single_quote_yaml_second(line, state, left, right)
352
+ if left
353
+ escape_single_quote_yaml_third(line, state, left, right)
354
+ end
355
+ if state[:need_quoto] && !state[:has_quoto]
356
+ state[:mes].output_info("2 need_quoto")
357
+ left + %q(') + right + %q(')
234
358
  else
235
359
  line
236
360
  end
@@ -1,3 +1,4 @@
1
1
  module Filex
2
- VERSION = "0.1.4"
2
+ # バージョン番号
3
+ VERSION = "0.1.5"
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - yasuo kominami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-28 00:00:00.000000000 Z
11
+ date: 2019-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
+ - ".rubocop.yml"
93
94
  - ".travis.yml"
94
95
  - Gemfile
95
96
  - LICENSE