Class: UniPropUtils::DownloaderWrapper

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

Constant Summary collapse

UNICODE_PUBLIC =

UNICODE_PUBLIC = “sw.it.aoyama.ac.jp/2022/sakaida/UCD/”

"https://www.unicode.org/Public/"

Class Method Summary collapse

Class Method Details

.download_unihan(version_name, cache_dir_path, unicode_beta: false, since: true) ⇒ Object

Unihan.zipをダウンロード



111
112
113
# File 'lib/uniprop/utils.rb', line 111

def download_unihan(version_name, cache_dir_path, unicode_beta: false, since: true)
  unicode_basename_download("unihan", version_name, cache_dir_path, unicode_beta: unicode_beta, since: since)
end

.download_version(version_name, cache_dir_path, excluded_extensions, excluded_directories, excluded_files, included_files, unicode_beta: false, since: true) ⇒ Object

version_nameに該当するバージョンのファイルをダウンロードする

Parameters:

  • version_name (String)
  • cache_dir_path (Pathname)
  • excluded_extensions (Array<String>)

    ダウンロードしない拡張子

  • excluded_directories (Array<String>)

    ダウンロードしないディレクトリの名前。excluded_directoriesに名前が含まれるディレクトリより下の階層にあるファイルは、ダウンロード対象から除外される

  • excluded_files (Array<String>)

    ダウンロードしないファイルの名前

  • included_files (Array<String>)

    excluded系引数に除外されている場合でも、included_filesに名前が含まれるファイルはダウンロードされる

  • unicode_beta (Boolean) (defaults to: false)

    ダウンロード対象がベータ版ならtrue



49
50
51
52
53
# File 'lib/uniprop/utils.rb', line 49

def download_version(version_name, cache_dir_path, excluded_extensions, excluded_directories, excluded_files, included_files, unicode_beta: false, since: true)
  file_urls = FileManager.filter_file(files_in_version(version_name), excluded_extensions, excluded_directories, excluded_files, included_files)

  file_urls.each { unicode_download(_1, cache_dir_path, unicode_beta: unicode_beta, since: since) }
end

.files_in(url) ⇒ Array<Pathname>

urlよりも下の階層にあるファイルのURLをPathnameオブジェクトで主t九

Parameters:

  • url (String)

    URLの絶対パスを表す文字列

Returns:

  • (Array<Pathname>)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/uniprop/utils.rb', line 69

def files_in(url)    
  doc = Nokogiri::HTML(URI.open(url))

  files = []
  doc.css('tr td a').each do |a|
    if a.keys.include?("href") && !a['href'].start_with?("/")
      if a['href'].end_with?("/")
        child_dir_path = Pathname.new(url) + Pathname.new(a['href'])

        files.concat(files_in(child_dir_path.to_s))
      else
        files << Pathname.new(url) + Pathname.new(a.content)
      end
    end
  end
  files
end

.files_in_version(version_name) ⇒ Array<Pathname>

version_nameで指定したバージョンに含まれるファイルのパスを取得

Parameters:

  • version_name (String)

Returns:

  • (Array<Pathname>)

    URLのPathnameのArray



58
59
60
61
62
63
64
# File 'lib/uniprop/utils.rb', line 58

def files_in_version(version_name)
  UniProp::Version.parse(version_name)

  version_path = Pathname.new(UNICODE_PUBLIC) + Pathname.new(version_name)

  files_in(version_path.to_s)
end

.find_file_path(basename_prefix, version_name) ⇒ Pathname

prefixがbasename_prefixと一致するファイルを取得

Parameters:

  • basename_prefix (String)
  • version_name (String)

Returns:

  • (Pathname)


91
92
93
94
95
96
97
# File 'lib/uniprop/utils.rb', line 91

def find_file_path(basename_prefix, version_name)
  files_in_version(version_name)
    # 例えば4.1.0にはPropList.txtとPropList.htmlの両方が存在
    # txtとzipのみを検索に使用
    .filter { ["txt", "zip"].include?(FileManager.ext_no_dot(_1)) }
    .find { UniProp::Alias.canonical(FileManager.prefix(_1)) == UniProp::Alias.canonical(basename_prefix) }
end

.get_version_namesArray<String>

現在公開されているバージョンの名前をすべて取得

Returns:

  • (Array<String>)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/uniprop/utils.rb', line 9

def get_version_names
  doc = Nokogiri::HTML(URI.open(UNICODE_PUBLIC))

  version_names = []
  doc.css('tr td a').each do |a|
    begin
      version_name = a.content[..-2]
      UniProp::Version.parse(version_name)
      version_names << version_name
    rescue UniProp::ParseError
    end
  end

  version_names
end

.unicode_basename_download(basename_prefix, version_name, cache_dir_path, unicode_beta: false, since: true) ⇒ Object

basename_prefixとversion_nameでファイルを指定してダウンロード



100
101
102
103
104
105
106
107
108
# File 'lib/uniprop/utils.rb', line 100

def unicode_basename_download(basename_prefix, version_name, cache_dir_path, unicode_beta: false, since: true)
  path = find_file_path(basename_prefix, version_name)
  
  if path
    unicode_download(path, cache_dir_path, unicode_beta: unicode_beta, since: since)
  else
    raise(UniProp::FileNotFoundError, "#{basename_prefix} is not found in #{version_name}")
  end
end

.unicode_download(url, cache_dir_path, unicode_beta: false, since: true) ⇒ Object

Note:

ベータ版ファイルをDownloader::downloadでダウンロードすると、ファイル名が異なる場合に古いファイルが削除されない。その問題を回避するため、ファイル名をprefixのみを使用してダウンロードするメソッド

URLを指定して1つのUnicodeファイルをダウンロード

Parameters:

  • url (Pathname)

    ダウンロードするファイルのURLの絶対パス

  • cache_dir_path (Pathname)

    ダウンロードしたファイルを保存するディレクトリの絶対パス。この下の階層に15.0.0などのバージョン名を表すディレクトリが作成される



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uniprop/utils.rb', line 29

def unicode_download(url, cache_dir_path, unicode_beta: false, since: true)
  relative_url = url.relative_path_from(UNICODE_PUBLIC) # バージョン名より下の階層のパス
  file_cache_path = Pathname.new(cache_dir_path)+relative_url.parent
  
  options = {cache_dir: false}
  if unicode_beta
    options[:unicode_beta] = "YES"
  end
  
  Downloader.download(UNICODE_PUBLIC+relative_url.to_s, FileManager.prefix_path(relative_url).to_s, dir=file_cache_path.to_s, since=since, options=options)
end