Class: UniPropUtils::DownloaderWrapper
- Inherits:
-
Object
- Object
- UniPropUtils::DownloaderWrapper
- 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
-
.download_unihan(version_name, cache_dir_path, unicode_beta: false, since: true) ⇒ Object
Unihan.zipをダウンロード.
-
.download_version(version_name, cache_dir_path, excluded_extensions, excluded_directories, excluded_files, included_files, unicode_beta: false, since: true) ⇒ Object
version_nameに該当するバージョンのファイルをダウンロードする.
-
.files_in(url) ⇒ Array<Pathname>
urlよりも下の階層にあるファイルのURLをPathnameオブジェクトで主t九.
-
.files_in_version(version_name) ⇒ Array<Pathname>
version_nameで指定したバージョンに含まれるファイルのパスを取得.
-
.find_file_path(basename_prefix, version_name) ⇒ Pathname
prefixがbasename_prefixと一致するファイルを取得.
-
.get_version_names ⇒ Array<String>
現在公開されているバージョンの名前をすべて取得.
-
.unicode_basename_download(basename_prefix, version_name, cache_dir_path, unicode_beta: false, since: true) ⇒ Object
basename_prefixとversion_nameでファイルを指定してダウンロード.
-
.unicode_download(url, cache_dir_path, unicode_beta: false, since: true) ⇒ Object
URLを指定して1つのUnicodeファイルをダウンロード.
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に該当するバージョンのファイルをダウンロードする
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九
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で指定したバージョンに含まれるファイルのパスを取得
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と一致するファイルを取得
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_names ⇒ 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ファイルをダウンロード
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 = {cache_dir: false} if unicode_beta [: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, =) end |