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.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -22
  3. data/Rakefile +45 -0
  4. data/docs/CLAUDE.md +9 -0
  5. data/docs/config.md +161 -0
  6. data/docs/copy_files.md +93 -0
  7. data/docs/extensions.md +125 -0
  8. data/docs/formats.md +816 -0
  9. data/docs/index.md +388 -0
  10. data/docs/path.md +668 -0
  11. data/docs/pgp.md +436 -0
  12. data/docs/streams.md +382 -0
  13. data/docs/tutorial.md +495 -0
  14. data/docs/upgrading.md +664 -0
  15. data/lib/io_streams/builder/deprecations.rb +250 -0
  16. data/lib/io_streams/builder.rb +316 -39
  17. data/lib/io_streams/bzip2/reader.rb +41 -2
  18. data/lib/io_streams/bzip2/writer.rb +35 -2
  19. data/lib/io_streams/deprecation/methods.rb +51 -0
  20. data/lib/io_streams/deprecation/once.rb +27 -0
  21. data/lib/io_streams/deprecation/option_names.rb +26 -0
  22. data/lib/io_streams/deprecation/words.rb +42 -0
  23. data/lib/io_streams/deprecation.rb +259 -0
  24. data/lib/io_streams/encode/reader.rb +58 -3
  25. data/lib/io_streams/encode/writer.rb +23 -2
  26. data/lib/io_streams/encode.rb +99 -0
  27. data/lib/io_streams/errors.rb +55 -0
  28. data/lib/io_streams/gzip/reader.rb +15 -0
  29. data/lib/io_streams/gzip/writer.rb +11 -2
  30. data/lib/io_streams/io_streams.rb +468 -28
  31. data/lib/io_streams/line/reader.rb +9 -3
  32. data/lib/io_streams/line/utf8_reader.rb +23 -0
  33. data/lib/io_streams/path.rb +227 -28
  34. data/lib/io_streams/paths/file.rb +115 -6
  35. data/lib/io_streams/paths/http.rb +92 -5
  36. data/lib/io_streams/paths/matcher.rb +6 -3
  37. data/lib/io_streams/paths/s3.rb +242 -20
  38. data/lib/io_streams/paths/sftp/net_ssh.rb +104 -0
  39. data/lib/io_streams/paths/sftp.rb +141 -57
  40. data/lib/io_streams/paths.rb +23 -0
  41. data/lib/io_streams/pgp/reader.rb +60 -2
  42. data/lib/io_streams/pgp/writer.rb +42 -6
  43. data/lib/io_streams/pgp.rb +92 -21
  44. data/lib/io_streams/reader.rb +54 -1
  45. data/lib/io_streams/record/reader.rb +71 -10
  46. data/lib/io_streams/record/writer.rb +17 -10
  47. data/lib/io_streams/row/header_row.rb +21 -0
  48. data/lib/io_streams/row/reader.rb +52 -15
  49. data/lib/io_streams/row/writer.rb +64 -13
  50. data/lib/io_streams/schema/columns.rb +456 -0
  51. data/lib/io_streams/schema/constraints.rb +99 -0
  52. data/lib/io_streams/schema/field.rb +967 -0
  53. data/lib/io_streams/schema/json_number.rb +56 -0
  54. data/lib/io_streams/schema/keys.rb +78 -0
  55. data/lib/io_streams/schema/pattern.rb +257 -0
  56. data/lib/io_streams/schema/source.rb +31 -0
  57. data/lib/io_streams/schema.rb +616 -0
  58. data/lib/io_streams/stream.rb +843 -69
  59. data/lib/io_streams/symmetric_encryption/reader.rb +12 -0
  60. data/lib/io_streams/symmetric_encryption/writer.rb +4 -0
  61. data/lib/io_streams/tabular/header.rb +60 -4
  62. data/lib/io_streams/tabular/header_mapping.rb +332 -0
  63. data/lib/io_streams/tabular/mapping.rb +165 -0
  64. data/lib/io_streams/tabular/parser/array.rb +5 -0
  65. data/lib/io_streams/tabular/parser/base.rb +34 -0
  66. data/lib/io_streams/tabular/parser/csv.rb +5 -0
  67. data/lib/io_streams/tabular/parser/fixed.rb +200 -1
  68. data/lib/io_streams/tabular/parser/fixed_fields.rb +160 -0
  69. data/lib/io_streams/tabular/parser/hash.rb +10 -0
  70. data/lib/io_streams/tabular/parser/json.rb +17 -0
  71. data/lib/io_streams/tabular/parser/json_fields.rb +132 -0
  72. data/lib/io_streams/tabular/parser/psv.rb +6 -2
  73. data/lib/io_streams/tabular/schema_mapping.rb +361 -0
  74. data/lib/io_streams/tabular.rb +344 -30
  75. data/lib/io_streams/utils.rb +467 -1
  76. data/lib/io_streams/version.rb +1 -1
  77. data/lib/io_streams/writer.rb +37 -1
  78. data/lib/io_streams/xlsx/cells.rb +127 -0
  79. data/lib/io_streams/xlsx/reader.rb +61 -1
  80. data/lib/io_streams/xlsx/schema_reader.rb +90 -0
  81. data/lib/io_streams/xlsx/v3_writer.rb +16 -0
  82. data/lib/io_streams/zip/reader.rb +11 -0
  83. data/lib/io_streams/zip/writer.rb +28 -2
  84. data/lib/iostreams.rb +21 -17
  85. metadata +50 -7
