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
data/docs/streams.md ADDED
@@ -0,0 +1,382 @@
1
+ ---
2
+ layout: default
3
+ title: Streams
4
+ description: >-
5
+ Reading and writing a path a block, line, row or record at a time, and the
6
+ pipeline of compression, encryption and format streams applied to it.
7
+ ---
8
+
9
+ Once you have a [path](path), you read from and write to it with a small, consistent set of methods.
10
+ Reading and writing always happen a block, line, or record at a time, so memory use stays low no
11
+ matter how large the file is. Choose how each chunk is delivered by passing a mode: the default
12
+ streams raw data, `:line` yields one line at a time, `:array` yields each row as an array, and
13
+ `:hash` yields each record as a hash keyed by the header row.
14
+
15
+ Read 128 characters at a time from the file:
16
+ ~~~ruby
17
+ IOStreams.path("example.csv").reader do |io|
18
+ while (data = io.read(128))
19
+ p data
20
+ end
21
+ end
22
+ ~~~
23
+
24
+ The stream that the block receives answers the methods of an IO that read, such as `#read` and `#eof?`. Rely on
25
+ those, rather than on the class of the stream, which depends on how the file is stored, such as a `Zlib::GzipReader`
26
+ for a `.gz` file. Where a library needs a `File`, copy the data into a local file first, such as with
27
+ `IOStreams.temp_file` and `#copy_to`, and read that file. To read the data from the start again, open the reader
28
+ again.
29
+
30
+ Read one line at a time from the file:
31
+ ~~~ruby
32
+ IOStreams.path("example.csv").each do |line|
33
+ puts line
34
+ end
35
+ ~~~
36
+
37
+ By default the line delimiter is auto-detected from the file, handling both Windows (`\r\n`)
38
+ and Linux (`\n`) line endings. To break the file up by something other than its line endings,
39
+ supply `delimiter`:
40
+ ~~~ruby
41
+ IOStreams.path("example.txt").each(:line, delimiter: "|") do |line|
42
+ puts line
43
+ end
44
+ ~~~
45
+
46
+ When a line can contain embedded newlines, such as a CSV field wrapped in double quotes that
47
+ spans multiple lines, supply `embedded_within` so those newlines are not treated as line endings:
48
+ ~~~ruby
49
+ IOStreams.path("example.csv").each(:line, embedded_within: '"') do |line|
50
+ puts line
51
+ end
52
+ ~~~
53
+
54
+ Notes:
55
+ * Newlines embedded within quoted fields are kept on the same line automatically when the resolved
56
+ tabular format quotes its fields, such as CSV (detected from a `.csv` file name or set explicitly via
57
+ `.format(:csv)`). `embedded_within` only needs to be supplied for quoted formats that are not
58
+ auto-detected, or to override the quote character.
59
+ * When reading rows or records, with `each(:array)` or `each(:hash)`, the format of the file decides where
60
+ its lines are split, since its parser must agree with them, so set the format with `format` instead.
61
+ Supplying `embedded_within` there is deprecated, and reports a deprecation warning, see
62
+ [Seeing deprecation warnings](upgrading#seeing-deprecation-warnings).
63
+ * A file that is named `.csv` but is actually pipe-delimited can avoid quote parsing by declaring its
64
+ real format with `.format(:psv)`, or when reading lines, by passing `embedded_within: nil` to disable it
65
+ explicitly:
66
+ ~~~ruby
67
+ IOStreams.path("pipe_delimited.csv").format(:psv).each(:line) do |line|
68
+ puts line
69
+ end
70
+ ~~~
71
+
72
+ Display each row from the csv file as an array:
73
+ ~~~ruby
74
+ IOStreams.path("example.csv").each(:array) do |array|
75
+ p array
76
+ end
77
+ ~~~
78
+
79
+ Display each row from the csv file as a hash, where the first line in the CSV file is the header:
80
+ ~~~ruby
81
+ IOStreams.path("example.csv").each(:hash) do |hash|
82
+ p hash
83
+ end
84
+ ~~~
85
+
86
+ The tabular format of the rows and records, such as CSV or JSON, is detected from the file name, or set with
87
+ `#format`, see [File Formats](formats). Set a schema on the path with `#schema` to read each value as the type of
88
+ its field, such as an `Integer` or a `Date`, with the names of the fields as the keys of each record, see
89
+ [Schema](formats#schema):
90
+ ~~~ruby
91
+ IOStreams.path("example.csv").schema(
92
+ fieldsMatch: "subset",
93
+ fields: [
94
+ {name: "name"},
95
+ {name: "zip_code", type: "integer"}
96
+ ]
97
+ ).each(:hash) do |hash|
98
+ p hash
99
+ end
100
+ # => {"name" => "Jack", "zip_code" => 1234}
101
+ ~~~
102
+
103
+ Write data to the file.
104
+ ~~~ruby
105
+ IOStreams.path("abc.txt").writer do |io|
106
+ io << "This"
107
+ io << " is "
108
+ io << " one line\n"
109
+ end
110
+ ~~~
111
+
112
+ Write lines to the file. By adding `:line` to `writer`, each write appends a new line character.
113
+ ~~~ruby
114
+ IOStreams.path("example.csv").writer(:line) do |file|
115
+ file << "these"
116
+ file << "are"
117
+ file << "all"
118
+ file << "separate"
119
+ file << "lines"
120
+ end
121
+ ~~~
122
+
123
+ Write an array (row) at a time to the file.
124
+ Each array is converted to csv before being compressed with zip.
125
+
126
+ ~~~ruby
127
+ IOStreams.path("example.csv").writer(:array) do |io|
128
+ io << ["name", "address", "zip_code"]
129
+ io << ["Jack", "There", "1234"]
130
+ io << ["Joe", "Over There somewhere", 1234]
131
+ end
132
+ ~~~
133
+
134
+ Write a hash (record) at a time to the file.
135
+ Each hash is converted to csv before being compressed with zip.
136
+ The header row is extracted from the first hash write that is performed.
137
+
138
+ ~~~ruby
139
+ IOStreams.path("example.csv").writer(:hash) do |stream|
140
+ stream << {name: "Jack", address: "There", zip_code: 1234}
141
+ stream << {zip_code: 1234, address: "Over There somewhere", name: "Joe"}
142
+ end
143
+ ~~~
144
+
145
+ Notes
146
+ * Any additional keys supplied during subsequent write operations will be ignored
147
+ since the header row has already been written to the file.
148
+ * The order of the header and values is determined by the order of the keys supplied
149
+ during the first write.
150
+ * The order of keys in the subsequent writes does not matter.
151
+
152
+ Stream into an in-memory buffer, useful for testing.
153
+ The original filename still needs to be supplied so that the streaming pipeline can still be inferred.
154
+
155
+ ~~~ruby
156
+ io = StringIO.new
157
+ IOStreams.stream(io).file_name("example.csv.gz").writer(:hash) do |stream|
158
+ stream << {name: "Jack", address: "There", zip_code: 1234}
159
+ stream << {name: "Joe", zip_code: 1234, address: "Over There somewhere"}
160
+ end
161
+ puts io.string
162
+ ~~~
163
+
164
+ Read a CSV file and write the output to an encrypted file in JSON format.
165
+
166
+ ~~~ruby
167
+ IOStreams.path("sample.json.enc").writer(:hash) do |output|
168
+ IOStreams.path("sample.csv").each(:hash) do |record|
169
+ output << record
170
+ end
171
+ end
172
+ ~~~
173
+
174
+ Read a zip file hosted on a HTTP Web Server, returning each row as a hash:
175
+ ~~~ruby
176
+ IOStreams.
177
+ path("https://www5.fdic.gov/idasp/Offices2.zip").
178
+ option(:zip, entry_file_name: "OFFICES2_ALL.CSV").
179
+ each(:hash) do |row|
180
+ p row
181
+ end
182
+ ~~~
183
+
184
+ Notes:
185
+ * By default IOStreams will read the first file in the zip file.
186
+ * To choose a specific file name within the zip file, supply: `entry_file_name`
187
+
188
+ ## Notes
189
+
190
+ * Reading a Zip file requires the entire file to be available locally, so reading from a
191
+ stream (for example S3 or HTTP) downloads it into a temp file first.
192
+ Writing Zip is fully streamed, no temp file is required.
193
+ * When writing, `entry_file_name` sets the name of the file entry within the zip file.
194
+ It defaults to the file name without the `.zip` extension, so writing to
195
+ `example.csv.zip` creates an entry named `example.csv`.
196
+ * Gzip is still recommended over Zip for very large files, since Zip files can only
197
+ be read via a local file.
198
+
199
+ ## Pipeline
200
+
201
+ If the file is compressed, the pipeline will infer the necessary streams that need to be applied to it:
202
+
203
+ ~~~ruby
204
+ path = IOStreams.path("somewhere/example.csv.gz")
205
+ # => #<IOStreams::Paths::File:somewhere/example.csv.gz pipeline={:gz=>{}}>
206
+
207
+ path.pipeline
208
+ # => {:gz=>{}}
209
+ ~~~
210
+
211
+ The `pipeline` above includes `:gz` to indicate that the file should compressed / decompressed with GZip.
212
+
213
+ #### Option
214
+
215
+ Each path supports several options which can be supplied using the `option` method.
216
+
217
+ Set the options for a stream in the pipeline for this file. Each stream can only be applied once and is uniquely
218
+ identified by its symbolic name.
219
+
220
+ To see the pipeline of streams that IOStreams would infer:
221
+ ~~~ruby
222
+ IOStreams.path("example.pgp").pipeline
223
+ # => {:pgp=>{}}
224
+
225
+ IOStreams.path("example.gz").pipeline
226
+ # => {:gz=>{}}
227
+
228
+ IOStreams.path("example.gz.pgp").pipeline
229
+ # => {:gz=>{}, :pgp=>{}}
230
+ ~~~
231
+
232
+ If the relevant stream is not found for this file it is ignored.
233
+ For example, if the file does not have a pgp extension then the pgp option is ignored.
234
+ ~~~ruby
235
+ IOStreams.path("example.csv.gz").
236
+ option(:pgp, passphrase: "receiver_passphrase").
237
+ read
238
+ ~~~
239
+
240
+ This is great way to pass in stream specific options for when they are required, and to still support
241
+ paths that do not use that stream. For example, the same code can support pgp encrypted, Symmetric Encryption encrypted,
242
+ and plain text files.
243
+ ~~~ruby
244
+ IOStreams.path("example.csv.enc").
245
+ option(:pgp, passphrase: "receiver_passphrase").
246
+ read
247
+ ~~~
248
+
249
+ To see the what value was previously set for a particular option:
250
+ ~~~ruby
251
+ path = IOStreams.path("example.pgp")
252
+ path.option(:pgp, passphrase: "receiver_passphrase")
253
+
254
+ path.setting(:pgp)
255
+ # => {:passphrase=>"receiver_passphrase"}
256
+ ~~~
257
+
258
+ #### Reading and writing need separate options
259
+
260
+ The options for a stream are passed to its reader when reading and to its writer when writing,
261
+ and the two directions usually accept different options. For example, the PGP writer needs
262
+ the `recipient` to encrypt for, while the PGP reader needs the `passphrase` for the private key.
263
+
264
+ Options are strict: an option that does not apply to the direction being used raises an
265
+ `ArgumentError` that names the direction it belongs to, rather than being silently ignored.
266
+ So configure one path for writing and a separate path for reading:
267
+
268
+ ~~~ruby
269
+ IOStreams.path("example.csv.pgp").
270
+ option(:pgp, recipient: "receiver@example.org").
271
+ write("name,login\nJack Jones,jjones\n")
272
+
273
+ IOStreams.path("example.csv.pgp").
274
+ option(:pgp, passphrase: "receiver_passphrase").
275
+ read
276
+ ~~~
277
+
278
+ Reading with the writer's options fails before any data is read:
279
+ ~~~ruby
280
+ IOStreams.path("example.csv.pgp").
281
+ option(:pgp, recipient: "receiver@example.org").
282
+ read
283
+ # ArgumentError: :recipient only applies when writing a :pgp stream and cannot be used when reading.
284
+ # Configure a separate path or stream without it for reading.
285
+ ~~~
286
+
287
+ An option that neither direction accepts, such as a misspelled one, raises an `ArgumentError`
288
+ that lists the valid options. The exception is BZip2, which ignores options it does not accept and
289
+ logs a warning instead. One that neither reading nor writing takes also reports a deprecation warning,
290
+ since in v3.0 it will raise an `ArgumentError` too, while v3.0 ignores an option of the other direction.
291
+
292
+ Options for a stream that is not in the pipeline are still ignored, as described above,
293
+ since they are not passed to any reader or writer. One that neither direction accepts reports a
294
+ deprecation warning when it is set, for every stream, since v3.0 raises an `ArgumentError` for it then.
295
+
296
+ #### Stream
297
+
298
+ The `stream` method stops IOStreams from inferring the streams for this path and only uses the specified streams.
299
+
300
+ For example when using a filename that does not have the necessary file extensions.
301
+ In this case the file was compressed with Zip, so tell IOStreams to unzip it:
302
+ ~~~ruby
303
+ path = IOStreams.path("tempfile2527")
304
+ path.stream(:zip)
305
+ path.read
306
+ ~~~
307
+
308
+ The above example could also be written as:
309
+ ~~~ruby
310
+ IOStreams.path("tempfile2527").
311
+ stream(:zip).
312
+ read
313
+ ~~~
314
+
315
+ Multiple streams can also be specified:
316
+
317
+ ~~~ruby
318
+ IOStreams.path("tempfile2527").
319
+ stream(:zip).
320
+ stream(:pgp, passphrase: "receiver_passphrase").
321
+ read
322
+ ~~~
323
+
324
+ Now that IOStreams is not inferring the pipeline from the filename, we can still see the above streams:
325
+ ~~~ruby
326
+ IOStreams.path("tempfile2527").
327
+ stream(:zip).
328
+ stream(:pgp, passphrase: "receiver_passphrase").
329
+ pipeline
330
+ # => {:zip=>{}, :pgp=>{:passphrase=>"receiver_passphrase"}}
331
+ ~~~
332
+
333
+
334
+ In this example the file contains JSON data that was compressed with Zip, and since we want to read each row as a hash:
335
+ ~~~ruby
336
+ IOStreams.path("tempfile2527").
337
+ stream(:zip).
338
+ format(:json).
339
+ each(:hash) do |row|
340
+ p row
341
+ end
342
+ ~~~
343
+
344
+ Alternatively if the original file name is available it can also be supplied allowing IOStreams to infer the above streams:
345
+ ~~~ruby
346
+ IOStreams.path("tempfile2527").
347
+ file_name("file.json.zip").
348
+ each(:hash) do |row|
349
+ p row
350
+ end
351
+ ~~~
352
+
353
+ To see the what value was previously set for a particular stream:
354
+ ~~~ruby
355
+ path = IOStreams.path("tempfile2527")
356
+ path.stream(:zip)
357
+ path.stream(:pgp, passphrase: "receiver_passphrase")
358
+
359
+ path.setting(:pgp)
360
+ # => {:passphrase=>"receiver_passphrase"}
361
+ ~~~
362
+
363
+ #### Raw
364
+
365
+ To read or write the data as it is stored, applying no streams, neither those that the file name implies nor any
366
+ set with `option` or `stream`, and no encoding, use `raw`. For example, to checksum a compressed file as it is
367
+ stored, or to write data that is already compressed:
368
+
369
+ ~~~ruby
370
+ Digest::SHA256.hexdigest(IOStreams.path("data.csv.gz").raw.read)
371
+
372
+ IOStreams.path("data.csv.gz").raw.write(gzipped_data)
373
+ ~~~
374
+
375
+ Setting a stream, option or encoding after `raw` raises `ArgumentError`, since the data is as stored.
376
+ `raw?` returns whether `raw` was called. `raw` replaces `stream(:none)`, which is deprecated: it still works, and
377
+ reports a deprecation warning, see [Upgrading](upgrading#deprecated-and-what-replaces-it).
378
+
379
+ To copy a file as it is stored, such as downloading a zip file without unzipping it, use
380
+ `copy_to(target, convert: false)` instead, since the target applies its own streams. To read a file that is not
381
+ named for its format, such as a CSV file called `data.txt`, set the file name that names its format, for example
382
+ `file_name("data.csv")`.