Class: Filex::Filex

Inherits:
Object
  • Object
show all
Defined in:
lib/filex.rb

Overview

ファイル操作用クラス

Class Method Summary collapse

Class Method Details

.check_and_expand_file(fname, objx, mes) ⇒ String

eRubyスクリプトファイルの存在チェック、ハッシュを用いてを置換後に1個の文字列に変換

Parameters:

  • fname (String)

    eRubyスクリプトファイル

  • objx (Hash)

    eRubyスクリプト置換用ハッシュ

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (String)

    eRubyスクリプトファイルの変換結果



162
163
164
165
166
167
168
169
# File 'lib/filex.rb', line 162

def self.check_and_expand_file(fname, objx, mes)
  strdata = check_and_load_file(fname, mes)
  mes.output_info("fname=#{fname}")
  mes.output_info("strdata=#{strdata}")
  mes.output_info("objx=#{objx}")
  strdata2 = expand_str(strdata, objx, mes, fname: fname)
  strdata2
end

.check_and_expand_file_lines(fname, data, mes) ⇒ Array<String>

eRubyスクリプトの存在チェック、ハッシュを用いて置換したものを行の配列として得る

Parameters:

  • fname (String)

    eRubyスクリプト名

  • data (Hash)

    eRubyスクリプト置換用ハッシュ

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (Array<String>)

    eRubyスクリプトの変換結果



86
87
88
# File 'lib/filex.rb', line 86

def self.check_and_expand_file_lines(fname, data, mes)
  check_and_expand_file(fname, data, mes).split("\n")
end

.check_and_expand_yamlfile(yamlfname, objx, mes) ⇒ Hash

YAML形式ファイルの存在チェック、(eRubyスクリプトとしての)YAMLファイルをハッシュを用いて置換した後にRubyのオブジェクトに変換

Parameters:

  • yamlfname (String)

    yamlファイル名(eRubyスクリプトでもある)

  • objx (Hash)

    eRubyスクリプト置換用ハッシュ

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (Hash)

    YAMLの変換結果



71
72
73
74
75
76
77
# File 'lib/filex.rb', line 71

def self.check_and_expand_yamlfile(yamlfname, objx, mes)
  lines = Filex.check_and_expand_file_lines(yamlfname, objx, mes)
  str = escape_by_single_quote_with_lines_in_yamlformat(lines, mes).join("\n")
  mes.output_debug("=str")
  mes.output_debug(str)
  load_yaml(str, mes)
end

.check_and_load_file(fname, mes) ⇒ String

テキストファイルの存在チェック、ファイルの内容を1個の文字列に変換

Parameters:

  • fname (String)

    ファイル名

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (String)

    ファイルの内容



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/filex.rb', line 96

