pdfh 0.1.4 → 0.1.5
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/.rubocop.yml +10 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +2 -2
- data/exe/pdfh +2 -1
- data/lib/ext/string.rb +13 -0
- data/lib/pdfh.rb +58 -41
- data/lib/pdfh/document.rb +66 -94
- data/lib/pdfh/month.rb +41 -0
- data/lib/pdfh/pdf_handler.rb +54 -0
- data/lib/pdfh/settings.rb +17 -7
- data/lib/pdfh/utils.rb +7 -2
- data/lib/pdfh/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84b58e13bf988d132601b4a22e7d58d2a9fedf0dbaefbe31263d49e441457361
|
4
|
+
data.tar.gz: c5c5faa977695d1e6cb0d42a692f1c951ce23d1fc07fe27e1dc714f16890a9e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c76919c53cc31219265484721f441dc36b5194e859ca8bb0c71528374012e72dab1dde8e3edf47745494501c27d0a0ffcbfb12480a385cef63dfe9dd20e8bffe
|
7
|
+
data.tar.gz: 041110ee2ffa94ce826d020d3e44e7779768358a3e1b8abe89b2743751ee1f1f46319d3ca680d8de9fab04c1dd816e14fa90571d327ab88772d3f53927dabf40
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.6.
|
1
|
+
ruby-2.6.2
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## v0.1.5
|
2
|
+
* Add print_cmd field in config file for information purposes
|
3
|
+
* Settings now validates a no existing directory
|
4
|
+
* Refactor for easier maintenance
|
5
|
+
|
6
|
+
## v0.1.4
|
7
|
+
* Add titleize format when writing new file
|
8
|
+
|
1
9
|
## v0.1.3
|
2
10
|
* Fixed copy companion files, which was not copying the files.
|
3
11
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pdfh (0.1.
|
4
|
+
pdfh (0.1.5)
|
5
5
|
colorize (~> 0.8.1)
|
6
6
|
|
7
7
|
GEM
|
@@ -12,7 +12,7 @@ GEM
|
|
12
12
|
diff-lcs (1.3)
|
13
13
|
docile (1.3.1)
|
14
14
|
hirb (0.7.3)
|
15
|
-
json (2.
|
15
|
+
json (2.2.0)
|
16
16
|
rake (10.5.0)
|
17
17
|
rspec (3.8.0)
|
18
18
|
rspec-core (~> 3.8.0)
|
data/exe/pdfh
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
require 'optparse'
|
5
5
|
require 'pdfh'
|
6
6
|
require 'pdfh/version'
|
7
|
+
require 'pdfh/utils'
|
7
8
|
|
8
9
|
options = {}
|
9
10
|
opt = OptionParser.new do |opts|
|
@@ -24,7 +25,7 @@ end
|
|
24
25
|
begin
|
25
26
|
opt.parse!
|
26
27
|
rescue OptionParser::InvalidOption => e
|
27
|
-
|
28
|
+
Pdfh.print_error e, exit_app: false
|
28
29
|
puts opt
|
29
30
|
exit 1
|
30
31
|
end
|
data/lib/ext/string.rb
ADDED
data/lib/pdfh.rb
CHANGED
@@ -12,41 +12,69 @@ module Pdfh
|
|
12
12
|
Verbose.active = options[:verbose]
|
13
13
|
Dry.active = options[:dry]
|
14
14
|
|
15
|
-
settings = Settings.new(search_config_file)
|
15
|
+
@settings = Settings.new(search_config_file)
|
16
|
+
puts "Destination path: #{@settings.base_path.light_blue}"
|
17
|
+
@settings.scrape_dirs.each do |work_directory|
|
18
|
+
process_directory(work_directory)
|
19
|
+
end
|
20
|
+
rescue StandardError => e
|
21
|
+
print_error e
|
22
|
+
end
|
16
23
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
base_name = File.basename(file, File.extname(file))
|
22
|
-
type = settings.match_doc_type(file)
|
23
|
-
unless type
|
24
|
-
ignored_files << base_name
|
25
|
-
next
|
26
|
-
end
|
24
|
+
def self.search_config_file
|
25
|
+
name = File.basename($PROGRAM_NAME)
|
26
|
+
names_to_look = ["#{name}.yml", "#{name}.yaml"]
|
27
|
+
dir_order = [Dir.pwd, File.expand_path('~')]
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
print_ident 'Sub-Type', doc.sub_type, :light_blue, width: pad
|
33
|
-
print_ident 'Period', doc.period, :light_blue, width: pad
|
34
|
-
print_ident 'New Name', doc.new_name, :light_blue, width: pad
|
35
|
-
print_ident 'Store Path', doc.store_path, :light_blue, width: pad
|
36
|
-
print_ident 'Other files', doc.companion_files(join: true), :light_blue, width: pad
|
37
|
-
doc.write_pdf(settings.base_path)
|
29
|
+
dir_order.each do |dir|
|
30
|
+
names_to_look.each do |file|
|
31
|
+
f = File.join(dir, file)
|
32
|
+
return f if File.file?(f)
|
38
33
|
end
|
34
|
+
end
|
39
35
|
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
raise StandardError, "no configuraton file (#{names_to_look.join(' or ')}) was found\n within paths: #{dir_order.join(', ')}"
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# @param [String] work_directory
|
41
|
+
def self.process_directory(work_directory)
|
42
|
+
print_separator work_directory
|
43
|
+
ignored_files = []
|
44
|
+
Dir["#{work_directory}/*.pdf"].each do |pdf_file|
|
45
|
+
type = @settings.match_doc_type(pdf_file)
|
46
|
+
if type
|
47
|
+
process_document(pdf_file, type)
|
48
|
+
else
|
49
|
+
ignored_files << basename_without_ext(pdf_file)
|
43
50
|
end
|
44
51
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
52
|
+
|
53
|
+
puts "\nNo account was matched for these PDF files:" unless ignored_files.empty?
|
54
|
+
ignored_files.each.with_index(1) { |file, index| print_ident index, file, :magenta }
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Generate document, and process actions
|
59
|
+
# @param [String] file
|
60
|
+
# @param [Type] type
|
61
|
+
# rubocop:disable Metrics/AbcSize
|
62
|
+
def self.process_document(file, type)
|
63
|
+
puts "Working on #{basename_without_ext(file).colorize(:light_green)}"
|
64
|
+
pad = 12
|
65
|
+
print_ident 'Type', type.name, :light_blue, width: pad
|
66
|
+
doc = Document.new(file, type)
|
67
|
+
print_ident 'Sub-Type', doc.sub_type, :light_blue, width: pad
|
68
|
+
print_ident 'Period', doc.period, :light_blue, width: pad
|
69
|
+
print_ident 'New Name', doc.new_name, :light_blue, width: pad
|
70
|
+
print_ident 'Store Path', doc.store_path, :light_blue, width: pad
|
71
|
+
print_ident 'Other files', doc.companion_files(join: true), :light_blue, width: pad
|
72
|
+
print_ident 'Print CMD', doc.print_cmd, :light_blue, width: pad
|
73
|
+
doc.write_pdf(@settings.base_path)
|
74
|
+
rescue StandardError => err
|
75
|
+
puts " Doc Error: #{err.message}".colorize(:red)
|
49
76
|
end
|
77
|
+
# rubocop:enable Metrics/AbcSize
|
50
78
|
|
51
79
|
def self.print_separator(title)
|
52
80
|
_rows, cols = `stty size`.split.map(&:to_i)
|
@@ -62,18 +90,7 @@ module Pdfh
|
|
62
90
|
puts "#{' ' * 4}#{field_str}: #{value_str}"
|
63
91
|
end
|
64
92
|
|
65
|
-
def self.
|
66
|
-
|
67
|
-
names_to_look = ["#{name}.yml", "#{name}.yaml"]
|
68
|
-
dir_order = [Dir.pwd, File.expand_path('~')]
|
69
|
-
|
70
|
-
dir_order.each do |dir|
|
71
|
-
names_to_look.each do |file|
|
72
|
-
f = File.join(dir, file)
|
73
|
-
return f if File.file?(f)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
raise StandardError, "no configuraton file (#{names_to_look.join(' or ')}) was found\n within paths: #{dir_order.join(', ')}"
|
93
|
+
def self.basename_without_ext(file)
|
94
|
+
File.basename(file, File.extname(file))
|
78
95
|
end
|
79
96
|
end
|
data/lib/pdfh/document.rb
CHANGED
@@ -1,73 +1,75 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'fileutils'
|
4
|
+
require 'pdfh/month'
|
5
|
+
require 'pdfh/pdf_handler'
|
4
6
|
require 'ext/string'
|
5
7
|
|
8
|
+
##
|
9
|
+
# main module
|
6
10
|
module Pdfh
|
7
|
-
using
|
11
|
+
using Extensions
|
12
|
+
|
13
|
+
##
|
14
|
+
# Regular Date Error, when there is not match
|
15
|
+
class ReDateError < StandardError
|
16
|
+
def initialize(message = 'No data matched your date regular expression')
|
17
|
+
super(message)
|
18
|
+
end
|
19
|
+
end
|
8
20
|
|
9
21
|
##
|
10
22
|
# Handles the PDF detected by the rules
|
11
|
-
# TODO: Replace command utils with this gem
|
12
|
-
# require 'pdf-reader'
|
13
|
-
#
|
14
|
-
# reader = PDF::Reader.new(temp)
|
15
|
-
# reader.pages.each do |page|
|
16
|
-
# @text << page.text
|
17
|
-
# end
|
18
23
|
class Document
|
19
|
-
|
20
|
-
|
21
|
-
MONTHS = {
|
22
|
-
enero: 1,
|
23
|
-
febrero: 2,
|
24
|
-
marzo: 3,
|
25
|
-
abril: 4,
|
26
|
-
mayo: 5,
|
27
|
-
junio: 6,
|
28
|
-
julio: 7,
|
29
|
-
agosto: 8,
|
30
|
-
septiembre: 9,
|
31
|
-
octubre: 10,
|
32
|
-
noviembre: 11,
|
33
|
-
diciembre: 12
|
34
|
-
}.freeze
|
24
|
+
attr_reader :text, :type, :file, :extra
|
35
25
|
|
36
26
|
def initialize(file, type, _options = {})
|
27
|
+
raise IOError, "File #{file} not found" unless File.exist?(file)
|
28
|
+
|
37
29
|
@file = file
|
38
30
|
@type = type
|
39
31
|
@month_offset = 0
|
40
32
|
@year_offset = 0
|
41
33
|
|
42
|
-
|
34
|
+
@pdf_doc = PdfHandler.new(file, @type.pwd)
|
43
35
|
|
44
36
|
Verbose.print "=== Type: #{type_name} =================="
|
45
|
-
@text =
|
46
|
-
Verbose.print " Text extracted: #{@text}"
|
37
|
+
@text = @pdf_doc.extract_text
|
47
38
|
@sub_type = extract_subtype(@type.sub_types)
|
48
39
|
Verbose.print " SubType: #{@sub_type}"
|
49
40
|
@month, @year, @extra = match_data
|
50
41
|
Verbose.print "==== Assigned: #{@month}, #{@year}, #{@extra} ==( Month, Year, Extra )================"
|
51
|
-
|
42
|
+
find_companion_files
|
43
|
+
end
|
44
|
+
|
45
|
+
def write_pdf(base_path)
|
46
|
+
full_path = File.join(base_path, store_path, new_name)
|
47
|
+
dir_path = File.join(base_path, store_path)
|
48
|
+
|
49
|
+
@pdf_doc.write_pdf(dir_path, full_path)
|
50
|
+
|
51
|
+
return if Dry.active?
|
52
|
+
|
53
|
+
make_document_backup
|
54
|
+
copy_companion_files(dir_path)
|
52
55
|
end
|
53
56
|
|
54
57
|
def period
|
55
|
-
|
56
|
-
|
57
|
-
"#{y}-#{m}"
|
58
|
+
formated_month = month.to_s.rjust(2, '0')
|
59
|
+
"#{year}-#{formated_month}"
|
58
60
|
end
|
59
61
|
|
60
62
|
def month
|
61
|
-
|
63
|
+
month = Month.normalize(@month) + @month_offset
|
62
64
|
|
63
|
-
case
|
65
|
+
case month
|
64
66
|
when 0 then
|
65
67
|
@year_offset = -1
|
66
68
|
12
|
67
69
|
when 13 then
|
68
70
|
@year_offset = 1
|
69
71
|
1
|
70
|
-
else
|
72
|
+
else month
|
71
73
|
end
|
72
74
|
end
|
73
75
|
|
@@ -99,11 +101,11 @@ module Pdfh
|
|
99
101
|
def new_name
|
100
102
|
template = @type.to_h.key?(:name_template) ? @type.name_template : '{original}'
|
101
103
|
new_name = template
|
102
|
-
.
|
103
|
-
.
|
104
|
-
.
|
105
|
-
.
|
106
|
-
.
|
104
|
+
.sub('{original}', file_name_only)
|
105
|
+
.sub('{period}', period)
|
106
|
+
.sub('{type}', type_name)
|
107
|
+
.sub('{subtype}', sub_type)
|
108
|
+
.sub('{extra}', extra || '')
|
107
109
|
"#{new_name}.pdf"
|
108
110
|
end
|
109
111
|
|
@@ -111,6 +113,13 @@ module Pdfh
|
|
111
113
|
@type.store_path.gsub(/\{YEAR\}/, year.to_s)
|
112
114
|
end
|
113
115
|
|
116
|
+
def print_cmd
|
117
|
+
return 'N/A' if type.print_cmd.nil? || type.print_cmd.empty?
|
118
|
+
|
119
|
+
relative_path = File.join(store_path, new_name)
|
120
|
+
"#{type.print_cmd} #{relative_path}"
|
121
|
+
end
|
122
|
+
|
114
123
|
def to_s
|
115
124
|
<<~STR
|
116
125
|
Name: #{file_name_only}
|
@@ -122,6 +131,7 @@ module Pdfh
|
|
122
131
|
New Name: #{new_name}
|
123
132
|
StorePath: #{store_path}
|
124
133
|
Companion: #{companion_files(join: true)}
|
134
|
+
Print Cmd: #{print_cmd}
|
125
135
|
STR
|
126
136
|
end
|
127
137
|
|
@@ -131,30 +141,6 @@ module Pdfh
|
|
131
141
|
@companion.empty? ? 'N/A' : @companion.join(', ')
|
132
142
|
end
|
133
143
|
|
134
|
-
def write_pdf(base_path)
|
135
|
-
Verbose.print '~~~~~~~~~~~~~~~~~~ Writing PDFs'
|
136
|
-
full_path = File.join(base_path, store_path, new_name)
|
137
|
-
dir_path = File.join(base_path, store_path)
|
138
|
-
|
139
|
-
raise IOError, "Path #{dir_path} was not found." unless Dir.exist?(dir_path)
|
140
|
-
|
141
|
-
password_opt = "--password='#{@type.pwd}'" if @type.pwd
|
142
|
-
cmd = %(qpdf #{password_opt} --decrypt '#{@file}' '#{full_path}')
|
143
|
-
Verbose.print " Write pdf command: #{cmd}"
|
144
|
-
|
145
|
-
return if Dry.active?
|
146
|
-
|
147
|
-
_result = `#{cmd}`
|
148
|
-
raise IOError, "File #{full_path} was not created." unless File.file?(full_path)
|
149
|
-
|
150
|
-
# Making backup of original
|
151
|
-
Dir.chdir(home_dir) do
|
152
|
-
File.rename(file, backup_name)
|
153
|
-
end
|
154
|
-
|
155
|
-
copy_companion_files(dir_path)
|
156
|
-
end
|
157
|
-
|
158
144
|
private
|
159
145
|
|
160
146
|
##
|
@@ -174,60 +160,46 @@ module Pdfh
|
|
174
160
|
nil
|
175
161
|
end
|
176
162
|
|
177
|
-
def normalize_month
|
178
|
-
month_num = @month.to_i
|
179
|
-
return month_num if month_num.positive?
|
180
|
-
|
181
|
-
if @month.size == 3
|
182
|
-
MONTHS.keys.each do |mon|
|
183
|
-
return MONTHS[mon] if mon.to_s[0, 3] == @month
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
MONTHS[@month.to_sym]
|
188
|
-
end
|
189
|
-
|
190
163
|
def home_dir
|
191
164
|
File.dirname(@file)
|
192
165
|
end
|
193
166
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
password_opt = "--password='#{@type.pwd}'" if @type.pwd
|
199
|
-
cmd = %(qpdf #{password_opt} --decrypt --stream-data=uncompress '#{@file}' '#{temp}')
|
200
|
-
Verbose.print " Command: #{cmd}"
|
201
|
-
_result = `#{cmd}`
|
202
|
-
Verbose.print 'Password removed.'
|
203
|
-
|
204
|
-
cmd2 = %(pdftotext -enc UTF-8 '#{temp}' -)
|
205
|
-
Verbose.print " Command: #{cmd2}"
|
206
|
-
`#{cmd2}`
|
207
|
-
end
|
208
|
-
|
167
|
+
##
|
168
|
+
# named matches can appear in any order with names 'd', 'm' and 'y'
|
169
|
+
# unamed matches needs to be in order month, year
|
170
|
+
# @return [Array] - format [month, year, day]
|
209
171
|
def match_data
|
210
172
|
Verbose.print '~~~~~~~~~~~~~~~~~~ RegEx'
|
211
173
|
Verbose.print " Using regex: #{@type.re_date}"
|
212
174
|
Verbose.print " named: #{@type.re_date.named_captures}"
|
213
175
|
matched = @type.re_date.match(@text)
|
176
|
+
raise ReDateError unless matched
|
177
|
+
|
214
178
|
Verbose.print " captured: #{matched.captures}"
|
215
179
|
|
216
180
|
return matched.captures.map(&:downcase) if @type.re_date.named_captures.empty?
|
217
181
|
|
218
|
-
extra = matched.captures.size > 2 ? matched[
|
182
|
+
extra = matched.captures.size > 2 ? matched[:d] : nil
|
219
183
|
[matched[:m].downcase, matched[:y], extra]
|
220
184
|
end
|
221
185
|
|
186
|
+
##
|
187
|
+
# Create a backup of original document
|
188
|
+
def make_document_backup
|
189
|
+
Dir.chdir(home_dir) do
|
190
|
+
File.rename(file, backup_name)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
222
194
|
def find_companion_files
|
223
195
|
Verbose.print '~~~~~~~~~~~~~~~~~~ Searching Companion files'
|
224
196
|
Verbose.print " Working on dir: #{home_dir}"
|
225
197
|
Dir.chdir(home_dir) do
|
226
198
|
all_files = Dir["#{file_name_only}.*"]
|
227
|
-
companion = all_files.reject { |
|
199
|
+
companion = all_files.reject { |file| file.include? 'pdf' }
|
228
200
|
Verbose.print " - #{companion.join(', ')}"
|
229
201
|
|
230
|
-
companion || []
|
202
|
+
@companion = companion || []
|
231
203
|
end
|
232
204
|
end
|
233
205
|
|
data/lib/pdfh/month.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pdfh
|
4
|
+
##
|
5
|
+
# Handles Month convertions
|
6
|
+
class Month
|
7
|
+
MONTHS = {
|
8
|
+
enero: 1,
|
9
|
+
febrero: 2,
|
10
|
+
marzo: 3,
|
11
|
+
abril: 4,
|
12
|
+
mayo: 5,
|
13
|
+
junio: 6,
|
14
|
+
julio: 7,
|
15
|
+
agosto: 8,
|
16
|
+
septiembre: 9,
|
17
|
+
octubre: 10,
|
18
|
+
noviembre: 11,
|
19
|
+
diciembre: 12
|
20
|
+
}.freeze
|
21
|
+
|
22
|
+
##
|
23
|
+
# @param [String] month
|
24
|
+
# @return [Integer]
|
25
|
+
def self.normalize(month)
|
26
|
+
# When param is a number
|
27
|
+
month_num = month.to_i
|
28
|
+
return month_num if month_num.between?(1, 12) # (1..12).include?(month_num)
|
29
|
+
|
30
|
+
# When param is a 3 char month: 'mar', 'nov'
|
31
|
+
if month.size == 3
|
32
|
+
MONTHS.keys.each do |mon|
|
33
|
+
return MONTHS[mon] if mon.to_s[0, 3] == month
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# When param has a direct match
|
38
|
+
MONTHS[month.to_sym]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pdfh
|
4
|
+
##
|
5
|
+
# Handles the Pdf document text extraction and password removal
|
6
|
+
# TODO: Replace command utils with this gem
|
7
|
+
# require 'pdf-reader'
|
8
|
+
#
|
9
|
+
# reader = PDF::Reader.new(temp)
|
10
|
+
# reader.pages.each do |page|
|
11
|
+
# @text << page.text
|
12
|
+
# end
|
13
|
+
class PdfHandler
|
14
|
+
attr_reader :file, :password
|
15
|
+
|
16
|
+
def initialize(file, password)
|
17
|
+
@file = file
|
18
|
+
@password = password
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Gets the text from the pdf in order to execute
|
23
|
+
# the regular expresiom matches
|
24
|
+
def extract_text
|
25
|
+
temp = `mktemp`.chomp
|
26
|
+
Verbose.print " --> #{temp} temporal file assigned."
|
27
|
+
|
28
|
+
password_opt = "--password='#{@password}'" if @password
|
29
|
+
cmd = %(qpdf #{password_opt} --decrypt --stream-data=uncompress '#{@file}' '#{temp}')
|
30
|
+
Verbose.print " Command: #{cmd}"
|
31
|
+
_result = `#{cmd}`
|
32
|
+
|
33
|
+
cmd2 = %(pdftotext -enc UTF-8 '#{temp}' -)
|
34
|
+
Verbose.print " Command: #{cmd2}"
|
35
|
+
text = `#{cmd2}`
|
36
|
+
Verbose.print " Text extracted: #{text}"
|
37
|
+
text
|
38
|
+
end
|
39
|
+
|
40
|
+
def write_pdf(dir_path, full_path)
|
41
|
+
Verbose.print '~~~~~~~~~~~~~~~~~~ Writing PDFs'
|
42
|
+
raise IOError, "Path #{dir_path} not found." unless Dir.exist?(dir_path)
|
43
|
+
|
44
|
+
password_opt = "--password='#{@password}'" if @password
|
45
|
+
cmd = %(qpdf #{password_opt} --decrypt '#{@file}' '#{full_path}')
|
46
|
+
Verbose.print " Write pdf command: #{cmd}"
|
47
|
+
|
48
|
+
return if Dry.active?
|
49
|
+
|
50
|
+
_result = `#{cmd}`
|
51
|
+
raise IOError, "File #{full_path} was not created." unless File.file?(full_path)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/pdfh/settings.rb
CHANGED
@@ -8,20 +8,18 @@ module Pdfh
|
|
8
8
|
##
|
9
9
|
# Handles the config yaml data mapping, and associates a file name with a doc type
|
10
10
|
class Settings
|
11
|
-
|
11
|
+
attr_reader :scrape_dirs, :base_path, :document_types
|
12
12
|
|
13
13
|
def initialize(file)
|
14
14
|
file_hash = YAML.load_file(file)
|
15
15
|
Verbose.print "Loaded configuration file: #{file}"
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
self.base_path = File.expand_path(file_hash['base_path'])
|
21
|
-
self.document_types = process_doc_types(file_hash['document_types'])
|
17
|
+
@scrape_dirs = process_scrape_dirs(file_hash['scrape_dirs'])
|
18
|
+
@base_path = File.expand_path(file_hash['base_path'])
|
19
|
+
@document_types = process_doc_types(file_hash['document_types'])
|
22
20
|
|
23
21
|
Verbose.print 'Processing directories:'
|
24
|
-
scrape_dirs.each { |
|
22
|
+
scrape_dirs.each { |dir| Verbose.print " - #{dir}" }
|
25
23
|
Verbose.print
|
26
24
|
end
|
27
25
|
|
@@ -38,6 +36,18 @@ module Pdfh
|
|
38
36
|
|
39
37
|
private
|
40
38
|
|
39
|
+
def process_scrape_dirs(scrape_dirs_list)
|
40
|
+
scrape_dirs_list.map do |dir|
|
41
|
+
expanded = File.expand_path(dir)
|
42
|
+
dir_exists = File.directory?(expanded)
|
43
|
+
if dir_exists
|
44
|
+
expanded
|
45
|
+
else
|
46
|
+
Verbose.print " ** Directory #{dir} does not exists."
|
47
|
+
end
|
48
|
+
end.compact
|
49
|
+
end
|
50
|
+
|
41
51
|
def process_doc_types(doc_types)
|
42
52
|
doc_types.map do |x|
|
43
53
|
object = OpenStruct.new(x)
|
data/lib/pdfh/utils.rb
CHANGED
@@ -2,9 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'colorize'
|
4
4
|
|
5
|
+
# Contains all generic short functionality
|
5
6
|
module Pdfh
|
6
|
-
class Error < StandardError; end
|
7
|
-
|
8
7
|
##
|
9
8
|
# Keeps Verbose option in whole project
|
10
9
|
class Verbose
|
@@ -34,4 +33,10 @@ module Pdfh
|
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
36
|
+
|
37
|
+
def self.print_error(exception, exit_app: true)
|
38
|
+
line = exception.backtrace[0].match(/:(?<line>\d+)/)[:line]
|
39
|
+
puts "Error, Line[#{line}]: #{exception.message}.".colorize(:red)
|
40
|
+
exit 1 if exit_app
|
41
|
+
end
|
37
42
|
end
|
data/lib/pdfh/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdfh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Isaias Piña
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01
|
11
|
+
date: 2019-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -119,8 +119,11 @@ files:
|
|
119
119
|
- bin/console
|
120
120
|
- bin/setup
|
121
121
|
- exe/pdfh
|
122
|
+
- lib/ext/string.rb
|
122
123
|
- lib/pdfh.rb
|
123
124
|
- lib/pdfh/document.rb
|
125
|
+
- lib/pdfh/month.rb
|
126
|
+
- lib/pdfh/pdf_handler.rb
|
124
127
|
- lib/pdfh/settings.rb
|
125
128
|
- lib/pdfh/utils.rb
|
126
129
|
- lib/pdfh/version.rb
|
@@ -148,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
151
|
- !ruby/object:Gem::Version
|
149
152
|
version: '0'
|
150
153
|
requirements: []
|
151
|
-
rubygems_version: 3.0.
|
154
|
+
rubygems_version: 3.0.3
|
152
155
|
signing_key:
|
153
156
|
specification_version: 4
|
154
157
|
summary: Organize PDF files
|