undrive_google 1.0.2 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58c9336e4f2abce68e28de0d2d80f14470d309df90c5f786366745d3304975fd
4
- data.tar.gz: 77071946f610d79e96346eacdb8c13998c744efe50b31f403241f680e74e6378
3
+ metadata.gz: 21db54609693c71de63f96619b5fa2d00b19d3bd1cd5ebe6c361b462c01159f6
4
+ data.tar.gz: a61c20ce99a83bdf0c73d982e8c91b6d9e8382e1047597e14872ee62a939c7bc
5
5
  SHA512:
6
- metadata.gz: 12bc586b4099a125a19d82c258256f915ca69ddd125c5ceaf7b1949d660398e68dd7897c9e01af0541d0b158d18033727ccb414760a45f33ad6fe86c2cc3a411
7
- data.tar.gz: a387a190061ef9f4df3d29e2a3aea5a116fafa0342c32972bcde91f4b4b3ccd13294139a9196e578d91b8288448cd5650ec40e663d0aef918af5ed6313414df4
6
+ metadata.gz: 6b127a2fbe3f90aac9aa13f97ef67a247f852cfc4bb141e103aa693833b3e5b71045f77661f0e4c663379dc5be0563e21bfdca9d37cfc808071c48f218238ec5
7
+ data.tar.gz: 211717e95caded549e29b6d89316abc1b32ee5ead71ebfd18b534a3c0b1c8b06bb3305e17f787e6284f7a957f42dde0a4ea7610540c35f22ef733b822d06af4f
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -10,8 +10,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
  ### Fixed
11
11
  ### Removed
12
12
 
