ykutils 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/makegrid ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ykutils"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ #require "irb"
15
+ #IRB.start(__FILE__)
16
+ #puts( make_grid(1 , 2) )
17
+
18
+ #puts Ykutils::Erubyx::make_grid_list()
19
+ min_row = ARGV[0].to_i
20
+ max_row = ARGV[1].to_i
21
+ min_colum = ARGV[2].to_i
22
+ max_colum = ARGV[3].to_i
23
+ puts Ykutils::Gridlist::make_grid_list_x(min_row, max_row, min_colum, max_colum)
data/bin/setup CHANGED
@@ -1,6 +1,7 @@
1
- #!/bin/bash
1
+ #!/usr/bin/env bash
2
2
  set -euo pipefail
3
3
  IFS=$'\n\t'
4
+ set -vx
4
5
 
5
6
  bundle install
6
7
 
@@ -0,0 +1,58 @@
1
+ module Ykutils
2
+ module DataStructOp
3
+ def exchange(target_ary, hash, item_key, index_key, content)
4
+ v = hash[item_key]
5
+ if v
6
+ target_ary[v[index_key]] = content
7
+ else
8
+ target_ary << content
9
+ end
10
+ end
11
+
12
+ def select_array(ary, num, &block)
13
+ ret_ary = []
14
+ if block
15
+ ary.each do |line|
16
+ ret_ary << [line, num] if block.call(line, num)
17
+ num += 1
18
+ end
19
+ else
20
+ ary.each do |line|
21
+ ret_ary << [line, num]
22
+ num += 1
23
+ end
24
+ end
25
+ ret_ary
26
+ end
27
+
28
+ def make_array(ary, num, &block)
29
+ ret_ary = []
30
+ if block
31
+ ary.each do |line|
32
+ ary = block.call(line, num)
33
+ ret_ary += ary if ary
34
+ num += 1
35
+ end
36
+ else
37
+ ret_ary = ary
38
+ end
39
+
40
+ ret_ary
41
+ end
42
+
43
+ def make_hash(ary, num, &block)
44
+ kv_ary = []
45
+ if block
46
+ ary.each do |line|
47
+ ary = block.call(line, num)
48
+ kv_ary += ary if ary
49
+ num += 1
50
+ end
51
+ else
52
+ kv_ary = ary.collect { |line| [line, nil] }.flatten
53
+ end
54
+
55
+ Hash[*kv_ary]
56
+ end
57
+ end
58
+ end
@@ -1,32 +1,29 @@
1
- require 'pp'
1
+ require "pp"
2
2
 
3
3
  module Ykutils
4
4
  module DebugUtils
5
5
  class DebugMes
6
- def DebugMes.init( debug = false )
6
+ def self.init(debug = false)
7
7
  @@debug = debug
8
8
  @@buf = []
9
9
  end
10
10
 
11
- def DebugMes.set_debug( debug )
11
+ def self.set_debug(debug)
12
12
  @@debug = debug
13
13
  end
14
14
 
15
- def DebugMes.puts_x( mes )
15
+ def self.puts_x(mes)
16
16
  @@buf ||= []
17
17
  @@buf << mes
18
- # puts "#{@@buf.size} |#{mes}"
19
-
18
+ # puts "#{@@buf.size} |#{mes}"
20
19
  end
21
20
 
22
- def DebugMes.get
23
- buf = @@buf
21
+ def self.get
22
+ @@buf
24
23
  # DebugMes.clear
25
-
26
- buf
27
24
  end
28
25
 
29
- def DebugMes.clear
26
+ def self.clear
30
27
  @@buf ||= []
31
28
  @@buf.clear
32
29
  end
@@ -37,7 +34,7 @@ module Ykutils
37
34
  end
38
35
 
39
36
  def puts_d(mes)
40
- DebugMes.puts_x( mes )
37
+ DebugMes.puts_x(mes)
41
38
  # puts mes
42
39
  end
43
40
 
@@ -56,22 +53,22 @@ module Ykutils
56
53
  end
57
54
 
58
55
  def debug_utils_init
59
- set_debug( false )
60
- set_warn( 0 )
56
+ set_debug(false)
57
+ set_warn(0)
61
58
 
62
- DebugMes.init( false )
59
+ DebugMes.init(false)
63
60
  end
64
61
 
65
- def set_debug( val )
62
+ def set_debug(val)
66
63
  @debugutils = val
