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
data/lib/io_streams/builder.rb
CHANGED
|
@@ -1,86 +1,192 @@
|
|
|
1
|
+
require "io_streams/builder/deprecations"
|
|
2
|
+
|
|
1
3
|
module IOStreams
|
|
2
4
|
# Build the streams that need to be applied to a path during reading or writing.
|
|
3
5
|
class Builder
|
|
6
|
+
include Deprecations
|
|
7
|
+
|
|
4
8
|
attr_accessor :file_name, :format_options
|
|
5
9
|
attr_reader :streams, :options
|
|
6
10
|
|
|
11
|
+
# Returns [Hash|String|IOStreams::Stream|nil] the schema of the rows and records as it was set, see #schema=.
|
|
12
|
+
attr_reader :schema
|
|
13
|
+
|
|
7
14
|
def initialize(file_name = nil)
|
|
8
15
|
@file_name = file_name
|
|
9
16
|
@streams = nil
|
|
10
17
|
@options = nil
|
|
11
|
-
@
|
|
18
|
+
@named_format = nil
|
|
19
|
+
@set_format = nil
|
|
12
20
|
@format_option = nil
|
|
21
|
+
@encoding = nil
|
|
22
|
+
@raw = false
|
|
23
|
+
@temp_file = false
|
|
24
|
+
@schema = nil
|
|
25
|
+
@schema_source = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# A copy has its own streams, options and encoding, so that changing them does not change the original. It shares
|
|
29
|
+
# the schema, which #schema= replaces rather than changes, so that the copies read its file once, see
|
|
30
|
+
# `IOStreams::Schema::Source`.
|
|
31
|
+
def initialize_copy(source)
|
|
32
|
+
super
|
|
33
|
+
@streams = @streams&.transform_values(&:dup)
|
|
34
|
+
@options = @options&.transform_values(&:dup)
|
|
35
|
+
@encoding = @encoding&.dup
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Sets the schema of the rows and records, see `IOStreams::Stream#schema`: a Hash, which is checked now, or the path
|
|
39
|
+
# or url of a JSON file that holds one, which is read the first time that a reader or writer needs it, see
|
|
40
|
+
# `IOStreams::Schema::Source`, or nil to remove it.
|
|
41
|
+
#
|
|
42
|
+
# A copy of a Hash or String is held, which is frozen, so that the caller changing the object that it supplied
|
|
43
|
+
# changes neither #schema nor the schema that is read, and a copy of a path, so that changing its path does not
|
|
44
|
+
# change the schema.
|
|
45
|
+
#
|
|
46
|
+
# Raises ArgumentError when the schema is not a Hash, a String or a path, or is a Hash that is not a valid schema.
|
|
47
|
+
def schema=(schema)
|
|
48
|
+
schema = schema.is_a?(IOStreams::Stream) ? schema.dup : Utils.frozen_copy(schema)
|
|
49
|
+
@schema_source = schema.nil? ? nil : IOStreams::Schema::Source.new(schema)
|
|
50
|
+
@schema = schema
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Returns [IOStreams::Schema|nil] the schema of the rows and records, reading its file the first time, see #schema=,
|
|
54
|
+
# or nil when none is set.
|
|
55
|
+
def tabular_schema
|
|
56
|
+
@schema_source&.schema
|
|
13
57
|
end
|
|
14
58
|
|
|
15
59
|
# Supply an option that is only applied once the file name extensions have been parsed.
|
|
16
60
|
# Note:
|
|
17
61
|
# - Cannot set both `stream` and `option`
|
|
62
|
+
#
|
|
63
|
+
# Setting the encode stream with `option(:encode, ...)` is deprecated, see #encoding, and reports a deprecation
|
|
64
|
+
# warning, see `IOStreams::Deprecation`.
|
|
18
65
|
def option(stream, **options)
|
|
19
66
|
stream = stream.to_sym unless stream.is_a?(Symbol)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@options ||= {}
|
|
25
|
-
if (opts = @options[stream])
|
|
26
|
-
opts.merge!(options)
|
|
27
|
-
else
|
|
28
|
-
@options[stream] = options.dup
|
|
29
|
-
end
|
|
67
|
+
raise_if_raw!("#option")
|
|
68
|
+
add_option(stream, options)
|
|
69
|
+
warn_deprecated_stream("option", stream)
|
|
70
|
+
warn_deprecated_options(stream, options)
|
|
30
71
|
self
|
|
31
72
|
end
|
|
32
73
|
|
|
74
|
+
# Setting the encode stream with `stream(:encode, ...)` is deprecated, see #encoding. Like any other stream set
|
|
75
|
+
# with `#stream`, it still stops the streams being taken from the file name, which #encoding keeps.
|
|
76
|
+
#
|
|
77
|
+
# Supplying `:none` is deprecated, see #raw. Since it does what #raw does, it can still be supplied after #raw.
|
|
78
|
+
#
|
|
79
|
+
# Each reports a deprecation warning, see `IOStreams::Deprecation`.
|
|
33
80
|
def stream(stream, **options)
|
|
34
81
|
stream = stream.to_sym unless stream.is_a?(Symbol)
|
|
35
|
-
|
|
82
|
+
raise_if_raw!("#stream") unless stream == :none
|
|
83
|
+
add_stream(stream, options)
|
|
84
|
+
warn_deprecated_stream("stream", stream)
|
|
85
|
+
warn_deprecated_options(stream, options)
|
|
86
|
+
self
|
|
87
|
+
end
|
|
36
88
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
89
|
+
# Sets the encoding of the text that the application reads or writes, and the other options of the encode
|
|
90
|
+
# stream, see `IOStreams::Encode`, merging them with those already set.
|
|
91
|
+
#
|
|
92
|
+
# Unlike `#option`, it can be combined with `#stream`, and needs no file name. Unlike `#stream`, it does not stop
|
|
93
|
+
# the streams being taken from the file name. The encode stream is applied closest to the application, after
|
|
94
|
+
# every other stream when reading, with the options set by `#option` or `#stream` for `:encode`, if any, and
|
|
95
|
+
# these on top of them.
|
|
96
|
+
#
|
|
97
|
+
# Raises ArgumentError when neither an encoding nor any options are supplied, when the encoding is supplied both
|
|
98
|
+
# as an argument and as the `encoding:` option, or for an option that the encode stream does not take, or a value
|
|
99
|
+
# that it does not accept, see `IOStreams::Encode.check`. They are checked when they are set, since a writer that
|
|
100
|
+
# raised for them would have replaced its file. The options set with `#option` or `#stream` for `:encode` are
|
|
101
|
+
# deprecated, and are still only checked when the stream is opened, as before.
|
|
102
|
+
def encoding(encoding = nil, **options)
|
|
103
|
+
raise_if_raw!("#encoding")
|
|
104
|
+
if encoding
|
|
105
|
+
raise(ArgumentError, "Supply the encoding as an argument or as `encoding:`, not both") if options.key?(:encoding)
|
|
43
106
|
|
|
44
|
-
|
|
45
|
-
if (opts = @streams[stream])
|
|
46
|
-
opts.merge!(options)
|
|
47
|
-
else
|
|
48
|
-
@streams[stream] = options.dup
|
|
107
|
+
options = {encoding: encoding, **options}
|
|
49
108
|
end
|
|
109
|
+
raise(ArgumentError, "Supply the encoding, or options for the encode stream") if options.empty?
|
|
110
|
+
|
|
111
|
+
merged = (@encoding || {}).merge(options)
|
|
112
|
+
Encode.check(**merged)
|
|
113
|
+
@encoding = merged
|
|
50
114
|
self
|
|
51
115
|
end
|
|
52
116
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
117
|
+
# Reads and writes the data as it is stored: applies no streams, neither those that the file name implies nor any
|
|
118
|
+
# already set with #stream or #option, and no encoding set with #encoding. For example, to download a zip file
|
|
119
|
+
# without unzipping it, or to write data that is already compressed.
|
|
120
|
+
#
|
|
121
|
+
# Since the data is as stored, setting a stream, option or encoding afterwards raises ArgumentError.
|
|
122
|
+
def raw
|
|
123
|
+
no_streams
|
|
124
|
+
@options = nil
|
|
125
|
+
@raw = true
|
|
126
|
+
self
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Returns [true|false] whether the data is read and written as it is stored, see #raw.
|
|
130
|
+
def raw?
|
|
131
|
+
@raw
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Returns [true|false] whether the text that the streams yield is taken as UTF-8: the text of a file with a
|
|
135
|
+
# schema, see #schema=, unless the application chose the encoding of its text, with an encode stream, whether set
|
|
136
|
+
# with #encoding, #option, #stream, or by a file name ending in `.encode`, or chose none with #raw, which reads the
|
|
137
|
+
# data as it is stored. Without a schema, the text is read as it always was.
|
|
138
|
+
def utf8_text?
|
|
139
|
+
!@schema.nil? && !raw? && !pipeline.key?(:encode)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Applies no streams, including an encoding set with #encoding, as `stream(:none)` does, for IOStreams itself.
|
|
143
|
+
#
|
|
144
|
+
# `temp_file: true` for the path of `IOStreams.temp_file`, which reads and writes its data as it is stored, while
|
|
145
|
+
# IOStreams v3.0 applies the streams that its file name implies, so that reading or writing it reports a deprecation
|
|
146
|
+
# warning while they would apply, see #warn_temp_file_streams.
|
|
147
|
+
def no_streams(temp_file: false)
|
|
148
|
+
@streams = {}
|
|
149
|
+
@encoding = nil
|
|
150
|
+
@temp_file = temp_file
|
|
151
|
+
self
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Sets the options of a stream as #option does, unless streams were already set with #stream, or no file name is
|
|
155
|
+
# set, when it adds the stream as #stream does.
|
|
156
|
+
#
|
|
157
|
+
# Setting the encode stream with `option_or_stream(:encode, ...)` is deprecated, see #encoding, and reports a
|
|
158
|
+
# deprecation warning, see `IOStreams::Deprecation`.
|
|
159
|
+
def option_or_stream(stream, **options)
|
|
160
|
+
stream = stream.to_sym unless stream.is_a?(Symbol)
|
|
161
|
+
raise_if_raw!("#option_or_stream") unless stream == :none
|
|
162
|
+
streams || file_name.nil? ? add_stream(stream, options) : add_option(stream, options)
|
|
163
|
+
warn_deprecated_stream("option_or_stream", stream)
|
|
164
|
+
warn_deprecated_options(stream, options)
|
|
165
|
+
self
|
|
61
166
|
end
|
|
62
167
|
|
|
63
168
|
# Return the options set for either a stream or option.
|
|
64
169
|
def setting(stream)
|
|
170
|
+
return encode_setting if stream == :encode && @encoding
|
|
65
171
|
return streams[stream] if streams
|
|
66
172
|
|
|
67
173
|
options[stream] if options
|
|
68
174
|
end
|
|
69
175
|
|
|
70
176
|
def reader(io_stream, &)
|
|
177
|
+
warn_deprecated_streams
|
|
71
178
|
execute(:reader, pipeline, io_stream, &)
|
|
72
179
|
end
|
|
73
180
|
|
|
74
181
|
def writer(io_stream, &)
|
|
182
|
+
warn_deprecated_streams
|
|
75
183
|
execute(:writer, pipeline, io_stream, &)
|
|
76
184
|
end
|
|
77
185
|
|
|
78
186
|
# Returns [Hash<Symbol:Hash>] the pipeline of streams
|
|
79
187
|
# with their options that will be applied when the reader or writer is invoked.
|
|
80
188
|
def pipeline
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
build_pipeline.freeze
|
|
189
|
+
with_encoding(streams ? streams.dup : build_pipeline).freeze
|
|
84
190
|
end
|
|
85
191
|
|
|
86
192
|
# Removes the named stream from the current pipeline.
|
|
@@ -88,25 +194,119 @@ module IOStreams
|
|
|
88
194
|
# Note: Any options must be set _before_ calling this method.
|
|
89
195
|
def remove_from_pipeline(stream_name)
|
|
90
196
|
@streams ||= build_pipeline
|
|
197
|
+
@encoding = nil if stream_name.to_sym == :encode
|
|
198
|
+
# The streams are now those that the application chose, see #warn_temp_file_streams.
|
|
199
|
+
@temp_file = false
|
|
91
200
|
@streams.delete(stream_name.to_sym)
|
|
92
201
|
end
|
|
93
202
|
|
|
94
203
|
# Returns the tabular format if set, otherwise tries to autodetect the format if the file_name has been set
|
|
95
204
|
# Returns [nil] if no format is set, or if it cannot be determined from the file_name
|
|
205
|
+
#
|
|
206
|
+
# The format that the file name names is kept once it is found, until a format is set, see #format=.
|
|
96
207
|
def format
|
|
97
|
-
@
|
|
208
|
+
@set_format || (@named_format ||= file_name ? Tabular.format_from_file_name(file_name) : nil)
|
|
98
209
|
end
|
|
99
210
|
|
|
211
|
+
# Sets the tabular format, or nil to detect it from the file name again, see #format.
|
|
100
212
|
def format=(format)
|
|
101
213
|
unless format.nil? || IOStreams::Tabular.registered_formats.include?(format)
|
|
102
214
|
raise(ArgumentError, "Invalid format: #{format.inspect}")
|
|
103
215
|
end
|
|
104
216
|
|
|
105
|
-
@
|
|
217
|
+
@set_format = format
|
|
218
|
+
@named_format = nil
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Returns [true|false] whether a format was set, see #format=, rather than taken from the file name, see #format.
|
|
222
|
+
def format_set?
|
|
223
|
+
!@set_format.nil?
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Returns [Symbol|nil] the tabular format of the text that the stream closest to the application reads, when that
|
|
227
|
+
# stream decides it, such as `:csv` for a spreadsheet, whose rows it reads as CSV, see
|
|
228
|
+
# `IOStreams::Xlsx::Reader.text_format`, otherwise nil, for a stream whose data is the text, such as gzip. The encode
|
|
229
|
+
# stream converts that text, so it is the stream after it.
|
|
230
|
+
def text_format
|
|
231
|
+
stream = pipeline.each_key.find { |name| name != :encode }
|
|
232
|
+
klass = stream && IOStreams.extensions[stream]&.reader_class
|
|
233
|
+
klass.text_format if klass.respond_to?(:text_format)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Returns [String] the quote character within which field delimiters and newlines may be
|
|
237
|
+
# embedded for the current tabular format, or [nil] when the format has no such quoting,
|
|
238
|
+
# or when the format cannot be determined.
|
|
239
|
+
#
|
|
240
|
+
# Used by the line reader to avoid treating a newline as a line ending when it is embedded
|
|
241
|
+
# within a quoted field (e.g. CSV). Delegates to the format's parser, so the per-format
|
|
242
|
+
# knowledge lives with the parser. Driven entirely by `format`, so an explicitly set format
|
|
243
|
+
# (e.g. `.format(:psv)`) overrides any extension auto-detected from the `file_name`.
|
|
244
|
+
def quote_character
|
|
245
|
+
format && IOStreams::Tabular.parser_class(format).quote_character
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Returns [Hash<Symbol, Array<Symbol>>] the settings of this stream that apply to what it writes, other than the
|
|
249
|
+
# options of each call: `streams:` the names of the streams of its pipeline, see #pipeline, which #stream, #option,
|
|
250
|
+
# #encoding and the file name set, and `tabular:` the names of those of its rows and records that were set: `:format`,
|
|
251
|
+
# when it was set, see #format=, rather than taken from the file name, and `:schema`, see #schema=.
|
|
252
|
+
def writer_settings
|
|
253
|
+
tabular = []
|
|
254
|
+
tabular << :format if format_set?
|
|
255
|
+
tabular << :schema unless schema.nil?
|
|
256
|
+
{streams: pipeline.keys, tabular: tabular}
|
|
106
257
|
end
|
|
107
258
|
|
|
108
259
|
private
|
|
109
260
|
|
|
261
|
+
# Sets the options of a stream that is applied once the file name extensions have been parsed, see #option.
|
|
262
|
+
def add_option(stream, options)
|
|
263
|
+
raise(ArgumentError, "Invalid stream: #{stream.inspect}") unless IOStreams.extensions.include?(stream)
|
|
264
|
+
raise(ArgumentError, "Cannot call both #option and #stream on the same streams instance}") if @streams
|
|
265
|
+
raise(ArgumentError, "Cannot call #option unless the `file_name` was already set}") unless file_name
|
|
266
|
+
|
|
267
|
+
@options ||= {}
|
|
268
|
+
if (opts = @options[stream])
|
|
269
|
+
opts.merge!(options)
|
|
270
|
+
else
|
|
271
|
+
@options[stream] = options.dup
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Adds a stream, in place of the streams that the file name implies, see #stream.
|
|
276
|
+
def add_stream(stream, options)
|
|
277
|
+
raise(ArgumentError, "Cannot call both #option and #stream on the same streams instance}") if @options
|
|
278
|
+
# `:none` applies no streams, including an encoding set with #encoding. It is the deprecated form of #raw.
|
|
279
|
+
return no_streams if stream == :none
|
|
280
|
+
raise(ArgumentError, "Invalid stream: #{stream.inspect}") unless IOStreams.extensions.include?(stream)
|
|
281
|
+
|
|
282
|
+
@streams ||= {}
|
|
283
|
+
if (opts = @streams[stream])
|
|
284
|
+
opts.merge!(options)
|
|
285
|
+
else
|
|
286
|
+
@streams[stream] = options.dup
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def raise_if_raw!(method_name)
|
|
291
|
+
return unless @raw
|
|
292
|
+
|
|
293
|
+
raise(ArgumentError, "Cannot call #{method_name} after #raw, which reads and writes the data as it is stored")
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# Returns [Hash] the options of the encode stream set with #option or #stream, if any, and those of #encoding.
|
|
297
|
+
def encode_setting
|
|
298
|
+
setting = streams ? streams[:encode] : options&.[](:encode)
|
|
299
|
+
(setting || {}).merge(@encoding)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Returns [Hash<Symbol:Hash>] the pipeline, with the encode stream first, closest to the application, when an
|
|
303
|
+
# encoding was set with #encoding.
|
|
304
|
+
def with_encoding(pipeline)
|
|
305
|
+
return pipeline unless @encoding
|
|
306
|
+
|
|
307
|
+
{encode: encode_setting}.merge(pipeline.except(:encode))
|
|
308
|
+
end
|
|
309
|
+
|
|
110
310
|
def build_pipeline
|
|
111
311
|
return {} unless file_name
|
|
112
312
|
|
|
@@ -127,7 +327,7 @@ module IOStreams
|
|
|
127
327
|
|
|
128
328
|
# Returns the streams for the supplied file_name
|
|
129
329
|
def parse_extensions
|
|
130
|
-
parts = ::
|
|
330
|
+
parts = Utils::PathName.basename(file_name).split(".")
|
|
131
331
|
extensions = []
|
|
132
332
|
while (extension = parts.pop)
|
|
133
333
|
sym = extension.downcase.to_sym
|
|
@@ -146,14 +346,91 @@ module IOStreams
|
|
|
146
346
|
block.call(io_stream)
|
|
147
347
|
elsif pipeline.size == 1
|
|
148
348
|
stream, opts = pipeline.first
|
|
149
|
-
|
|
349
|
+
open_stream(type, stream, io_stream, opts, &block)
|
|
150
350
|
else
|
|
151
351
|
# Daisy chain multiple streams together
|
|
152
352
|
last = pipeline.keys.inject(block) do |inner, stream_sym|
|
|
153
|
-
->(io) {
|
|
353
|
+
->(io) { open_stream(type, stream_sym, io, pipeline[stream_sym], &inner) }
|
|
154
354
|
end
|
|
155
355
|
last.call(io_stream)
|
|
156
356
|
end
|
|
157
357
|
end
|
|
358
|
+
|
|
359
|
+
def open_stream(type, stream, io_stream, opts, &)
|
|
360
|
+
klass = stream_class(type, stream)
|
|
361
|
+
validate_options(type, stream, klass, opts)
|
|
362
|
+
klass.open(io_stream, **taken_options(type, stream, klass, opts), &)
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
# Returns [Class] the class that reads or writes the stream: the class registered for it, or, to read a file with a
|
|
366
|
+
# schema, see #schema=, the class that the registered reader answers for it, when it reads a file with a schema
|
|
367
|
+
# differently, such as the reader of a spreadsheet, see `IOStreams::Xlsx::Reader.schema_reader`.
|
|
368
|
+
def stream_class(type, stream)
|
|
369
|
+
klass = class_for_stream(type, stream)
|
|
370
|
+
return klass if type != :reader || @schema.nil? || !klass.respond_to?(:schema_reader)
|
|
371
|
+
|
|
372
|
+
klass.schema_reader
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# Returns [Hash] the options of the stream that the class is opened with: all of them, unless the class takes any
|
|
376
|
+
# option, since it does not declare them, see #validate_options, while it answers those that it takes in IOStreams
|
|
377
|
+
# 3.0, as `IOStreams::Bzip2::Reader` does until v3.0, see #v3_class_option_names. It is then opened with those
|
|
378
|
+
# alone, and a warning is logged for each of the others, which it would ignore, see
|
|
379
|
+
# `IOStreams::Utils.warn_unknown_options`, naming the stream as it was set: one that the class of the other
|
|
380
|
+
# direction takes, which v3.0 accepts, and ignores, or one that neither takes, which v3.0 raises ArgumentError for.
|
|
381
|
+
def taken_options(type, stream, klass, opts)
|
|
382
|
+
known = v3_class_option_names(klass) if option_names(klass).nil?
|
|
383
|
+
return opts if known.nil?
|
|
384
|
+
|
|
385
|
+
unknown = opts.except(*known)
|
|
386
|
+
return opts if unknown.empty?
|
|
387
|
+
|
|
388
|
+
other = IOStreams.extensions[stream].public_send(type == :reader ? :writer_class : :reader_class)
|
|
389
|
+
other_names = (v3_class_option_names(other) if other) || []
|
|
390
|
+
Utils.warn_unknown_options(unknown, stream, type, known, other_names)
|
|
391
|
+
opts.slice(*known)
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# Options are strict: an option the stream does not accept raises instead of being ignored.
|
|
395
|
+
# One option hash is shared by the reader and the writer for a stream, so when the option
|
|
396
|
+
# is only valid in the other direction the message says so.
|
|
397
|
+
#
|
|
398
|
+
# Streams registered via `IOStreams.register_extension` need not inherit from `IOStreams::Reader`
|
|
399
|
+
# or `IOStreams::Writer`, so a class that does not declare `option_names` is not validated here.
|
|
400
|
+
def validate_options(type, stream, klass, opts)
|
|
401
|
+
accepted = option_names(klass)
|
|
402
|
+
return if accepted.nil?
|
|
403
|
+
|
|
404
|
+
unknown = opts.keys - accepted
|
|
405
|
+
return if unknown.empty?
|
|
406
|
+
|
|
407
|
+
other_type = type == :reader ? :writer : :reader
|
|
408
|
+
other_names = option_names(IOStreams.extensions[stream].send("#{other_type}_class")) || []
|
|
409
|
+
other_only = unknown & other_names
|
|
410
|
+
invalid = unknown - other_names
|
|
411
|
+
direction = type == :reader ? "reading" : "writing"
|
|
412
|
+
|
|
413
|
+
messages = []
|
|
414
|
+
if other_only.any?
|
|
415
|
+
messages << "#{list(other_only)} only #{other_only.size == 1 ? 'applies' : 'apply'} when " \
|
|
416
|
+
"#{type == :reader ? 'writing' : 'reading'} a #{stream.inspect} stream and cannot be used when " \
|
|
417
|
+
"#{direction}. Configure a separate path or stream without #{other_only.size == 1 ? 'it' : 'them'} " \
|
|
418
|
+
"for #{direction}."
|
|
419
|
+
end
|
|
420
|
+
if invalid.any?
|
|
421
|
+
valid = accepted.empty? ? "none" : list(accepted)
|
|
422
|
+
messages << "Unknown #{invalid.size == 1 ? 'option' : 'options'} #{list(invalid)} when #{direction} " \
|
|
423
|
+
"a #{stream.inspect} stream. Valid options: #{valid}."
|
|
424
|
+
end
|
|
425
|
+
raise(ArgumentError, messages.join(" "))
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
def option_names(klass)
|
|
429
|
+
klass.option_names if klass.respond_to?(:option_names)
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
def list(names)
|
|
433
|
+
names.map(&:inspect).join(", ")
|
|
434
|
+
end
|
|
158
435
|
end
|
|
159
436
|
end
|
|
@@ -1,12 +1,51 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
module Bzip2
|
|
3
3
|
class Reader < IOStreams::Reader
|
|
4
|
+
OPTION_NAMES = %i[autoclose first_only small].freeze
|
|
5
|
+
|
|
6
|
+
# Not declared until v3.0, so that an unknown option logs a warning instead of raising `ArgumentError`.
|
|
7
|
+
def self.option_names
|
|
8
|
+
nil
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Returns [Array<Symbol>] the options that this class takes, which v3.0 declares as its `option_names`, see
|
|
12
|
+
# `IOStreams::Deprecation::OptionNames#known_option_names`, so that an option that neither it nor the writer takes
|
|
13
|
+
# is reported as deprecated when it is set, while one of the writer is not, since v3.0 accepts it, and ignores it,
|
|
14
|
+
# and so that a path opens it with these alone, and logs the others, see `IOStreams::Builder#taken_options`.
|
|
15
|
+
def self.known_option_names
|
|
16
|
+
OPTION_NAMES
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns [Array<Symbol>] the methods that the stream that v3.0 supplies to a reader block, in place of the
|
|
20
|
+
# `Bzip2::FFI::Reader` that this reader yields, answers: those of an IO that read, see
|
|
21
|
+
# `IOStreams::Reader.v3_stream_methods`.
|
|
22
|
+
def self.v3_stream_methods(_options)
|
|
23
|
+
V3_READ_METHODS
|
|
24
|
+
end
|
|
25
|
+
|
|
4
26
|
# Read from a Bzip2 stream, decompressing the contents as it is read
|
|
5
|
-
|
|
27
|
+
#
|
|
28
|
+
# Any other option is ignored, and logs a warning. One that the writer does not take either also reports a
|
|
29
|
+
# deprecation warning when it is set, see .known_option_names, since it will raise `ArgumentError` in v3.0.
|
|
30
|
+
#
|
|
31
|
+
# Parameters are passed through to `Bzip2::FFI::Reader`:
|
|
32
|
+
# autoclose: [true|false]
|
|
33
|
+
# Close the input stream when the reader is closed.
|
|
34
|
+
# Default: false
|
|
35
|
+
#
|
|
36
|
+
# first_only: [true|false]
|
|
37
|
+
# Only decompress the first of any consecutive bzip2 structures in the input.
|
|
38
|
+
# Default: false
|
|
39
|
+
#
|
|
40
|
+
# small: [true|false]
|
|
41
|
+
# Use an alternative decompression algorithm that uses less memory but is slower.
|
|
42
|
+
# Default: false
|
|
43
|
+
def self.stream(input_stream, autoclose: false, first_only: false, small: false, **unknown)
|
|
44
|
+
Utils.warn_unknown_options(unknown, :bz2, :reader, OPTION_NAMES)
|
|
6
45
|
Utils.load_soft_dependency("bzip2-ffi", "Bzip2", "bzip2/ffi") unless defined?(::Bzip2::FFI)
|
|
7
46
|
|
|
8
47
|
begin
|
|
9
|
-
io = ::Bzip2::FFI::Reader.new(input_stream,
|
|
48
|
+
io = ::Bzip2::FFI::Reader.new(input_stream, autoclose: autoclose, first_only: first_only, small: small)
|
|
10
49
|
yield io
|
|
11
50
|
ensure
|
|
12
51
|
io&.close
|
|
@@ -1,12 +1,45 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
module Bzip2
|
|
3
3
|
class Writer < IOStreams::Writer
|
|
4
|
+
OPTION_NAMES = %i[autoclose block_size work_factor].freeze
|
|
5
|
+
|
|
6
|
+
# Not declared until v3.0, so that an unknown option logs a warning instead of raising `ArgumentError`.
|
|
7
|
+
def self.option_names
|
|
8
|
+
nil
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Returns [Array<Symbol>] the options that this class takes, which v3.0 declares as its `option_names`, see
|
|
12
|
+
# `IOStreams::Deprecation::OptionNames#known_option_names`, so that an option that neither it nor the reader takes
|
|
13
|
+
# is reported as deprecated when it is set, while one of the reader is not, since v3.0 accepts it, and ignores it,
|
|
14
|
+
# and so that a path opens it with these alone, and logs the others, see `IOStreams::Builder#taken_options`.
|
|
15
|
+
def self.known_option_names
|
|
16
|
+
OPTION_NAMES
|
|
17
|
+
end
|
|
18
|
+
|
|
4
19
|
# Write to a stream, compressing with Bzip2
|
|
5
|
-
|
|
20
|
+
#
|
|
21
|
+
# Any other option is ignored, and logs a warning. One that the reader does not take either also reports a
|
|
22
|
+
# deprecation warning when it is set, see .known_option_names, since it will raise `ArgumentError` in v3.0.
|
|
23
|
+
#
|
|
24
|
+
# Parameters are passed through to `Bzip2::FFI::Writer`:
|
|
25
|
+
# autoclose: [true|false]
|
|
26
|
+
# Close the output stream when the writer is closed.
|
|
27
|
+
# Default: false
|
|
28
|
+
#
|
|
29
|
+
# block_size: [Integer]
|
|
30
|
+
# Compression block size, from 1 (100k) to 9 (900k).
|
|
31
|
+
# Default: 9
|
|
32
|
+
#
|
|
33
|
+
# work_factor: [Integer]
|
|
34
|
+
# How much effort to spend on highly repetitive input before falling back
|
|
35
|
+
# to a slower algorithm, from 0 to 250. 0 uses the libbz2 default.
|
|
36
|
+
# Default: 0
|
|
37
|
+
def self.stream(input_stream, autoclose: false, block_size: nil, work_factor: nil, **unknown)
|
|
38
|
+
Utils.warn_unknown_options(unknown, :bz2, :writer, OPTION_NAMES)
|
|
6
39
|
Utils.load_soft_dependency("bzip2-ffi", "Bzip2", "bzip2/ffi") unless defined?(::Bzip2::FFI)
|
|
7
40
|
|
|
8
41
|
begin
|
|
9
|
-
io = ::Bzip2::FFI::Writer.new(input_stream,
|
|
42
|
+
io = ::Bzip2::FFI::Writer.new(input_stream, autoclose: autoclose, block_size: block_size, work_factor: work_factor)
|
|
10
43
|
yield io
|
|
11
44
|
ensure
|
|
12
45
|
io&.close
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module IOStreams
|
|
2
|
+
module Deprecation
|
|
3
|
+
# A module whose methods report a deprecation warning when the application calls them, and then call the method of
|
|
4
|
+
# the object that it extends, see .deprecate: for the methods of an object that IOStreams hands the application, such
|
|
5
|
+
# as the `Zlib::GzipReader` that a reader block receives for a `.gz` file, that the object that v3.0 hands it in its
|
|
6
|
+
# place does not answer, such as `#rewind`.
|
|
7
|
+
class Methods < ::Module
|
|
8
|
+
@modules = {}
|
|
9
|
+
@mutex = Mutex.new
|
|
10
|
+
|
|
11
|
+
# Extends the supplied object, which IOStreams hands to the application, with a module that reports a deprecation
|
|
12
|
+
# warning for each of its public methods that the object in its place in v3.0 does not answer: those other than
|
|
13
|
+
# the supplied methods, which that object answers, and the methods of every object. Each warning is the name of the
|
|
14
|
+
# method, such as `#rewind`, followed by the supplied message, such as "is deprecated on ...", or by the message of
|
|
15
|
+
# its name when the message is a Hash, such as one that names what replaces it, otherwise that of nil.
|
|
16
|
+
#
|
|
17
|
+
# The module is prepended to the singleton class of the object, rather than extending it, so that its methods
|
|
18
|
+
# come before those that the object defines on itself, such as the `#path` of a `Zlib::GzipReader`. The object
|
|
19
|
+
# keeps its class, and answers the same methods, which each still call its own method. Only the calls that the
|
|
20
|
+
# application makes itself report a warning, see `IOStreams::Deprecation.warn_call`, and none while deprecation
|
|
21
|
+
# warnings are off. The module is built once for each class of object, and kept, so that each object costs one
|
|
22
|
+
# `prepend`, and nothing else until a deprecated method is called.
|
|
23
|
+
#
|
|
24
|
+
# Returns the object.
|
|
25
|
+
def self.deprecate(object, kept_methods, message)
|
|
26
|
+
return object if object.frozen?
|
|
27
|
+
|
|
28
|
+
key = [object.class, object.singleton_methods.sort, kept_methods, message]
|
|
29
|
+
deprecated = @mutex.synchronize do
|
|
30
|
+
@modules[key] ||= new(object.public_methods - ::Object.public_instance_methods - kept_methods, message)
|
|
31
|
+
end
|
|
32
|
+
object.singleton_class.prepend(deprecated)
|
|
33
|
+
object
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Defines a method for each of the supplied names that reports the deprecation warning of its name, followed by the
|
|
37
|
+
# supplied message, or its own message when it is a Hash, see .deprecate, when the application calls it, and then
|
|
38
|
+
# calls the method of the object.
|
|
39
|
+
def initialize(names, message)
|
|
40
|
+
super()
|
|
41
|
+
names.each do |name|
|
|
42
|
+
warning = "##{name} #{message.is_a?(Hash) ? message.fetch(name) { message.fetch(nil) } : message}".freeze
|
|
43
|
+
define_method(name) do |*args, **kwargs, &block|
|
|
44
|
+
::IOStreams::Deprecation.warn_call(warning) if ::Warning[:deprecated]
|
|
45
|
+
super(*args, **kwargs, &block)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IOStreams
|
|
4
|
+
module Deprecation
|
|
5
|
+
# Reports a deprecation warning the first time that it applies, for an object that would otherwise report one for each
|
|
6
|
+
# value that it converts, such as a field of a schema, which converts a value of each row of a file. The object holds
|
|
7
|
+
# one for each of its deprecations, which is not frozen with it, so that an object that is frozen, as a field is,
|
|
8
|
+
# still reports each one once.
|
|
9
|
+
class Once
|
|
10
|
+
# Reports the deprecation warning that the block returns, see `IOStreams::Deprecation.warn`, unless this has
|
|
11
|
+
# reported it already, or deprecation warnings are off, see `IOStreams::Deprecation.on?`, when the block is not
|
|
12
|
+
# called, so that a caller builds its message only when it is reported. The block returns nil when the
|
|
13
|
+
# deprecation does not apply to the value at hand, which leaves it to be reported for a later one.
|
|
14
|
+
#
|
|
15
|
+
# Returns nil.
|
|
16
|
+
def warn
|
|
17
|
+
return if @reported || !Deprecation.on?
|
|
18
|
+
|
|
19
|
+
message = yield
|
|
20
|
+
return if message.nil?
|
|
21
|
+
|
|
22
|
+
@reported = true
|
|
23
|
+
Deprecation.warn(message)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IOStreams
|
|
4
|
+
module Deprecation
|
|
5
|
+
# The names of the options that a reader or a writer takes in IOStreams 3.0, which `IOStreams::Reader` and
|
|
6
|
+
# `IOStreams::Writer` extend, so that each of their subclasses answers them. `#option`, `#stream` and
|
|
7
|
+
# `#option_or_stream` report a deprecation warning for an option that IOStreams 3.0 raises ArgumentError for when it
|
|
8
|
+
# is set: one that the reader or the writer of the stream replaces, see #replaced_option_names, and one that neither
|
|
9
|
+
# of them takes, see #known_option_names, such as a misspelled one, see `IOStreams::Builder#option`.
|
|
10
|
+
module OptionNames
|
|
11
|
+
# Returns [Array<Symbol>] the names of the options that this class takes in IOStreams 3.0 too, or [nil] when they
|
|
12
|
+
# are not known, since it does not declare its options: by default those that it declares, see `.option_names`,
|
|
13
|
+
# other than those that it replaces, see #replaced_option_names.
|
|
14
|
+
def known_option_names
|
|
15
|
+
names = option_names
|
|
16
|
+
names && (names - replaced_option_names.keys)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns [Hash<Symbol, Symbol>] the deprecated options that this class still takes, which IOStreams 3.0 does not,
|
|
20
|
+
# with the option that replaces each, which the deprecation warning names. None by default.
|
|
21
|
+
def replaced_option_names
|
|
22
|
+
{}
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|