Repository navigation
Add RemoteInput.open #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kou
merged 13 commits into
red-data-tools:main
from
tikkss:remote-input-open-without-cache
Sep 26, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5907838
Add RemoteInput.open without cache
tikkss c547b94
Make RemoteInput a class instead of a module
tikkss 2d2a9d2
Remove mode
tikkss f97ed5b
Use *fallback_urls instead of fallback_urls:
tikkss be1293c
Provide encoding options like IO.new
tikkss 0f23e1f
Use the same cache ID for the same URL
tikkss 3d1c22a
Add clear_cache
tikkss 1a9ecd6
test: clear garbage cache
tikkss 4d2ee34
Remove validate_encoding
tikkss 66b48a5
Make local_path private API
tikkss aca2d48
Use path instead of local_path
tikkss a6e5e42
Fix missing rename
tikkss 834f206
Use url_path instead of path
tikkss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,80 @@ | ||
| require_relative "remote_input/downloader" | ||
| require_relative "remote_input/cache-path" | ||
| require_relative "remote_input/downloader" | ||
| require_relative "remote_input/zip-extractor" | ||
|
|
||
| module RemoteInput | ||
| class RemoteInput | ||
| class << self | ||
| def open(...) | ||
| input = new(...) | ||
| if block_given? | ||
| begin | ||
| yield(input) | ||
| ensure | ||
| input.close | ||
| end | ||
| else | ||
| input | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def initialize(url, | ||
| *fallback_urls, | ||
| encoding: nil, | ||
| internal_encoding: nil, | ||
| external_encoding: nil, | ||
| **http_options) | ||
| @url = URI(url) | ||
| @encoding = encoding | ||
| @internal_encoding = internal_encoding | ||
| @external_encoding = external_encoding | ||
| @downloader = Downloader.new(url, *fallback_urls, **http_options) | ||
| @cache_path = nil | ||
| @local_file = nil | ||
| @closed = false | ||
| end | ||
|
|
||
| def read(maxlen=nil, out_string=nil) | ||
| local_file.read(maxlen, out_string) | ||
| end | ||
|
|
||
| def close | ||
| @local_file.close if @local_file and not @local_file.closed? | ||
| @closed = true | ||
| end | ||
|
|
||
| def clear_cache | ||
| cache_path.remove | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def path | ||
| cache_path.base_dir + File.basename(normalize_path) | ||
| end | ||
|
|
||
| def cache_path | ||
| return @cache_path if @cache_path | ||
| dirname = File.dirname(normalize_path).delete_suffix("/") | ||
| cache_id = "#{@url.host}#{dirname}".tr("/", "-") | ||
| @cache_path = CachePath.new(cache_id) | ||
| end | ||
|
|
||
| def normalize_path | ||
| url_path = @url.path | ||
| url_path = "/" if url_path.empty? | ||
| url_path += "data" if url_path.end_with?("/") | ||
| url_path | ||
| end | ||
|
|
||
| def local_file | ||
| raise IOError, "closed stream" if @closed | ||
| return @local_file if @local_file | ||
| @downloader.download(path) | ||
| options = {} | ||
| options[:encoding] = @encoding if @encoding | ||
| options[:internal_encoding] = @internal_encoding if @internal_encoding | ||
| options[:external_encoding] = @external_encoding if @external_encoding | ||
| @local_file = path.open(**options) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| module RemoteInput | ||
| class RemoteInput | ||
| class CachePath | ||
| def initialize(id) | ||
| @id = id | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| module RemoteInput | ||
| class RemoteInput | ||
| class Error < StandardError | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| module RemoteInput | ||
| class RemoteInput | ||
| VERSION = "0.0.0" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| require 'zip' | ||
|
|
||
| module RemoteInput | ||
| class RemoteInput | ||
| class ZipExtractor | ||
| def initialize(path) | ||
| @path = path | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| require_relative "helper" | ||
|
|
||
| class RemoteInputTest < Test::Unit::TestCase | ||
| def test_read_once | ||
| open_input do |input| | ||
| input.send(:path).parent.mkpath | ||
| input.send(:path).write("12") | ||
| assert_equal("12", input.read) | ||
| end | ||
| end | ||
|
|
||
| def test_read_twice | ||
| open_input do |input| | ||
| input.send(:path).parent.mkpath | ||
| input.send(:path).write("12") | ||
| assert_equal("1", input.read(1)) | ||
| assert_equal("2", input.read(1)) | ||
| end | ||
| end | ||
|
|
||
| def test_read_with_encoding | ||
| open_input(encoding: "Windows-31J:UTF-8") do |input| | ||
| input.send(:path).parent.mkpath | ||
| input.send(:path).write("入力", encoding: "Windows-31J:UTF-8") | ||
| assert_equal("入力", input.read) | ||
| end | ||
| end | ||
|
|
||
| def test_read_after_close | ||
| open_input do |input| | ||
| input.close | ||
| assert_raise(IOError.new("closed stream")) do | ||
| input.read | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_open_with_block | ||
| opened_input = nil | ||
| open_input do |input| | ||
| opened_input = input | ||
| end | ||
| assert_raise(IOError.new("closed stream")) do | ||
| opened_input.read | ||
| end | ||
| end | ||
|
|
||
| def test_open_with_block_raised | ||
| opened_input = nil | ||
| assert_raise(RuntimeError.new("error in block")) do | ||
| open_input do |input| | ||
| opened_input = input | ||
| raise "error in block" | ||
| end | ||
| end | ||
| assert_raise(IOError.new("closed stream")) do | ||
| opened_input.read | ||
| end | ||
| end | ||
|
|
||
| data("no path", ["/example.com/data", "https://example.com"]) | ||
| data("root", ["/example.com/data", "https://example.com/"]) | ||
| data("file", ["/example.com/file", "https://example.com/file"]) | ||
| data("query", ["/example.com/file", "https://example.com/file?a=b"]) | ||
| data("directory", ["/example.com-a/data", "https://example.com/a/"]) | ||
| data("nested file", ["/example.com-a/file", "https://example.com/a/file"]) | ||
| data("deeply nested", ["/example.com-a-b/file", "https://example.com/a/b/file"]) | ||
| def test_path(data) | ||
| expected, url = data | ||
| RemoteInput.open(url) do |input| | ||
| assert do | ||
| input.send(:path).to_s.end_with?(expected) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_clear_cache | ||
| open_input do |input| | ||
| input.send(:path).parent.mkpath | ||
| input.send(:path).write("1") | ||
| input.clear_cache | ||
| assert do | ||
| not input.send(:path).parent.exist? | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def open_input(**options) | ||
| input = RemoteInput.open("https://example.com/file", **options) | ||
| begin | ||
| yield(input) | ||
| ensure | ||
| input.close | ||
| input.clear_cache | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.