67
- DebugMes.set_debug( val )
64
+ DebugMes.set_debug(val)
68
65
  end
69
66
 
70
67
  def get_debug
71
68
  @debugutils
72
69
  end
73
70
 
74
- def set_warn( val )
71
+ def set_warn(val)
75
72
  @warn = val
76
73
  end
77
74
 
@@ -79,45 +76,41 @@ module Ykutils
79
76
  @debugutils
80
77
  end
81
78
 
82
- def d_exit( num )
83
- exit( num ) if @debugutils
79
+ def d_exit(num)
80
+ exit(num) if @debugutils
84
81
  end
85
82
 
86
- def d_puts( str )
83
+ def d_puts(str)
87
84
  puts(str) if @debugutils
88
85
  end
89
86
 
90
- def w1_puts( str )
91
- unless @warn
92
- set_warn( 0 )
93
- end
87
+ def w1_puts(str)
88
+ set_warn(0) unless @warn
94
89
  puts(str) if @debugutils or @warn >= 1
95
90
  end
96
91
 
97
- def w2_puts( str )
98
- unless @warn
99
- set_warn( 0 )
100
- end
92
+ def w2_puts(str)
93
+ set_warn(0) unless @warn
101
94
  puts(str) if @debugutils or @warn >= 2
102
95
  end
103
96
 
104
- def d_caller( num )
105
- pp caller( 0 ) if @debugutils
97
+ def d_caller(_num)
98
+ pp caller(0) if @debugutils
106
99
  end
107
100
 
108
- def puts_no_empty( mes )
101
+ def puts_no_empty(mes)
109
102
  puts mes if mes != ""
110
103
  end
111
104
 
112
- def d_puts_no_empty( mes )
105
+ def d_puts_no_empty(mes)
113
106
  puts mes if mes != "" and @debugutils
114
107
  end
115
108
 
116
- def d_p( it )
109
+ def d_p(it)
117
110
  p it if @debugutils
118
111
  end
119
112
 
120
- def d_pp( it )
113
+ def d_pp(it)
121
114
  if @debugutils
122
115
  pp "@debugutils=#{@debugutils}"
123
116
  pp caller(0)
