iostreams 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -22
- data/Rakefile +45 -0
- data/docs/CLAUDE.md +9 -0
- data/docs/config.md +161 -0
- data/docs/copy_files.md +93 -0
- data/docs/extensions.md +125 -0
- data/docs/formats.md +816 -0
- data/docs/index.md +388 -0
- data/docs/path.md +668 -0
- data/docs/pgp.md +436 -0
- data/docs/streams.md +382 -0
- data/docs/tutorial.md +495 -0
- data/docs/upgrading.md +664 -0
- data/lib/io_streams/builder/deprecations.rb +250 -0
- data/lib/io_streams/builder.rb +316 -39
- data/lib/io_streams/bzip2/reader.rb +41 -2
- data/lib/io_streams/bzip2/writer.rb +35 -2
- data/lib/io_streams/deprecation/methods.rb +51 -0
- data/lib/io_streams/deprecation/once.rb +27 -0
- data/lib/io_streams/deprecation/option_names.rb +26 -0
- data/lib/io_streams/deprecation/words.rb +42 -0
- data/lib/io_streams/deprecation.rb +259 -0
- data/lib/io_streams/encode/reader.rb +58 -3
- data/lib/io_streams/encode/writer.rb +23 -2
- data/lib/io_streams/encode.rb +99 -0
- data/lib/io_streams/errors.rb +55 -0
- data/lib/io_streams/gzip/reader.rb +15 -0
- data/lib/io_streams/gzip/writer.rb +11 -2
- data/lib/io_streams/io_streams.rb +468 -28
- data/lib/io_streams/line/reader.rb +9 -3
- data/lib/io_streams/line/utf8_reader.rb +23 -0
- data/lib/io_streams/path.rb +227 -28
- data/lib/io_streams/paths/file.rb +115 -6
- data/lib/io_streams/paths/http.rb +92 -5
- data/lib/io_streams/paths/matcher.rb +6 -3
- data/lib/io_streams/paths/s3.rb +242 -20
- data/lib/io_streams/paths/sftp/net_ssh.rb +104 -0
- data/lib/io_streams/paths/sftp.rb +141 -57
- data/lib/io_streams/paths.rb +23 -0
- data/lib/io_streams/pgp/reader.rb +60 -2
- data/lib/io_streams/pgp/writer.rb +42 -6
- data/lib/io_streams/pgp.rb +92 -21
- data/lib/io_streams/reader.rb +54 -1
- data/lib/io_streams/record/reader.rb +71 -10
- data/lib/io_streams/record/writer.rb +17 -10
- data/lib/io_streams/row/header_row.rb +21 -0
- data/lib/io_streams/row/reader.rb +52 -15
- data/lib/io_streams/row/writer.rb +64 -13
- data/lib/io_streams/schema/columns.rb +456 -0
- data/lib/io_streams/schema/constraints.rb +99 -0
- data/lib/io_streams/schema/field.rb +967 -0
- data/lib/io_streams/schema/json_number.rb +56 -0
- data/lib/io_streams/schema/keys.rb +78 -0
- data/lib/io_streams/schema/pattern.rb +257 -0
- data/lib/io_streams/schema/source.rb +31 -0
- data/lib/io_streams/schema.rb +616 -0
- data/lib/io_streams/stream.rb +843 -69
- data/lib/io_streams/symmetric_encryption/reader.rb +12 -0
- data/lib/io_streams/symmetric_encryption/writer.rb +4 -0
- data/lib/io_streams/tabular/header.rb +60 -4
- data/lib/io_streams/tabular/header_mapping.rb +332 -0
- data/lib/io_streams/tabular/mapping.rb +165 -0
- data/lib/io_streams/tabular/parser/array.rb +5 -0
- data/lib/io_streams/tabular/parser/base.rb +34 -0
- data/lib/io_streams/tabular/parser/csv.rb +5 -0
- data/lib/io_streams/tabular/parser/fixed.rb +200 -1
- data/lib/io_streams/tabular/parser/fixed_fields.rb +160 -0
- data/lib/io_streams/tabular/parser/hash.rb +10 -0
- data/lib/io_streams/tabular/parser/json.rb +17 -0
- data/lib/io_streams/tabular/parser/json_fields.rb +132 -0
- data/lib/io_streams/tabular/parser/psv.rb +6 -2
- data/lib/io_streams/tabular/schema_mapping.rb +361 -0
- data/lib/io_streams/tabular.rb +344 -30
- data/lib/io_streams/utils.rb +467 -1
- data/lib/io_streams/version.rb +1 -1
- data/lib/io_streams/writer.rb +37 -1
- data/lib/io_streams/xlsx/cells.rb +127 -0
- data/lib/io_streams/xlsx/reader.rb +61 -1
- data/lib/io_streams/xlsx/schema_reader.rb +90 -0
- data/lib/io_streams/xlsx/v3_writer.rb +16 -0
- data/lib/io_streams/zip/reader.rb +11 -0
- data/lib/io_streams/zip/writer.rb +28 -2
- data/lib/iostreams.rb +21 -17
- metadata +50 -7
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IOStreams
|
|
4
|
+
module Deprecation
|
|
5
|
+
# The words of the message of a deprecation warning that depend on how many things it is about, such as the options
|
|
6
|
+
# that are deprecated, so that each message says "is" and "it" of one, and "are" and "them" of more, see .warn.
|
|
7
|
+
class Words
|
|
8
|
+
# Parameters:
|
|
9
|
+
# items: [Array]
|
|
10
|
+
# The things that the message is about, such as the names of options.
|
|
11
|
+
def initialize(items)
|
|
12
|
+
@items = items
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Returns [String] the items as the keywords that a caller supplies them as, such as `compress:, level:`, and an
|
|
16
|
+
# item that is not a Symbol, which is not a keyword, as it is written, such as `"acl"`.
|
|
17
|
+
def keywords
|
|
18
|
+
@items.map { |item| item.is_a?(Symbol) ? "#{item}:" : item.inspect }.join(", ")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Returns [true|false] whether the message is about one item.
|
|
22
|
+
def one?
|
|
23
|
+
@items.size == 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Returns [String] "are", or "is" of one item.
|
|
27
|
+
def are
|
|
28
|
+
one? ? "is" : "are"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Returns [String] "them", or "it" of one item.
|
|
32
|
+
def them
|
|
33
|
+
one? ? "it" : "them"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Returns [String] "they are", or "it is" of one item.
|
|
37
|
+
def they_are
|
|
38
|
+
one? ? "it is" : "they are"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rbconfig"
|
|
4
|
+
|
|
5
|
+
module IOStreams
|
|
6
|
+
# Reports the use of a deprecated method or option as a Ruby deprecation warning, see `Kernel#warn`, so that Ruby
|
|
7
|
+
# decides whether and where it is reported, as it does for its own deprecations, rather than a setting of IOStreams:
|
|
8
|
+
# - Only while deprecation warnings are on, see `Warning[:deprecated]`. They are off by default, so production stays
|
|
9
|
+
# quiet, and on in tests run by Minitest or test-unit, or with `ruby -W:deprecated` or `RUBYOPT=-W:deprecated`.
|
|
10
|
+
# - Written to `$stderr`, unless the application routes them elsewhere, such as to its log, or raises them in its
|
|
11
|
+
# tests, by overriding `Warning.warn`.
|
|
12
|
+
#
|
|
13
|
+
# Each warning names the line of the application that used what is deprecated, rather than a line of IOStreams, of
|
|
14
|
+
# Ruby itself, or of a gem that IOStreams called with a block, see .application_location.
|
|
15
|
+
module Deprecation
|
|
16
|
+
autoload :Methods, "io_streams/deprecation/methods"
|
|
17
|
+
autoload :Once, "io_streams/deprecation/once"
|
|
18
|
+
autoload :OptionNames, "io_streams/deprecation/option_names"
|
|
19
|
+
autoload :Words, "io_streams/deprecation/words"
|
|
20
|
+
|
|
21
|
+
# The directory of the files of this library, whose lines are skipped when finding the line that used it.
|
|
22
|
+
LIB_DIR = "#{__dir__}/".freeze
|
|
23
|
+
private_constant :LIB_DIR
|
|
24
|
+
|
|
25
|
+
# The file that loads this library, whose lines are skipped too. Only the files of this library are skipped, rather
|
|
26
|
+
# than every file of the directory that holds them, which can hold other libraries, such as `site_ruby`, or the
|
|
27
|
+
# `lib` directory of an application that it is copied into.
|
|
28
|
+
LIB_FILE = File.expand_path("../iostreams.rb", __dir__).freeze
|
|
29
|
+
private_constant :LIB_FILE
|
|
30
|
+
|
|
31
|
+
# The text that each warning starts with, after the location of the line that used what is deprecated.
|
|
32
|
+
PREFIX = "IOStreams: "
|
|
33
|
+
private_constant :PREFIX
|
|
34
|
+
|
|
35
|
+
# A warning reported by .warn, as `Kernel#warn` passes it to `Warning.warn`, see .warning?.
|
|
36
|
+
WARNING = /\A(?:.+:\d+: warning: )?#{Regexp.escape(PREFIX)}/
|
|
37
|
+
private_constant :WARNING
|
|
38
|
+
|
|
39
|
+
# The start of the paths of the core methods that Ruby writes in Ruby, such as `Kernel#tap`, see .core?.
|
|
40
|
+
CORE_PATH = "<internal:"
|
|
41
|
+
private_constant :CORE_PATH
|
|
42
|
+
|
|
43
|
+
# The start of the paths of the standard library of JRuby, when it is loaded from its jar.
|
|
44
|
+
JAR_PATH = "uri:classloader:"
|
|
45
|
+
private_constant :JAR_PATH
|
|
46
|
+
|
|
47
|
+
# The files of this module, whose methods find the line that called a deprecated method, see .calling_location.
|
|
48
|
+
OWN_FILES = ["#{__dir__}/deprecation.rb", "#{__dir__}/deprecation/methods.rb"].freeze
|
|
49
|
+
private_constant :OWN_FILES
|
|
50
|
+
|
|
51
|
+
# The end of the names of the files of the standard library of Ruby that define its delegators: `delegate.rb`, which
|
|
52
|
+
# defines `SimpleDelegator` and `DelegateClass`, and `forwardable.rb`, which defines `Forwardable`, see .delegator?.
|
|
53
|
+
DELEGATOR_FILES = %w[/delegate.rb /forwardable.rb].freeze
|
|
54
|
+
private_constant :DELEGATOR_FILES
|
|
55
|
+
|
|
56
|
+
# The directory of the standard library of Ruby.
|
|
57
|
+
RUBY_LIB_DIRS = [RbConfig::CONFIG["rubylibdir"]].compact.map { |dir| "#{dir}/" }.freeze
|
|
58
|
+
private_constant :RUBY_LIB_DIRS
|
|
59
|
+
|
|
60
|
+
# Reports that the application used something deprecated, unless deprecation warnings are off, with the message,
|
|
61
|
+
# which says what is deprecated and what to use instead, after "IOStreams: ", at the line of the application that
|
|
62
|
+
# used it, see .application_location, in the same form as `Kernel#warn` names the line of its caller, such as
|
|
63
|
+
# `app/jobs/export_job.rb:12: warning: IOStreams: stream(:none) is deprecated, ...`, or without a line when the
|
|
64
|
+
# call stack has none outside this library.
|
|
65
|
+
#
|
|
66
|
+
# Returns nil.
|
|
67
|
+
def self.warn(message)
|
|
68
|
+
return unless on?
|
|
69
|
+
|
|
70
|
+
report(message, application_location)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Reports, as .warn does, that the application uses something deprecated while this library is loaded, such as a
|
|
74
|
+
# version of Ruby that IOStreams 3.0 does not support, at the line of the application that loaded it, see
|
|
75
|
+
# .loading_location, rather than at a line of a gem that requires it for the application, such as Bundler.
|
|
76
|
+
#
|
|
77
|
+
# Returns nil.
|
|
78
|
+
def self.warn_on_load(message)
|
|
79
|
+
return unless on?
|
|
80
|
+
|
|
81
|
+
report(message, loading_location)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Reports, as .warn does, that the application called a deprecated method of an object that IOStreams handed it,
|
|
85
|
+
# such as the stream that a reader block receives, which calls this from that method, see
|
|
86
|
+
# `IOStreams::Deprecation::Methods`, at the line that called the method, see .calling_location, when it is the
|
|
87
|
+
# application's own: not a line of IOStreams, which calls the methods of the object for itself, of a gem, such as the
|
|
88
|
+
# gem whose object it is, which calls its own methods, or of a library that the application handed the object to,
|
|
89
|
+
# including the standard library of Ruby, see .standard_library?. So a method that the application hands IOStreams
|
|
90
|
+
# as the block itself, such as `reader(&:rewind)`, is not reported, since IOStreams calls it, while one that it hands
|
|
91
|
+
# a core method of Ruby, such as `io.then(&:rewind)`, or calls through a delegator of Ruby that wraps the object,
|
|
92
|
+
# such as `SimpleDelegator.new(io).rewind`, is, since they call it for the application.
|
|
93
|
+
#
|
|
94
|
+
# Returns nil.
|
|
95
|
+
def self.warn_call(message)
|
|
96
|
+
return unless Warning[:deprecated]
|
|
97
|
+
|
|
98
|
+
location = calling_location
|
|
99
|
+
return if location.nil? || library?(location) || gem?(location, installed_gem_dirs) || standard_library?(location)
|
|
100
|
+
|
|
101
|
+
report(message, location)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Returns [true|false] whether the message, as `Kernel#warn` passes it to `Warning.warn`, is a warning reported by
|
|
105
|
+
# .warn, so that an application, or its tests, can tell the deprecation warnings of IOStreams from the others.
|
|
106
|
+
def self.warning?(message)
|
|
107
|
+
WARNING.match?(message)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Returns [true|false] whether deprecation warnings are on, see `Warning[:deprecated]`, so that .warn reports them,
|
|
111
|
+
# for a caller to find out whether something is deprecated only while they are, where finding it out costs more
|
|
112
|
+
# than the call to .warn.
|
|
113
|
+
def self.on?
|
|
114
|
+
Warning[:deprecated]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Returns [Thread::Backtrace::Location|nil] the line of the application that used this library, or nil when the
|
|
118
|
+
# call stack has none outside this library. It is the first line of the call stack outside this library, other
|
|
119
|
+
# than:
|
|
120
|
+
# - A line of Ruby itself, see .ruby?, which runs on behalf of its caller, such as `Kernel#tap`, or `Open3.popen2e`
|
|
121
|
+
# that this library calls with a block, as Ruby names the caller of its own core methods.
|
|
122
|
+
# - A line of a gem that this library called with a block, such as `Net::SFTP.start`, which has a line of this
|
|
123
|
+
# library further out in the call stack. A line of a gem that called this library, such as one of Rocket Job, is
|
|
124
|
+
# the one returned, since it is the gem that used it.
|
|
125
|
+
#
|
|
126
|
+
# The call stack is walked outward from the caller, until the line of the application is found, rather than
|
|
127
|
+
# creating all of it.
|
|
128
|
+
def self.application_location
|
|
129
|
+
gem_dirs = installed_gem_dirs
|
|
130
|
+
candidate = nil
|
|
131
|
+
Thread.each_caller_location do |location|
|
|
132
|
+
if library?(location)
|
|
133
|
+
candidate = nil
|
|
134
|
+
elsif gem?(location, gem_dirs)
|
|
135
|
+
candidate ||= location
|
|
136
|
+
elsif !ruby?(location)
|
|
137
|
+
return candidate || location
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
candidate
|
|
141
|
+
end
|
|
142
|
+
private_class_method :application_location
|
|
143
|
+
|
|
144
|
+
# Returns [Thread::Backtrace::Location|nil] the line of the application that loaded this library, such as its
|
|
145
|
+
# `require "iostreams"`, or its `Bundler.require`: the first line of the call stack outside this library, Ruby
|
|
146
|
+
# itself, see .ruby?, and the installed gems, since a gem that loads this library, such as Bundler, Bootsnap, or a gem
|
|
147
|
+
# that requires it, loads it for the application. Otherwise the first line of an installed gem outside this library,
|
|
148
|
+
# for an application that is itself an installed gem, or nil when the call stack has neither.
|
|
149
|
+
def self.loading_location
|
|
150
|
+
gem_dirs = installed_gem_dirs
|
|
151
|
+
candidate = nil
|
|
152
|
+
Thread.each_caller_location do |location|
|
|
153
|
+
next if library?(location) || ruby?(location)
|
|
154
|
+
return location unless gem?(location, gem_dirs)
|
|
155
|
+
|
|
156
|
+
candidate ||= location
|
|
157
|
+
end
|
|
158
|
+
candidate
|
|
159
|
+
end
|
|
160
|
+
private_class_method :loading_location
|
|
161
|
+
|
|
162
|
+
# Returns [Thread::Backtrace::Location|nil] the line that called the deprecated method that called .warn_call: the
|
|
163
|
+
# first line of the call stack outside the files of this module, other than a line of a core method, see .core?, or
|
|
164
|
+
# of a delegator of Ruby, see .delegator?, which calls the method for its caller, such as `Kernel#then` in
|
|
165
|
+
# `io.then(&:rewind)`, or the `SimpleDelegator` in `SimpleDelegator.new(io).rewind`. Ruby names the line that
|
|
166
|
+
# called a core method written in C in its place, and CRuby 4.0 does the same for one written in Ruby, so skipping
|
|
167
|
+
# the lines that earlier versions name gives the same line on each version of Ruby.
|
|
168
|
+
def self.calling_location
|
|
169
|
+
Thread.each_caller_location do |location|
|
|
170
|
+
next if OWN_FILES.include?(location.absolute_path || location.path) || core?(location) || delegator?(location)
|
|
171
|
+
|
|
172
|
+
return location
|
|
173
|
+
end
|
|
174
|
+
nil
|
|
175
|
+
end
|
|
176
|
+
private_class_method :calling_location
|
|
177
|
+
|
|
178
|
+
# Writes the message as a deprecation warning, see .warn, at the supplied line, or without a line when it is nil.
|
|
179
|
+
def self.report(message, location)
|
|
180
|
+
text = "#{PREFIX}#{message}"
|
|
181
|
+
text = "#{location.path}:#{location.lineno}: warning: #{text}" if location
|
|
182
|
+
Kernel.warn(text, category: :deprecated)
|
|
183
|
+
nil
|
|
184
|
+
end
|
|
185
|
+
private_class_method :report
|
|
186
|
+
|
|
187
|
+
# Returns [true|false] whether the line of the call stack is in a file of this library. Code run with `eval` has
|
|
188
|
+
# only the file name that it was given.
|
|
189
|
+
def self.library?(location)
|
|
190
|
+
file = location.absolute_path || location.path
|
|
191
|
+
file.start_with?(LIB_DIR) || file == LIB_FILE
|
|
192
|
+
end
|
|
193
|
+
private_class_method :library?
|
|
194
|
+
|
|
195
|
+
# Returns [true|false] whether the line of the call stack is in the code of Ruby itself: a core method, see .core?,
|
|
196
|
+
# or a file of its standard library, see .standard_library?.
|
|
197
|
+
def self.ruby?(location)
|
|
198
|
+
core?(location) || standard_library?(location)
|
|
199
|
+
end
|
|
200
|
+
private_class_method :ruby?
|
|
201
|
+
|
|
202
|
+
# Returns [true|false] whether the line of the call stack is in a core method of Ruby that is not written in C: one
|
|
203
|
+
# written in Ruby, such as `Kernel#tap`, in a file whose name starts with `<internal:`, which CRuby before 4.0 and
|
|
204
|
+
# JRuby name in the call stack, or one of JRuby written in Java. Ruby names the line that called a core method
|
|
205
|
+
# written in C in its place, as CRuby 4.0 does for one written in Ruby.
|
|
206
|
+
def self.core?(location)
|
|
207
|
+
path = location.path
|
|
208
|
+
path.start_with?(CORE_PATH) || path.end_with?(".java")
|
|
209
|
+
end
|
|
210
|
+
private_class_method :core?
|
|
211
|
+
|
|
212
|
+
# Returns [true|false] whether the line of the call stack is in a file of the standard library of Ruby, see
|
|
213
|
+
# RUBY_LIB_DIRS, or of that of JRuby, when it is loaded from its jar.
|
|
214
|
+
def self.standard_library?(location)
|
|
215
|
+
location.path.start_with?(JAR_PATH) || in_dirs?(location, RUBY_LIB_DIRS)
|
|
216
|
+
end
|
|
217
|
+
private_class_method :standard_library?
|
|
218
|
+
|
|
219
|
+
# Returns [true|false] whether the line of the call stack is in a delegator of the standard library of Ruby, see
|
|
220
|
+
# DELEGATOR_FILES, which calls the method of the object that it wraps for its caller, as a core method does: the
|
|
221
|
+
# `#method_missing` of a `SimpleDelegator`, or a method that `DelegateClass` or `Forwardable` defines. The other
|
|
222
|
+
# files of the standard library call the methods of an object for themselves, such as a library that the
|
|
223
|
+
# application hands the object to, see .warn_call.
|
|
224
|
+
def self.delegator?(location)
|
|
225
|
+
location.path.end_with?(*DELEGATOR_FILES) && standard_library?(location)
|
|
226
|
+
end
|
|
227
|
+
private_class_method :delegator?
|
|
228
|
+
|
|
229
|
+
# Returns [true|false] whether the line of the call stack is in a file of a gem, in one of the directories of the
|
|
230
|
+
# installed gems.
|
|
231
|
+
def self.gem?(location, gem_dirs)
|
|
232
|
+
in_dirs?(location, gem_dirs)
|
|
233
|
+
end
|
|
234
|
+
private_class_method :gem?
|
|
235
|
+
|
|
236
|
+
# Returns [true|false] whether the file of the line of the call stack is in one of the directories, by the path
|
|
237
|
+
# that it was loaded from, or its absolute path, which differs when the path holds a link.
|
|
238
|
+
def self.in_dirs?(location, dirs)
|
|
239
|
+
location.path.start_with?(*dirs) || location.absolute_path&.start_with?(*dirs) || false
|
|
240
|
+
end
|
|
241
|
+
private_class_method :in_dirs?
|
|
242
|
+
|
|
243
|
+
# Returns [Array<String>] the directories of the installed gems, see `Gem.path`, each ending with "/". They are kept
|
|
244
|
+
# for as long as `Gem.path` returns the same Array, since Bundler replaces it when it changes them, so that a
|
|
245
|
+
# deprecated method that a gem calls for each line, see .warn_call, builds them once, rather than on each call.
|
|
246
|
+
def self.installed_gem_dirs
|
|
247
|
+
return [] unless defined?(::Gem)
|
|
248
|
+
|
|
249
|
+
path = ::Gem.path
|
|
250
|
+
cached = @installed_gem_dirs
|
|
251
|
+
return cached.last if cached&.first.equal?(path)
|
|
252
|
+
|
|
253
|
+
dirs = path.map { |dir| dir.end_with?("/") ? dir : "#{dir}/" }.freeze
|
|
254
|
+
@installed_gem_dirs = [path, dirs].freeze
|
|
255
|
+
dirs
|
|
256
|
+
end
|
|
257
|
+
private_class_method :installed_gem_dirs
|
|
258
|
+
end
|
|
259
|
+
end
|
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
module Encode
|
|
3
3
|
class Reader < IOStreams::Reader
|
|
4
|
+
def self.option_names
|
|
5
|
+
%i[encoding cleaner replace]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# The methods that the stream that v3.0 supplies to a reader block for the encoding answers: those of an IO that
|
|
9
|
+
# read, and #input_stream, see .v3_stream_methods.
|
|
10
|
+
V3_METHODS = [*V3_READ_METHODS, :input_stream].freeze
|
|
11
|
+
private_constant :V3_METHODS
|
|
12
|
+
|
|
13
|
+
# Returns [Array<Symbol>] the methods that the stream that v3.0 supplies to a reader block for the encoding answers,
|
|
14
|
+
# in place of this reader, see `IOStreams::Reader.v3_stream_methods`. It has no #encoding or #cleaner, which the
|
|
15
|
+
# settings of the path replace, see .v3_stream_advice.
|
|
16
|
+
def self.v3_stream_methods(_options)
|
|
17
|
+
V3_METHODS
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Returns [Hash<Symbol:String>] what replaces #encoding and #cleaner, see `IOStreams::Reader.v3_stream_advice`.
|
|
21
|
+
def self.v3_stream_advice
|
|
22
|
+
V3_STREAM_ADVICE
|
|
23
|
+
end
|
|
24
|
+
|
|
4
25
|
attr_reader :encoding, :cleaner
|
|
5
26
|
|
|
6
27
|
NOT_PRINTABLE = /[^[:print:]|\r\n]/
|
|
@@ -56,7 +77,11 @@ module IOStreams
|
|
|
56
77
|
|
|
57
78
|
# Returns [String] data returned from the input stream.
|
|
58
79
|
# Returns [nil] if end of file and no further data was read.
|
|
59
|
-
|
|
80
|
+
#
|
|
81
|
+
# As IO#read does, the data is read into the supplied buffer, which is returned, and which is empty once the end
|
|
82
|
+
# of the file was reached, so that `IO.copy_stream`, which supplies one, can read it, such as for a copy without a
|
|
83
|
+
# mode from a stream with an encoding.
|
|
84
|
+
def read(size = nil, outbuf = nil)
|
|
60
85
|
block =
|
|
61
86
|
if @read_cache_buffer
|
|
62
87
|
begin
|
|
@@ -71,11 +96,18 @@ module IOStreams
|
|
|
71
96
|
end
|
|
72
97
|
|
|
73
98
|
# EOF reached?
|
|
74
|
-
|
|
99
|
+
unless block
|
|
100
|
+
outbuf&.clear
|
|
101
|
+
return
|
|
102
|
+
end
|
|
75
103
|
|
|
104
|
+
block = text(block, size) if text_bytes?(block)
|
|
76
105
|
block = block.encode(@encoding, **@encoding_options) unless block.encoding == @encoding
|
|
77
106
|
block = @cleaner.call(block, @replace) if @cleaner
|
|
78
|
-
block
|
|
107
|
+
return outbuf.replace(block) if outbuf
|
|
108
|
+
|
|
109
|
+
# A copy of the buffer that it reads into, which the next read writes over, so that the caller keeps the data.
|
|
110
|
+
block.equal?(@read_cache_buffer) ? block.dup : block
|
|
79
111
|
end
|
|
80
112
|
|
|
81
113
|
def self.extract_cleaner(cleaner)
|
|
@@ -91,6 +123,29 @@ module IOStreams
|
|
|
91
123
|
cleaner
|
|
92
124
|
end
|
|
93
125
|
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
# Returns [true|false] whether the binary data that a read returned can hold the bytes of text in the encoding,
|
|
130
|
+
# rather than text to convert into it, see #text. Without `replace:` it reads into a buffer of the encoding, whose
|
|
131
|
+
# bytes are then the text, as `IO#read` keeps the encoding of the buffer that it is supplied for a read of a length.
|
|
132
|
+
# A stream that returns the bytes as binary instead, since its `#read` takes no buffer, as `Zlib::GzipReader` on
|
|
133
|
+
# JRuby, or makes the buffer binary, as `Bzip2::FFI::Reader` does, holds the same text, as does a read of the whole
|
|
134
|
+
# of the data, which `IO#read` returns as binary, and converting it from binary would reject each character that is
|
|
135
|
+
# not ASCII. With `replace:` it is converted from binary, replacing each character that is not ASCII, as before.
|
|
136
|
+
def text_bytes?(block)
|
|
137
|
+
@replace.nil? && block.encoding == Encoding::BINARY
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Returns [String] the bytes of the binary data as text in the encoding, in a String of its own, rather than the
|
|
141
|
+
# String that the input stream returned, which it can still hold, such as a buffer that it reuses. Those of a read
|
|
142
|
+
# of a length are taken as they are, as `IO#read` reads them into a buffer of the encoding, since a character can
|
|
143
|
+
# be split between two reads. The whole of the data is taken only when it is valid in the encoding; otherwise the
|
|
144
|
+
# data itself is returned, which is converted from binary, raising for each character that is not ASCII, as before.
|
|
145
|
+
def text(block, size)
|
|
146
|
+
text = ::String.new(block, encoding: @encoding)
|
|
147
|
+
size.nil? && !text.valid_encoding? ? block : text
|
|
148
|
+
end
|
|
94
149
|
end
|
|
95
150
|
end
|
|
96
151
|
end
|
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
module Encode
|
|
3
3
|
class Writer < IOStreams::Writer
|
|
4
|
+
def self.option_names
|
|
5
|
+
%i[encoding cleaner replace]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# The methods that the stream that v3.0 supplies to a writer block for the encoding answers: those of an IO that
|
|
9
|
+
# write, #finish and #output_stream, see .v3_stream_methods.
|
|
10
|
+
V3_METHODS = [*V3_WRITE_METHODS, :finish, :output_stream].freeze
|
|
11
|
+
private_constant :V3_METHODS
|
|
12
|
+
|
|
13
|
+
# Returns [Array<Symbol>] the methods that the stream that v3.0 supplies to a writer block for the encoding answers,
|
|
14
|
+
# in place of this writer, see `IOStreams::Writer.v3_stream_methods`. It has no #encoding or #cleaner, which the
|
|
15
|
+
# settings of the path replace, see .v3_stream_advice.
|
|
16
|
+
def self.v3_stream_methods(_options)
|
|
17
|
+
V3_METHODS
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Returns [Hash<Symbol:String>] what replaces #encoding and #cleaner, see `IOStreams::Reader.v3_stream_advice`.
|
|
21
|
+
def self.v3_stream_advice
|
|
22
|
+
V3_STREAM_ADVICE
|
|
23
|
+
end
|
|
24
|
+
|
|
4
25
|
attr_reader :encoding, :cleaner
|
|
5
26
|
|
|
6
27
|
# Write a line at a time to a file or stream
|
|
@@ -46,7 +67,7 @@ module IOStreams
|
|
|
46
67
|
# Write a line to the output stream
|
|
47
68
|
#
|
|
48
69
|
# Example:
|
|
49
|
-
# IOStreams.path('a.txt').
|
|
70
|
+
# IOStreams.path('a.txt').encoding('UTF-8').writer do |stream|
|
|
50
71
|
# stream << 'first line' << 'second line'
|
|
51
72
|
# end
|
|
52
73
|
def <<(record)
|
|
@@ -58,7 +79,7 @@ module IOStreams
|
|
|
58
79
|
# Returns [Integer] the number of bytes written.
|
|
59
80
|
#
|
|
60
81
|
# Example:
|
|
61
|
-
# IOStreams.path('a.txt').
|
|
82
|
+
# IOStreams.path('a.txt').encoding('UTF-8').writer do |stream|
|
|
62
83
|
# count = stream.write('first line')
|
|
63
84
|
# puts "Wrote #{count} bytes to the output file"
|
|
64
85
|
# end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IOStreams
|
|
4
|
+
# Converts the text that the application reads or writes to an encoding, and optionally cleanses it, see
|
|
5
|
+
# `IOStreams::Encode::Reader` and `IOStreams::Encode::Writer`.
|
|
6
|
+
module Encode
|
|
7
|
+
autoload :Reader, "io_streams/encode/reader"
|
|
8
|
+
autoload :Writer, "io_streams/encode/writer"
|
|
9
|
+
|
|
10
|
+
# What the application uses in place of the #encoding and #cleaner of the reader and writer of the encode stream,
|
|
11
|
+
# which v3.0 removes: the settings of the path, which return what was set in both versions, see
|
|
12
|
+
# `IOStreams::Reader.v3_stream_advice`.
|
|
13
|
+
V3_STREAM_ADVICE = {
|
|
14
|
+
encoding: "use the #encoding of the path instead, which returns the encoding that was set, such as \"UTF-8\", or " \
|
|
15
|
+
"Encoding.find(path.encoding) for its Encoding",
|
|
16
|
+
cleaner: "use path.setting(:encode)[:cleaner] instead, which returns the cleaner that was set, such as :printable"
|
|
17
|
+
}.freeze
|
|
18
|
+
private_constant :V3_STREAM_ADVICE
|
|
19
|
+
|
|
20
|
+
# Returns [Array<Symbol>] the names of the options of the encode stream, which its reader and writer both take.
|
|
21
|
+
def self.option_names
|
|
22
|
+
Reader.option_names | Writer.option_names
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Raises ArgumentError for the options of the encode stream that its reader or writer would raise for once the text
|
|
26
|
+
# is read or written: an option that it does not take, an encoding that Ruby does not know, or that is neither a
|
|
27
|
+
# String nor an Encoding, one that Ruby cannot convert text to, see .check_convertible, a `replace` that is not a
|
|
28
|
+
# String, or a cleaner that is not the name of a built-in rule. Also for a cleaner that is neither a Symbol nor a
|
|
29
|
+
# Proc, which the reader and writer ignore, so that the text would not be cleansed.
|
|
30
|
+
#
|
|
31
|
+
# So that `IOStreams::Builder#encoding` raises when the options are set, rather than once a writer has replaced the
|
|
32
|
+
# file that it writes.
|
|
33
|
+
def self.check(**options)
|
|
34
|
+
unknown = options.keys - option_names
|
|
35
|
+
if unknown.any?
|
|
36
|
+
raise(ArgumentError, "Unknown encoding #{unknown.size == 1 ? 'option' : 'options'} #{list(unknown)}. " \
|
|
37
|
+
"Valid options: #{list(option_names)}.")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
check_encoding(options[:encoding]) if options.key?(:encoding)
|
|
41
|
+
check_replace(options[:replace])
|
|
42
|
+
check_cleaner(options[:cleaner])
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.check_encoding(encoding)
|
|
47
|
+
unless encoding.is_a?(String) || encoding.is_a?(Encoding)
|
|
48
|
+
raise(ArgumentError, "Invalid encoding #{encoding.inspect}: supply the name of an encoding, such as " \
|
|
49
|
+
"\"UTF-8\" or \"Windows-1252\", or an Encoding")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
check_convertible(encoding, encoding.is_a?(String) ? Encoding.find(encoding) : encoding)
|
|
53
|
+
end
|
|
54
|
+
private_class_method :check_encoding
|
|
55
|
+
|
|
56
|
+
# Raises ArgumentError for an encoding that Ruby has no converter for, such as "UTF-7", so that it raises when it is
|
|
57
|
+
# set, rather than Encoding::ConverterNotFoundError once the text is read or written. The text that is read and
|
|
58
|
+
# written is converted to the encoding, such as from the UTF-8 that the application writes. An ASCII compatible
|
|
59
|
+
# encoding needs no converter for ASCII text, so the text of one that Ruby cannot convert, such as "Windows-1258",
|
|
60
|
+
# is still read and written as it is.
|
|
61
|
+
def self.check_convertible(option, encoding)
|
|
62
|
+
return if encoding.ascii_compatible? || converter?(Encoding::UTF_8, encoding)
|
|
63
|
+
|
|
64
|
+
raise(ArgumentError, "Invalid encoding #{option.inspect}: Ruby cannot convert text to or from #{encoding}")
|
|
65
|
+
end
|
|
66
|
+
private_class_method :check_convertible
|
|
67
|
+
|
|
68
|
+
# Returns [true|false] whether Ruby has a converter of text from the source encoding to the destination encoding.
|
|
69
|
+
def self.converter?(source, destination)
|
|
70
|
+
Encoding::Converter.new(source, destination)
|
|
71
|
+
true
|
|
72
|
+
rescue Encoding::ConverterNotFoundError
|
|
73
|
+
false
|
|
74
|
+
end
|
|
75
|
+
private_class_method :converter?
|
|
76
|
+
|
|
77
|
+
def self.check_replace(replace)
|
|
78
|
+
return if replace.nil? || replace.is_a?(String)
|
|
79
|
+
|
|
80
|
+
raise(ArgumentError, "Invalid replace #{replace.inspect}: supply the String that replaces each invalid character")
|
|
81
|
+
end
|
|
82
|
+
private_class_method :check_replace
|
|
83
|
+
|
|
84
|
+
def self.check_cleaner(cleaner)
|
|
85
|
+
unless cleaner.nil? || cleaner.is_a?(Symbol) || cleaner.is_a?(Proc)
|
|
86
|
+
raise(ArgumentError,
|
|
87
|
+
"Invalid cleaner #{cleaner.inspect}: supply the name of a built-in rule, such as :printable, or a Proc")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
Reader.extract_cleaner(cleaner)
|
|
91
|
+
end
|
|
92
|
+
private_class_method :check_cleaner
|
|
93
|
+
|
|
94
|
+
def self.list(names)
|
|
95
|
+
names.map(&:inspect).join(", ")
|
|
96
|
+
end
|
|
97
|
+
private_class_method :list
|
|
98
|
+
end
|
|
99
|
+
end
|
data/lib/io_streams/errors.rb
CHANGED
|
@@ -18,6 +18,10 @@ module IOStreams
|
|
|
18
18
|
class CommunicationsFailure < Error
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
# When a path is not within any of the allowed paths, see `IOStreams.add_allowed_path`.
|
|
22
|
+
class AccessDenied < Error
|
|
23
|
+
end
|
|
24
|
+
|
|
21
25
|
# When the specified delimiter is not found in the supplied stream / file
|
|
22
26
|
class DelimiterNotFound < Error
|
|
23
27
|
end
|
|
@@ -29,6 +33,57 @@ module IOStreams
|
|
|
29
33
|
class ValueTooLong < Error
|
|
30
34
|
end
|
|
31
35
|
|
|
36
|
+
# A value that is not one of the type of its field in the schema, such as `"abc"` for an integer,
|
|
37
|
+
# see `IOStreams::Stream#schema`.
|
|
38
|
+
class InvalidValue < Error
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Text that is not valid in the encoding that it is read or written in, such as bytes that are not valid UTF-8 in
|
|
42
|
+
# a file read with a schema, see `IOStreams::Stream#schema`.
|
|
43
|
+
#
|
|
44
|
+
# It is an `Encoding::UndefinedConversionError`, as the encode stream raises, so that it can be rescued as one.
|
|
45
|
+
#
|
|
46
|
+
# Example:
|
|
47
|
+
# IOStreams.path("data.csv").schema("people.json").each(:hash) { |hash| p hash }
|
|
48
|
+
# # => IOStreams::Errors::InvalidEncoding: "\xFF" is not valid UTF-8
|
|
49
|
+
class InvalidEncoding < ::Encoding::UndefinedConversionError
|
|
50
|
+
# Returns [Integer] the offset of the first invalid byte within the data, which IOStreams 3.0 gives, or [nil] in
|
|
51
|
+
# IOStreams 2.2, which does not count the bytes of the data read.
|
|
52
|
+
attr_reader :byte_offset
|
|
53
|
+
|
|
54
|
+
# Returns [Integer] the number of the line that the invalid data is on, counting from 1,
|
|
55
|
+
# or [nil] when the data is not read a line at a time.
|
|
56
|
+
attr_reader :line_number
|
|
57
|
+
|
|
58
|
+
# Returns [String] the invalid bytes, as binary, like `Encoding::UndefinedConversionError#error_char`.
|
|
59
|
+
attr_reader :error_char
|
|
60
|
+
|
|
61
|
+
# Returns [Encoding] the encoding that the invalid bytes were read or written as, binary for data read from a file,
|
|
62
|
+
# and the encoding that they are not valid in, like `Encoding::UndefinedConversionError#source_encoding` and
|
|
63
|
+
# `#destination_encoding`.
|
|
64
|
+
attr_reader :source_encoding, :destination_encoding
|
|
65
|
+
|
|
66
|
+
def initialize(message, byte_offset: nil, line_number: nil, error_char: nil, source_encoding: nil,
|
|
67
|
+
destination_encoding: nil)
|
|
68
|
+
@byte_offset = byte_offset
|
|
69
|
+
@line_number = line_number
|
|
70
|
+
@error_char = error_char
|
|
71
|
+
@source_encoding = source_encoding
|
|
72
|
+
@destination_encoding = destination_encoding
|
|
73
|
+
super(message)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Returns [String] the name of the #source_encoding, such as "ASCII-8BIT", or nil.
|
|
77
|
+
def source_encoding_name
|
|
78
|
+
source_encoding&.name
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Returns [String] the name of the #destination_encoding, such as "UTF-8", or nil.
|
|
82
|
+
def destination_encoding_name
|
|
83
|
+
destination_encoding&.name
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
32
87
|
class MalformedDataError < RuntimeError
|
|
33
88
|
attr_reader :line_number
|
|
34
89
|
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
module Gzip
|
|
3
3
|
class Reader < IOStreams::Reader
|
|
4
|
+
def self.option_names
|
|
5
|
+
[]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# The methods that the stream that v3.0 supplies to a reader block for gzip answers: those of an IO that read, and
|
|
9
|
+
# #close, see .v3_stream_methods.
|
|
10
|
+
V3_METHODS = [*V3_READ_METHODS, :close].freeze
|
|
11
|
+
private_constant :V3_METHODS
|
|
12
|
+
|
|
13
|
+
# Returns [Array<Symbol>] the methods that the stream that v3.0 supplies to a reader block, in place of the
|
|
14
|
+
# `Zlib::GzipReader` that this reader yields, answers, see `IOStreams::Reader.v3_stream_methods`.
|
|
15
|
+
def self.v3_stream_methods(_options)
|
|
16
|
+
V3_METHODS
|
|
17
|
+
end
|
|
18
|
+
|
|
4
19
|
# Read from a gzip stream, decompressing the contents as it is read
|
|
5
20
|
def self.stream(input_stream)
|
|
6
21
|
io = ::Zlib::GzipReader.new(input_stream)
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
module Gzip
|
|
3
3
|
class Writer < IOStreams::Writer
|
|
4
|
+
def self.option_names
|
|
5
|
+
%i[level]
|
|
6
|
+
end
|
|
7
|
+
|
|
4
8
|
# Write to a stream, compressing with GZip
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
#
|
|
10
|
+
# Parameters
|
|
11
|
+
# level: [Integer]
|
|
12
|
+
# Compression level, from 0 (no compression) to 9 (best compression).
|
|
13
|
+
# Default: Zlib::DEFAULT_COMPRESSION
|
|
14
|
+
def self.stream(input_stream, level: nil, &block)
|
|
15
|
+
io = ::Zlib::GzipWriter.new(input_stream, level)
|
|
7
16
|
block.call(io)
|
|
8
17
|
ensure
|
|
9
18
|
io&.close
|