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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fedc839021ed6f9515ed91d584477debad754f05476169ee1bff4d0b5b161ee6
|
|
4
|
+
data.tar.gz: 8ac9d35f276306b6eb3feff0667229e0b8e5c61b7b37b05682c7a192f201a713
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 24aa6dd4c78acaed7bcc50d3c807a328e8f6828714fd17f8af245d68d32d8e5cc8e8810938e5f3f4ed66295369fc786b5c158fa2ef9a2d6037760a732bc35326
|
|
7
|
+
data.tar.gz: a2bd8ce524d37ab054de318b16176203515da7e24bf6907711fd521f5e7043ef55114c87529595a7fec727c73138b29baab55e80da1d20dee0c5b231dfabbe63
|
data/README.md
CHANGED
|
@@ -11,33 +11,16 @@ Production Ready, heavily used in production environments, many as part of Rocke
|
|
|
11
11
|
|
|
12
12
|
## Documentation
|
|
13
13
|
|
|
14
|
-
Start with the [IOStreams tutorial](https://iostreams.
|
|
14
|
+
Start with the [IOStreams tutorial](https://iostreams.reidmorrison.com/tutorial) to get a great introduction to IOStreams.
|
|
15
15
|
|
|
16
|
-
Next, checkout the remaining [IOStreams documentation](https://iostreams.
|
|
16
|
+
Next, checkout the remaining [IOStreams documentation](https://iostreams.reidmorrison.com/)
|
|
17
17
|
|
|
18
18
|
See the [CHANGELOG](CHANGELOG.md) for the release history and notable changes.
|
|
19
19
|
|
|
20
|
-
## Upgrading
|
|
20
|
+
## Upgrading
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
- **Ruby 3.2 or later is now required.** Older Ruby versions are no longer supported.
|
|
25
|
-
- **Writing Zip files now requires the `zip_kit` gem.** The retired `zip_tricks` gem has been replaced by its successor, `zip_kit`. If your application writes Zip files, replace `gem "zip_tricks"` with `gem "zip_kit"` in your Gemfile. Reading Zip files is unaffected. The IOStreams API itself is unchanged.
|
|
26
|
-
- **The deprecated pre-v1.6 API has been removed.** The `IOStreams::Deprecated` mix-in described below no longer exists. Any code still using those old apis must move to the current `IOStreams.path` / `IOStreams.stream` API.
|
|
27
|
-
- **The deprecated PGP writer `compression:` option has been removed.** Use `compress:` instead (available since v1.11.0).
|
|
28
|
-
- **`IOStreams::Pgp.logger` and `IOStreams::Pgp.logger=` have been removed.** Logging is now configured centrally for the whole library via `IOStreams.logger` / `IOStreams.logger=`. Replace `IOStreams::Pgp.logger = my_logger` with `IOStreams.logger = my_logger`. [Semantic Logger](https://logger.rocketjob.io) is detected automatically when loaded.
|
|
29
|
-
|
|
30
|
-
## Upgrading to v1.6
|
|
31
|
-
|
|
32
|
-
The old, deprecated api's are no longer loaded by default with v1.6. To add back the deprecated api support, add
|
|
33
|
-
the following line to your code:
|
|
34
|
-
|
|
35
|
-
~~~ruby
|
|
36
|
-
IOStreams.include(IOStreams::Deprecated)
|
|
37
|
-
~~~
|
|
38
|
-
|
|
39
|
-
It is important to move any of the old deprecated apis over to the new api, since they will be removed in a future
|
|
40
|
-
release.
|
|
22
|
+
See [Upgrading IOStreams](https://iostreams.reidmorrison.com/upgrading) for the changes that may need
|
|
23
|
+
updates to your application, and the security settings to review, when upgrading.
|
|
41
24
|
|
|
42
25
|
## Versioning
|
|
43
26
|
|
data/Rakefile
CHANGED
|
@@ -19,6 +19,51 @@ task :console do
|
|
|
19
19
|
exec "irb -I lib -r iostreams"
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
desc "Generate docs/llms-full.txt from docs/llms.txt and the doc pages it links to"
|
|
23
|
+
task :llms_full do
|
|
24
|
+
require "uri"
|
|
25
|
+
|
|
26
|
+
docs_dir = File.join(__dir__, "docs")
|
|
27
|
+
llms_path = File.join(docs_dir, "llms.txt")
|
|
28
|
+
out_path = File.join(docs_dir, "llms-full.txt")
|
|
29
|
+
|
|
30
|
+
llms_txt = File.read(llms_path)
|
|
31
|
+
header = llms_txt[/\A.*?(?=\n## Docs)/m]&.strip
|
|
32
|
+
raise "Could not find intro text before '## Docs' in #{llms_path}" unless header
|
|
33
|
+
|
|
34
|
+
docs_section = llms_txt[/^## Docs\n(.*?)(?=\n## |\z)/m, 1]
|
|
35
|
+
raise "Could not find '## Docs' section in #{llms_path}" unless docs_section
|
|
36
|
+
|
|
37
|
+
sections = docs_section.each_line.filter_map do |line|
|
|
38
|
+
next unless line =~ /^- \[(?<title>[^\]]+)\]\((?<url>[^)]+)\)/
|
|
39
|
+
|
|
40
|
+
title = $~[:title]
|
|
41
|
+
path = URI.parse($~[:url]).path.sub(%r{\A/}, "")
|
|
42
|
+
path = "index" if path.empty?
|
|
43
|
+
file = File.join(docs_dir, "#{path}.md")
|
|
44
|
+
raise "Missing doc file for #{$~[:url]}: #{file}" unless File.exist?(file)
|
|
45
|
+
|
|
46
|
+
raw = File.read(file)
|
|
47
|
+
|
|
48
|
+
# The page heading lives in front matter, which the shared docs theme
|
|
49
|
+
# renders as the page's h1. Front matter is stripped below, so lift the
|
|
50
|
+
# heading back out and re-emit it. `heading` wins over `title` where a page
|
|
51
|
+
# sets both, which is the same precedence the theme uses. Falls back to the
|
|
52
|
+
# link text in llms.txt for a page that sets neither.
|
|
53
|
+
front_matter = raw[/\A---\n(.*?)\n---\n/m, 1].to_s
|
|
54
|
+
heading = %w[heading title].
|
|
55
|
+
filter_map { |key| front_matter[/^#{key}:[ \t]*(.+)$/, 1] }.
|
|
56
|
+
first.to_s.strip.delete_prefix('"').delete_suffix('"')
|
|
57
|
+
heading = title if heading.empty?
|
|
58
|
+
|
|
59
|
+
body = raw.sub(/\A---\n.*?\n---\n/m, "").strip
|
|
60
|
+
"# #{heading}\n\n#{body}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
File.write(out_path, "#{header}\n\n#{sections.join("\n\n---\n\n")}\n")
|
|
64
|
+
puts "Wrote #{out_path}"
|
|
65
|
+
end
|
|
66
|
+
|
|
22
67
|
Rake::TestTask.new(:test) do |t|
|
|
23
68
|
t.pattern = "test/**/*_test.rb"
|
|
24
69
|
t.verbose = true
|
data/docs/CLAUDE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Documentation site
|
|
2
|
+
|
|
3
|
+
User-facing documentation is a Jekyll site under [docs/](docs/), published to iostreams.reidmorrison.com. The markdown pages are what matter when reading or updating documentation.
|
|
4
|
+
|
|
5
|
+
**The look and feel is not in this repo.** `docs/_config.yml` sets `remote_theme: reidmorrison/rm-docs-theme@v1`, and the layout, stylesheet, sidebar and syntax highlighting all come from there. This repo holds only its content: the markdown pages and `docs/images`. **Do not add a `docs/_layouts`, `docs/stylesheets` or `docs/javascripts` directory**; they were deleted deliberately, because six gem repos each carried a near-identical copy of the same theme and the copies had drifted. A styling change belongs in `rm-docs-theme`, where it reaches every doc site at once. `v1` is a moving major tag, so theme fixes arrive on the next build; breaking changes go to `v2` and are opted into by editing the pin. `jekyll-remote-theme` must stay in `plugins`: GitHub Pages enables it on its own, but a local build does not, and without it every page silently renders with no layout. Preview against a local theme checkout with `~/src/rm-docs-theme/bin/preview ~/src/iostreams/docs`.
|
|
6
|
+
|
|
7
|
+
**A page's title lives in its front matter**, not in a `#` heading at the top of the markdown; the theme renders it as the page's `h1`. `title` is the browser title and the default heading, `heading` overrides the h1 where the two should differ, and `description` is the page's meta description. `index.md` sets `heading` only, so the home page keeps the tuned SEO `<title>` from `_config.yml`. Adding or renaming a page means editing the `nav` block in `docs/_config.yml` and `docs/llms.txt`.
|
|
8
|
+
|
|
9
|
+
The site also serves two files for AI assistants: [docs/llms.txt](docs/llms.txt), a hand-maintained index of the docs pages, and `docs/llms-full.txt`, all pages concatenated, regenerated with `bundle exec rake llms_full`. **After editing any `docs/*.md` page, re-run `bundle exec rake llms_full`** and commit the result; never edit `llms-full.txt` by hand. That task reads the page heading out of the front matter, so a page that sets neither `heading` nor `title` falls back to its link text in `llms.txt`.
|
data/docs/config.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
---
|
|
2
|
+
layout: default
|
|
3
|
+
title: Configuring IOStreams
|
|
4
|
+
description: >-
|
|
5
|
+
Named roots via IOStreams.add_root so the same code targets different storage
|
|
6
|
+
per environment, plus the temp directory and logger settings.
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## add_root
|
|
10
|
+
|
|
11
|
+
Roots allow paths to reference a particular root directory, so that all path names are appended to that root.
|
|
12
|
+
Their primary purpose is to allow the exact same code to run in production and development, yet use completely
|
|
13
|
+
different data sources in each. For example, in production a root can point to an S3 bucket, while in
|
|
14
|
+
development it points to the local file system.
|
|
15
|
+
|
|
16
|
+
Roots are configured via an initializer at startup. `IOStreams.join` then joins the supplied path
|
|
17
|
+
elements onto the named root, using the `:default` root whenever a root is not supplied.
|
|
18
|
+
|
|
19
|
+
Set the default root for this environment in an initializer:
|
|
20
|
+
~~~ruby
|
|
21
|
+
IOStreams.add_root(:default, "/var/my_app/files")
|
|
22
|
+
~~~
|
|
23
|
+
|
|
24
|
+
Now the default root path is available:
|
|
25
|
+
~~~ruby
|
|
26
|
+
IOStreams.root
|
|
27
|
+
# => #<IOStreams::Paths::File:/var/my_app/files pipeline={}>
|
|
28
|
+
|
|
29
|
+
IOStreams.root.to_s
|
|
30
|
+
# => "/var/my_app/files"
|
|
31
|
+
~~~
|
|
32
|
+
|
|
33
|
+
Comparing the final path using `path` and then `join` that uses a root path:
|
|
34
|
+
~~~ruby
|
|
35
|
+
IOStreams.path("/var/my_app/files", "my_test_file.txt").to_s
|
|
36
|
+
# => "/var/my_app/files/my_test_file.txt"
|
|
37
|
+
|
|
38
|
+
IOStreams.join("my_test_file.txt").to_s
|
|
39
|
+
# => "/var/my_app/files/my_test_file.txt"
|
|
40
|
+
~~~
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
Using `path`:
|
|
44
|
+
~~~ruby
|
|
45
|
+
IOStreams.path("/var/my_app/files", "my_test_file.txt").write("Hello World")
|
|
46
|
+
~~~
|
|
47
|
+
|
|
48
|
+
With the default root path configured the above code can be simplified by using `join` since it resolves to the same path.
|
|
49
|
+
~~~ruby
|
|
50
|
+
IOStreams.join("my_test_file.txt").write("Hello World")
|
|
51
|
+
~~~
|
|
52
|
+
|
|
53
|
+
Multiple roots can be setup, for example one for input files, another for output files, another for
|
|
54
|
+
reports, etc. During development the roots can all point to a common location, while in production
|
|
55
|
+
they could be completely different S3 buckets.
|
|
56
|
+
|
|
57
|
+
For example add special paths for `downloads` and `uploads`.
|
|
58
|
+
~~~ruby
|
|
59
|
+
IOStreams.add_root(:downloads, "/var/my_app/downloads")
|
|
60
|
+
IOStreams.add_root(:uploads, "/var/my_app/uploads")
|
|
61
|
+
~~~
|
|
62
|
+
|
|
63
|
+
An example that writes a file into the `/var/my_app/downloads` directory:
|
|
64
|
+
~~~ruby
|
|
65
|
+
IOStreams.join("my_test_file.txt", root: :downloads).write("Hello World")
|
|
66
|
+
~~~
|
|
67
|
+
|
|
68
|
+
The other benefit is that the root paths used in an application are externalized from the code base. That way the
|
|
69
|
+
roots can be changed to different locations depending on the environment.
|
|
70
|
+
|
|
71
|
+
We can also change the storage mechanism by changing the root:
|
|
72
|
+
~~~ruby
|
|
73
|
+
IOStreams.add_root(:downloads, "s3://my-app-bucket-name/downloads")
|
|
74
|
+
IOStreams.add_root(:uploads, "s3://my-app-bucket-name/uploads")
|
|
75
|
+
~~~
|
|
76
|
+
|
|
77
|
+
Now the application will write to S3 and the code does not change at all.
|
|
78
|
+
~~~ruby
|
|
79
|
+
IOStreams.join("my_test_file.txt", root: :downloads).write("Hello World")
|
|
80
|
+
~~~
|
|
81
|
+
|
|
82
|
+
To use or query a configured root path:
|
|
83
|
+
~~~ruby
|
|
84
|
+
IOStreams.root(:downloads).to_s
|
|
85
|
+
# => "s3://my-app-bucket-name/downloads"
|
|
86
|
+
~~~
|
|
87
|
+
|
|
88
|
+
The whole process shares a root, so set the streams, options, encoding, format and schema of a file on the path
|
|
89
|
+
joined to the root, rather than on the root itself. A path joined to a root keeps the arguments of the root, such as
|
|
90
|
+
its credentials or S3 options, but not the streams, options, encoding, format or schema set on the root:
|
|
91
|
+
~~~ruby
|
|
92
|
+
IOStreams.join("daily.csv.pgp", root: :uploads).option(:pgp, passphrase: "secret").each(:hash) do |record|
|
|
93
|
+
p record
|
|
94
|
+
end
|
|
95
|
+
~~~
|
|
96
|
+
|
|
97
|
+
## temp_dir
|
|
98
|
+
|
|
99
|
+
When working with large files the standard temp file system location can be too small to handle downloading large
|
|
100
|
+
files. For example to decrypt a pgp file from S3, because GnuPG is not streaming capable and only operates on local files.
|
|
101
|
+
|
|
102
|
+
By default IOStreams looks up the location to store temp files in the following order:
|
|
103
|
+
* `ENV['TMPDIR']`
|
|
104
|
+
* `ENV['TMP']`
|
|
105
|
+
* `ENV['TEMP']`
|
|
106
|
+
* `Etc.systmpdir`
|
|
107
|
+
* `/tmp` (if it exists)
|
|
108
|
+
* Otherwise `.`
|
|
109
|
+
|
|
110
|
+
To explicity set the temp file location the following config option can be used:
|
|
111
|
+
|
|
112
|
+
~~~ruby
|
|
113
|
+
IOStreams.temp_dir = "/var/really_big_temp"
|
|
114
|
+
~~~
|
|
115
|
+
|
|
116
|
+
### temp_file
|
|
117
|
+
|
|
118
|
+
To work with a temporary file directly, `IOStreams.temp_file` yields a path inside `temp_dir`
|
|
119
|
+
and deletes the file when the block completes:
|
|
120
|
+
|
|
121
|
+
~~~ruby
|
|
122
|
+
IOStreams.temp_file("export", ".csv") do |path|
|
|
123
|
+
path.write("Hello World")
|
|
124
|
+
# ... use the temp file ...
|
|
125
|
+
end
|
|
126
|
+
# The temp file has been deleted.
|
|
127
|
+
~~~
|
|
128
|
+
|
|
129
|
+
The first argument is a base file name to include in the generated temp file name, and the
|
|
130
|
+
optional second argument is the file extension.
|
|
131
|
+
|
|
132
|
+
## logger
|
|
133
|
+
|
|
134
|
+
IOStreams can log debug information, such as the external commands it runs for PGP and SFTP.
|
|
135
|
+
|
|
136
|
+
When [Semantic Logger](https://logger.reidmorrison.com) is loaded it is detected automatically, and IOStreams
|
|
137
|
+
logs to it without any additional configuration.
|
|
138
|
+
|
|
139
|
+
To use a different logger, or to log when Semantic Logger is not present, assign any logger that
|
|
140
|
+
responds to the standard logging methods:
|
|
141
|
+
|
|
142
|
+
~~~ruby
|
|
143
|
+
require "logger"
|
|
144
|
+
IOStreams.logger = Logger.new($stdout)
|
|
145
|
+
~~~
|
|
146
|
+
|
|
147
|
+
To disable logging entirely, set the logger to `nil`:
|
|
148
|
+
|
|
149
|
+
~~~ruby
|
|
150
|
+
IOStreams.logger = nil
|
|
151
|
+
~~~
|
|
152
|
+
|
|
153
|
+
IOStreams also logs warnings for behavior that will change in the next major version. See
|
|
154
|
+
[Coming in v3.0](upgrading#coming-in-v30).
|
|
155
|
+
|
|
156
|
+
## enforce_column_restrictions
|
|
157
|
+
|
|
158
|
+
Deprecated in v2.2, and removed in v3.0, with the column options that it applies to every input,
|
|
159
|
+
such as `allowed_columns`. A [schema](formats#schema) applies to every input instead, including the
|
|
160
|
+
keys of each JSON record, see
|
|
161
|
+
[Moving the column options to a schema](upgrading#moving-the-column-options-to-a-schema).
|
data/docs/copy_files.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
---
|
|
2
|
+
layout: default
|
|
3
|
+
title: Copying Between Files
|
|
4
|
+
description: >-
|
|
5
|
+
Using IOStreams.copy to move a file between storage locations and to compress,
|
|
6
|
+
decompress, encrypt or decrypt it on the way.
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
File copying can be used to:
|
|
10
|
+
* copy from one storage location to another.
|
|
11
|
+
* create a decrypted / encrypted copy of an existing file.
|
|
12
|
+
* create a decompressed / compressed copy of an existing file.
|
|
13
|
+
|
|
14
|
+
## Examples
|
|
15
|
+
|
|
16
|
+
Decompress `example.csv.gz` into `example.csv`:
|
|
17
|
+
|
|
18
|
+
~~~ruby
|
|
19
|
+
source = IOStreams.path("example.csv.gz")
|
|
20
|
+
IOStreams.path("example.csv").copy_from(source)
|
|
21
|
+
~~~
|
|
22
|
+
|
|
23
|
+
Decrypt a file encrypted with Symmetric Encryption:
|
|
24
|
+
|
|
25
|
+
~~~ruby
|
|
26
|
+
source = IOStreams.path("example.csv.enc")
|
|
27
|
+
IOStreams.path("example.csv").copy_from(source)
|
|
28
|
+
~~~
|
|
29
|
+
|
|
30
|
+
Encrypt a file using PGP encryption so that it can only be read by `receiver@example.org`.
|
|
31
|
+
|
|
32
|
+
~~~ruby
|
|
33
|
+
source = IOStreams.path("example.csv")
|
|
34
|
+
target = IOStreams.path("example.csv.pgp")
|
|
35
|
+
target.option(:pgp, recipient: "receiver@example.org")
|
|
36
|
+
target.copy_from(source)
|
|
37
|
+
~~~
|
|
38
|
+
|
|
39
|
+
When the file name does not have file extensions that would allow IOStreams to infer what streams to apply,
|
|
40
|
+
the streams can be explicitly set using `stream`.
|
|
41
|
+
|
|
42
|
+
In this example, the file `CUSTOMER_DATA` has no extensions, so `stream(:enc)` tells IOStreams
|
|
43
|
+
that its contents were encrypted with Symmetric Encryption. The decrypted contents are then
|
|
44
|
+
PGP encrypted and written to `xyz.csv.pgp` using the pgp key for `receiver@example.org`.
|
|
45
|
+
|
|
46
|
+
~~~ruby
|
|
47
|
+
input = IOStreams.path("CUSTOMER_DATA").stream(:enc)
|
|
48
|
+
IOStreams.path("xyz.csv.pgp").option(:pgp, recipient: "receiver@example.org").copy_from(input)
|
|
49
|
+
~~~
|
|
50
|
+
|
|
51
|
+
To copy a file _without_ performing any conversions (ignore file extensions), set `convert` to `false`:
|
|
52
|
+
|
|
53
|
+
~~~ruby
|
|
54
|
+
input = IOStreams.path("sample.json.zip")
|
|
55
|
+
IOStreams.path("sample.copy").copy_from(input, convert: false)
|
|
56
|
+
~~~
|
|
57
|
+
|
|
58
|
+
Custom stream conversions can be applied to both the source and the target in a single copy.
|
|
59
|
+
Here the source is read as binary and the target is PGP encrypted:
|
|
60
|
+
|
|
61
|
+
~~~ruby
|
|
62
|
+
source = IOStreams.path("source_file").encoding("BINARY")
|
|
63
|
+
IOStreams.path("target_file.pgp").option(:pgp, passphrase: "hello").copy_from(source)
|
|
64
|
+
~~~
|
|
65
|
+
|
|
66
|
+
To convert the contents row by row, or record by record, during the copy, supply `mode`.
|
|
67
|
+
For example, copy a CSV file into JSON, parsing and rendering each record:
|
|
68
|
+
|
|
69
|
+
~~~ruby
|
|
70
|
+
source = IOStreams.path("source_file.csv")
|
|
71
|
+
IOStreams.path("target_file.json").copy_from(source, mode: :hash)
|
|
72
|
+
~~~
|
|
73
|
+
|
|
74
|
+
Notes:
|
|
75
|
+
* `mode` accepts `:line`, `:array`, or `:hash`, and only applies when `convert` is `true`. Any other mode raises
|
|
76
|
+
`ArgumentError` before the source or the target is opened.
|
|
77
|
+
* Without a `mode`, a copy into a file of another format, such as `people.csv` into `people.json` or `people.psv`,
|
|
78
|
+
copies the text of the source as it is, so that the target holds CSV text. This is deprecated, and reports a
|
|
79
|
+
deprecation warning, since IOStreams 3.0 converts the rows of such a copy into the format of the target, as the
|
|
80
|
+
`:array` mode does: supply `mode: :array` to convert the rows, or `mode: :hash` when either file is JSON, whose rows
|
|
81
|
+
are records, or `mode: :line` to copy the text as it is, which give the same result in both versions. A copy within
|
|
82
|
+
a format, such as `.csv.gz` into `.csv`, from or to a file whose format is not known, such as an IO or a `.txt` file,
|
|
83
|
+
or with `#raw`, is copied as it is in both, and so is a spreadsheet into CSV, since its text is CSV.
|
|
84
|
+
|
|
85
|
+
## When a copy fails
|
|
86
|
+
|
|
87
|
+
* The source is opened before the target, so a source that does not exist, or cannot be read, raises before the target
|
|
88
|
+
is changed.
|
|
89
|
+
* A copy that raises before the target is opened, such as for a schema file that does not exist, leaves the target as
|
|
90
|
+
it was.
|
|
91
|
+
* When the copy fails part way, the target is not left with part of the file: a local file is removed, and an S3 or
|
|
92
|
+
SFTP file is only uploaded once the copy has written all of it, so an existing S3 object is kept.
|
|
93
|
+
* A move only deletes the source once the copy completed.
|
data/docs/extensions.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
---
|
|
2
|
+
layout: default
|
|
3
|
+
title: File Extensions
|
|
4
|
+
description: >-
|
|
5
|
+
How the extensions in a file name, such as .csv.gz.pgp, decide which streams
|
|
6
|
+
IOStreams applies when reading or writing, and how to register your own.
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
IOStreams uses the extensions in the file name to determine which streams to apply when
|
|
10
|
+
reading or writing a file. Multiple extensions are applied in order, so `sample.csv.gz.pgp`
|
|
11
|
+
is first decrypted with PGP and then decompressed with GZip when read.
|
|
12
|
+
|
|
13
|
+
Supported extensions:
|
|
14
|
+
|
|
15
|
+
| Extension | Stream | Read | Write | Required gem / program |
|
|
16
|
+
|:-----------------|:---------------------|:-----|:------|:----------------------------------|
|
|
17
|
+
| `.bz2` | BZip2 | Yes | Yes | `bzip2-ffi` |
|
|
18
|
+
| `.enc` | Symmetric Encryption | Yes | Yes | `symmetric-encryption` |
|
|
19
|
+
| `.gz`, `.gzip` | GZip | Yes | Yes | None (Ruby standard library) |
|
|
20
|
+
| `.zip` | Zip | Yes | Yes | `rubyzip` (read), `zip_kit` (write). On JRuby the built-in Java zip support is used for reading. |
|
|
21
|
+
| `.pgp`, `.gpg` | PGP | Yes | Yes | GnuPG command line program (`gpg`) |
|
|
22
|
+
| `.xlsx`, `.xlsm` | Excel Spreadsheet | Yes | No | `creek` |
|
|
23
|
+
|
|
24
|
+
The gems above are soft dependencies: IOStreams does not require them for installation,
|
|
25
|
+
they only need to be added to the `Gemfile` when the corresponding extension is used.
|
|
26
|
+
|
|
27
|
+
## Compression options
|
|
28
|
+
|
|
29
|
+
GZip accepts a compression `level` when writing, from `0` (no compression) to `9` (best compression):
|
|
30
|
+
|
|
31
|
+
~~~ruby
|
|
32
|
+
IOStreams.path("sample.csv.gz").option(:gz, level: 9).write(data)
|
|
33
|
+
~~~
|
|
34
|
+
|
|
35
|
+
BZip2 passes its options through to `bzip2-ffi`: `block_size` (`1` to `9`) and `work_factor` (`0` to `250`)
|
|
36
|
+
when writing, and `small` and `first_only` when reading:
|
|
37
|
+
|
|
38
|
+
~~~ruby
|
|
39
|
+
IOStreams.path("sample.csv.bz2").option(:bz2, block_size: 9).write(data)
|
|
40
|
+
IOStreams.path("sample.csv.bz2").option(:bz2, small: true).read
|
|
41
|
+
~~~
|
|
42
|
+
|
|
43
|
+
Options are strict, so an option that a stream does not accept raises an `ArgumentError`.
|
|
44
|
+
See [Streams](streams#reading-and-writing-need-separate-options). The exception is BZip2, which
|
|
45
|
+
ignores any other option and logs a warning. One that neither reading nor writing takes also reports a
|
|
46
|
+
deprecation warning when it is set, since in v3.0 it will raise an `ArgumentError` too, while v3.0
|
|
47
|
+
ignores an option of the other direction, as BZip2 does.
|
|
48
|
+
|
|
49
|
+
## Reading an Excel Spreadsheet
|
|
50
|
+
|
|
51
|
+
Each row in the spreadsheet is converted into a CSV line, so the regular `:line`, `:array`,
|
|
52
|
+
and `:hash` modes apply:
|
|
53
|
+
|
|
54
|
+
~~~ruby
|
|
55
|
+
IOStreams.path("spreadsheet.xlsx").each(:hash) do |record|
|
|
56
|
+
p record
|
|
57
|
+
end
|
|
58
|
+
~~~
|
|
59
|
+
|
|
60
|
+
Notes:
|
|
61
|
+
* Its rows are read as CSV, so leave the format of the path unset, or set it to `format(:csv)`.
|
|
62
|
+
* Since the underlying `creek` gem operates on files, when reading from a stream (for example S3 or HTTP)
|
|
63
|
+
the contents are first downloaded into a temp file.
|
|
64
|
+
* Writing xlsx files is not supported.
|
|
65
|
+
|
|
66
|
+
## Character encoding
|
|
67
|
+
|
|
68
|
+
The encode stream converts the character encoding of the text that the application reads or writes.
|
|
69
|
+
Set it with `encoding`, which keeps the streams that the file name implies, and also works for a stream without a
|
|
70
|
+
file name, or with streams set with `stream`:
|
|
71
|
+
|
|
72
|
+
~~~ruby
|
|
73
|
+
IOStreams.path("sample.csv.gz").
|
|
74
|
+
encoding("UTF-8", cleaner: :printable, replace: "").
|
|
75
|
+
each do |line|
|
|
76
|
+
puts line
|
|
77
|
+
end
|
|
78
|
+
~~~
|
|
79
|
+
|
|
80
|
+
The encode stream is applied closest to the application, so above the data is decompressed first, and then
|
|
81
|
+
converted. Each call to `encoding` merges its options with those already set, and `encoding` without any
|
|
82
|
+
arguments returns the encoding that was set, or nil. An option that the encode stream does not take, or a value
|
|
83
|
+
that it does not accept, such as an encoding that Ruby does not know, raises `ArgumentError` when it is set, before
|
|
84
|
+
any file is written.
|
|
85
|
+
|
|
86
|
+
`encoding` replaces `option(:encode, ...)`, `stream(:encode, ...)` and a file name ending in `.encode`, which also
|
|
87
|
+
apply the encode stream. They are deprecated: each still works, and reports a deprecation warning, see
|
|
88
|
+
[Upgrading](upgrading#deprecated-and-what-replaces-it).
|
|
89
|
+
|
|
90
|
+
Options:
|
|
91
|
+
|
|
92
|
+
* `encoding: [String|Encoding]`
|
|
93
|
+
The target encoding, for example `"UTF-8"`, `"US-ASCII"`, or `"ASCII-8BIT"`.
|
|
94
|
+
Default: `"UTF-8"`
|
|
95
|
+
|
|
96
|
+
* `replace: [String]`
|
|
97
|
+
The character to replace with when a character cannot be converted to the target encoding.
|
|
98
|
+
Default: nil (raise `Encoding::UndefinedConversionError` on invalid characters)
|
|
99
|
+
|
|
100
|
+
* `cleaner: [nil|Symbol|Proc]`
|
|
101
|
+
Cleanse the data. Built-in rules:
|
|
102
|
+
* `:printable` removes all non-printable characters except `\r` and `\n`.
|
|
103
|
+
* `:replace_non_printable` replaces all non-printable characters except `\r` and `\n`
|
|
104
|
+
with the `replace` value, or an empty string when `replace` is nil.
|
|
105
|
+
A Proc can also be supplied to perform custom cleansing; it is called with the data
|
|
106
|
+
and the `replace` value after every read or write.
|
|
107
|
+
Default: nil
|
|
108
|
+
|
|
109
|
+
## Registering a custom extension
|
|
110
|
+
|
|
111
|
+
To add a new extension, supply its reader and writer classes. Both must implement `.open`
|
|
112
|
+
that yields a stream implementing `#read` or `#write` respectively. See any of the streams
|
|
113
|
+
under `lib/io_streams` for examples.
|
|
114
|
+
|
|
115
|
+
~~~ruby
|
|
116
|
+
IOStreams.register_extension(:xls, MyXls::Reader, MyXls::Writer)
|
|
117
|
+
~~~
|
|
118
|
+
|
|
119
|
+
Similarly, to support a new storage location, supply a Path class for its URI scheme.
|
|
120
|
+
See [IOStreams::Paths::S3](https://github.com/reidmorrison/iostreams/blob/main/lib/io_streams/paths/s3.rb)
|
|
121
|
+
for an example of what is required.
|
|
122
|
+
|
|
123
|
+
~~~ruby
|
|
124
|
+
IOStreams.register_scheme(:gcs, MyGoogleCloudStoragePath)
|
|
125
|
+
~~~
|