@@ -0,0 +1,14 @@
1
+ require 'tilt'
2
+
3
+ module Ykutils
4
+ module Erubyx
5
+ module_function
6
+
7
+ def erubi_render(template_hash, scope, value_hash)
8
+ unless template_hash[:OBJ]
9
+ template_hash[:OBJ] = Tilt::ErubiTemplate.new{ template_hash[:TEMPLATE] }
10
+ end
11
+ template_hash[:OBJ].render( scope, value_hash )
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,401 @@
1
+ require "ykutils/stringutils"
2
+ require "ykutils/retcodex"
3
+ require "ykutils/debugutils"
4
+
5
+ module Ykutils
6
+ module FileOpUtils
7
+ include StringUtils
8
+ include DebugUtils
9
+
10
+ WRITABLE_FILE_PATH = 11
11
+ VALID_FILE_PATH = 10
12
+ ABNORMAL_STRING = -5
13
+ INVALID_FILE_PATH = -10
14
+ NOT_WRITABLE_FILE_PATH = -11
15
+
16
+ VALID_PATH = 20
17
+ INVALID_PATH = -20
18
+
19
+ def valid?
20
+ @valid
21
+ end
22
+
23
+ def valid_fpath(fname)
24
+ ret = VALID_FILE_PATH
25
+ if normal_string?(fname)
26
+ ret = INVALID_FILE_PATH unless File.file?(fname)
27
+ else
28
+ ret = ABNORMAL_STRING
29
+ end
30
+ ret
31
+ end
32
+
33
+ def valid_path(path)
34
+ ret = VALID_PATH
35
+ if normal_string?(path)
36
+ ret = INVALID_PATH unless File.exist?(path)
37
+ else
38
+ ret = ABNORMAL_STRING
39
+ end
40
+ ret
41
+ end
42
+
43
+ def valid_fpath_with_message(fname)
44
+ d_puts("valid_fpath_with_message|fname=#{fname}")
45
+
46
+ mes = ""
47
+ ret = valid_fpath(fname)
48
+ ret_bool = (ret == VALID_FILE_PATH)
49
+
50
+ case ret
51
+ when ABNORMAL_STRING
52
+ mes = "abnormal string|#{fname}"
53
+ @valid = false
54
+
55
+ d_puts("1 fname=#{fname}")
56
+ d_caller(0)
57
+ when INVALID_PATH
58
+ mes = "Can't find #{fname}"
59
+ d_caller(0)
60
+ @valid = false
61
+
62
+ d_puts("2 fname=#{fname}")
63
+ d_caller(0)
64
+ else
65
+ mes = ""
66
+ d_puts("3 fname=#{fname}")
67
+ end
68
+ RetCode2.new(ret, ret_bool, mes)
69
+ end
70
+
71
+ def valid_fname?(fname)
72
+ ret = false
73
+ if normal_string?(fname)
74
+ if File.file?(fname)
75
+ ret = true
76
+ else
77
+ d_puts("FileOpUtils:valid_fname? 2")
78
+ end
79
+ else
80
+ d_puts("FileOpUtils:valid_fname? 1")
81
+ end
82
+ ret
83
+ end
84
+
85
+ def valid_readable_fname?(fname)
86
+ ret = false
87
+ ret = true if normal_string?(fname) and File.file?(fname) and File.readable?(fname)
88
+ ret
89
+ end
90
+
91
+ def valid_writable_fpath_with_message(fname)
92
+ mes = ""
93
+
94
+ ret = valid_fpath(fname)
95
+
96
+ ret_bool = (ret == VALID_FILE_PATH)
97
+
98
+ case ret
99
+ when ABNORMAL_STRING
100
+ mes = "abnormal string|#{fname}"
101
+ @valid = false
102
+ d_puts("1 fname=#{fname}")
103
+ d_caller(0)
104
+ when INVALID_FILE_PATH
105
+ mes = "Can't find #{fname}"
106
+ d_caller(0)
107
+ @valid = false
108
+ d_puts("2 fname=#{fname}")
109
+ d_caller(0)
110
+ else
111
+ if File.writable?(fname)
112
+ mes = ""
113
+ ret = WRITABLE_FILE_PATH
114
+ else
115
+ mes = "Can't write #{fname}"
116
+ @valid = false
117
+ ret = NOT_WRITABLE_FILE_PATH
118
+ ret_bool = false
119
+ d_puts("3 fname=#{fname}")
120
+ d_caller(0)
121
+ end
122
+ end
123
+ RetCode2.new(ret, ret_bool, mes)
124
+ end
125
+
126
+ def valid_writable_fname?(fname)
127
+ ret = false
128
+ if normal_string?(fname)
129
+ if File.file?(fname)
130
+ ret = true if File.writable?(fname)
131
+ else
132
+ unless File.directory?(fname)
133
+ dir, filename = File.split(fname)
134
+ ret = valid_writable_directory?(dir)
135
+ end
136
+ end
137
+ end
138
+ ret
139
+ end
140
+
141
+ def valid_readable_directory?(path)
142
+ ret = false
143
+ ret = true if File.directory?(path) && File.readable?(path)
144
+ ret
145
+ end
146
+
147
+ def valid_writable_directory?(path)
148
+ ret = false
149
+ ret = true if File.directory?(path) && File.writable?(path)
150
+ ret
151
+ end
152
+
153
+ def write_file_with_template(input_file_path, output_file)
154
+ ary = []
155
+ begin
156
+ File.open(input_file_path, "r") do |file|
157
+ while line = file.gets
158
+ line.chomp!
159
+ ary << file_content_process(line)
160
+ end
161
+ end
162
+ rescue StandardError => e
163
+ pp e
164
+ pp e.traceback
165
+ @valid = false
166
+ end
167
+ if @valid
168
+ content = ary.join("\n")
169
+ begin
170
+ output_file.write(content)
171
+ rescue StandardError => e
172
+ pp e
173
+ pp e.traceback
174
+ @valid = false
175
+ end
176
+ end
177
+ end
178
+
179
+ def rewrite_file(from_path_1, from_path_2, to_path_1, to_path_2)
180
+ d_p("rewrite_file")
181
+ d_p("from_path_1=#{from_path_1}")
182
+ d_p("from_path_2=#{from_path_2}")
183
+ d_p("to_path_1=#{to_path_1}")
184
+ d_p("to_path_2=#{to_path_2}")
185
+
186
+ mes = prepare_file_copy(from_path_1, from_path_2, to_path_1, to_path_2)
187
+
188
+ puts_no_empty(mes)
189
+
190
+ return false unless @valid
191
+
192
+ from_path = File.join(from_path_1, from_path_2)
193
+ to_path = File.join(to_path_1, to_path_2)
194
+ ary = []
195
+ begin
196
+ File.open(from_path, "r") do |file|
197
+ while line = file.gets
198
+ line.chomp!
199
+ ary << file_content_process(line)
200
+ end
201
+ end
202
+ rescue StandardError => e
203
+ pp e
204
+ pp e.traceback
205
+ @valid = false
206
+ end
207
+
208
+ return false unless @valid
209
+
210
+ content = ary.join("\n")
211
+
212
+ begin
213
+ File.write(to_path, content)
214
+ rescue StandardError => e
215
+ pp e
216
+ pp e.traceback
217
+ @valid = false
218
+ end
219
+ end
220
+
221
+ def prepare_file_copy(from_path_1, from_path_2, to_path_1, to_path_2)
222
+ mes = ""
223
+
224
+ d_p("prepare_file_copy")
225
+ d_p("from_path_1=#{from_path_1}")
226
+ d_p("from_path_2=#{from_path_2}")
227
+ d_p("to_path_1=#{to_path_1}")
228
+ d_p("to_path_2=#{to_path_2}")
229
+
230
+ if prepare_source_dir(from_path_1, from_path_2)
231
+ unless prepare_dest_dir(to_path_1, to_path_2)
232
+ mes = "Can't write #{to_path_1}/#{to_path_1}"
233
+ d_caller(0)
234
+ @valid = false
235
+ end
236
+ else
237
+ mes = "Can't read #{from_path_1}/#{from_path_2}"
238
+ @valid = false
239
+ end
240
+
241
+ mes
242
+ end
243
+
244
+ def prepare_source_dir(from_path_1, from_path_2)
245
+ state = false
246
+ from_path = File.join(from_path_1, from_path_2)
247
+ if valid_readable_directory?(from_path_1)
248
+ dirname, filename = File.split(from_path)
249
+ state = true if valid_readable_directory?(dirname) && valid_readable_fname?(from_path)
250
+ end
251
+ state
252
+ end
253
+
254
+ def prepare_dest_dir(to_path_1, to_path_2)
255
+ state = true
256
+ unless valid_writable_directory?(to_path_1)
257
+ if File.exist?(to_path_1)
258
+ d_caller(0)
259
+ return false
260
+ end
261
+
262
+ begin
263
+ FileUtils.mkdir_p(to_path_1)
264
+ rescue StandardError => e
265
+ state = false
266
+ pp e
267
+ pp e.traceback
268
+ end
269
+ end
270
+ unless valid_writable_directory?(to_path_1)
271
+ d_caller(0)
272
+ return false
273
+ end
274
+
275
+ to_path = File.join(to_path_1, to_path_2)
276
+ dirname, filename = File.split(to_path)
277
+ unless File.exist?(dirname)
278
+ begin
279
+ FileUtils.mkdir_p(dirname)
280
+ rescue StandardError => e
281
+ state = false
282
+ pp caller(0)
283
+ end
284
+ end
285
+
286
+ unless state
287
+ d_caller(0)
288
+ return false
289
+ end
290
+
291
+ unless valid_writable_directory?(dirname)
292
+ d_caller(0)
293
+ return false
294
+ end
295
+
296
+ if File.file?(to_path) && !valid_writable_fname?(to_path)
297
+ d_caller(0)
298
+ return false
299
+ end
300
+
301
+ true
302
+ end
303
+
304
+ def copy_file(from_path_1, from_path_2, to_path_1, to_path_2)
305
+ retry_flag = false
306
+ begin
307
+ from_path = File.join(from_path_1, from_path_2)
308
+ to_path = File.join(to_path_1, to_path_2)
309
+ FileUtils.cp(from_path, to_path)
310
+ d_puts("Copy #{from_path} -> #{to_path}")
311
+ rescue StandardError => e
312
+ retry_flag = prepare_file_copy(from_path_1, from_path_2, to_path_1, to_path_2)
313
+ end
314
+
315
+ if retry_flag
316
+ begin
317
+ FileUtils.cp(from_path, to_path)
318
+ d_puts("Copy #{from_path} -> #{to_path}")
319
+ rescue StandardError => e
320
+ @valid = false
321
+ end
322
+ end
323
+
324
+ d_puts("Don't Copy #{from_path} -> #{to_path}") unless @valid
325
+ end
326
+
327
+ def find_file(path, dirname, filename)
328
+ ret = false
329
+ ary = path.split(File::SEPARATOR)
330
+ left = ary.index(dirname)
331
+ d_p(ary)
332
+ d_p(left)
333
+ if left
334
+ sub_ary = ary[(left + 1)..-1]
335
+ ret = true if sub_ary && sub_ary.index(filename)
336
+ end
337
+
338
+ ret
339
+ end
340
+
341
+ def check_filepath(fname_ary)
342
+ ret = RetCode2.new(ret, false, "")
343
+ mes_ary = []
344
+ fname_ary.each do |fname|
345
+ ret2 = valid_fpath_with_message(fname)
346
+ d_puts("==check_filepath")
347
+ d_puts_no_empty(ret2.mes)
348
+ d_p(ret2)
349
+ if ret2.bool
350
+ ret.ret = fname
351
+ ret.bool = true
352
+ break
353
+ elsif ret2.ret == FileOpUtils::ABNORMAL_STRING
354
+ ret.ret = ret2.ret
355
+ mes_ary << "Can't find common (#{common_fname})"
356
+ end
357
+ end
358
+ ret.mes = mes_ary.join("\n") unless ret.bool
359
+ ret
360
+ end
361
+
362
+ def make_fpath_list(fname, parent_dir_ary)
363
+ ary = [fname]
364
+ parent_dir_ary.each do |dir|
365
+ ary << File.join(dir, fname) if dir
366
+ end
367
+ ary
368
+ end
369
+
370
+ def reform_filename(fname, add_string)
371
+ path = Pathname(fname)
372
+ basename = path.basename(".*")
373
+ dirname = path.dirname
374
+ extname = path.extname
375
+
376
+ dirname.join(basename.to_s + add_string + extname.to_s)
377
+ end
378
+
379
+ def determine_encoding(pn)
380
+ encodings = [Encoding::UTF_8, Encoding::EUC_JP, Encoding::Shift_JIS, Encoding::CP932]
381
+
382
+ valid_enc = nil
383
+ encodings.each do |enc|
384
+ next if valid_enc
385
+
386
+ s = pn.expand_path.read(encoding: enc)
387
+ next unless s.valid_encoding?
388
+
389
+ begin
390
+ cs = s.encode(Encoding::CP932)
391
+ valid_enc = enc
392
+ rescue StandardError => e
393
+ # puts "Conversion Error! #{y}"
394
+ # puts y
395
+ end
396
+ end
397
+
398
+ valid_enc
399
+ end
400
+ end
401
+ end
@@ -1,9 +1,9 @@
1
1
  module Ykutils
