Class: UniPropUtils::FileManager

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

Class Method Summary collapse

Class Method Details

.basename_no_ext(path) ⇒ Object



303
304
305
306
# File 'lib/uniprop/utils.rb', line 303

def basename_no_ext(path)
  name = path.basename.to_s
  name.slice(0..(name.size-path.extname.size-1))
end

.child?(parent, child) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
258
259
# File 'lib/uniprop/utils.rb', line 253

def child?(parent, child)
  if parent.class == String
    child.descend.any? { _1.basename.to_s == parent }
  elsif parent.class == Pathname
    downcase_path(child).to_s.include? downcase_path(parent).to_s
  end
end

.downcase_path(path) ⇒ Object



289
290
291
# File 'lib/uniprop/utils.rb', line 289

def downcase_path(path)
  Pathname.new(path.cleanpath.to_s.split("/").map { _1.downcase }.join("/")).cleanpath
end

.ext_no_dot(path) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/uniprop/utils.rb', line 293

def ext_no_dot(path)  
  path = Pathname.new(path)
  ext = path.extname
  if ext.empty?
    return ""
  else
    return ext[1..]
  end
end

.filter_file(files, excluded_extensions, excluded_directories, excluded_files, included_files) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/uniprop/utils.rb', line 220

def filter_file(files, excluded_extensions, excluded_directories, excluded_files, included_files)
  files = files.dup
  original_files = files.dup
  excluded_extensions = excluded_extensions.map { _1.downcase }
  excluded_files = excluded_files.map { _1.downcase }
  included_files = included_files.map { _1.downcase }

  # remove files by excluded_extensions

  files = files.reject { excluded_extensions.include? ext_no_dot(downcase_path(_1)).downcase }
  
  # remove files by excluded_directories

  excluded_directories.each do |dir|
    files = files.reject { child?(dir, downcase_path(_1)) }
  end

  # remove files by excluded_files

  files = files.reject { excluded_files.include? prefix(downcase_path(_1)) }

  # remove test files

  files = files.reject { prefix(_1).end_with? "Test" }

  # add files by included_files

  original_files.each do |ori_f|
    included_files.each do |inc_f|
      if (prefix(ori_f).downcase==inc_f || ori_f.basename.to_s.downcase==inc_f) && !files.include?(ori_f)
        files << ori_f
      end
    end
  end

  files
end

.prefix(path) ⇒ String

pathの-数字より前の文字列を取得

Parameters:

  • path (Pathname)

Returns:

  • (String)


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/uniprop/utils.rb', line 264

def prefix(path)
  path = Pathname.new(path)
  m = basename_no_ext(path).match(/^([\.\-0-9a-zA-Z_ ]+)-([\.0-9a-zA-Z_ ]+)$/)

  if m
    before_hyphen = m[1]
    after_hyphen = m[2]

    if after_hyphen.start_with?(/[0-9]/)
      return before_hyphen
    else
      return m[0]
    end
  else
    return basename_no_ext(path)
  end
end

.prefix_path(path) ⇒ Pathname

pathのbasename部分をprefixのみに変更したPathnameを取得

Parameters:

  • path (Pathname)

Returns:

  • (Pathname)


285
286
287
# File 'lib/uniprop/utils.rb', line 285

def prefix_path(path)
  path.parent + Pathname.new(prefix(path) + path.extname)
end

.recursive_unzip(paths) ⇒ Object

pathsの中に含まれるzipファイルを全て展開。zipファイルの中にzipファイルがある場合には再帰的に展開。

Parameters:

  • paths (Iterable<Pathname>)


343
344
345
346
347
# File 'lib/uniprop/utils.rb', line 343

def recursive_unzip(paths)
  loop do 
    return if !unzip(paths)
  end
end

.unihan_file?(file, unihan_file_names = nil) ⇒ Boolean

Note:

unihan_file_namesでUnihanのファイル名を指定できる。nilの場合、Unihan*のワイルドカードが使用される。

pathがUnihanのファイルかを判定

Parameters:

  • file (Pathname/String)

    ファイルのパスまたはbasename_prefixに相当する文字列

  • unihan_file_names (Array<String>) (defaults to: nil)

Returns:

  • (Boolean)


353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/uniprop/utils.rb', line 353

def unihan_file?(file, unihan_file_names=nil)
  if file.class==Pathname
    file = prefix(file)
  end
  file = UniProp::Alias.canonical(file)

  if unihan_file_names
    unihan_file_names = unihan_file_names.map { UniProp::Alias.canonical(_1) }

    return unihan_file_names.include?(file)
  else
    return file.match?(/unihan.*/)
  end
end

.unzip(paths) ⇒ Boolean

pathsの中に含まれるzipファイルを全て展開

Parameters:

  • paths (Iterable<Pathname>)

Returns:

  • (Boolean)

    1つ以上のファイルが展開された場合true



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/uniprop/utils.rb', line 311

def unzip(paths)
  unzipped_f = false # 返り値用フラグ


  paths.each do |path|
    if ext_no_dot(path).downcase == "zip"
      
      # dir/hoge.zipを展開した場合、dir/unzipped/hoge に保存

      unzipped_cache_path = path.parent+Pathname.new("unzipped")+Pathname.new(prefix(path))

      FileUtils.mkdir(unzipped_cache_path.parent) if !unzipped_cache_path.parent.exist?

      # 既に展開済みファイルが存在する場合、展開処理は行わない

      if unzipped_cache_path.exist?
        break
      else
        FileUtils.mkdir(unzipped_cache_path)
      end

      Zip::File.open(path) do |zip_file|
        zip_file.each do |entry|
          zip_file.extract(entry, unzipped_cache_path+Pathname.new(entry.name))
        end
      end
      unzipped_f = true
    end
  end

  unzipped_f
end