def self.check_and_load_file(fname, mes)
  size = File.size?(fname)
  if size && (size > 0)
    begin
      strdata = File.read(fname)
    rescue IOError => e
      mesg = "Can't read #{fname}"
      mes.output_fatal(mesg)
      mes.output_exception(e)
      exit(mes.ec("EXIT_CODE_CANNOT_READ_FILE"))
    rescue SystemCallError => e
      mesg = "Can't write #{fname}"
      mes.output_fatal(mesg)
      mes.output_exception(e)
      exit(mes.ec("EXIT_CODE_CANNOT_READ_FILE"))
    end
  else
    mesg = %Q(Can not find #{fname} or is empty| size=|#{size}|)
    mes.output_error(mesg)
    exit(mes.ec("EXIT_CODE_CANNOT_FIND_FILE_OR_EMPTY"))
  end

  if strdata.strip.empty?
    mesg = %Q(#{fname} is empty)
    mes.output_fatal(mesg)
    exit(mes.ec("EXIT_CODE_FILE_IS_EMPTY"))
  else
    digest = Digest::MD5.hexdigest(strdata)
    mes.output_info(digest)
  end

  strdata
end

.check_and_load_yamlfile(yamlfname, mes) ⇒ Hash

YAML形式ファイルをRubyのオブジェクトに変換

Parameters:

  • yamlfname (String)

    yamlファイル名

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (Hash)

    YAMLの変換結果



59
60
61
62
# File 'lib/filex.rb', line 59

def self.check_and_load_yamlfile(yamlfname, mes)
  str = Filex.check_and_load_file(yamlfname, mes)
  load_yaml(str, mes)
end

.check_colon_in_right(right, state) ⇒ Array<String>

YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第2段階)

Parameters:

  • right (String)

    対象文字列の分割右側部分

  • state (Hash)

    状態

Options Hash (state):

  • :mes (Messagex)

    Messagexクラスのインスタンス

  • :has_quoto (Booleand)

    true: 対象文字列がエスケープされている false: 対象文字列はエスケープされていない

Returns:

  • (Array<String>)

    第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/filex.rb', line 278

def self.check_colon_in_right(right, state)
  left3, right3 = colon_not_space(right)
  state[:mes].output_info("52|left3=#{left3}|right3=#{right3}")
  state[:need_quoto] = true
  return [nil, nil] if left3

  left2, right2 = colon(right)
  state[:mes].output_info("53|left2=#{left2}|right2=#{right2}")
  state[:need_quoto] = true
  [left2, right2]
end

.colon(str) ⇒ Array<String>

最初に現れる「:」を区切り文字として、文字列を2つに分割する

Parameters:

  • str (String)

    分割対象文字列

Returns:

  • (Array<String>)

    第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分



204
205
206
207
208
209
210
211
# File 'lib/filex.rb', line 204

def self.colon(str)
  if (m = /^(\s*([^\s]+):)(.*)$/.match(str))
    left = m[1]
    right = m[3]
  end

  [left, right]
end

.colon_not_space(str) ⇒ Array<String>

最初に現れる「:」と空白文字以外の文字の組み合わせを区切り文字列として、文字列を2つに分割する

Parameters:

  • str (String)

    分割対象文字列

Returns:

  • (Array<String>)

    第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分



190
191
192
193
194
195
196
197
# File 'lib/filex.rb', line 190

def self.colon_not_space(str)
  if (m = /^(\s*([^\s]+):[^\s])(.*)$/.match(str))
    left = m[1]
    right = m[3]
  end

  [left, right]
end

.colon_space(str) ⇒ Array<String>

最初に現れる「:」と空白文字の組み合わせを区切り文字列として、文字列を2つに分割する

Parameters:

  • str (String)

    分割対象文字列

Returns:

  • (Array<String>)

    第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分



176
177
178
179
180
181
182
183
# File 'lib/filex.rb', line 176

def self.colon_space(str)
  if (m = /^(\s*([^\s]+):\s)(.*)$/.match(str))
    left = m[1]
    right = m[3]
  end

  [left, right]
end

.escape_by_single_quote_with_lines_in_yamlformat(lines, mes) ⇒ Array<String>

YAML形式の文字列の配列に(必要であれば)シングルクォーテーションでのエスケープを追加

Parameters:

  • lines (Array<String>)

    対象行の配列

  • mes (Messagex)

    :mes Messagexクラスのインスタンス

Returns:

  • (Array<String>)

    必要なエスケープがされた対象文字列



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/filex.rb', line 362

def self.escape_by_single_quote_with_lines_in_yamlformat(lines, mes)
  state = { mes: mes }
  lines.map do |line|
    state[:need_quoto] = false
    state[:has_quoto] = false

    left, right = escape_single_quote_yaml_first(line, state)
    left, right = escape_single_quote_yaml_second(line, state, left, right)
    if left
      escape_single_quote_yaml_third(line, state, left, right)
    end
    if state[:need_quoto] && !state[:has_quoto]
      state[:mes].output_info("2 need_quoto")
      left + %q(') + right + %q(')
    else
      line
    end
  end
end

.escape_single_quote_yaml_first(line, state) ⇒ Array<String>

YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第1段階)