@@ -0,0 +1,250 @@
1
+ module IOStreams
2
+ class Builder
3
+ # What v2.2 reports about the streams, options and format of a builder as deprecation warnings: those that
4
+ # IOStreams v3.0 removes or rejects, and those whose results change in v3.0, with what to use instead. It is part
5
+ # of the Builder, which includes it, since the Builder owns the settings that it reports on.
6
+ module Deprecations
7
+ # A format set with #format= that is not the format of the text that the streams read, see #text_format, such as
8
+ # `:psv` for a spreadsheet, whose rows are read as CSV, is deprecated, since v3.0 raises ArgumentError when its
9
+ # lines, rows or records are read, rather than read them in that format. Reports a deprecation warning when they are
10
+ # read, as v3.0 raises then, whichever way the streams were set.
11
+ #
12
+ # So is reading them in the format that the file name names, such as `:psv` for `report.psv.xlsx`, without a format
13
+ # set, since v3.0 reads them in the format of the text whatever the file name, so that `format(:csv)` reads them
14
+ # the same in both versions.
15
+ #
16
+ # Neither is looked for while deprecation warnings are off, see `IOStreams::Deprecation.on?`, since finding the
17
+ # format of the text would build the pipeline once more for each read.
18
+ def warn_text_format
19
+ return unless Deprecation.on?
20
+
21
+ format = self.format
22
+ return if format.nil?
23
+
24
+ text_format = self.text_format
25
+ return if text_format.nil? || format == text_format
26
+
27
+ if @set_format
28
+ Deprecation.warn("format(#{format.inspect}) is deprecated when the streams read the rows as " \
29
+ "#{text_format.inspect}, such as those of a spreadsheet, since v3.0 raises ArgumentError for " \
30
+ "it, set format(#{text_format.inspect}) instead, or no format")
31
+ else
32
+ Deprecation.warn("the format #{format.inspect} that the file name names is deprecated when the streams read " \
33
+ "the rows as #{text_format.inspect}, such as those of a spreadsheet, since v3.0 reads them as " \
34
+ "#{text_format.inspect} whatever the file name, set format(#{text_format.inspect}) to read " \
35
+ "them so in both versions")
36
+ end
37
+ end
38
+
39
+ # An encoding set with #encoding is deprecated when the rows and records of a spreadsheet are read, see
40
+ # `IOStreams::Stream#warn_cell_rows`, which are its cells rather than lines of text, since v3.0 raises ArgumentError
41
+ # for it then. Its lines still take it. Without it, a schema reads the text of the cells as UTF-8, see #utf8_text?,
42
+ # as v3.0 does.
43
+ def warn_cells_encoding
44
+ return if @encoding.nil?
45
+
46
+ Deprecation.warn("#encoding is deprecated when the rows and records of a spreadsheet are read, which are its " \
47
+ "cells rather than lines of text, since v3.0 raises ArgumentError for it, so remove it, and " \
48
+ "give its columns a schema, with which v2.2 reads the text of its cells as UTF-8; " \
49
+ "each(:line) still takes it")
50
+ end
51
+
52
+ # Returns [Array(Array<Symbol>, String, Hash|nil)|nil] the methods that the stream that the application's block
53
+ # receives in v3.0 answers, its name for a deprecation warning, such as "the :gz stream", and what replaces each
54
+ # method that v3.0 removes, when the stream says, see `IOStreams::Reader.v3_stream_advice`, when the stream that it
55
+ # receives in v2.2 answers others, otherwise nil. The stream closest to the application decides, with its options,
56
+ # see `IOStreams::Reader.v3_stream_methods`. Without any streams, the block receives the IO that they would read or
57
+ # write, which the supplied source describes, see `IOStreams::Stream#v3_source` and `IOStreams::Stream#v3_target`.
58
+ #
59
+ # Parameters:
60
+ # type: [:reader|:writer]
61
+ # source: [Array(Array<Symbol>, String)|nil]
62
+ # The methods and the name of the IO that the streams read or write in v3.0, when it differs from the one in
63
+ # v2.2, otherwise nil.
64
+ #
65
+ # Yields, only when the stream closest to the application asks and is the only stream, to ask whether the streams
66
+ # read a regular local file in v3.0. The block answers true or false.
67
+ #
68
+ # Asked before the streams are opened, so it raises nothing, not even for a stream without a class of the type, which
69
+ # raises as before once it is opened.
70
+ def v3_stream(type, source)
71
+ streams = pipeline
72
+ name, options = streams.first
73
+ return source if name.nil?
74
+
75
+ klass = IOStreams.extensions[name]&.public_send(:"#{type}_class")
76
+ return unless klass.respond_to?(:v3_stream_methods)
77
+
78
+ methods = klass.v3_stream_methods(options) { streams.size == 1 && yield }
79
+ return unless methods
80
+
81
+ [methods, "the #{name.inspect} stream", (klass.v3_stream_advice if klass.respond_to?(:v3_stream_advice))]
82
+ end
83
+
84
+ # Returns [Symbol|nil] the format that the rows and records of this file are read and written in, when IOStreams
85
+ # v3.0 reads and writes them in its `:text` format instead, as the text of each line: when no format was set, so
86
+ # that it is taken from a file name whose format extension is `.text`, see `IOStreams::Tabular.text_file_format`,
87
+ # unless a stream reads the lines in a format of its own, such as the CSV of a spreadsheet, which v3.0 reads them
88
+ # in, whatever format the file name names. Returns nil otherwise.
89
+ def text_file_format
90
+ return if format_set? || file_name.nil? || text_format
91
+
92
+ Tabular.text_file_format(file_name)
93
+ end
94
+
95
+ # Returns [Symbol|nil] the tabular format of the data that a copy without a mode reads from these streams, or
96
+ # writes to them, when IOStreams v3.0 converts the rows of such a copy between it and another format, see
97
+ # `IOStreams::Stream#warn_copy_format`: the format of the text that the streams read, see #text_format, such as
98
+ # `:csv` for a spreadsheet, otherwise #format. Returns nil when v3.0 copies the data as it is: when the format is
99
+ # not known, such as for an IO without a file name or a `.txt` file, when the data is read and written as it is
100
+ # stored, see #raw, for a file that v3.0 reads in its `:text` format, see #text_file_format, and for a format whose
101
+ # rows are Ruby objects rather than lines of text, such as `:array` and `:hash`, see `IOStreams::Tabular.text_rows?`.
102
+ def copy_format
103
+ return if raw? || text_file_format
104
+
105
+ format = text_format || self.format
106
+ format if format && Tabular.text_rows?(format)
107
+ end
108
+
109
+ private
110
+
111
+ # Reports a deprecation warning for supplying `:none`, see #raw, or for setting the encode stream, see #encoding,
112
+ # which names the method that the application called.
113
+ def warn_deprecated_stream(method, stream)
114
+ case stream
115
+ when :none
116
+ Deprecation.warn("#{method}(:none) is deprecated, use #raw to read or write the data as it is stored, " \
117
+ "or #file_name to read a file that is not named for its format")
118
+ when :encode
119
+ # Like any other stream set with #stream, stream(:encode, ...) stops the streams being taken from the file
120
+ # name, which #encoding keeps.
121
+ if method == "stream"
122
+ Deprecation.warn("stream(:encode, ...) is deprecated, use #encoding instead, which keeps the streams that " \
123
+ "the file name implies, #raw to read or write the data as it is stored, or #file_name to " \
124
+ "read a file that is not named for its format")
125
+ else
126
+ Deprecation.warn("#{method}(:encode, ...) is deprecated, use #encoding instead")
127
+ end
128
+ end
129
+ end
130
+
131
+ # Reports a deprecation warning for each option of the stream that IOStreams 3.0 raises ArgumentError for when it is
132
+ # set with #option, #stream or #option_or_stream, whether or not the file name includes the stream, so that it is
133
+ # reported wherever the code runs, rather than only where a file name includes the stream: one that the reader or
134
+ # the writer of the stream replaces, with the option that replaces it, see
135
+ # `IOStreams::Deprecation::OptionNames#replaced_option_names`, and one that neither of them takes in IOStreams 3.0,
136
+ # such as a misspelled one, see #v3_option_names. An option of the other direction is not reported, since v3.0
137
+ # accepts it, and ignores it, nor are the options of the encode stream, which is deprecated itself, see
138
+ # #warn_deprecated_stream, or of `:none`, which takes none.
139
+ def warn_deprecated_options(stream, options)
140
+ return if options.empty? || %i[encode none].include?(stream)
141
+
142
+ classes = v3_stream_classes(stream)
143
+ replaced = classes.each_with_object({}) do |klass, names|
144
+ names.merge!(klass.replaced_option_names) if klass.respond_to?(:replaced_option_names)
145
+ end.slice(*options.keys)
146
+ replaced.each_pair { |name, replacement| Deprecation.warn("#{name}: is deprecated, use #{replacement}: instead") }
147
+
148
+ known = v3_option_names(classes)
149
+ names = known ? options.keys - replaced.keys - known : []
150
+ return if names.empty?
151
+
152
+ words = Deprecation::Words.new(names)
153
+ Deprecation.warn("#{words.keywords} #{words.are} deprecated for a #{stream.inspect} stream, since neither its " \
154
+ "reader nor its writer takes #{words.them}, so correct or remove #{words.them}")
155
+ end
156
+
157
+ # Returns [Array<Class|Module>] the classes that read and write the stream in IOStreams 3.0, as far as they are
158
+ # known: the reader and the writer registered for it, and, in place of a writer that it does not have, the stand-in
159
+ # for the one that IOStreams 3.0 adds, when its reader names one, such as for `.xlsx`, see
160
+ # `IOStreams::Xlsx::Reader.v3_writer`. None when the stream is not registered.
161
+ def v3_stream_classes(stream)
162
+ extension = IOStreams.extensions[stream]
163
+ return [] unless extension
164
+
165
+ reader = extension.reader_class
166
+ writer = extension.writer_class
167
+ writer ||= reader.v3_writer if reader.respond_to?(:v3_writer)
168
+ [reader, writer].compact
169
+ end
170
+
171
+ # Returns [Array<Symbol>|nil] the options that IOStreams 3.0 accepts when they are set for a stream of the supplied
172
+ # classes, see #v3_stream_classes: those that its reader or its writer takes, or nil when they are not known, since
173
+ # a class does not declare them, as one that an application registers need not.
174
+ def v3_option_names(classes)
175
+ names = classes.map { |klass| v3_class_option_names(klass) }
176
+ names.flatten.uniq unless names.empty? || names.include?(nil)
177
+ end
178
+
179
+ # Returns [Array<Symbol>|nil] the options that the class takes in IOStreams 3.0, see
180
+ # `IOStreams::Deprecation::OptionNames#known_option_names`, or those that it declares when it does not answer them,
181
+ # as a class that an application registers need not, or nil when it does neither.
182
+ def v3_class_option_names(klass)
183
+ return klass.known_option_names if klass.respond_to?(:known_option_names)
184
+
185
+ klass.option_names if klass.respond_to?(:option_names)
186
+ end
187
+
188
+ # Reports a deprecation warning, when the streams are opened, for those that are deprecated, or whose results
189
+ # change in IOStreams v3.0.
190
+ def warn_deprecated_streams
191
+ warn_encode_extension
192
+ warn_temp_file_streams
193
+ warn_extension_options
194
+ end
195
+
196
+ # A file name ending in `.encode` is deprecated, since file names do not name the encode stream, see #encoding.
197
+ # Once the encode stream is set, with #encoding or `option(:encode, ...)`, the file name adds nothing to it, so it
198
+ # is only reported when the file name is what applies the encode stream.
199
+ def warn_encode_extension
200
+ return if streams || @encoding || options&.key?(:encode)
201
+ return if file_name.nil? || !parse_extensions.include?(:encode)
202
+
203
+ Deprecation.warn("a file name ending in .encode is deprecated, since file names do not name the encode " \
204
+ "stream, use #encoding instead")
205
+ end
206
+
207
+ # The path of `IOStreams.temp_file` reads and writes its data as it is stored, see #no_streams, while IOStreams v3.0
208
+ # applies the streams that its file name implies, so reading or writing it is deprecated while it has no streams of
209
+ # its own, and its file name implies some, other than `.encode`, which v3.0 no longer applies, see #raw, which reads
210
+ # and writes the data as it is stored on both.
211
+ def warn_temp_file_streams
212
+ return unless @temp_file && streams.empty? && file_name
213
+ return if (parse_extensions - [:encode]).empty?
214
+
215
+ Deprecation.warn("reading or writing the path of IOStreams.temp_file is deprecated when its extension names " \
216
+ "streams, such as .gz, since v3.0 applies them, where v2.2 reads and writes the data as it is " \
217
+ "stored: call #raw on the path to read and write the data as it is stored")
218
+ end
219
+
220
+ # The options set for another extension of the format of an extension in the file name, such as
221
+ # `option(:pgp, ...)` for a `.gpg` file, are ignored, while IOStreams v3.0 applies the options of a format to each of
222
+ # its extensions, and then those set for the extension in the file name, which take precedence. So they are
223
+ # deprecated unless those of the extension itself set each of them too, with which both apply the same options.
224
+ def warn_extension_options
225
+ return if streams || options.nil? || file_name.nil?
226
+
227
+ parse_extensions.each do |stream|
228
+ own = options[stream] || {}
229
+ options.each_pair do |name, opts|
230
+ next if name == stream || (opts.keys - own.keys).empty? || !same_format?(name, stream)
231
+
232
+ Deprecation.warn("option(:#{name}, ...) is deprecated for a file name with the extension .#{stream}, which " \
233
+ "v2.2 does not apply it to, since v3.0 applies the options of a format to each of its " \
234
+ "extensions: set them with option(:#{stream}, ...)")
235
+ end
236
+ end
237
+ end
238
+
239
+ # Returns [true|false] whether the extensions are of the same format, whose reader and writer they share, such as
240
+ # `.pgp` and `.gpg`, or `.gz` and `.gzip`.
241
+ def same_format?(extension, other)
242
+ registered = IOStreams.extensions[extension]
243
+ other = IOStreams.extensions[other]
244
+ return false if registered.nil? || other.nil?
245
+
246
+ registered.reader_class == other.reader_class && registered.writer_class == other.writer_class
247
+ end
248
+ end
249
+ end
250
+ end