13
+ ## 1.1.0 - 2022-11-29
14
+ ### Added
15
+ - Integration test for unzip transformation with complex zip file (Peter's Resume!)
16
+ ### Changed
17
+ - rename_proc now defaults to `nil` (meaning not renaming)
18
+ - Set it you need files renamed according to a pattern
19
+ ### Fixed
20
+ - Support HTML zip archives that have assets (e.g. /images/*)
21
+
22
+ ## 1.0.3 - 2022-11-01
23
+ ### Added
24
+ - Support for sweep option
25
+ ### Fixed
26
+ - Documentation for how to use CLI (with config file, Gemfile, etc. See README)
27
+
13
28
  ## 1.0.2 - 2022-10-27
14
- ###
29
+ ### Fixed
15
30
  - Fixed typo in warning when wrong version of google_drive is loaded
16
31
 
17
32
  ## 1.0.1 - 2022-10-27
data/README.md CHANGED
@@ -33,12 +33,52 @@ Therefore, if you need to unzip to HTML add another line to the `Gemfile` from a
33
33
  gem "rubyzip", github: "rubyzip/rubyzip", branch: "master"
34
34
  ```
35
35
 
36
- When liberating your files, ensure the script will use the Gemfile:
36
+ When liberating your files, ensure the script will use the Gemfile if it isn't in the same directory where you are running the `undrive_google` command:
37
37
  ```shell
38
38
  BUNDLE_GEMFILE=path/to/Gemfile bundle update
39
- BUNDLE_GEMFILE=path/to/Gemfile undrive_google -c path/to/config
39
+ BUNDLE_GEMFILE=path/to/Gemfile bundle exec undrive_google -c path/to/config
40
+ ```
41
+ NOTE: If the Gemfile and the config are in the same, current, directory, you can simply run:
42
+ ```shell
43
+ bundle exec undrive_google
44
+ ```
45
+
46
+ My complete `Gemfile` looks like this:
47
+ ```ruby
48
+ # frozen_string_literal: true
49
+
50
+ source "https://rubygems.org"
51
+
52
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
53
+ git_source(:gitlab) { |repo_name| "https://gitlab.com/#{repo_name}" }
54
+
55
+ gem "undrive_google", "~> 1.0.3"
56
+
57
+ # See: https://github.com/gimite/google-drive-ruby/pull/427
58
+ gem "google_drive", github: "pboling/google-drive-ruby", branch: "pboling-epub-mimetype"
59
+
60
+ # See: https://github.com/rubyzip/rubyzip#updating-to-version-30
61
+ gem "rubyzip", github: "rubyzip/rubyzip", branch: "master"
40
62
  ```
41
63
 
64
+ My `undrive_google.yml` config file looks like this (sanitized a bit):
65
+ ```yaml
66
+ file_id: "the-key-to-my-google-drive-file(find-in-the-url)"
67
+ key_file: serviceid-1234567890.json
68
+ dir: /my/path/to/my/cv
69
+ rename_html: resume.html
70
+ rename_pattern:
71
+ - " "
72
+ - "_"
73
+ extensions: zip
74
+ keep_zip: true
75
+ unzip: true
76
+ verbose: true
77
+ sweep: true
78
+ ```
79
+
80
+ The liberated files get published at [https://railsbling.com/cv](https://railsbling.com/cv).
81
+
42
82
  ## Story Time
43
83
 
44
84
  Imagine Google Drive is a 🐭
@@ -145,9 +185,10 @@ keep_zip: true
145
185
  # Only applies to files not explicitly specified with rename-<type>
146
186
  # Will never apply to the html file unzipped from the .zip
147
187
  # Used as: file_name.gsub(rename_pattern[0], rename_pattern[1])
148
- rename_pattern:
149
- - "_"
150
- - " "
188
+ # By default, no renaming, must be specified
189
+ # rename_pattern:
190
+ # - "_"
191
+ # - " "
151
192
 
152
193
  # [PATH]
153
194
  dir: '' # defaults to current working directory
data/exe/undrive_google CHANGED
@@ -3,11 +3,7 @@
3
3
 
4
4
  DEBUG = ENV["UNDRIVE_DEBUG"] == "true"
5
5
 
6
- # Std-lib
7
- require "optparse"
8
-
9
6
  # Gems
10
- require "bundler/setup"
11
7
  require "byebug" if DEBUG
12
8
 
13
9
  # This library
@@ -25,7 +25,7 @@ module UndriveGoogle
25
25
 
26
26
  if Options.instance.unzip
27
27
  @tr.unzip = Transformations::Unzip.new(@file_path)
28
- @tr.fix_html = Transformations::FixHtml.new(@tr.unzip)
28
+ @tr.fix_html = Transformations::FixHtml.new(@tr.unzip) if Options.instance.lang || Options.instance.title
29
29
  @tr.rename_html = Transformations::RenameHtml.new(@tr.unzip) if Options.instance.rename[:html]
30
30
  end
31
31
 
@@ -41,8 +41,9 @@ module UndriveGoogle
41
41
  def rename
42
42
  exact_name = Options.instance.rename[extension]
43
43
  return exact_name if exact_name
44
+ return file.title unless Options.instance.rename_proc
44
45
 
45
- Options.instance.rename_proc.call(file.title)
46
+ Options.instance.rename[extension] = Options.instance.rename_proc.call(file.title)
46
47
  end
47
48
 
48
49
  # The zip is an HTML file packaged in a .zip archive
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Actions
5
+ # Empty the directory before putting liberated files into it?
6
+ class Sweep
7
+ attr_accessor :dir
8
+
9
+ def initialize
10
+ @dir = Options.instance.dir
11
+ end
12
+
13
+ def sweep!
14
+ return if !dir || dir.length.zero? || dir == "/"
15
+ return unless Dir.exist?(dir)
16
+ return if Dir.empty?(dir)
17
+
18
+ puts "Sweeping directory clean #{dir}" if Options.instance.verbose
19
+ FileUtils.rm_rf Dir.glob("#{dir}/*")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -8,10 +8,11 @@ module UndriveGoogle
8
8
 
9
9
  # @return nil
10
10
  def liberate!
11
+ sweep! if Options.instance.sweep
11
12
  @exports = []
12
13
  extensions.each do |format|
13
14
  liberate = Actions::Liberate.new(format)
14
- exports << liberate.file_path
15
+ @exports << liberate.file_path
15
16
  liberate.liberate!
16
17
  end
17
18
 
@@ -20,6 +21,11 @@ module UndriveGoogle
20
21
 
21
22
  private
22
23
 
24
+ def sweep!
25
+ sweep = Actions::Sweep.new
26
+ sweep.sweep!
27
+ end
28
+
23
29
  def extensions
24
30
  Options.instance.extensions
25
31
  end
@@ -21,6 +21,9 @@ module UndriveGoogle
21
21
  rename[key.to_sym] = @args.delete(k.to_sym)
22
22
  end
23
23
  end
24
+ @args[:dir] = @args[:dir].strip if key?(:dir)
25
+ @args[:lang] = @args[:lang].strip if key?(:lang)
26
+ @args[:title] = @args[:title].strip if key?(:title)
24
27
  @args[:extensions] = Helpers::Parse.extensions(@args[:extensions]) if key?(:extensions)
25
28
  rename_pattern = @args.delete(:rename_pattern)
26
29
  @args[:rename_proc] = Helpers::Parse.rename_proc(rename_pattern) if rename_pattern
@@ -48,7 +51,7 @@ module UndriveGoogle
48
51
 
49
52
  def validate(config)
50
53
  unknown = config.keys - YAML_KEYS
51
- unknown.reject! { |key| key.start_with?("rename") }
54
+ unknown -= ALL_FILE_TYPES.map { |f| "rename_#{f}".to_sym }
52
55
  return if unknown.empty?
53
56
 
54
57
  raise UndriveGoogle::Error, "Unhandled config keys #{unknown.inspect}"
@@ -8,13 +8,13 @@ module UndriveGoogle
8
8
 
9
9
  def extensions(val)
10
10
  if val.is_a?(Array)
11
- unknown = val.map(&:to_sym) - FILE_TYPES
11
+ unknown = val.map(&:to_sym) - DL_FILE_TYPES
12
12
  raise UndriveGoogle::Error, "Unknown extensions: #{unknown}" if unknown.any?
13
13
 
14
14
  val
15
15
  elsif val == "all"
16
- FILE_TYPES
17
- elsif FILE_TYPES.include?(val.to_sym)
16
+ DL_FILE_TYPES
17
+ elsif DL_FILE_TYPES.include?(val.to_sym)
18
18
  [val]
19
19
  else
20
20
  raise UndriveGoogle::Error, "Unknown extensions: #{val}"
@@ -7,7 +7,7 @@ module UndriveGoogle
7
7
 
8
8
  attr_accessor :config_yaml, :key_file, :file_id, :file_by, :extensions, :unzip,
9
9
  :keep_zip, :rename, :rename_proc, :lang, :title,
10
- :dir, :verbose
10
+ :dir, :sweep, :verbose
11
11
 
12
12
  def initialize
13
13
  load_yaml(true)
@@ -31,6 +31,7 @@ module UndriveGoogle
31
31
  dir_option(parser)
32
32
  lang_option(parser)
33
33
  title_option(parser)
34
+ sweep_option(parser)
34
35
  verbose_option(parser)
35
36
 
36
37
  parser.separator ""
@@ -96,7 +97,7 @@ module UndriveGoogle
96
97
  end
97
98
 
98
99
  def rename_options(parser)
99
- (FILE_TYPES + [:html]).each do |ft|
100
+ ALL_FILE_TYPES.each do |ft|
100
101
  parser.on("--rename-#{ft} [FILENAME]", "Rename #{ft} to FILENAME") do |rename|
101
102
  self.rename[ft] = rename
102
103
  end
@@ -109,19 +110,27 @@ module UndriveGoogle
109
110
  def dir_option(parser)
110
111
  parser.on("-d", "--dir PATH", String,
111
112
  "Path to directory where liberated files will go") do |dir|
112
- self.dir = dir
113
+ self.dir = dir.strip
113
114
  end
114
115
  end
115
116
 
116
117
  def lang_option(parser)
117
118
  parser.on("-l", "--lang LANG", String, 'Add lang="LANG" attribute to <html> tag of unzipped HTML') do |lang|
118
- self.lang = lang
119
+ self.lang = lang.strip
119
120
  end
120
121
  end
121
122
 
122
123
  def title_option(parser)
123
124
  parser.on("-t", "--title TITLE", String, "Add <title>TITLE</title> element to unzipped HTML") do |title|
124
- self.title = title
125
+ self.title = title.strip
126
+ end
127
+ end
128
+
129
+ def sweep_option(parser)
130
+ # Boolean switch.
131
+ parser.on("-s", "--[no-]sweep",
132
+ "Delete all files from directory specified by -d or --dir prior to liberation") do |sweep|
133
+ self.sweep = sweep
125
134
  end
126
135
  end
127
136
 
@@ -138,14 +147,15 @@ module UndriveGoogle
138
147
  self.key_file = KEY_FILE_PATH
139
148
  self.file_id = nil
140
149
  self.file_by = :key
141
- self.extensions = FILE_TYPES # On the command line, "all" expands to FILE_TYPES
150
+ self.extensions = DL_FILE_TYPES # "all" expands to FILE_TYPES
142
151
  self.unzip = true
143
152
  self.keep_zip = true
144
153
  self.rename = {}
145
- self.rename_proc = RENAME_PROC
154
+ self.rename_proc = nil
146
155
  self.title = nil
147
156
  self.dir = nil
148
157
  self.lang = "en"
158
+ self.sweep = false
149
159
  self.verbose = false
150
160
  end
151
161
 
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UndriveGoogle
4
- class Transformation < Struct.new(:download,
5
- :unzip,
6
- :fix_html,
7
- :rename_html,
8
- :delete_zip,
9
- keyword_init: true)
4
+ Transformation = Struct.new(:download,
5
+ :unzip,
6
+ :fix_html,
7
+ :rename_html,
8
+ :delete_zip,
9
+ keyword_init: true) do
10
10
  def liberate!
11
11
  each(&:process)
12
12
  end
@@ -17,6 +17,7 @@ module UndriveGoogle
17
17
 
18
18
  def_delegator :@unzip, :html_path
19
19
 
20
+ # @param unzip [UndriveGoogle::Transformations::Unzip]
20
21
  def initialize(unzip)
21
22
  @unzip = unzip
22
23
  @lang = Options.instance.lang
@@ -25,20 +26,29 @@ module UndriveGoogle
25
26
 
26
27
  # @return nil
27
28
  def process
28
- return unless lang || title
29
+ return unless lang_valid? || title_valid?
30
+ raise UndriveGoogle::Error, "No file at #{html_path}" unless File.exist?(html_path)
29
31
 
30
32
  puts "Checking html in #{html_path}" if Options.instance.verbose
31
33
  file = File.open(html_path)
32
34
  html = file.read
33
- return unless html
35
+ return unless html.length >= 12 # open & close <html> or <head> is at least 12 chars
34
36
 
35
37
  puts "Fixing html in #{html_path}" if Options.instance.verbose
36
- html.sub!("<html>", "<html lang=\"#{lang}\">") if lang
37
- html.sub!("<head>", "<head><title>#{title}</title>") if title
38
+ html.sub!("<html>", "<html lang=\"#{lang}\">") if lang_valid?
39
+ html.sub!("<head>", "<head><title>#{title}</title>") if title_valid?
38
40
  File.write(html_path, html)
39
41
 
40
42
  nil
41
43
  end
44
+
45
+ def lang_valid?
46
+ lang && lang.length >= 2
47
+ end
48
+
49
+ def title_valid?
50
+ title && title.length >= 1
51
+ end
42
52
  end
43
53
  end
44
54
  end
@@ -10,6 +10,7 @@ module UndriveGoogle
10
10
 
11
11
  def_delegator :@unzip, :html_path
12
12
 
13
+ # @param unzip [UndriveGoogle::Transformations::Unzip]
13
14
  def initialize(unzip)
14
15
  @unzip = unzip
15
16
  end
@@ -4,10 +4,11 @@ module UndriveGoogle
4
4
  module Transformations
5
5
  # Download a particular version of the file.
6
6
  class Unzip
7
- attr_accessor :file_path, :html_path
7
+ attr_accessor :file_path, :html_path, :destination
8
8
 
9
- def initialize(file_path)
9
+ def initialize(file_path, destination = nil)
10
10
  @file_path = file_path
11
+ @destination = destination || File.dirname(file_path)
11
12
  end
12
13
 
13
14
  # @return nil
@@ -23,16 +24,11 @@ module UndriveGoogle
23
24
 
24
25
  private
25
26
 
26
- def destination
27
- File.dirname(file_path)
28
- end
29
-
30
27
  def extract_zip(file, destination)
31
- FileUtils.mkdir_p(destination)
32
-
33
28
  Zip::File.open(file) do |zip_file|
34
29
  zip_file.each do |f|
35
30
  @html_path = File.join(destination, f.name)
31
+ FileUtils.mkdir_p(File.dirname(@html_path))
36
32
  zip_file.extract(f, @html_path) unless File.exist?(@html_path)
37
33
  end
38
34
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module UndriveGoogle
4
4
  module Version
5
- VERSION = "1.0.2"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Std-lib
4
- require "psych" # The Ruby YAML library
4
+ require "fileutils" # The Ruy File Utilities Library
5
5
  require "forwardable" # The Ruby Delegation library
6
+ require "optparse" # The Ruby CLI Options Parsing library
7
+ require "psych" # The Ruby YAML library
6
8
  require "singleton" # The Ruby Singleton library
7
9
 
8
10
  # third party gems
@@ -13,13 +15,16 @@ rescue LoadError
13
15
  end
14
16
  require "version_gem"
15
17
  require "google_drive"
16
- gd_fixed = GoogleDrive::Util::EXT_TO_CONTENT_TYPE.key?('.epub')
17
- warn "[WARN] Your version of google_drive does not support .epub. If you need .epub support see: https://sr.ht/~galtzo/undrive_google/#note-export-epub-amp-unzip-html" unless gd_fixed
18
+ HAS_EPUB = GoogleDrive::Util::EXT_TO_CONTENT_TYPE.key?(".epub")
19
+ unless HAS_EPUB
20
+ warn "[WARN] Your version of google_drive does not support .epub. If you need .epub support see: https://sr.ht/~galtzo/undrive_google/#note-export-epub-amp-unzip-html"
21
+ end
18
22
 
19
23
  require_relative "undrive_google/version"
20
24
 
21
25
  require_relative "undrive_google/helpers/parse"
22
26
  require_relative "undrive_google/actions/liberate"
27
+ require_relative "undrive_google/actions/sweep"
23
28
  require_relative "undrive_google/transformations/delete_zip"
24
29
  require_relative "undrive_google/transformations/download"
25
30
  require_relative "undrive_google/transformations/fix_html"
@@ -39,7 +44,15 @@ module UndriveGoogle
39
44
  title: :title,
40
45
  key: :key
41
46
  }.freeze
42
- FILE_TYPES = %i[docx odt rtf pdf txt zip epub].freeze
47
+ # epub is conditionally in DL_FILE_TYPES depending on the google_drive gem used.
48
+ DL_FILE_TYPES = if HAS_EPUB
49
+ %i[docx odt rtf pdf txt zip epub].freeze
50
+ else
51
+ %i[docx odt rtf pdf txt zip].freeze
52
+ end
53
+ # html is not in DL_FILE_TYPES because you can't download it directly.
54
+ # html is downloaded as zip, and must then be unzipped.
55
+ ALL_FILE_TYPES = DL_FILE_TYPES + %i[html].freeze
43
56
  RENAME_PROC = ->(orig) { orig.tr(" ", "_") }
44
57
  YAML_KEYS = %i[
45
58
  key_file
@@ -52,6 +65,7 @@ module UndriveGoogle
52
65
  title
53
66
  dir
54
67
  lang
68
+ sweep
55
69
  verbose
56
70
  ].freeze
57
71
  CONFIG_YAML_PATH = "undrive_google.yaml"
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undrive_google
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
@@ -36,7 +36,7 @@ cert_chain:
36
36
  gwGrEXGQGDZ0NIgBcmvMOqlXjkGQwQvugKycJ024z89+fz2332vdZIKTrSxJrXGk
37
37
  4/bR9A==
38
38
  -----END CERTIFICATE-----
39
- date: 2022-10-27 00:00:00.000000000 Z
39
+ date: 2022-11-29 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: google_drive
@@ -117,6 +117,7 @@ files:
117
117
  - exe/undrive_google
118
118
  - lib/undrive_google.rb
119
119
  - lib/undrive_google/actions/liberate.rb
120
+ - lib/undrive_google/actions/sweep.rb
120
121
  - lib/undrive_google/captive_file.rb
121
122
  - lib/undrive_google/cli.rb
122
123
  - lib/undrive_google/config_file.rb
@@ -138,7 +139,7 @@ metadata:
138
139
  source_code_uri: https://git.sr.ht/~galtzo/undrive_google
139
140
  changelog_uri: https://git.sr.ht/~galtzo/undrive_google
140
141
  bug_tracker_uri: https://todo.sr.ht/~galtzo/undrive_google
141
- documentation_uri: https://www.rubydoc.info/gems/undrive_google/1.0.2
142
+ documentation_uri: https://www.rubydoc.info/gems/undrive_google/1.1.0
142
143
  wiki_uri: https://man.sr.ht/~galtzo/undrive_google/
143
144
  funding_uri: https://liberapay.com/pboling
144
145
  mailing_list_uri: https://lists.sr.ht/~galtzo/undrive_google-devel
@@ -158,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  - !ruby/object:Gem::Version
159
160
  version: '0'
160
161
  requirements: []
161
- rubygems_version: 3.3.24
162
+ rubygems_version: 3.3.26
162
163
  signing_key:
163
164
  specification_version: 4
164
165
  summary: "\U0001F3F4 Liberate files from your Google Drive"
metadata.gz.sig CHANGED
Binary file