2
2
  class FilePermision
3
3
  attr_reader :owner, :group, :other
4
-
4
+
5
5
  class PermisionEntry
6
- def initialize( str )
6
+ def initialize(str)
7
7
  @read = str[0].chr
8
8
  @write = str[1].chr
9
9
  @exec = str[2].chr
@@ -14,14 +14,14 @@ module Ykutils
14
14
  end
15
15
 
16
16
  def to_hash
17
- { "read" => @read , "write" => @write , "exec" => @exec }
17
+ { "read" => @read, "write" => @write, "exec" => @exec }
18
18
  end
19
19
  end
20
20
 
21
- def initialize( str )
22
- @owner = PermisionEntry.new( str[0..2] )
23
- @group = PermisionEntry.new( str[3..5] )
24
- @other = PermisionEntry.new( str[6..8] )
21
+ def initialize(str)
22
+ @owner = PermisionEntry.new(str[0..2])
23
+ @group = PermisionEntry.new(str[3..5])
24
+ @other = PermisionEntry.new(str[6..8])
25
25
  end
26
26
 
27
27
  def to_s
@@ -29,8 +29,7 @@ module Ykutils
29
29
  end
30
30
 
31
31
  def to_hash
32
- h = { "owner" => @owner.to_hash , "group" => @group.to_hash , "other" => @other.to_hash }
32
+ h = { "owner" => @owner.to_hash, "group" => @group.to_hash, "other" => @other.to_hash }
33
33
  end
34
34
  end
35
-
36
35
  end