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
|
@@ -35,41 +35,134 @@ module IOStreams
|
|
|
35
35
|
# # => "files/file.xls"
|
|
36
36
|
#
|
|
37
37
|
# For Files
|
|
38
|
-
# IOStreams.path('blah.zip').
|
|
39
|
-
# IOStreams.path('blah.zip').
|
|
40
|
-
# IOStreams.path('blah.zip').
|
|
41
|
-
# IOStreams.path('blah.zip').
|
|
38
|
+
# IOStreams.path('blah.zip').encoding('BINARY').each(:line) { |line| puts line }
|
|
39
|
+
# IOStreams.path('blah.zip').encoding('UTF-8').each(:line) { |line| puts line }
|
|
40
|
+
# IOStreams.path('blah.zip').encoding('UTF-8').each(:hash) { |hash| p hash }
|
|
41
|
+
# IOStreams.path('blah.zip').encoding('UTF-8').read
|
|
42
42
|
# IOStreams.path('blah.csv.zip').each(:line) { |line| puts line }
|
|
43
43
|
# IOStreams.path('blah.zip').option(:pgp, passphrase: 'receiver_passphrase').read
|
|
44
44
|
# IOStreams.path('blah.zip').stream(:zip).stream(:pgp, passphrase: 'receiver_passphrase').read
|
|
45
|
-
# IOStreams.path('blah.zip').stream(:zip).
|
|
45
|
+
# IOStreams.path('blah.zip').stream(:zip).encoding('BINARY').read
|
|
46
46
|
#
|
|
47
|
+
# A name that IOStreams v3.0 reads as another file or url, such as a local name starting with `~/`, a `file://` url, or
|
|
48
|
+
# an SFTP or HTTP url without a path, is deprecated, and reports a deprecation warning, see
|
|
49
|
+
# `IOStreams::Path.name_deprecation`.
|
|
47
50
|
def self.path(*elements, **args)
|
|
51
|
+
build_path(elements, args, :name_deprecation)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Returns [Hash] the options for creating the path of the url with `IOStreams.path(url, **options)`, with the value
|
|
55
|
+
# of each sensitive option, such as a password, replaced with "[FILTERED]", so that they can be displayed, for
|
|
56
|
+
# example by an application that stores a url and its options to create the path later.
|
|
57
|
+
#
|
|
58
|
+
# The path class of the url's scheme decides which options are sensitive, see `IOStreams::Path.redact_options`.
|
|
59
|
+
# Every value is replaced when the url is not valid, since then that class is not known, or when the class
|
|
60
|
+
# registered for the scheme, see `IOStreams.register_scheme`, cannot say which are sensitive.
|
|
61
|
+
#
|
|
62
|
+
# Example:
|
|
63
|
+
# IOStreams.redact_path_options("sftp://example.org/a.csv", username: "jack", password: "secret")
|
|
64
|
+
# # => {username: "jack", password: "[FILTERED]"}
|
|
65
|
+
def self.redact_path_options(url, options)
|
|
66
|
+
klass = Paths.path_class(url.to_s)
|
|
67
|
+
return klass.redact_options(options) if klass.respond_to?(:redact_options)
|
|
68
|
+
|
|
69
|
+
options.to_h { |name, _value| [name, Utils::FILTERED] }
|
|
70
|
+
rescue ArgumentError, ::URI::Error
|
|
71
|
+
options.to_h { |name, _value| [name, Utils::FILTERED] }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Returns [Hash<Symbol:Hash>] the options of each stream, as supplied to `IOStreams::Path#stream`, with the value of
|
|
75
|
+
# each sensitive option, such as a PGP passphrase, replaced with "[FILTERED]", so that they can be displayed.
|
|
76
|
+
#
|
|
77
|
+
# The extension registered for each stream decides which of its options are sensitive, see
|
|
78
|
+
# `IOStreams::Extension#redact_options`, as it does for `#inspect`. Every value of a stream that is not registered is
|
|
79
|
+
# replaced, as are options that are not a Hash.
|
|
80
|
+
#
|
|
81
|
+
# Example:
|
|
82
|
+
# IOStreams.redact_stream_options(pgp: {passphrase: "secret", recipient: "a@b.org"})
|
|
83
|
+
# # => {pgp: {passphrase: "[FILTERED]", recipient: "a@b.org"}}
|
|
84
|
+
def self.redact_stream_options(streams)
|
|
85
|
+
Utils.redact_streams(streams) { |name| @extensions[name.to_s.to_sym] }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Returns [IOStreams::Path] the path of the supplied elements, joined into one name or url, created with the supplied
|
|
89
|
+
# arguments by the path class of its scheme, see `IOStreams::Paths.build`, reporting the deprecation warning, if any,
|
|
90
|
+
# that the supplied class method of that class gives for the name, see `IOStreams::Path.name_deprecation`, as the
|
|
91
|
+
# application supplied it.
|
|
92
|
+
def self.build_path(elements, args, deprecation)
|
|
48
93
|
return elements.first if (elements.size == 1) && args.empty? && elements.first.is_a?(IOStreams::Path)
|
|
49
94
|
|
|
50
|
-
|
|
51
|
-
path
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
95
|
+
name = Utils::PathName.join(*elements.collect(&:to_s))
|
|
96
|
+
path = Paths.build(name, **args)
|
|
97
|
+
warn_deprecated_name(path.class, deprecation, name)
|
|
98
|
+
path
|
|
99
|
+
end
|
|
100
|
+
private_class_method :build_path
|
|
101
|
+
|
|
102
|
+
# Reports the deprecation warning that the supplied class method of the path class gives for a name or url that the
|
|
103
|
+
# application supplied, if any: `:name_deprecation` when IOStreams v3.0 reads it as another file or url, or
|
|
104
|
+
# `:location_deprecation` when it reads it as another location, which the allowed paths compare, see
|
|
105
|
+
# `IOStreams::Path.name_deprecation`.
|
|
106
|
+
def self.warn_deprecated_name(klass, deprecation, name)
|
|
107
|
+
message = klass.public_send(deprecation, name) if klass.respond_to?(deprecation)
|
|
108
|
+
Deprecation.warn(message) if message
|
|
109
|
+
end
|
|
110
|
+
private_class_method :warn_deprecated_name
|
|
111
|
+
|
|
112
|
+
# Reports a deprecation warning for the name of the directory of a pattern, see .warn_deprecated_name. A pattern that
|
|
113
|
+
# is not a valid url, such as one with braces, is matched within the directory before its first pattern character,
|
|
114
|
+
# which the warnings do not apply to, see `IOStreams::Paths::Matcher`.
|
|
115
|
+
def self.warn_deprecated_pattern(pattern)
|
|
116
|
+
klass = Paths.path_class(pattern)
|
|
117
|
+
rescue ArgumentError, ::URI::Error
|
|
118
|
+
nil
|
|
119
|
+
else
|
|
120
|
+
warn_deprecated_name(klass, :name_deprecation, pattern)
|
|
121
|
+
end
|
|
122
|
+
private_class_method :warn_deprecated_pattern
|
|
123
|
+
|
|
124
|
+
# Returns [true|false] whether the object is a `Pathname`, which IOStreams v3.0 reads as the name of a file.
|
|
125
|
+
def self.pathname?(object)
|
|
126
|
+
return false unless defined?(::Pathname)
|
|
127
|
+
|
|
128
|
+
object.is_a?(::Pathname)
|
|
55
129
|
end
|
|
130
|
+
private_class_method :pathname?
|
|
56
131
|
|
|
57
132
|
# For an existing IO Stream
|
|
58
133
|
# IOStreams.stream(io).file_name('blah.zip').encoding('BINARY').read
|
|
59
134
|
# IOStreams.stream(io).file_name('blah.zip').encoding('BINARY').each(:line){ ... }
|
|
60
135
|
# IOStreams.stream(io).file_name('blah.csv.zip').each(:line) { ... }
|
|
61
136
|
# IOStreams.stream(io).stream(:zip).stream(:pgp, passphrase: 'receiver_passphrase').read
|
|
137
|
+
#
|
|
138
|
+
# A `Pathname` is deprecated, since IOStreams v3.0 raises ArgumentError for it, as for a String: it reports a
|
|
139
|
+
# deprecation warning, see `IOStreams::Deprecation`.
|
|
62
140
|
def self.stream(io_stream)
|
|
63
141
|
return io_stream if io_stream.is_a?(Stream)
|
|
64
142
|
|
|
143
|
+
if pathname?(io_stream)
|
|
144
|
+
Deprecation.warn("a Pathname supplied to IOStreams.stream is deprecated, since v3.0 raises ArgumentError for it, " \
|
|
145
|
+
"as for a String: supply IOStreams.path(pathname) instead, and call #raw on it to read or " \
|
|
146
|
+
"write the data as it is stored")
|
|
147
|
+
end
|
|
65
148
|
Stream.new(io_stream)
|
|
66
149
|
end
|
|
67
150
|
|
|
68
151
|
# For processing by either a file name or an open IO stream.
|
|
152
|
+
#
|
|
153
|
+
# A `Pathname` is deprecated, which it reads and writes as an IO, since IOStreams v3.0 returns the path of the file
|
|
154
|
+
# that it names, which applies the streams of its file name: it reports a deprecation warning, see
|
|
155
|
+
# `IOStreams::Deprecation`. So is one supplied to `#copy_from`, `#copy_to` or `#move_to`, which call it.
|
|
69
156
|
def self.new(file_name_or_io)
|
|
70
157
|
return file_name_or_io if file_name_or_io.is_a?(Stream)
|
|
71
|
-
|
|
72
|
-
|
|
158
|
+
return path(file_name_or_io) if file_name_or_io.is_a?(String)
|
|
159
|
+
return stream(file_name_or_io) unless pathname?(file_name_or_io)
|
|
160
|
+
|
|
161
|
+
Deprecation.warn("a Pathname supplied to IOStreams.new, #copy_from, #copy_to or #move_to is deprecated, since " \
|
|
162
|
+
"v3.0 takes it as the name of a file, applying the streams that its file name implies, rather " \
|
|
163
|
+
"than as an IO: supply pathname.to_s instead, and call #raw on its path to read or write the " \
|
|
164
|
+
"data as it is stored")
|
|
165
|
+
Stream.new(file_name_or_io)
|
|
73
166
|
end
|
|
74
167
|
|
|
75
168
|
# Join the supplied path elements to a root path.
|
|
@@ -119,8 +212,17 @@ module IOStreams
|
|
|
119
212
|
#
|
|
120
213
|
# Example:
|
|
121
214
|
# IOStreams.temp_file("export", ".csv") { |path| path.write("Hello World") }
|
|
215
|
+
#
|
|
216
|
+
# Note: The temp file is accessible even when it is not within the allowed paths, see `IOStreams.add_allowed_path`.
|
|
217
|
+
#
|
|
218
|
+
# The path reads and writes its data as it is stored, whatever its extension, while IOStreams v3.0 applies the streams
|
|
219
|
+
# that its extension implies, such as `.gz`, so reading or writing it without #raw, which reads and writes the data
|
|
220
|
+
# as it is stored on both, is deprecated when its extension implies streams, and reports a deprecation warning, see
|
|
221
|
+
# `IOStreams::Deprecation`.
|
|
122
222
|
def self.temp_file(basename, extension = "")
|
|
123
|
-
Utils.temp_file_name(basename, extension)
|
|
223
|
+
Utils.temp_file_name(basename, extension) do |file_name|
|
|
224
|
+
yield(Paths::File.new(file_name).send(:permit!).send(:no_streams, temp_file: true))
|
|
225
|
+
end
|
|
124
226
|
end
|
|
125
227
|
|
|
126
228
|
# Returns [IOStreams::Paths::File] current or named users home path
|
|
@@ -215,6 +317,7 @@ module IOStreams
|
|
|
215
317
|
# "**.rb" "lib/song.rb" true
|
|
216
318
|
# "*" "dave/.profile" true
|
|
217
319
|
def self.each_child(pattern, case_sensitive: false, directories: false, hidden: false, &block)
|
|
320
|
+
warn_deprecated_pattern(pattern)
|
|
218
321
|
matcher = Paths::Matcher.new(nil, pattern, case_sensitive: case_sensitive, hidden: hidden)
|
|
219
322
|
|
|
220
323
|
# When the pattern includes an exact file name without any pattern characters
|
|
@@ -231,16 +334,97 @@ module IOStreams
|
|
|
231
334
|
end
|
|
232
335
|
|
|
233
336
|
# Add a named root path
|
|
337
|
+
#
|
|
338
|
+
# Returns [IOStreams::Path] the root path, which the whole process shares, see `IOStreams.join`. Changing it in place,
|
|
339
|
+
# such as with `#stream` or `#option`, whether through this path, a path supplied as the root, `IOStreams.root`,
|
|
340
|
+
# `IOStreams.roots`, or `IOStreams.join` without elements, is deprecated, and reports a deprecation warning, since v3.0
|
|
341
|
+
# keeps its own frozen copy of the root, which this method returns, and the others return copies of it, so that
|
|
342
|
+
# changing them no longer changes the root: change a copy of it instead, such as `IOStreams.root(:uploads).dup`, or a
|
|
343
|
+
# path joined to it, such as `IOStreams.join("a.csv", root: :uploads)`.
|
|
234
344
|
def self.add_root(root, *elements, **args)
|
|
235
345
|
raise(ArgumentError, "Invalid characters in root name #{root.inspect}") unless root.to_s =~ /\A\w+\Z/
|
|
236
346
|
|
|
237
|
-
|
|
347
|
+
path = path(*elements, **args)
|
|
348
|
+
# The path class of a scheme registered with `IOStreams.register_scheme` need not be an `IOStreams::Path`.
|
|
349
|
+
path.send(:root!, root.to_sym) if path.is_a?(Path)
|
|
350
|
+
@root_paths[root.to_sym] = path
|
|
238
351
|
end
|
|
239
352
|
|
|
240
353
|
def self.roots
|
|
241
354
|
@root_paths.dup
|
|
242
355
|
end
|
|
243
356
|
|
|
357
|
+
# Restrict IOStreams to only access paths within the supplied path.
|
|
358
|
+
#
|
|
359
|
+
# Once any allowed path has been added, reading, writing, listing, deleting or otherwise accessing
|
|
360
|
+
# a path that is not within one of the allowed paths raises `IOStreams::Errors::AccessDenied`.
|
|
361
|
+
# This prevents an untrusted file name, for example one supplied by a user, from accessing anything
|
|
362
|
+
# else that the process can access.
|
|
363
|
+
#
|
|
364
|
+
# Parameters: Same as `IOStreams.path`
|
|
365
|
+
#
|
|
366
|
+
# Returns [String] the normalized path that was added, against which paths are compared.
|
|
367
|
+
#
|
|
368
|
+
# Example:
|
|
369
|
+
# IOStreams.add_allowed_path("/var/data/uploads")
|
|
370
|
+
# IOStreams.add_allowed_path("s3://my-bucket/exports")
|
|
371
|
+
#
|
|
372
|
+
# IOStreams.path("/var/data/uploads/file.csv").read
|
|
373
|
+
# IOStreams.path("/etc/passwd").read
|
|
374
|
+
# # => IOStreams::Errors::AccessDenied
|
|
375
|
+
#
|
|
376
|
+
# Notes:
|
|
377
|
+
# * By default no allowed paths are added, and every path is accessible.
|
|
378
|
+
# * Add allowed paths in an initializer at startup, where they cannot be changed by untrusted input.
|
|
379
|
+
# * Paths are normalized before they are compared, so `..` cannot be used to leave an allowed path:
|
|
380
|
+
# * Local file names are resolved to their real path, following symbolic links.
|
|
381
|
+
# A relative path is resolved against the current working directory when it is added.
|
|
382
|
+
# * For S3 the bucket must match, and keys containing `.` or `..` segments are denied.
|
|
383
|
+
# * For SFTP and HTTP the host and port must match, and `.` and `..` segments are resolved.
|
|
384
|
+
# * `#each_child` skips children that are not within the allowed paths, for example a symbolic link
|
|
385
|
+
# to a file elsewhere.
|
|
386
|
+
# * Temp files created by `IOStreams.temp_file` are always accessible.
|
|
387
|
+
# * Paths from a scheme registered with `IOStreams.register_scheme` are denied, unless its path class
|
|
388
|
+
# implements the private method `#allowed_location`.
|
|
389
|
+
# * A local file could be replaced with a symbolic link after it is checked but before it is opened.
|
|
390
|
+
# Allowed paths do not prevent this, so do not allow paths where untrusted users can create files.
|
|
391
|
+
def self.add_allowed_path(*elements, **args)
|
|
392
|
+
location = allowed_location(build_path(elements, args, :location_deprecation))
|
|
393
|
+
@allowed_paths_mutex.synchronize { @allowed_paths = (@allowed_paths + [location]).uniq.freeze }
|
|
394
|
+
location
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
# Removes a path previously added with `IOStreams.add_allowed_path`.
|
|
398
|
+
#
|
|
399
|
+
# Returns [String] the normalized path that was removed.
|
|
400
|
+
def self.delete_allowed_path(*elements, **args)
|
|
401
|
+
location = allowed_location(build_path(elements, args, :location_deprecation))
|
|
402
|
+
@allowed_paths_mutex.synchronize { @allowed_paths = (@allowed_paths - [location]).freeze }
|
|
403
|
+
location
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
# Returns [Array<String>] the normalized allowed paths, see `IOStreams.add_allowed_path`.
|
|
407
|
+
def self.allowed_paths
|
|
408
|
+
@allowed_paths
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# Returns [true|false] whether the supplied path can be accessed, see `IOStreams.add_allowed_path`.
|
|
412
|
+
#
|
|
413
|
+
# Always true when no allowed paths have been added.
|
|
414
|
+
#
|
|
415
|
+
# Parameters: Same as `IOStreams.path`
|
|
416
|
+
def self.allowed_path?(*elements, **args)
|
|
417
|
+
build_path(elements, args, :location_deprecation).send(:allowed?)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def self.allowed_location(path)
|
|
421
|
+
path.send(:allowed_location)
|
|
422
|
+
rescue Errors::AccessDenied => e
|
|
423
|
+
raise(ArgumentError, e.message)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
private_class_method :allowed_location
|
|
427
|
+
|
|
244
428
|
# Set the temporary path to use when creating local temp files.
|
|
245
429
|
def self.temp_dir=(temp_dir)
|
|
246
430
|
temp_dir = File.expand_path(temp_dir)
|
|
@@ -259,6 +443,54 @@ module IOStreams
|
|
|
259
443
|
|
|
260
444
|
@temp_dir = nil
|
|
261
445
|
|
|
446
|
+
# Apply `allowed_columns`, `required_columns` and `skip_unknown` to every input when reading records.
|
|
447
|
+
#
|
|
448
|
+
# When false, they only apply to a header row read from the file, and only when `cleanse_header` is true.
|
|
449
|
+
# They are ignored for JSON and `:hash` input, when `columns:` is supplied, and with `cleanse_header: false`,
|
|
450
|
+
# and a warning is logged when applying them would change the result.
|
|
451
|
+
#
|
|
452
|
+
# When true, they also apply to the supplied `columns:`, to a header row read with `cleanse_header: false`,
|
|
453
|
+
# and to the keys of each JSON or `:hash` record. JSON keys are cleansed like a header row, unless
|
|
454
|
+
# `cleanse_header` is false, unknown keys are skipped or raise `IOStreams::Errors::InvalidHeader`,
|
|
455
|
+
# and a record missing a required column raises `IOStreams::Errors::InvalidHeader`.
|
|
456
|
+
#
|
|
457
|
+
# Since the format is usually inferred from the file name, set this to true when the allowed columns
|
|
458
|
+
# restrict what an uploaded file can set, so that renaming the file to `.json` cannot bypass them.
|
|
459
|
+
#
|
|
460
|
+
# Default: false.
|
|
461
|
+
#
|
|
462
|
+
# Example:
|
|
463
|
+
# IOStreams.enforce_column_restrictions = true
|
|
464
|
+
#
|
|
465
|
+
# Deprecated: it only applies to the column options, which are deprecated, see `IOStreams::Stream#each`, and which
|
|
466
|
+
# v3.0 removes, with this setting. Describe the columns of the file with `IOStreams::Stream#schema` instead, whose
|
|
467
|
+
# `fieldsMatch` and `constraints` apply to every input. It still applies to the column options, and reports a
|
|
468
|
+
# deprecation warning, see `IOStreams::Deprecation`, so keep it until they are replaced with a schema. The setting
|
|
469
|
+
# is held by `IOStreams::Tabular::HeaderMapping`, which applies it, and reads it without a deprecation warning.
|
|
470
|
+
def self.enforce_column_restrictions=(enforce)
|
|
471
|
+
raise(ArgumentError, "enforce_column_restrictions must be true or false") unless [true, false].include?(enforce)
|
|
472
|
+
|
|
473
|
+
Deprecation.warn("IOStreams.enforce_column_restrictions= #{ENFORCE_COLUMN_RESTRICTIONS_DEPRECATION}")
|
|
474
|
+
Tabular::HeaderMapping.enforce_column_restrictions = enforce
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
# Returns [true|false] whether column restrictions apply to every input, see `IOStreams.enforce_column_restrictions=`.
|
|
478
|
+
#
|
|
479
|
+
# Deprecated, as `IOStreams.enforce_column_restrictions=` is. It reports a deprecation warning, see
|
|
480
|
+
# `IOStreams::Deprecation`.
|
|
481
|
+
def self.enforce_column_restrictions?
|
|
482
|
+
Deprecation.warn("IOStreams.enforce_column_restrictions? #{ENFORCE_COLUMN_RESTRICTIONS_DEPRECATION}")
|
|
483
|
+
Tabular::HeaderMapping.enforce_column_restrictions?
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
# Why `IOStreams.enforce_column_restrictions=` and `IOStreams.enforce_column_restrictions?` are deprecated, and what
|
|
487
|
+
# to use instead.
|
|
488
|
+
ENFORCE_COLUMN_RESTRICTIONS_DEPRECATION =
|
|
489
|
+
"is deprecated, since it only applies to the column options, such as allowed_columns:, which are deprecated: " \
|
|
490
|
+
"describe the columns of the file with #schema instead, whose fieldsMatch and constraints apply to every input, " \
|
|
491
|
+
"and then remove it".freeze
|
|
492
|
+
private_constant :ENFORCE_COLUMN_RESTRICTIONS_DEPRECATION
|
|
493
|
+
|
|
262
494
|
# Returns [Logger] the logger used by IOStreams for debug logging.
|
|
263
495
|
#
|
|
264
496
|
# When SemanticLogger is loaded a SemanticLogger instance is used by default,
|
|
@@ -278,32 +510,110 @@ module IOStreams
|
|
|
278
510
|
|
|
279
511
|
# Register a file extension and the reader and writer streaming classes
|
|
280
512
|
#
|
|
513
|
+
# Returns [IOStreams::Extension] the extension registered, whose setters, such as `writer_class=`, are deprecated,
|
|
514
|
+
# see `IOStreams::Extension::Deprecations`.
|
|
515
|
+
#
|
|
516
|
+
# Registering `:encode` or `:none`, in any case, is deprecated, see RESERVED_EXTENSIONS, as is a name that IOStreams
|
|
517
|
+
# 3.0 does not register as it is, see .extension_name_deprecation: one that ends with a line break, or that is not in
|
|
518
|
+
# lower case. It still registers the extension, and reports a deprecation warning, see `IOStreams::Deprecation`.
|
|
519
|
+
#
|
|
281
520
|
# Example:
|
|
282
521
|
# # MyXls::Reader and MyXls::Writer must implement .open
|
|
283
522
|
# register_extension(:xls, MyXls::Reader, MyXls::Writer)
|
|
284
523
|
def self.register_extension(extension, reader_class, writer_class)
|
|
285
524
|
raise(ArgumentError, "Invalid extension #{extension.inspect}") unless extension.nil? || extension.to_s =~ /\A\w+\Z/
|
|
286
525
|
|
|
287
|
-
|
|
526
|
+
if (reason = extension_name_deprecation(extension, RESERVED_EXTENSIONS))
|
|
527
|
+
Deprecation.warn("IOStreams.register_extension(#{extension.inspect}, ...) is deprecated, since #{reason}")
|
|
528
|
+
end
|
|
529
|
+
@extensions[extension&.to_sym] = Extension.registered(reader_class, writer_class)
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
# The names that IOStreams 3.0 reserves, so that registering an extension of one of them, in any case, which it raises
|
|
533
|
+
# ArgumentError for, is deprecated, see .register_extension, with why, and what to do instead.
|
|
534
|
+
RESERVED_EXTENSIONS = {
|
|
535
|
+
encode: "the encode stream is built in, which IOStreams 3.0 does not let an extension replace: register the " \
|
|
536
|
+
"reader and writer under another name instead, and apply them with #stream, or set the encoding of the " \
|
|
537
|
+
"text with #encoding",
|
|
538
|
+
none: "IOStreams 3.0 reserves :none, which stream(:none) took to apply no streams, register the stream under " \
|
|
539
|
+
"another name instead"
|
|
540
|
+
}.freeze
|
|
541
|
+
private_constant :RESERVED_EXTENSIONS
|
|
542
|
+
|
|
543
|
+
# Registers an extension that IOStreams builds in, as .register_extension does, whose methods of a Struct, other than
|
|
544
|
+
# `reader_class` and `writer_class`, are deprecated too, see `IOStreams::Extension::Deprecations`.
|
|
545
|
+
def self.register_built_in_extension(extension, reader_class, writer_class)
|
|
546
|
+
@extensions[extension] = Extension.registered(reader_class, writer_class, built_in: true)
|
|
288
547
|
end
|
|
548
|
+
private_class_method :register_built_in_extension
|
|
289
549
|
|
|
290
550
|
# De-Register a file extension
|
|
291
551
|
#
|
|
292
|
-
# Returns [
|
|
552
|
+
# Returns [IOStreams::Extension] the extension removed, or nil if the extension was not registered
|
|
553
|
+
#
|
|
554
|
+
# Deregistering `:encode`, in any case, is deprecated, since the encode stream is built in, which IOStreams 3.0 does
|
|
555
|
+
# not deregister, see BUILT_IN_STREAMS. It still removes it, so that `#encoding` then raises ArgumentError. So is a
|
|
556
|
+
# name that IOStreams 3.0 does not deregister as it is, see .extension_name_deprecation: one that ends with a line
|
|
557
|
+
# break, or that is not in lower case. Each reports a deprecation warning, see `IOStreams::Deprecation`.
|
|
293
558
|
#
|
|
294
559
|
# Example:
|
|
295
560
|
# deregister_extension(:xls)
|
|
296
561
|
def self.deregister_extension(extension)
|
|
297
562
|
raise(ArgumentError, "Invalid extension #{extension.inspect}") unless extension.to_s =~ /\A\w+\Z/
|
|
298
563
|
|
|
564
|
+
if (reason = extension_name_deprecation(extension, BUILT_IN_STREAMS))
|
|
565
|
+
Deprecation.warn("IOStreams.deregister_extension(#{extension.inspect}) is deprecated, since #{reason}")
|
|
566
|
+
end
|
|
299
567
|
@extensions.delete(extension.to_sym)
|
|
300
568
|
end
|
|
301
569
|
|
|
302
|
-
#
|
|
570
|
+
# The streams that IOStreams 3.0 builds in, rather than registers, so that deregistering one of them, in any case,
|
|
571
|
+
# which it does not deregister, is deprecated, see .deregister_extension, with why, and what to do instead.
|
|
572
|
+
BUILT_IN_STREAMS = {
|
|
573
|
+
encode: "the encode stream is built in, which IOStreams 3.0 does not deregister, so remove it: IOStreams 3.0 " \
|
|
574
|
+
"applies the encode stream only where #encoding sets it"
|
|
575
|
+
}.freeze
|
|
576
|
+
private_constant :BUILT_IN_STREAMS
|
|
577
|
+
|
|
578
|
+
# Returns [String|nil] why the name of an extension, as the application supplied it to .register_extension or
|
|
579
|
+
# .deregister_extension, is deprecated, or nil when IOStreams 3.0 takes it as it is:
|
|
580
|
+
# - A name that ends with a line break, such as `"xz\n"`, which the check of the name accepts, while IOStreams 3.0
|
|
581
|
+
# raises ArgumentError for it.
|
|
582
|
+
# - A name of the supplied reasons, in any case, such as RESERVED_EXTENSIONS, with the reason that it gives.
|
|
583
|
+
# - A name that is not in lower case, such as `:XZ`, since IOStreams 3.0 registers and deregisters it in lower case,
|
|
584
|
+
# as `:xz`, which matches the file names that end in `.xz` in any case, as their extensions are compared.
|
|
585
|
+
def self.extension_name_deprecation(extension, reasons)
|
|
586
|
+
name = extension.to_s
|
|
587
|
+
lower = name.downcase
|
|
588
|
+
if name.end_with?("\n")
|
|
589
|
+
"IOStreams 3.0 raises ArgumentError for a name that ends with a line break, remove the line break"
|
|
590
|
+
elsif reasons.key?(lower.to_sym)
|
|
591
|
+
reasons[lower.to_sym]
|
|
592
|
+
elsif name != lower
|
|
593
|
+
"IOStreams 3.0 takes the name of an extension in lower case, as #{lower.to_sym.inspect}, which matches the file " \
|
|
594
|
+
"names that end in .#{lower}, in any case, use #{lower.to_sym.inspect} instead"
|
|
595
|
+
end
|
|
596
|
+
end
|
|
597
|
+
private_class_method :extension_name_deprecation
|
|
598
|
+
|
|
599
|
+
# Returns [Hash<Symbol, IOStreams::Extension>] the extension registered for each file extension, see
|
|
600
|
+
# .register_extension.
|
|
601
|
+
#
|
|
602
|
+
# Each extension answers `reader_class` and `writer_class`, as the format that IOStreams 3.0 returns for each of
|
|
603
|
+
# its built-in extensions does, such as `IOStreams::Gzip` for `:gz`. Its other methods of a Struct, such as `to_a`,
|
|
604
|
+
# `to_h` or `[]`, are deprecated for a built-in extension, and its setters, such as `writer_class=`, for every
|
|
605
|
+
# extension, see `IOStreams::Extension::Deprecations`.
|
|
303
606
|
def self.extensions
|
|
304
607
|
@extensions.dup
|
|
305
608
|
end
|
|
306
609
|
|
|
610
|
+
# Returns [Array<Symbol>] the names of the formats of the rows and records of a file that `IOStreams::Stream#format`
|
|
611
|
+
# accepts, such as `:csv`, `:json` or `:fixed`, frozen. The list grows as formats are added, so check whether a name
|
|
612
|
+
# is included, rather than rely on its order or its length.
|
|
613
|
+
def self.tabular_formats
|
|
614
|
+
Tabular.registered_formats.freeze
|
|
615
|
+
end
|
|
616
|
+
|
|
307
617
|
# Register a URI scheme and the path class that handles it
|
|
308
618
|
#
|
|
309
619
|
# Example:
|
|
@@ -322,26 +632,140 @@ module IOStreams
|
|
|
322
632
|
@schemes[scheme_name&.to_sym] || raise(ArgumentError, "Unknown Scheme type: #{scheme_name.inspect}")
|
|
323
633
|
end
|
|
324
634
|
|
|
325
|
-
|
|
635
|
+
# The reader and writer classes registered for a file extension, see `IOStreams.register_extension`.
|
|
636
|
+
Extension = Struct.new(:reader_class, :writer_class) do
|
|
637
|
+
# Returns [Array<Symbol>] the options whose values must not be displayed, such as a passphrase, as declared
|
|
638
|
+
# by the reader and the writer with `sensitive_option_names`.
|
|
639
|
+
def sensitive_option_names
|
|
640
|
+
[reader_class, writer_class].flat_map do |klass|
|
|
641
|
+
(klass.sensitive_option_names if klass.respond_to?(:sensitive_option_names)) || []
|
|
642
|
+
end.uniq
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
# Returns [Hash] the options with the value of each sensitive option replaced with "[FILTERED]", so that
|
|
646
|
+
# they can be displayed, for example by `#inspect`.
|
|
647
|
+
#
|
|
648
|
+
# Sensitive options are those that the reader or the writer declares, see #sensitive_option_names, and,
|
|
649
|
+
# as a precaution, any option whose name suggests a secret, such as one that contains `password` or `token`, see
|
|
650
|
+
# `IOStreams::Utils.redact_options`, which owns that list.
|
|
651
|
+
def redact_options(options)
|
|
652
|
+
Utils.redact_options(options, sensitive_option_names)
|
|
653
|
+
end
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
class Extension
|
|
657
|
+
# Returns [IOStreams::Extension] a new extension of the supplied reader and writer classes, as
|
|
658
|
+
# `IOStreams.register_extension` registers it, whose setters are deprecated, or as IOStreams registers one that it
|
|
659
|
+
# builds in (built_in: true), whose other methods of a Struct are deprecated too, see Deprecations.
|
|
660
|
+
def self.registered(reader_class, writer_class, built_in: false)
|
|
661
|
+
new(reader_class, writer_class).tap { |extension| extension.send(:registered!, built_in) }
|
|
662
|
+
end
|
|
663
|
+
|
|
664
|
+
# Reports a deprecation warning, see `IOStreams::Deprecation`, for the methods of a Struct of a registered extension
|
|
665
|
+
# that IOStreams 3.0 does not answer, see .registered, and then does as before. It is prepended to the class, rather
|
|
666
|
+
# than extended onto each extension, so that an extension keeps its class, which JRuby's `Struct#eql?` compares.
|
|
667
|
+
#
|
|
668
|
+
# - The setters of a registered extension, such as `writer_class=`, and `[]=`, change the extension registered for
|
|
669
|
+
# the whole process, since `IOStreams.extensions` returns a copy of the registry that shares its extensions, which
|
|
670
|
+
# IOStreams 3.0 freezes. They still report one once it is no longer registered, such as one that
|
|
671
|
+
# `IOStreams.deregister_extension` returned, see #setter_deprecation, since IOStreams 3.0 raises for them there too.
|
|
672
|
+
# - For an extension that IOStreams builds in, such as `:gz`, IOStreams 3.0 returns the format that it registers,
|
|
673
|
+
# such as `IOStreams::Gzip`, rather than a Struct, see `IOStreams.extensions`. The format answers `reader_class`
|
|
674
|
+
# and `writer_class`, but none of the other methods of a Struct, such as #to_a, nor those of `Enumerable`, which
|
|
675
|
+
# call #each.
|
|
676
|
+
#
|
|
677
|
+
# An extension that is not registered, such as one that an application creates, reports nothing.
|
|
678
|
+
module Deprecations
|
|
679
|
+
# The methods that change an extension.
|
|
680
|
+
SETTERS = %i[reader_class= writer_class= []=].freeze
|
|
681
|
+
|
|
682
|
+
# The methods of a Struct that read an extension, other than `reader_class` and `writer_class`, and other than
|
|
683
|
+
# those that every object answers, such as `==` and `inspect`.
|
|
684
|
+
STRUCT_METHODS = %i[[] to_a values deconstruct to_h each each_pair members size length values_at select filter dig
|
|
685
|
+
deconstruct_keys].freeze
|
|
686
|
+
|
|
687
|
+
SETTERS.each do |name|
|
|
688
|
+
define_method(name) do |*args|
|
|
689
|
+
Deprecation.warn("##{name} #{setter_deprecation}") if @registered
|
|
690
|
+
super(*args)
|
|
691
|
+
end
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
STRUCT_METHODS.each do |name|
|
|
695
|
+
define_method(name) do |*args, &block|
|
|
696
|
+
if @built_in
|
|
697
|
+
Deprecation.warn("##{name} on a built-in extension of IOStreams.extensions is deprecated, since IOStreams " \
|
|
698
|
+
"3.0 returns its format, such as IOStreams::Gzip for :gz, rather than a Struct, use " \
|
|
699
|
+
"#reader_class and #writer_class instead")
|
|
700
|
+
end
|
|
701
|
+
super(*args, &block)
|
|
702
|
+
end
|
|
703
|
+
end
|
|
704
|
+
|
|
705
|
+
# Prints the extension for `pp` as it prints any other Struct, but reads each member with its reader, such as
|
|
706
|
+
# `reader_class`, rather than with #[], as `pp` does.
|
|
707
|
+
def pretty_print(printer)
|
|
708
|
+
printer.group(1, "#<struct #{self.class.name}", ">") do
|
|
709
|
+
printer.seplist(self.class.members, -> { printer.text(",") }) do |member|
|
|
710
|
+
printer.breakable
|
|
711
|
+
printer.text("#{member}=")
|
|
712
|
+
printer.group(1) do
|
|
713
|
+
printer.breakable("")
|
|
714
|
+
printer.pp(public_send(member))
|
|
715
|
+
end
|
|
716
|
+
end
|
|
717
|
+
end
|
|
718
|
+
end
|
|
719
|
+
|
|
720
|
+
private
|
|
721
|
+
|
|
722
|
+
# Marks this extension as registered, see .registered, by IOStreams itself when built_in.
|
|
723
|
+
def registered!(built_in)
|
|
724
|
+
@registered = true
|
|
725
|
+
@built_in = built_in
|
|
726
|
+
end
|
|
727
|
+
|
|
728
|
+
# Returns [String] why a setter of this extension, which was registered, see .registered, is deprecated, after the
|
|
729
|
+
# name of the setter: while it is registered, that the setter changes the extension registered for the whole
|
|
730
|
+
# process, and once it is not, such as after `IOStreams.deregister_extension` removed it, or another extension was
|
|
731
|
+
# registered in its place, that IOStreams 3.0 does not allow it either, since it freezes the extension that it
|
|
732
|
+
# registers, and returns the format of one that it builds in, such as `IOStreams::Gzip`, which has no setters.
|
|
733
|
+
def setter_deprecation
|
|
734
|
+
if IOStreams.extensions.each_value.any? { |extension| extension.equal?(self) }
|
|
735
|
+
"on an extension of IOStreams.extensions is deprecated, since it changes the registered extension for the " \
|
|
736
|
+
"whole process, which IOStreams 3.0 does not allow, use IOStreams.register_extension to change it instead"
|
|
737
|
+
else
|
|
738
|
+
"on an extension that is no longer registered, such as one that IOStreams.deregister_extension returned, is " \
|
|
739
|
+
"deprecated, since IOStreams 3.0 does not allow it on an extension that it registered, supply the classes " \
|
|
740
|
+
"to IOStreams.register_extension instead"
|
|
741
|
+
end
|
|
742
|
+
end
|
|
743
|
+
end
|
|
744
|
+
prepend Deprecations
|
|
745
|
+
end
|
|
326
746
|
|
|
327
747
|
# Hold root paths
|
|
328
748
|
@root_paths = {}
|
|
329
749
|
|
|
750
|
+
# Hold allowed paths. Replaced rather than modified, so that it can be read without a lock.
|
|
751
|
+
@allowed_paths = [].freeze
|
|
752
|
+
@allowed_paths_mutex = Mutex.new
|
|
753
|
+
|
|
330
754
|
# A registry to hold formats for processing files during upload or download
|
|
331
755
|
@extensions = {}
|
|
332
756
|
@schemes = {}
|
|
333
757
|
|
|
334
758
|
# Register File extensions
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
759
|
+
register_built_in_extension(:bz2, IOStreams::Bzip2::Reader, IOStreams::Bzip2::Writer)
|
|
760
|
+
register_built_in_extension(:enc, IOStreams::SymmetricEncryption::Reader, IOStreams::SymmetricEncryption::Writer)
|
|
761
|
+
register_built_in_extension(:gz, IOStreams::Gzip::Reader, IOStreams::Gzip::Writer)
|
|
762
|
+
register_built_in_extension(:gzip, IOStreams::Gzip::Reader, IOStreams::Gzip::Writer)
|
|
763
|
+
register_built_in_extension(:zip, IOStreams::Zip::Reader, IOStreams::Zip::Writer)
|
|
764
|
+
register_built_in_extension(:pgp, IOStreams::Pgp::Reader, IOStreams::Pgp::Writer)
|
|
765
|
+
register_built_in_extension(:gpg, IOStreams::Pgp::Reader, IOStreams::Pgp::Writer)
|
|
766
|
+
register_built_in_extension(:xlsx, IOStreams::Xlsx::Reader, nil)
|
|
767
|
+
register_built_in_extension(:xlsm, IOStreams::Xlsx::Reader, nil)
|
|
768
|
+
register_built_in_extension(:encode, IOStreams::Encode::Reader, IOStreams::Encode::Writer)
|
|
345
769
|
|
|
346
770
|
# Register Schemes
|
|
347
771
|
#
|
|
@@ -357,4 +781,20 @@ module IOStreams
|
|
|
357
781
|
register_scheme(:https, IOStreams::Paths::HTTP)
|
|
358
782
|
register_scheme(:sftp, IOStreams::Paths::SFTP)
|
|
359
783
|
register_scheme(:s3, IOStreams::Paths::S3)
|
|
784
|
+
|
|
785
|
+
# Reports a deprecation warning when the supplied version of Ruby is older than 3.3, which IOStreams 3.0 requires.
|
|
786
|
+
#
|
|
787
|
+
# Called once, as IOStreams is loaded, so that the warning names the line of the application that loaded it, such as
|
|
788
|
+
# its `Bundler.require`, see `IOStreams::Deprecation.warn_on_load`. It is only reported while deprecation warnings
|
|
789
|
+
# are on as IOStreams is loaded, such as with `RUBYOPT=-W:deprecated`, and RubyGems, which compares the versions, is
|
|
790
|
+
# loaded, which it is unless Ruby runs with `--disable-gems`.
|
|
791
|
+
def self.warn_unsupported_ruby(version = RUBY_VERSION)
|
|
792
|
+
return unless defined?(::Gem::Version) && ::Gem::Version.new(version) < ::Gem::Version.new("3.3")
|
|
793
|
+
|
|
794
|
+
Deprecation.warn_on_load("Ruby #{version} is deprecated, since IOStreams 3.0 requires Ruby 3.3 or later, so " \
|
|
795
|
+
"upgrade Ruby")
|
|
796
|
+
end
|
|
797
|
+
private_class_method :warn_unsupported_ruby
|
|
798
|
+
|
|
799
|
+
warn_unsupported_ruby
|
|
360
800
|
end
|
|
@@ -19,7 +19,8 @@ module IOStreams
|
|
|
19
19
|
# Create a delimited stream reader from the supplied input stream.
|
|
20
20
|
#
|
|
21
21
|
# Lines returned will be in the encoding of the input stream.
|
|
22
|
-
# To change the encoding of returned lines, use IOStreams::Encode::Reader.
|
|
22
|
+
# To change the encoding of returned lines, use IOStreams::Encode::Reader. The lines of a file with a schema are
|
|
23
|
+
# read by `IOStreams::Line::Utf8Reader` instead, which takes them as UTF-8.
|
|
23
24
|
#
|
|
24
25
|
# Parameters
|
|
25
26
|
# input_stream
|
|
@@ -43,7 +44,8 @@ module IOStreams
|
|
|
43
44
|
# For CSV files set `embedded_within: '"'`
|
|
44
45
|
#
|
|
45
46
|
# Note:
|
|
46
|
-
# * When
|
|
47
|
+
# * When reached via `IOStreams::Stream`, `embedded_within` defaults to the quote character of the
|
|
48
|
+
# resolved tabular format (e.g. `"` for CSV). See `IOStreams::Builder#quote_character`.
|
|
47
49
|
def initialize(input_stream, delimiter: nil, buffer_size: 65_536, embedded_within: nil)
|
|
48
50
|
super(input_stream)
|
|
49
51
|
|
|
@@ -93,7 +95,9 @@ module IOStreams
|
|
|
93
95
|
line = _readline
|
|
94
96
|
if line && @embedded_within
|
|
95
97
|
initial_line_number = @line_number
|
|
96
|
-
|
|
98
|
+
# Count the delimiters incrementally, since recounting the whole line each time is quadratic.
|
|
99
|
+
embedded_count = line.count(@embedded_within)
|
|
100
|
+
while embedded_count.odd?
|
|
97
101
|
if eof? || line.length > @buffer_size * 10
|
|
98
102
|
raise(Errors::MalformedDataError.new(
|
|
99
103
|
"Unbalanced delimited field, delimiter: #{@embedded_within}",
|
|
@@ -101,6 +105,7 @@ module IOStreams
|
|
|
101
105
|
))
|
|
102
106
|
end
|
|
103
107
|
line << @delimiter
|
|
108
|
+
embedded_count += @delimiter.count(@embedded_within)
|
|
104
109
|
next_line = _readline
|
|
105
110
|
if next_line.nil?
|
|
106
111
|
raise(Errors::MalformedDataError.new(
|
|
@@ -109,6 +114,7 @@ module IOStreams
|
|
|
109
114
|
))
|
|
110
115
|
end
|
|
111
116
|
line << next_line
|
|
117
|
+
embedded_count += next_line.count(@embedded_within)
|
|
112
118
|
end
|
|
113
119
|
end
|
|
114
120
|
line
|