Parameters:

  • line (String)

    対象文字列

  • state (Hash)

    状態

Returns:

  • (Array<String>)

    第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/filex.rb', line 233

def self.escape_single_quote_yaml_first(line, state)
  # lineに対して": "での分割を試みる
  left, right = colon_space(line)
  state[:mes].output_info("F1|left=#{left}|right=#{right}")
  # 分割できなければ、lineに対して":"での分割を試みる
  left, right = colon(line) unless left
  state[:mes].output_info("F2|left=#{left}|right=#{right}")

  # 右側部分に"-"が含まれていれば、"- "で区切れるか調べる
  if right&.index("-")
    left, right = hyphen_space(line)
    state[:mes].output_info("F3|left=#{left}")
    if left
      state[:need_quoto] = true
      state[:mes].output_info("NQ|1|need_quoto=#{state[:need_quoto]}")
    end
  end

  # lineが":"が分割できなければ、lineに対して"- "での分割を試みる
  unless left
    left, right = hyphen_space(line)
    state[:mes].output_info("F4|left=#{left}|right=#{right}")
    if right&.index("-") == 0
      state[:mes].output_info("F-X|index=0|right.size=#{right.size}")
      if right.size == 1
        left += right
        right = nil
      else
        state[:need_quoto] = true
      end
    end
  end

  state[:mes].output_info("FE|left=#{left}|right=#{right}")
  [left, right]
end

.escape_single_quote_yaml_second(line, state, left, right) ⇒ Array<String>

YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第2段階)

Parameters:

  • line (String)

    対象文字列

  • state (Hash)

    状態

  • left (String)

    対象文字列の分割左側部分

  • right (String)

    対象文字列の分割右側部分

Options Hash (state):

  • :mes (Messagex)

    Messagexクラスのインスタンス

  • :has_quoto (Booleand)

    true: 対象文字列がエスケープされている false: 対象文字列はエスケープされていない

Returns:

  • (Array<String>)

    第0要素 分割された文字列の左側部分、第1要素 分割された文字列の右側部分



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/filex.rb', line 300

def self.escape_single_quote_yaml_second(line, state, left, right)
  return [left, right] if right.nil? || right.strip.empty?

  state[:mes].output_info("S1|left=#{left}|right=#{right}")
  state[:has_quoto] = true if right.index("'")

  if right.index(":")
    state[:mes].output_info("S2|left=#{left}|right=#{right}")

    return([left, right]) if /\d:/.match?(right)
    state[:mes].output_info("S3|left=#{left}|right=#{right}")
  end
  left2, right2 = colon_space(right)
  state[:mes].output_info("S4|left2=#{left2}|right2=#{right2}")

  unless left2
    left2, right2 = check_colon_in_right(right, state)
  end

  if left2
    left += left2
    right = right2
    state[:mes].output_info("S5|left=#{left}|right=#{right}")
  end
  state[:mes].output_info("S6|left=#{left}|right=#{right}")
  [left, right]
end

.escape_single_quote_yaml_third(line, state, left, right) ⇒ void

This method returns an undefined value.

YAML形式の文字列に、シングルクォーテーションでのエスケープが必要かを調べる(第3段階)

Parameters:

  • line (String)

    対象文字列

  • state (Hash)

    状態

  • left (String)

    対象文字列の分割左側部分

  • right (String)

    対象文字列の分割右側部分

Options Hash (state):

  • :mes (Messagex)

    Messagexクラスのインスタンス

  • :has_quoto (Booleand)

    true: 対象文字列がエスケープされている false: 対象文字列はエスケープされていない



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/filex.rb', line 338

