pdfh 0.1.2
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +2 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +52 -0
- data/LICENSE +21 -0
- data/README.md +53 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/exe/pdfh +32 -0
- data/lib/pdfh.rb +79 -0
- data/lib/pdfh/document.rb +234 -0
- data/lib/pdfh/settings.rb +51 -0
- data/lib/pdfh/utils.rb +37 -0
- data/lib/pdfh/version.rb +5 -0
- data/pdfh.gemspec +48 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 95cec308eb73971828236c5ce0d322a5df4c3124d611aa34a81a1c7187ebd9f0
|
4
|
+
data.tar.gz: d8ae0ae4019d53bb61415521f768d5f1a8eab01f2257bd3089fbff47ce3926a9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5237b1934642a3e52c8bd17b4598298c10c66bb90fb9961e9798b12466f9a0d5873f9443c896fe1fb243c6365981aa714c14a126ae2854a8a185fccc9bb77fa1
|
7
|
+
data.tar.gz: 3a04a2ff6debcf3af1c7fd6ed794daac06d6f853f3b51b3ff3bab4a91a8831125c61f1873a7b4d02ef65e24b15419162260fd911c8c112c60ed3287242ea87ae
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pdfh
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.6.0
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at iax7@users.noreply.github.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pdfh (0.1.2)
|
5
|
+
colorize (~> 0.8.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ansi (1.5.0)
|
11
|
+
colorize (0.8.1)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
docile (1.3.1)
|
14
|
+
hirb (0.7.3)
|
15
|
+
json (2.1.0)
|
16
|
+
rake (10.5.0)
|
17
|
+
rspec (3.8.0)
|
18
|
+
rspec-core (~> 3.8.0)
|
19
|
+
rspec-expectations (~> 3.8.0)
|
20
|
+
rspec-mocks (~> 3.8.0)
|
21
|
+
rspec-core (3.8.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-expectations (3.8.2)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.8.0)
|
26
|
+
rspec-mocks (3.8.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-support (3.8.0)
|
30
|
+
simplecov (0.16.1)
|
31
|
+
docile (~> 1.1)
|
32
|
+
json (>= 1.8, < 3)
|
33
|
+
simplecov-html (~> 0.10.0)
|
34
|
+
simplecov-console (0.4.2)
|
35
|
+
ansi
|
36
|
+
hirb
|
37
|
+
simplecov
|
38
|
+
simplecov-html (0.10.2)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler (~> 1.17.2)
|
45
|
+
pdfh!
|
46
|
+
rake (~> 10.0)
|
47
|
+
rspec (~> 3.0)
|
48
|
+
simplecov (~> 0.16.1)
|
49
|
+
simplecov-console (~> 0.4.2)
|
50
|
+
|
51
|
+
BUNDLED WITH
|
52
|
+
1.17.2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Isaias Piña
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Pdfh
|
2
|
+
|
3
|
+
Examine all PDF files in scrape directories, remove password (if has one), rename and copy to a new directory using regular expresions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install pdfh
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
After installing this gem you need to create your configuration file on your home folder.
|
14
|
+
`pdfh.yml`
|
15
|
+
```yaml
|
16
|
+
---
|
17
|
+
scrape_dirs: # Directories where all pdf's are going to be analyzed
|
18
|
+
- ~/Downloads
|
19
|
+
base_path: ~/PDFs # Directory where all matching documents will be copied
|
20
|
+
document_types:
|
21
|
+
- name: Document From Bank # Description
|
22
|
+
re_file: '.*MyBankReg\.pdf' # Regular expression to match its filename
|
23
|
+
re_date: 'al \d{1,2} de (\w+) del? (\d+)' # Date regular expresion
|
24
|
+
pwd: base64string # [OPTIONAL] Password if the document is protected
|
25
|
+
store_path: "{YEAR}/bank_docs" # Relative path to copy this document
|
26
|
+
name_template: '{period} {subtype}' # Template for new filename when copied
|
27
|
+
sub_types: # [OPTIONAL] In case your need an extra category
|
28
|
+
- name: Account1 # Regular expresion to match this subtype
|
29
|
+
month_offset: -1 # [OPTIONAL] Integer value to adjust month
|
30
|
+
```
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
|
+
|
36
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
37
|
+
|
38
|
+
```bash
|
39
|
+
build pdfh.gemspec
|
40
|
+
gem install pdfh-*
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pdfh. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
50
|
+
|
51
|
+
## Code of Conduct
|
52
|
+
|
53
|
+
Everyone interacting in the Pdfh project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pdfh/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'pdfh'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require 'pry'
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/pdfh
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'optparse'
|
5
|
+
require 'pdfh'
|
6
|
+
require 'pdfh/version'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
opt = OptionParser.new do |opts|
|
10
|
+
# Process ARGV
|
11
|
+
opts.banner = "Usage: #{opts.program_name} [options]"
|
12
|
+
opts.on('-v', '--verbose', 'Show more output, useful for debug') { |o| options[:verbose] = o }
|
13
|
+
opts.on('-d', '--dry', 'Dry run, does not write new pdf') { |o| options[:dry] = o }
|
14
|
+
opts.on_tail('-V', '--version', 'Show version') do
|
15
|
+
puts "#{opts.program_name} v#{Pdfh::VERSION}"
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
19
|
+
puts opts
|
20
|
+
exit
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
opt.parse!
|
26
|
+
rescue OptionParser::InvalidOption => e
|
27
|
+
puts "Error, #{e.message}".colorize(:red)
|
28
|
+
puts opt
|
29
|
+
exit 1
|
30
|
+
end
|
31
|
+
|
32
|
+
Pdfh.main(options)
|
data/lib/pdfh.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pdfh/version'
|
4
|
+
require 'pdfh/settings'
|
5
|
+
require 'pdfh/document'
|
6
|
+
require 'pdfh/utils'
|
7
|
+
|
8
|
+
##
|
9
|
+
# Gem entry point
|
10
|
+
module Pdfh
|
11
|
+
def self.main(options = {})
|
12
|
+
Verbose.active = options[:verbose]
|
13
|
+
Dry.active = options[:dry]
|
14
|
+
|
15
|
+
settings = Settings.new(search_config_file)
|
16
|
+
|
17
|
+
settings.scrape_dirs.each do |work_directory|
|
18
|
+
print_separator(work_directory)
|
19
|
+
ignored_files = []
|
20
|
+
Dir["#{work_directory}/*.pdf"].each do |file|
|
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
|
27
|
+
|
28
|
+
pad = 12
|
29
|
+
puts "Working on #{base_name.colorize(:light_green)}"
|
30
|
+
print_ident 'Type', type.name, :light_blue, width: pad
|
31
|
+
doc = Document.new(file, type)
|
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)
|
38
|
+
end
|
39
|
+
|
40
|
+
puts "\nNo account was matched for these PDF files:" unless ignored_files.empty?
|
41
|
+
ignored_files.each.with_index(1) do |file, index|
|
42
|
+
print_ident index, file, :red
|
43
|
+
end
|
44
|
+
end
|
45
|
+
rescue StandardError => e
|
46
|
+
line = e.backtrace[0].match(/:(\d+)/)[1]
|
47
|
+
puts "Error, #{e.message}. #{line}".colorize(:red)
|
48
|
+
exit 1
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.print_separator(title)
|
52
|
+
_rows, cols = `stty size`.split.map(&:to_i)
|
53
|
+
sep = "\n#{'-' * 40} #{title} "
|
54
|
+
remaining_cols = cols - sep.size
|
55
|
+
sep += '-' * remaining_cols if remaining_cols.positive?
|
56
|
+
puts sep.colorize(:light_yellow)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.print_ident(field, value, color = :green, width: 3)
|
60
|
+
field_str = field.to_s.rjust(width)
|
61
|
+
value_str = value.colorize(color)
|
62
|
+
puts "#{' ' * 4}#{field_str}: #{value_str}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.search_config_file
|
66
|
+
name = File.basename($PROGRAM_NAME)
|
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(', ')}"
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,234 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pdfh
|
4
|
+
##
|
5
|
+
# Handles the PDF detected by the rules
|
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 Document
|
14
|
+
attr_accessor :text, :type, :file, :extra
|
15
|
+
|
16
|
+
MONTHS = {
|
17
|
+
enero: 1,
|
18
|
+
febrero: 2,
|
19
|
+
marzo: 3,
|
20
|
+
abril: 4,
|
21
|
+
mayo: 5,
|
22
|
+
junio: 6,
|
23
|
+
julio: 7,
|
24
|
+
agosto: 8,
|
25
|
+
septiembre: 9,
|
26
|
+
octubre: 10,
|
27
|
+
noviembre: 11,
|
28
|
+
diciembre: 12
|
29
|
+
}.freeze
|
30
|
+
|
31
|
+
def initialize(file, type, _options = {})
|
32
|
+
@file = file
|
33
|
+
@type = type
|
34
|
+
@month_offset = 0
|
35
|
+
@year_offset = 0
|
36
|
+
|
37
|
+
raise IOError, "File #{file} not found" unless File.exist?(file)
|
38
|
+
|
39
|
+
Verbose.print "=== Type: #{type_name} =================="
|
40
|
+
@text = pdf_text
|
41
|
+
Verbose.print " Text extracted: #{@text}"
|
42
|
+
@sub_type = extract_subtype(@type.sub_types)
|
43
|
+
Verbose.print " SubType: #{@sub_type}"
|
44
|
+
@month, @year, @extra = match_data
|
45
|
+
Verbose.print "==== Assigned: #{@month}, #{@year}, #{@extra} ==( Month, Year, Extra )================"
|
46
|
+
end
|
47
|
+
|
48
|
+
def period
|
49
|
+
m = month.to_s.rjust(2, '0')
|
50
|
+
y = year
|
51
|
+
"#{y}-#{m}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def month
|
55
|
+
m = normalize_month + @month_offset
|
56
|
+
|
57
|
+
case m
|
58
|
+
when 0 then
|
59
|
+
@year_offset = -1
|
60
|
+
12
|
61
|
+
when 13 then
|
62
|
+
@year_offset = 1
|
63
|
+
1
|
64
|
+
else m
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def year
|
69
|
+
tmp = @year.size == 2 ? "20#{@year}" : @year
|
70
|
+
tmp.to_i + @year_offset
|
71
|
+
end
|
72
|
+
|
73
|
+
def file_name_only
|
74
|
+
File.basename(@file, File.extname(@file))
|
75
|
+
end
|
76
|
+
|
77
|
+
def file_name
|
78
|
+
File.basename(@file)
|
79
|
+
end
|
80
|
+
|
81
|
+
def backup_name
|
82
|
+
"#{file_name}.bkp"
|
83
|
+
end
|
84
|
+
|
85
|
+
def type_name
|
86
|
+
@type ? @type.name : 'N/A'
|
87
|
+
end
|
88
|
+
|
89
|
+
def sub_type
|
90
|
+
@sub_type ? @sub_type.name : 'N/A'
|
91
|
+
end
|
92
|
+
|
93
|
+
def new_name
|
94
|
+
template = @type.to_h.key?(:name_template) ? @type.name_template : '{original}'
|
95
|
+
new_name = template
|
96
|
+
.gsub(/\{original\}/, file_name_only)
|
97
|
+
.gsub(/\{period\}/, period)
|
98
|
+
.gsub(/\{type\}/, type_name)
|
99
|
+
.gsub(/\{subtype\}/, sub_type)
|
100
|
+
.gsub(/\{extra\}/, extra || '')
|
101
|
+
"#{new_name}.pdf"
|
102
|
+
end
|
103
|
+
|
104
|
+
def store_path
|
105
|
+
@type.store_path.gsub(/\{YEAR\}/, year.to_s)
|
106
|
+
end
|
107
|
+
|
108
|
+
def to_s
|
109
|
+
<<~STR
|
110
|
+
Name : #{file_name_only}
|
111
|
+
Type : #{type_name}
|
112
|
+
Sub Type : #{sub_type}
|
113
|
+
Period : #{period}
|
114
|
+
File Path: #{file}
|
115
|
+
File Name: #{file_name}
|
116
|
+
New Name : #{new_name}
|
117
|
+
StorePath: #{store_path}
|
118
|
+
STR
|
119
|
+
end
|
120
|
+
|
121
|
+
def companion_files(join: false)
|
122
|
+
files = find_companion_files
|
123
|
+
|
124
|
+
files unless join
|
125
|
+
|
126
|
+
files.empty? ? 'N/A' : files.join(', ')
|
127
|
+
end
|
128
|
+
|
129
|
+
def write_pdf(base_path)
|
130
|
+
full_path = File.join(base_path, store_path, new_name)
|
131
|
+
dir_path = File.join(base_path, store_path)
|
132
|
+
|
133
|
+
raise IOError, "Path #{dir_path} was not found." unless Dir.exist?(dir_path)
|
134
|
+
|
135
|
+
password_opt = "--password='#{@type.pwd}'" if @type.pwd
|
136
|
+
cmd = %(qpdf #{password_opt} --decrypt '#{@file}' '#{full_path}')
|
137
|
+
Verbose.print " Write pdf command: #{cmd}"
|
138
|
+
|
139
|
+
return if Dry.active?
|
140
|
+
|
141
|
+
_result = `#{cmd}`
|
142
|
+
raise IOError, "File #{full_path} was not created." unless File.file?(full_path)
|
143
|
+
|
144
|
+
# Making backup of original
|
145
|
+
Dir.chdir(home_dir) do
|
146
|
+
File.rename(file, backup_name)
|
147
|
+
end
|
148
|
+
|
149
|
+
copy_companion_files(dir_path)
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
##
|
155
|
+
# @param [Array] subtypes
|
156
|
+
# @return [OpenStruct]
|
157
|
+
def extract_subtype(sub_types)
|
158
|
+
return nil if sub_types.nil? || sub_types.empty?
|
159
|
+
|
160
|
+
sub_types.each do |st|
|
161
|
+
is_matched = Regexp.new(st['name']).match?(@text)
|
162
|
+
next unless is_matched
|
163
|
+
|
164
|
+
sub = OpenStruct.new(st)
|
165
|
+
@month_offset = sub.month_offset || 0
|
166
|
+
return sub
|
167
|
+
end
|
168
|
+
nil
|
169
|
+
end
|
170
|
+
|
171
|
+
def normalize_month
|
172
|
+
month_num = @month.to_i
|
173
|
+
return month_num if month_num.positive?
|
174
|
+
|
175
|
+
if @month.size == 3
|
176
|
+
MONTHS.keys.each do |mon|
|
177
|
+
return MONTHS[mon] if mon.to_s[0, 3] == @month
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
MONTHS[@month.to_sym]
|
182
|
+
end
|
183
|
+
|
184
|
+
def home_dir
|
185
|
+
File.dirname(@file)
|
186
|
+
end
|
187
|
+
|
188
|
+
def pdf_text
|
189
|
+
temp = `mktemp`.chomp
|
190
|
+
Verbose.print " --> #{temp} temporal file assigned."
|
191
|
+
|
192
|
+
password_opt = "--password='#{@type.pwd}'" if @type.pwd
|
193
|
+
cmd = %(qpdf #{password_opt} --decrypt --stream-data=uncompress '#{@file}' '#{temp}')
|
194
|
+
Verbose.print " Command: #{cmd}"
|
195
|
+
_result = `#{cmd}`
|
196
|
+
Verbose.print 'Password removed.'
|
197
|
+
|
198
|
+
cmd2 = %(pdftotext -enc UTF-8 '#{temp}' -)
|
199
|
+
Verbose.print " Command: #{cmd2}"
|
200
|
+
`#{cmd2}`
|
201
|
+
end
|
202
|
+
|
203
|
+
def match_data
|
204
|
+
Verbose.print '~~~~~~~~~~~~~~~~~~ RegEx'
|
205
|
+
Verbose.print " Using regex: #{@type.re_date}"
|
206
|
+
Verbose.print " named: #{@type.re_date.named_captures}"
|
207
|
+
matched = @type.re_date.match(@text)
|
208
|
+
Verbose.print " captured: #{matched.captures}"
|
209
|
+
|
210
|
+
return matched.captures.map(&:downcase) if @type.re_date.named_captures.empty?
|
211
|
+
|
212
|
+
extra = matched.captures.size > 2 ? matched[3] : nil
|
213
|
+
[matched[:m].downcase, matched[:y], extra]
|
214
|
+
end
|
215
|
+
|
216
|
+
def find_companion_files
|
217
|
+
Verbose.print '~~~~~~~~~~~~~~~~~~ Searching Companion files'
|
218
|
+
Verbose.print " Working on dir: #{home_dir}"
|
219
|
+
Dir.chdir(home_dir) do
|
220
|
+
all_files = Dir["#{file_name_only}.*"]
|
221
|
+
companion = all_files.reject { |f| f.include? 'pdf' }
|
222
|
+
|
223
|
+
companion || []
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
def copy_companion_files(destination)
|
228
|
+
find_companion_files.each do |file|
|
229
|
+
Verbose.print " cp #{file} --> #{destination}"
|
230
|
+
FileUtils.cp(file, destination)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'base64'
|
6
|
+
|
7
|
+
module Pdfh
|
8
|
+
##
|
9
|
+
# Handles the config yaml data mapping, and associates a file name with a doc type
|
10
|
+
class Settings
|
11
|
+
attr_accessor :scrape_dirs, :base_path, :document_types
|
12
|
+
|
13
|
+
def initialize(file)
|
14
|
+
file_hash = YAML.load_file(file)
|
15
|
+
Verbose.print "Loaded configuration file: #{file}"
|
16
|
+
|
17
|
+
self.scrape_dirs = file_hash['scrape_dirs'].map do |d|
|
18
|
+
File.expand_path(d)
|
19
|
+
end
|
20
|
+
self.base_path = File.expand_path(file_hash['base_path'])
|
21
|
+
self.document_types = process_doc_types(file_hash['document_types'])
|
22
|
+
|
23
|
+
Verbose.print 'Processing directories:'
|
24
|
+
scrape_dirs.each { |d| Verbose.print " - #{d}" }
|
25
|
+
Verbose.print
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# @param [String] file_name
|
30
|
+
# @return [OpenStruct]
|
31
|
+
def match_doc_type(file_name)
|
32
|
+
document_types.each do |type|
|
33
|
+
match = type.re_file.match(file_name)
|
34
|
+
return type if match
|
35
|
+
end
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def process_doc_types(doc_types)
|
42
|
+
doc_types.map do |x|
|
43
|
+
object = OpenStruct.new(x)
|
44
|
+
object.re_file = Regexp.new(object.re_file)
|
45
|
+
object.re_date = Regexp.new(object.re_date)
|
46
|
+
object.pwd = object.pwd ? Base64.decode64(object.pwd) : nil
|
47
|
+
object
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/pdfh/utils.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'colorize'
|
4
|
+
|
5
|
+
module Pdfh
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
##
|
9
|
+
# Keeps Verbose option in whole project
|
10
|
+
class Verbose
|
11
|
+
@active = false
|
12
|
+
class << self
|
13
|
+
attr_writer :active
|
14
|
+
|
15
|
+
def active?
|
16
|
+
@active
|
17
|
+
end
|
18
|
+
|
19
|
+
def print(msg = '')
|
20
|
+
puts msg.colorize(:cyan) if active?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Keeps Dry run option in whole project
|
27
|
+
class Dry
|
28
|
+
@active = false
|
29
|
+
class << self
|
30
|
+
attr_writer :active
|
31
|
+
|
32
|
+
def active?
|
33
|
+
@active
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/pdfh/version.rb
ADDED
data/pdfh.gemspec
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'pdfh/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'pdfh'
|
9
|
+
spec.version = Pdfh::VERSION
|
10
|
+
spec.authors = ['Isaias Piña']
|
11
|
+
spec.email = ['iax7@users.noreply.github.com']
|
12
|
+
|
13
|
+
spec.summary = 'Organize PDF files'
|
14
|
+
spec.description = 'Examine all PDF files in scrape directories, remove password (if has one), rename and copy to a new directory using regular expresions.'
|
15
|
+
spec.homepage = 'https://github.com/iax7/pdfh'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
spec.required_ruby_version = '>= 2.6.0'
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
23
|
+
|
24
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
25
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
26
|
+
spec.metadata['changelog_uri'] = 'https://raw.githubusercontent.com/iax7/pdfh/master/CHANGELOG.md'
|
27
|
+
else
|
28
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
29
|
+
'public gem pushes.'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Specify which files should be added to the gem when it is released.
|
33
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
34
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
35
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
36
|
+
end
|
37
|
+
spec.bindir = 'exe'
|
38
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
39
|
+
spec.require_paths = ['lib']
|
40
|
+
|
41
|
+
spec.add_dependency 'colorize', '~> 0.8.1'
|
42
|
+
|
43
|
+
spec.add_development_dependency 'bundler', '~> 1.17.2'
|
44
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
45
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
46
|
+
spec.add_development_dependency 'simplecov', '~> 0.16.1'
|
47
|
+
spec.add_development_dependency 'simplecov-console', '~> 0.4.2'
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdfh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Isaias Piña
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.17.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.17.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.16.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.16.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov-console
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.4.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.4.2
|
97
|
+
description: Examine all PDF files in scrape directories, remove password (if has
|
98
|
+
one), rename and copy to a new directory using regular expresions.
|
99
|
+
email:
|
100
|
+
- iax7@users.noreply.github.com
|
101
|
+
executables:
|
102
|
+
- pdfh
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".rubocop.yml"
|
109
|
+
- ".ruby-gemset"
|
110
|
+
- ".ruby-version"
|
111
|
+
- ".travis.yml"
|
112
|
+
- CHANGELOG.md
|
113
|
+
- CODE_OF_CONDUCT.md
|
114
|
+
- Gemfile
|
115
|
+
- Gemfile.lock
|
116
|
+
- LICENSE
|
117
|
+
- README.md
|
118
|
+
- Rakefile
|
119
|
+
- bin/console
|
120
|
+
- bin/setup
|
121
|
+
- exe/pdfh
|
122
|
+
- lib/pdfh.rb
|
123
|
+
- lib/pdfh/document.rb
|
124
|
+
- lib/pdfh/settings.rb
|
125
|
+
- lib/pdfh/utils.rb
|
126
|
+
- lib/pdfh/version.rb
|
127
|
+
- pdfh.gemspec
|
128
|
+
homepage: https://github.com/iax7/pdfh
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata:
|
132
|
+
allowed_push_host: https://rubygems.org
|
133
|
+
homepage_uri: https://github.com/iax7/pdfh
|
134
|
+
source_code_uri: https://github.com/iax7/pdfh
|
135
|
+
changelog_uri: https://raw.githubusercontent.com/iax7/pdfh/master/CHANGELOG.md
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: 2.6.0
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubygems_version: 3.0.1
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Organize PDF files
|
155
|
+
test_files: []
|