def self.escape_single_quote_yaml_third(line, state, left, right)
  return if right.nil? || right.strip.empty?
  state[:mes].output_info("T1|left=#{left}|right=#{right}")

  return if state[:need_quoto]
  state[:mes].output_info("T2|left=#{left}|right=#{right}")

  return unless (right.index(":") || right.index("*"))
  state[:mes].output_info("T3|left=#{left}|right=#{right}")

  state[:mes].output_info("T4 not need_quoto")
  unless right.index("-")
    state[:need_quoto] = true
    state[:mes].output_info("NQ|T5|need_quoto=#{state[:need_quoto]}")
  end
  state[:mes].output_info("T6 need_quoto=#{state[:need_quoto]}")
end

.expand_str(eruby_str, data, mes, fnames = {}) ⇒ String

eRubyスクリプト文字列を、ハッシュを用いて置換した内容を、1個の文字列に変換

Parameters:

  • eruby_str (String)

    eRubyスクリプト文字列

  • data (Hash)

    eRubyスクリプト置換用ハッシュ

  • mes (Messagex)

    Messagexクラスのインスタンス

  • fnames (Hash) (defaults to: {})

    入力ファイル名群

Returns:

  • (String)

    eRubyスクリプトの変換結果



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/filex.rb', line 138

def self.expand_str(eruby_str, data, mes, fnames={})
  begin
    mes.output_info("eruby_str=|#{eruby_str}|")
    mes.output_info("data=#{data}")
    strdata = Erubis::Eruby.new(eruby_str).result(data)
  rescue NameError => e
    mes.output_exception(e)
    fnames.map {|x| mes.output_fatal(%Q(#{x[0]}=#{x[1]})) }
    exit(mes.ec("EXIT_CODE_NAME_ERROR_EXCEPTION_IN_ERUBY"))
  rescue Error => e
    mes.output_exception(e)
    fnames.map {|x| mes.output_fatal(%Q(#{x[0]}=#{x[1]})) }
    exit(mes.ec("EXIT_CODE_ERROR_EXCEPTION_IN_ERUBY"))
  end
  strdata
end

.hyphen_space(str) ⇒ Array<String>

最初に現れる「-」と空白文字の組み合わせを区切り文字列として、文字列を2つに分割する

Parameters:

  • str (String)

    分割対象文字列

Returns:

  • (Array<String>)

    第0要素 分割文字列の左側部分、第1要素 分割文字列の右側部分



218
219
220
221
222
223
224
225
# File 'lib/filex.rb', line 218

def self.hyphen_space(str)
  if (m = /^(\s*((\-\s+)+))(.*)$/.match(str))
    left = m[1]
    right = m[4]
  end

  [left, right]
end

.load_yaml(str, mes) ⇒ Hash

YAML形式文字列をRubyのオブジェクトに変換

Parameters:

  • str (String)

    YAML形式の文字列

  • mes (Messagex)

    Messagexクラスのインスタンス

Returns:

  • (Hash)

    YAMLの変換結果



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/filex.rb', line 40

def self.load_yaml(str, mes)
  yamlhs = {}
  begin
    yamlhs = YAML.safe_load(str, [Date, Symbol])
  rescue Error => e
    mes.output_exception(e)
    mes.output_fatal("str=#{str}")
    exit(mes.ec("EXIT_CODE_CANNOT_ANALYZE_YAMLFILE"))
  end

  yamlhs
end

.setup(mes) ⇒ void

This method returns an undefined value.

Filexクラスで利用する終了ステータスの登録

Parameters:

  • mes (Messagex)

    Messagexクラスのインスタンス



28
29
30
31
32
33
# File 'lib/filex.rb', line 28

def self.setup(mes)
  mes.add_exitcode("EXIT_CODE_CANNOT_ANALYZE_YAMLFILE")
  mes.add_exitcode("EXIT_CODE_NAME_ERROR_EXCEPTION_IN_ERUBY")
  mes.add_exitcode("EXIT_CODE_ERROR_EXCEPTION_IN_ERUBY")
  mes.add_exitcode("EXIT_CODE_FILE_IS_EMPTY")
end