pdfmd 1.4.3 → 1.5.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
  SHA1:
3
- metadata.gz: 553f3eff11cc2292beaccc52470f9cc22f7a2a6c
4
- data.tar.gz: fc0a114519fc15e859c3f5eaa1321fa198f0afdf
3
+ metadata.gz: 71b69c4e8462e3e8e9b8fe27df5fc9b6a472292b
4
+ data.tar.gz: 55cb0726b99937d3cbaad55bb803d8c652315f4c
5
5
  SHA512:
6
- metadata.gz: 76d44d0d86b428268f2fb1c9f46d0423715fa31b8caa47aa1db2a26665f8b641aae468c06251efe05cdb5499643e2cef210b89157c95fac3d9ea8b5ab44eb12d
7
- data.tar.gz: 344df1960429d50f417495fe51a7831056d2bdc11307e045bc988889ebe72d0cb200fae0ed0a7f86cad987f477d99ac4d908ac876e3fbaf33334877405c53e93
6
+ metadata.gz: 01a4b983e7eaaade73851e51d8193ad5e3c6911db00f87852b816c0848b2d0042c3acc651419e1d77226129582691d544fef85cce4395eb665fa51274eee7d64
7
+ data.tar.gz: 87b8640a0b5c3a1286a5fcd214afdc01c2c1f72a07a996da93a50603caa13a20699e6d907dcd18c03344266d686e7dc934114b93289e2e200da006b8506d5e4a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # Version 1.5.0
2
+ - Added option 'dryrun' to command 'sort'.
3
+ - Added option 'logfilepath' to command 'sort'
4
+ - Added more Hiera support to command 'sort'.
5
+ - Added logfilepath to command 'sort'.
6
+ - Added logentry for the answer of the interaction with the command 'sort' (parameter -i).
7
+ - Bugfix: logic for parameters was not working correctly.
8
+ - Change: Default value for logging is not 'false'.
9
+ - Bugfix: Entry in Logfile shows now if the file is moved or copied.
10
+ - Bugfix: Error message when logging and the logfile did not exist yet. File is now created correctly when necessary.
11
+ - Added Hiera support to command 'rename'.
12
+ - Command 'rename': Changed hiera parameter 'destination' to 'outputdir'.
13
+ - Added Tests for 'sort','rename' and 'show'
14
+
1
15
  # Version 1.4.3
2
16
  - Bugfix: Commata in author field showed up in the filename after renaming.
3
17
 
data/README.md CHANGED
@@ -60,24 +60,27 @@ So in order to get more information just run the required _help_ command:
60
60
 
61
61
  ```
62
62
  # Show general possibilities:
63
- $ pdfmd.rb
63
+ $ pdfmd
64
64
 
65
65
  # Show more information about <action>
66
- $ pdfmd.rb help <action>
66
+ $ pdfmd help <action>
67
67
  ```
68
68
 
69
69
  My usual workflow is like this:
70
70
 
71
71
  ```
72
- $ pdfmd.rb show test.pdf
73
- $ pdfmd.rb edit -t all test.pdf
72
+ $ pdfmd show test.pdf
73
+ $ pdfmd edit -t all test.pdf
74
74
  ...
75
- $ pdfmd.rb rename test.pdf
75
+ $ pdfmd rename test.pdf
76
76
  $ mv 20150101-me-dok-testdocument.pdf /my/pdf/directory
77
+ ...
78
+ $ pdfmd sort .
77
79
  ```
78
80
 
79
81
  There's an underlogic in the renaming and sorting of the files according to the metadata. Make sure you read at least the help-information before you use it.
80
82
 
83
+ It's also usefull to define some default settings in Hiera to avoid unnecessary typing.
81
84
 
82
85
  __HINT__: Before you start using the script, make sure you have a backup of your files or you know what you're doing. If you loose information/files I will not be able to help you.
83
86
 
@@ -90,8 +93,15 @@ pdfmd::config:
90
93
  sort:
91
94
  destination : /data/tmp
92
95
  copy : true
93
- logfile : /var/log/pdfmd.log
96
+ log : true
97
+ logfilepath : /var/log/pdfmd.log # Needs create/write rights on this file
94
98
  interactive : false
99
+ rename:
100
+ #allkeywords : true # Does not make sense in combination with _keywords_
101
+ keywords : 2
102
+ outputdir : /data/output/sorted
103
+ copy : true
104
+
95
105
  ```
96
106
 
97
107
  Information about which hiera configuration settings are available can be either found in `pdfmd help <command>` or `pdfmd explain hiera`.
data/Rakefile ADDED
@@ -0,0 +1,141 @@
1
+ require 'rake'
2
+ require 'tmpdir'
3
+ require 'fileutils'
4
+
5
+ TMPDIR = Dir.mktmpdir
6
+ EXAMPLEPDF = './test/test_default.pdf'
7
+ TARGETPDF = TMPDIR + '/' + File.basename(EXAMPLEPDF)
8
+ PDFMD = './lib/pdfmd.rb'
9
+ $testResults = Hash.new
10
+
11
+ desc 'test pdfmd'
12
+ task :test do
13
+ sections = ARGV.last
14
+ #puts ARGV.last
15
+ #puts sections
16
+ task sections.to_sym do ; end
17
+
18
+ case sections
19
+ when 'rename'
20
+ rename
21
+ when 'show'
22
+ show
23
+ when 'sort'
24
+ sort
25
+ else
26
+ show
27
+ rename
28
+ sort
29
+ end
30
+
31
+ end
32
+
33
+ #
34
+ # init Tmpdir
35
+ # Reset the temporary directory into a known state
36
+ # 1. Delete the tmp dir
37
+ # 2. Create the tmp dir
38
+ # 3. copy the default file back into it
39
+ def initTmpDir
40
+ FileUtils.rm_rf TMPDIR
41
+ FileUtils.mkdir TMPDIR
42
+ FileUtils.cp(EXAMPLEPDF, TMPDIR + '/')
43
+ end
44
+
45
+ #
46
+ # Testing command 'sort'
47
+ #
48
+ def sort
49
+
50
+ puts "Testing command 'sort'"
51
+ require_relative './test/test_sort.rb'
52
+
53
+ # Cleanup after Tests
54
+ FileUtils.rm_rf TMPDIR
55
+ showTestResults
56
+
57
+ end
58
+
59
+
60
+ #
61
+ # Testing command 'show'
62
+ #
63
+ def show
64
+
65
+ puts "Testing command 'show'"
66
+ require_relative './test/test_show.rb'
67
+ # Cleanup after Tests
68
+ FileUtils.rm_rf TMPDIR
69
+ showTestResults
70
+
71
+ end
72
+
73
+ # Testing command 'rename'
74
+ #
75
+ def rename
76
+
77
+ puts "Testing command 'rename'"
78
+ require_relative './test/test_rename.rb'
79
+
80
+ # Cleanup after Tests
81
+ FileUtils.rm_rf TMPDIR
82
+ showTestResults
83
+
84
+ end # End of Task test rename
85
+
86
+ ################################################################################
87
+ # Helper methods
88
+ ################################################################################
89
+
90
+ #
91
+ # Show the test results
92
+ def showTestResults
93
+
94
+ $testResults.sort.each do |key,value|
95
+ if value[:result] == 'OK'
96
+ puts 'Test ' + key + ' : ' + value[:result].to_s
97
+ else
98
+ puts 'Test ' + key + ' : ' + value[:result].to_s
99
+ puts ' Command: ' + value[:command].to_s
100
+ exit 1
101
+ end
102
+ end
103
+ end
104
+
105
+ #
106
+ # Read the PDF files in the TMPDIR
107
+ def readFilesInDir(targetdir)
108
+ filedata = Hash.new
109
+ files= Dir.glob(targetdir + "/*.pdf")
110
+ files.sort.each do |filename|
111
+ filedata[filename] = readExifData(filename)
112
+ end
113
+ return filedata
114
+ end
115
+
116
+ #
117
+ # Read the Exifdata of a given file
118
+ def readExifData(filepath)
119
+
120
+ exifdata = Hash.new
121
+
122
+ exifdatatext = `exiftool #{filepath}`
123
+ exifdatatext.each_line do |line|
124
+ case line
125
+ when /^author.*/i
126
+ exifdata['author'] = line.split(' : ').last.chomp
127
+ when /^title.*/i
128
+ exifdata['title'] = line.split(' : ').last.chomp
129
+ when /^subject.*/i
130
+ exifdata['subject'] = line.split(' : ').last.chomp
131
+ when /^keywords.*/i
132
+ exifdata['keywords'] = line.split(' : ').last.chomp
133
+ when /^Create\ Date.*/i
134
+ exifdata['createdate'] = line.split(' : ').last.chomp
135
+ end
136
+ end
137
+ return exifdata
138
+ end
139
+
140
+
141
+
@@ -12,7 +12,14 @@ Configure default settings in hiera:
12
12
  ---
13
13
  pdfmd::config:
14
14
  sort:
15
- destination : /tmp/output
15
+ destination : /data/output
16
+ copy : true
17
+ log : true
18
+ logfilepath : /var/log/pdfmd.log
19
+ interactive : true
20
+ rename:
21
+ allkeywords : true
22
+ keywords : 4
23
+ outputdir : /data/output/sorted
16
24
  copy : true
17
- logfile : /var/log/pdfmd.log
18
25
 
data/lib/pdfmd/rename.rb CHANGED
@@ -1,16 +1,18 @@
1
1
  #
2
2
  # Thor command 'rename'
3
3
  #
4
- # TODO: Define outputdir from Hiera
5
- # TODO: Add option for copy when renaming
6
4
  # TODO: Add option to create outputdir if not existing
7
5
  # TODO: Define option to create outputdir via Hiera
8
6
  #
9
- filename = ENV.fetch('PDFMD_FILENAME')
10
- allkeywords = ENV.fetch('PDFMD_ALLKEYWORDS')
11
- outputdir = ENV.fetch('PDFMD_OUTPUTDIR') == 'false' ? false : ENV.fetch('PDFMD_OUTPUTDIR')
12
- dryrun = ENV.fetch('PDFMD_DRYRUN') == 'false' ? false : true
13
- numberKeywords = ENV.fetch('PDFMD_NUMBERKEYWORDS').to_i
7
+ require_relative '../string_extend'
8
+
9
+ filename = ENV.fetch('PDFMD_FILENAME')
10
+ opt_allkeywords = ENV.fetch('PDFMD_ALLKEYWORDS')
11
+ outputdir = ENV.fetch('PDFMD_OUTPUTDIR') == 'false' ? false : ENV.fetch('PDFMD_OUTPUTDIR')
12
+ dryrun = ENV.fetch('PDFMD_DRYRUN') == 'false' ? false : true
13
+ opt_numberKeywords = ENV.fetch('PDFMD_NUMBERKEYWORDS')
14
+ opt_copy = ENV.fetch('PDFMD_COPY')
15
+ hieraDefaults = queryHiera('pdfmd::config')
14
16
 
15
17
  metadata = readMetadata(filename).each do |key,value|
16
18
 
@@ -23,6 +25,63 @@ metadata = readMetadata(filename).each do |key,value|
23
25
 
24
26
  end
25
27
 
28
+
29
+ # Determine the status of allkeywords
30
+ # Default value is false
31
+ if opt_allkeywords == 'true'
32
+ opt_allkeywords = true
33
+ elsif opt_allkeywords.blank? and
34
+ not hieraDefaults['rename'].nil? and
35
+ not hieraDefaults['rename']['allkeywords'].nil?
36
+
37
+ opt_allkeywords = hieraDefaults['rename']['allkeywords']
38
+
39
+ elsif opt_allkeywords == 'false' or
40
+ opt_allkeywords.blank?
41
+
42
+ opt_allkeywords = false
43
+
44
+ end
45
+
46
+ #
47
+ # Determine the number of keywords
48
+ # Default value is 3
49
+ if opt_numberKeywords.blank? and
50
+ not hieraDefaults['rename'].nil? and
51
+ not hieraDefaults['rename']['keywords'].nil?
52
+
53
+ opt_numberKeywords = hieraDefaults['rename']['keywords']
54
+
55
+ elsif opt_numberKeywords.to_i.is_a? Integer and
56
+ opt_numberKeywords.to_i > 0
57
+
58
+ opt_numberKeywords = opt_numberKeywords.to_i
59
+
60
+ else
61
+
62
+ opt_numberKeywords = 3
63
+
64
+ end
65
+
66
+ #
67
+ # Determine the status of the copy parameter
68
+ if opt_copy.blank? and
69
+ not hieraDefaults['rename'].nil? and
70
+ not hieraDefaults['rename']['copy'].nil?
71
+
72
+ opt_copy = hieraDefaults['rename']['copy']
73
+
74
+ elsif opt_copy == 'true'
75
+
76
+ opt_copy = true
77
+
78
+ else
79
+
80
+ opt_copy = false
81
+
82
+ end
83
+
84
+
26
85
  date = metadata['createdate'].gsub(/\ \d{2}\:\d{2}\:\d{2}.*$/,'').gsub(/\:/,'')
27
86
  author = metadata['author'].gsub(/\./,'_').gsub(/\-/,'').gsub(/\s/,'_').gsub(/\,/,'_').gsub(/\_\_/,'_')
28
87
  I18n.enforce_available_locales = false
@@ -98,8 +157,8 @@ if not metadata['keywords'].empty?
98
157
 
99
158
  # Exit condition limits the number of keywords used in the filename
100
159
  # unless all keywords shall be added
101
- if not allkeywords.empty?
102
- counter > numberKeywords-1 ? break : counter = counter + 1
160
+ if not opt_allkeywords
161
+ counter >= opt_numberKeywords-1 ? break : counter = counter + 1
103
162
  end
104
163
  if value.match(/(kvi|fak|ord|kdn)/i)
105
164
  keywords == '' ? keywords = '-' + value : keywords = value + '-' + keywords
@@ -107,6 +166,7 @@ if not metadata['keywords'].empty?
107
166
  keywords == '' ? keywords = '-' + value : keywords.concat('-' + value)
108
167
  end
109
168
  end
169
+
110
170
  # Normalise the keywords as well
111
171
  #
112
172
  I18n.enforce_available_locales = false
@@ -136,17 +196,25 @@ newFilename = date + '-' +
136
196
 
137
197
  # Output directory checks
138
198
  if outputdir
199
+
139
200
  if not File.exist?(outputdir)
140
201
  puts "Error: output dir '#{outputdir}' not found. Abort."
141
202
  exit 1
142
203
  end
204
+
143
205
  else
144
- # Output to Inputdir
145
- outputdir = File.dirname(filename)
206
+
207
+ # Try to get the outputdir from hiera
208
+ outputdir = (not hieraDefaults['rename'].nil? and not hieraDefaults['rename']['outputdir'].nil?) ? hieraDefaults['rename']['outputdir'] : File.dirname(filename)
209
+
146
210
  end
147
211
 
148
212
  if not dryrun and filename != newFilename.downcase
149
- `mv -v '#{filename}' '#{outputdir}/#{newFilename.downcase}'`
213
+
214
+ # Copy of me the file to the new name
215
+ command = opt_copy ? 'cp' : 'mv'
216
+ `#{command} -v '#{filename}' '#{outputdir}/#{newFilename.downcase}'`
217
+
150
218
  else
151
- puts filename + "\n => " + newFilename.downcase
219
+ puts filename + "\n => " + outputdir + '/' + newFilename.downcase
152
220
  end
data/lib/pdfmd/sort.rb CHANGED
@@ -1,25 +1,39 @@
1
+ #
2
+ # == File: sort.rb
3
+ #
4
+ # Actions for the sort command
5
+ #
1
6
  inputDir = ENV.fetch('PDFMD_INPUTDIR')
2
7
 
3
8
  require_relative('./methods.rb')
9
+ require_relative '../string_extend.rb'
4
10
  require 'fileutils'
5
11
 
6
12
  opt_destination = ENV.fetch('PDFMD_DESTINATION')
13
+ opt_dryrun = ENV.fetch('PDFMD_DRYRUN') == 'true' ? true : false
7
14
  opt_copy = ENV.fetch('PDFMD_COPY')
8
15
  opt_log = ENV.fetch('PDFMD_LOG')
16
+ opt_logfilepath = ENV.fetch('PDFMD_LOGFILEPATH')
9
17
  opt_interactive = ENV.fetch('PDFMD_INTERACTIVE')
18
+ hieraDefaults = queryHiera('pdfmd::config')
10
19
 
11
- hieraDefaults = queryHiera('pdfmd::config')
12
-
13
- copyAction = opt_copy.empty? ? false : true
14
- if opt_copy.nil? and hieraDefaults['sort']['copy'] == true
20
+ # Determin the setting for the copy/move action when sorting
21
+ # Use HieraDefaults if nothing has been set.
22
+ copyAction = opt_copy
23
+ if copyAction.blank? and hieraDefaults['sort']['copy'] == true
15
24
  copyAction = true
16
- puts 'Setting action to copy based on Hiera.'
25
+ elsif copyAction.blank? or copyAction == 'false'
26
+ copyAction = false
17
27
  end
18
28
 
19
- interactiveAction = opt_interactive.empty? ? false : true
20
- if opt_interactive.empty? and hieraDefaults['sort']['interactive'] == true
29
+ # Determine the setting for interaction
30
+ if opt_interactive.blank? and hieraDefaults['sort']['interactive'] == true
31
+ puts 'Setting interactive from hiera'
32
+ interactiveAction = true
33
+ elsif opt_interactive == 'true'
21
34
  interactiveAction = true
22
- puts 'Setting interactive to true based on Hiera.'
35
+ elsif opt_interactive.blank? or opt_interactive == 'false'
36
+ interactiveAction = false
23
37
  end
24
38
 
25
39
  # Fetch alternate destination from hiera if available
@@ -35,26 +49,55 @@ if destination.nil? or destination == ''
35
49
  puts 'Abort.'
36
50
  exit 1
37
51
  end
52
+ end
38
53
 
54
+ # Determine the state of the logging
55
+ if (opt_log.blank? and hieraDefaults['sort']['log'] == true) or
56
+ opt_log == 'true'
57
+ logenable = true
58
+ elsif opt_log.blank? or opt_log == 'false'
59
+ logenable = false
39
60
  end
40
61
 
41
- logenable = opt_log
42
- logfile = !hieraDefaults['sort']['logfile'].nil? ? hieraDefaults['sort']['logfile'] : Dir.pwd.chomp('/') + '/' + Pathname.new(__FILE__).basename + '.log'
62
+ if logenable
63
+
64
+ if opt_logfilepath.blank? and
65
+ ( hieraDefaults['sort']['logfilepath'].nil? or
66
+ hieraDefaults['sort']['logfilepath'].blank? or
67
+ hieraDefaults['sort'].nil? )
68
+
69
+ logfile = Dir.pwd.chomp('/') + '/' + File.basename(ENV['PDFMD'], '.*') + '.log'
70
+
71
+ elsif not opt_logfilepath.blank?
72
+
73
+ if File.directory? opt_logfilepath
74
+ abort('Logfilepath is a directory. Abort.')
75
+ exit 1
76
+ end
77
+
78
+ logfile = opt_logfilepath
43
79
 
44
- # Check that logfilepath exists and is writeable
45
- if !File.writable?(logfile)
46
- puts "Cannot write '#{logfile}. Abort."
47
- exit 1
80
+ elsif opt_logfilepath.blank? and
81
+ not hieraDefaults['sort']['logfilepath'].blank?
82
+
83
+ logfile = hieraDefaults['sort']['logfilepath']
84
+
85
+ else
86
+
87
+ logfile = Dir.pwd.chomp('/') + '/' + File.basename(ENV['PDFMD'], '.*') + '.log'
88
+
89
+ end
90
+
91
+ $logger = Logger.new(logfile)
48
92
  end
49
- logenable ? $logger = Logger.new(logfile) : ''
50
93
 
51
94
  # Input validation
52
95
  !File.exist?(inputDir) ? abort('Input directory does not exist. Abort.'): ''
53
- File.directory?(inputDir) ? '' : abort('Input is a single file')
96
+ File.directory?(inputDir) ? '' : abort('Input is a single file. Not implemented yet. Abort.')
54
97
  File.file?(destination) ? abort("Output '#{destination}' is an existing file. Cannot create directory with the same name. Abort") : ''
55
98
  unless File.directory?(destination)
56
99
  FileUtils.mkdir_p(destination)
57
- $logger.info("Destination '#{destination}' has been created.")
100
+ logenable ? $logger.info("Destination '#{destination}' has been created.") : ''
58
101
  end
59
102
 
60
103
  # Iterate through all files
@@ -63,6 +106,7 @@ Dir[inputDir.chomp('/') + '/*.pdf'].sort.each do |file|
63
106
  if interactiveAction
64
107
  answer = readUserInput("Process '#{file}' ([y]/n): ")
65
108
  answer = answer.empty? ? 'y' : answer
109
+ logenable ? $logger.info("Interactive answer for file '#{file}' : #{answer}") : ''
66
110
  answer.match(/y/) ? '' : next
67
111
  end
68
112
 
@@ -80,15 +124,17 @@ Dir[inputDir.chomp('/') + '/*.pdf'].sort.each do |file|
80
124
 
81
125
  filedestination = destination.chomp('/') + '/' + author + '/' + Pathname.new(file).basename.to_s
82
126
 
127
+
83
128
  # Final check before touching the filesystem
84
129
  if not File.exist?(filedestination)
85
- $logger.info("File '#{file}' => '#{filedestination}'")
86
130
 
87
131
  # Move/Copy the file
88
- if copyAction
89
- FileUtils.cp(file, filedestination)
132
+ if copyAction
133
+ opt_dryrun ? '' : FileUtils.cp(file, filedestination)
134
+ logenable ? $logger.info("File copied '#{file}' => '#{filedestination}'") : ''
90
135
  else
91
- FileUtils.mv(file,filedestination)
136
+ opt_dryrun ? '' : FileUtils.mv(file,filedestination)
137
+ logenable ? $logger.info("File moved '#{file}' => '#{filedestination}'") : ''
92
138
  end
93
139
 
94
140
  else
@@ -96,5 +142,6 @@ Dir[inputDir.chomp('/') + '/*.pdf'].sort.each do |file|
96
142
  end
97
143
  else
98
144
  logenable ? $logger.warn("Missing tag 'Author' for file '#{file}'. Skipping.") : (puts "Missing tag 'Author' for file '#{file}'. Skipping")
145
+ next
99
146
  end
100
147
  end
data/lib/pdfmd.rb CHANGED
@@ -41,9 +41,9 @@
41
41
  # TODO: Include password protected PDF documents as well
42
42
  # TODO: Fix broken PDF files automatically
43
43
  # TODO: Enable logging in more functions than only "sort"
44
+ # TODO: command 'hiera' to show the current settings
44
45
  # TODO: Read this: http://lostechies.com/derickbailey/2011/04/29/writing-a-thor-application/
45
46
  # TODO: ... and this: http://blog.paracode.com/2012/05/17/building-your-tools-with-thor/
46
- # TODO: Create Gem: http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/
47
47
  # gs \
48
48
  # -o repaired.pdf \
49
49
  # -sDEVICE=pdfwrite \
@@ -62,7 +62,7 @@ require "i18n"
62
62
  require 'pathname'
63
63
  require 'logger'
64
64
 
65
- VERSION = '1.4.2'
65
+ VERSION = '1.5.0'
66
66
 
67
67
  # Include general usage methods
68
68
  require_relative('pdfmd/methods.rb')
@@ -316,12 +316,18 @@ class DOC < Thor
316
316
 
317
317
  [*log|l*]
318
318
  \x5 Disable/Enable the logging.
319
- \x5 Default: enabled.
319
+
320
+ Default: enabled.
321
+
322
+ [*logfilepath|p*]
323
+ \x5 Set an alternate path for the logfile. If not path is chosen, the logfile is being created in the current working directory as `pdfmd.log`.
320
324
 
321
325
  [*interactive|i*]
322
- \x5 Disable/Enable interactive sorting. This will ask for confirmation for
323
- \x5 each sorting action.
324
- \x5 Default: disabled.
326
+ \x5 Disable/Enable interactive sorting. This will ask for confirmation for each sorting action.
327
+
328
+ Default: disabled.
329
+
330
+
325
331
 
326
332
  === Replacement rules
327
333
 
@@ -343,27 +349,28 @@ class DOC < Thor
343
349
  \x5sort:
344
350
  \x5 key: value
345
351
 
352
+ See the README file for an example how to define the values in Hiera.
353
+
346
354
  === Hiera defaults
347
355
 
348
- The following values can be influenced by the hiera configuration in the
349
- section 'sort'. Commandline parameter will overwrite the defaults coming
350
- from hiera unless otherwise notet.
356
+ The following values can be influenced by the hiera configuration in the section 'sort'. Commandline parameter will overwrite the defaults coming from hiera unless otherwise notet.
351
357
 
352
358
  [*copy*]
353
359
  \x5 If set to true copies the files from the source directory instead of moving them.
354
360
 
355
361
  [*destination*]
356
- \x5 Specifies the default output directory (root-directory). Either this or the
357
- command line parameter for destinations must be set.
362
+ \x5 Specifies the default output directory (root-directory). Either this or the command line parameter for destinations must be set.
363
+
364
+ [*log*]
365
+ \x5 Enables (true) or disables (false) logging.
358
366
 
359
- [*logfile*]
360
- \x5 Specifies the default path for the logfile output. If this is not
361
- specfied a logfile with the scriptname + '.log' will be created in the
362
- current working directory.
367
+ [*logfilepath*]
368
+ \x5 Specifes the default path for the logfile. If no path is set and logging is enable, the logfile will be created in the current working directory.
369
+
370
+ Default is the current working directory with the filename `pdfmd.log`
363
371
 
364
372
  [*interactive*]
365
- \x5 If set to true, each file must be acknowledged to be processed when
366
- running the script.
373
+ \x5 If set to true, each file must be acknowledged to be processed when running the script.
367
374
 
368
375
  === Example
369
376
 
@@ -377,15 +384,20 @@ class DOC < Thor
377
384
  LONGDESC
378
385
  method_option :destination, :aliases => '-d', :required => false, :type => :string, :desc => 'Defines the output directory'
379
386
  method_option :copy, :aliases => '-c', :required => false, :type => :boolean, :desc => 'Copy files instead of moving them'
380
- method_option :log, :aliases => '-l', :required => false, :type => :boolean, :desc => 'Enable/Disable creation of log files', :default => true
381
- method_option :interactive, :aliases => '-i', :required => false, :type => :boolean, :desc => 'Enable/Disable interactive sort'
387
+ method_option :log, :aliases => '-l', :required => false, :type => :boolean, :desc => 'Enable/Disable creation of log files'
388
+ method_option :logfilepath, :aliases => '-p', :required => false, :type => :string, :desc => 'Change the default logfilepath'
389
+ method_option :interactive, :aliases => '-i', :required => false, :type => :boolean, :desc => 'Enable/Disable interactive sorting'
390
+ method_option :dryrun, :aliases => '-n', :required => false, :type => :boolean, :desc => 'Run without changing something'
382
391
  def sort(inputDir)
383
392
 
384
393
  ENV['PDFMD_INPUTDIR'] = inputDir
385
394
  ENV['PDFMD_DESTINATION'] = options[:destination].to_s
386
395
  ENV['PDFMD_COPY'] = options[:copy].to_s
387
396
  ENV['PDFMD_LOG'] = options[:log].to_s
397
+ ENV['PDFMD_LOGFILEPATH'] = options[:logfilepath].to_s
388
398
  ENV['PDFMD_INTERACTIVE'] = options[:interactive].to_s
399
+ ENV['PDFMD_DRYRUN'] = options['dryrun'].to_s
400
+ ENV['PDFMD'] = __FILE__
389
401
  require_relative('./pdfmd/sort.rb')
390
402
 
391
403
  end
@@ -408,13 +420,31 @@ class DOC < Thor
408
420
  --all-keywords, -a
409
421
  \x5 Use all keywords from the meta information in the file name and ignore the limit.
410
422
 
423
+ Hiera parameter: allkeywords [true|false]
424
+
425
+ Default: false
426
+
411
427
  --keywwords, -k
412
428
  \x5 Set the number of keywords used in the filename to a new value.
413
- \x5 Default: 3
429
+
430
+ Hiera parameter: keywords <integer>
431
+
432
+ Default: 3
414
433
 
415
434
  --outputdir, -o
416
435
  \x5 Rename the file and move it to the directory defined in '--outputdir'.
417
436
 
437
+ Hiera parameter: outputdir </file/path/>
438
+
439
+ Default: current file directory
440
+
441
+ --copy, -c
442
+ \x5 Copy the file instead of moving it to the new name or destination.
443
+
444
+ Hiera parameter: copy [true|false]
445
+
446
+ Default: false
447
+
418
448
  The directory must exist at runtime.
419
449
 
420
450
  == Example
@@ -502,9 +532,10 @@ class DOC < Thor
502
532
 
503
533
  LONGDESC
504
534
  method_option :dryrun, :type => :boolean, :aliases => '-n', :desc => 'Run without making changes', :default => false, :required => false
505
- method_option :allkeywords, :type => :boolean, :aliases => '-a', :desc => 'Add all keywords (no limit)', :default => false, :required => false
506
- method_option :keywords, :type => :numeric, :aliases => '-k', :desc => 'Number of keywords to include (Default: 3)', :default => 3, :required => false
507
- method_option :outputdir, :aliases => '-o', :type => :string, :desc => 'Speficy output directory', :default => :false, :required => :false
535
+ method_option :allkeywords, :type => :boolean, :aliases => '-a', :desc => 'Add all keywords (no limit)', :required => false
536
+ method_option :keywords, :type => :numeric, :aliases => '-k', :desc => 'Number of keywords to include (Default: 3)', :required => false
537
+ method_option :outputdir, :aliases => '-o', :type => :string, :desc => 'Speficy output directory', :default => false, :required => :false
538
+ method_option :copy, :aliases => '-c', :type => :boolean, :desc => 'Copy instead of moving the file when renaming'
508
539
  def rename(filename)
509
540
 
510
541
  ENV['PDFMD_FILENAME'] = filename
@@ -512,6 +543,7 @@ class DOC < Thor
512
543
  ENV['PDFMD_ALLKEYWORDS'] = options[:allkeywords].to_s
513
544
  ENV['PDFMD_OUTPUTDIR'] = options[:outputdir].to_s
514
545
  ENV['PDFMD_NUMBERKEYWORDS'] = options[:keywords].to_s
546
+ ENV['PDFMD_COPY'] = options[:copy].to_s
515
547
  require_relative('./pdfmd/rename.rb')
516
548
 
517
549
  end
@@ -0,0 +1,25 @@
1
+ #
2
+ # Helper file with class extensions for string
3
+ #
4
+ class String
5
+
6
+ #
7
+ # Boolean function
8
+ #
9
+ # 'true|t|yes|y|1' == true
10
+ # 'false|f|no|n|0' == false
11
+ # emtpy string is error
12
+ #
13
+ def to_bool
14
+ return true if self == true || self =~ (/\A(true|t|yes|y|1)\Z/i)
15
+ return false if self == false || self =~ (/\A(false|f|no|n|0)\Z/i)
16
+ raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
17
+ end
18
+
19
+ #
20
+ # method to check if a string is empty
21
+ #
22
+ def blank?
23
+ self.strip.empty?
24
+ end
25
+ end
data/pdfmd.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'pdfmd'
3
- s.version = '1.4.3'
3
+ s.version = `grep -m 1 VERSION lib/pdfmd.rb | awk -F"'" '{ print $2 '}`
4
4
  s.date = Time.now.strftime("%Y-%m-%d").to_s
5
5
  s.summary = "pdfmd - pdf-meta-data management"
6
6
  s.description = <<-EOF
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.executable = 'pdfmd'
18
18
  s.homepage = 'https://github.com/Micronarrativ/ruby-pmd'
19
19
  s.license = 'MIT'
20
- s.add_dependency "thor", '0.19.1'
21
- s.add_dependency 'highline', '1.7.1'
22
- s.add_dependency 'fileutils', '0.7'
23
- s.add_dependency 'i18n', '0.6.11'
20
+ s.add_dependency "thor", '~>0.19', '>= 0.19.1'
21
+ s.add_dependency 'highline', '~>1.7', '>= 1.7.1'
22
+ s.add_dependency 'fileutils', '~>0.7','>= 0.7'
23
+ s.add_dependency 'i18n', '~>0.6', '>= 0.6.11'
24
24
  end
Binary file
@@ -0,0 +1,72 @@
1
+ # Standard renaming
2
+ # Test 001
3
+ initTmpDir
4
+ commandparameter = " rename -o #{TMPDIR}"
5
+ `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
6
+ files = readFilesInDir(TMPDIR)
7
+ if files.size == 1 and
8
+ File.basename(files.keys[0]) == '19700101-example_author-dok-some_keywords-kdn1111111-test_subject.pdf'
9
+ result = 'OK'
10
+ else
11
+ result = 'failed'
12
+ end
13
+ $testResults = { '001' => {:result => result, :command => commandparameter }}
14
+
15
+
16
+ # Test 002
17
+ # renaming with copy
18
+ initTmpDir
19
+ commandparameter = " rename -c -o #{TMPDIR}"
20
+ `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
21
+ files = readFilesInDir(TMPDIR)
22
+ if files.size == 2 and
23
+ File.basename(files.keys[0]) == '19700101-example_author-dok-some_keywords-kdn1111111-test_subject.pdf' and
24
+ File.basename(files.keys[1]) == 'test_default.pdf'
25
+ result = 'OK'
26
+ else
27
+ result = 'failed'
28
+ end
29
+ $testResults.store('002', {:result => result, :command => commandparameter })
30
+
31
+ # Test 003
32
+ # Testing Dryrun
33
+ initTmpDir
34
+ commandparameter = ' rename -n '
35
+ `#{PDFMD} #{commandparameter} #{TARGETPDF} >>/dev/null`.chomp
36
+ files = readFilesInDir(TMPDIR)
37
+ if files.size == 1 and
38
+ File.basename(files.keys[0]) == 'test_default.pdf'
39
+ result = 'OK'
40
+ else
41
+ result = 'failed'
42
+ end
43
+ $testResults.store('003', {:result => result, :command => commandparameter })
44
+
45
+ # Test 004
46
+ # Testing all keywords (-a)
47
+ initTmpDir
48
+ commandparameter = " rename -a -o #{TMPDIR}"
49
+ `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
50
+ files = readFilesInDir(TMPDIR)
51
+ if files.size == 1 and
52
+ File.basename(files.keys[0]) == '19700101-example_author-dok-some_keywords-kdn1111111-test_subject-author-some_feature-customernumber_1111111.pdf'
53
+ result = 'OK'
54
+ else
55
+ result = 'failed'
56
+ end
57
+ $testResults.store('004', {:result => result, :command => commandparameter })
58
+
59
+ # Test 005
60
+ # Testing number of keywords
61
+ # this might be buggy
62
+ initTmpDir
63
+ commandparameter = " rename -k 1 -o #{TMPDIR}"
64
+ `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
65
+ files = readFilesInDir(TMPDIR)
66
+ if files.size == 1 and
67
+ File.basename(files.keys[0]) == '19700101-example_author-dok-test_subject.pdf'
68
+ result = 'OK'
69
+ else
70
+ result = 'failed'
71
+ end
72
+ $testResults.store('005', {:result => result, :command => commandparameter })
data/test/test_show.rb ADDED
@@ -0,0 +1,45 @@
1
+ # Standard Show
2
+ # Test 001
3
+ initTmpDir
4
+ commandparameter = ' show '
5
+ showContent = `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
6
+ expectedContent = 'Author : Example Author
7
+ Creator : Writer
8
+ CreateDate : 1970:01:01 00:00:00
9
+ Subject : Test Subject
10
+ Title : Test Dokument
11
+ Keywords : Some Keywords, Author, some feature, Customernumber 1111111, Kundenummer 1111111'
12
+ if showContent == expectedContent
13
+ result = 'OK'
14
+ else
15
+ result = 'failed'
16
+ end
17
+ $testResults = { '001' => {:result => result, :command => commandparameter }}
18
+
19
+ # Show single tags
20
+ # Test 002
21
+ initTmpDir
22
+ commandparameter = ' show -t author'
23
+ showContent = `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
24
+ expectedContent = 'Example Author'
25
+ if showContent == expectedContent
26
+ result = 'OK'
27
+ else
28
+ result = 'failed'
29
+ end
30
+ $testResults.store('002', {:result => result, :command => commandparameter })
31
+
32
+ # Test 003
33
+ # Show multiple tags
34
+ initTmpDir
35
+ commandparameter = ' show -t author,subject'
36
+ showContent = `#{PDFMD} #{commandparameter} #{TARGETPDF}`.chomp
37
+ expectedContent = 'Example Author
38
+ Test Subject'
39
+ if showContent == expectedContent
40
+ result = 'OK'
41
+ else
42
+ result = 'failed'
43
+ end
44
+ $testResults.store('003', {:result => result, :command => commandparameter })
45
+
data/test/test_sort.rb ADDED
@@ -0,0 +1,97 @@
1
+ # Testing the results of the command 'sort'
2
+ #
3
+ # Test 001
4
+ # Testing Abort when running on a single file
5
+ initTmpDir
6
+ commandparameter = " sort -d #{TMPDIR}/target "
7
+ `#{PDFMD} #{commandparameter} #{TARGETPDF} 2>/dev/null`
8
+ if $?.to_s.match(/.*exit 1/)
9
+ result = 'OK'
10
+ else
11
+ result = 'failed'
12
+ end
13
+ $testResults = { '001' => {:result => result, :command => commandparameter }}
14
+
15
+
16
+ # Test 002
17
+ # Testing Sorting on a dir
18
+ initTmpDir
19
+ commandparameter = " sort -d #{TMPDIR}/target "
20
+ `#{PDFMD} #{commandparameter} #{TMPDIR}`
21
+ files = readFilesInDir(TMPDIR + '/target/example_author')
22
+ if files.size == 1 and
23
+ File.basename(files.keys[0]) == 'test_default.pdf'
24
+ result = 'OK'
25
+ else
26
+ result = 'failed'
27
+ end
28
+ $testResults.store('002',{:result => result, :command => commandparameter })
29
+
30
+ # Test 003
31
+ # Testing sorting on a dir with copy instead of moving
32
+ initTmpDir
33
+ commandparameter = " sort -d #{TMPDIR}/target -c "
34
+ `#{PDFMD} #{commandparameter} #{TMPDIR}`
35
+ filesTarget = readFilesInDir(TMPDIR + '/target/example_author')
36
+ filesSource = readFilesInDir(TMPDIR)
37
+ if filesTarget.size == 1 and
38
+ filesSource.size == 1 and
39
+ File.basename(filesTarget.keys[0]) == 'test_default.pdf' and
40
+ File.basename(filesSource.keys[0]) == 'test_default.pdf'
41
+ result = 'OK'
42
+ else
43
+ result = 'failed'
44
+ end
45
+ $testResults.store('003',{:result => result, :command => commandparameter })
46
+
47
+ # Test 004
48
+ # Testing sorting on a dir with dryrun option
49
+ initTmpDir
50
+ commandparameter = " sort -d #{TMPDIR}/target -n "
51
+ `#{PDFMD} #{commandparameter} #{TMPDIR}`
52
+ filesTarget = readFilesInDir(TMPDIR + '/target/example_author')
53
+ filesSource = readFilesInDir(TMPDIR)
54
+ if filesTarget.size == 0 and
55
+ filesSource.size == 1 and
56
+ File.basename(filesSource.keys[0]) == 'test_default.pdf'
57
+ result = 'OK'
58
+ else
59
+ result = 'failed'
60
+ end
61
+ $testResults.store('004',{:result => result, :command => commandparameter })
62
+
63
+ # Test 005
64
+ # Testing sorting on a dir with log creation
65
+ # and default log location (changed in this case)
66
+ initTmpDir
67
+ commandparameter = " sort -d #{TMPDIR}/target -l -p #{TMPDIR}/pdfmd.log"
68
+ `#{PDFMD} #{commandparameter} #{TMPDIR}`
69
+ filesTarget = readFilesInDir(TMPDIR + '/target/example_author')
70
+ filesSource = readFilesInDir(TMPDIR)
71
+ if filesTarget.size == 1 and
72
+ filesSource.size == 0 and
73
+ File.basename(filesTarget.keys[0]) == 'test_default.pdf' and
74
+ File.exist?(TMPDIR + '/pdfmd.log')
75
+ result = 'OK'
76
+ else
77
+ result = 'failed'
78
+ end
79
+ $testResults.store('005',{:result => result, :command => commandparameter })
80
+
81
+ # Test 006
82
+ # Testing sorting on a dir without log creation
83
+ initTmpDir
84
+ commandparameter = " sort -d #{TMPDIR}/target -l false -p #{TMPDIR}/pdfmd.log"
85
+ `#{PDFMD} #{commandparameter} #{TMPDIR}`
86
+ filesTarget = readFilesInDir(TMPDIR + '/target/example_author')
87
+ filesSource = readFilesInDir(TMPDIR)
88
+ if filesTarget.size == 1 and
89
+ filesSource.size == 0 and
90
+ File.basename(filesTarget.keys[0]) == 'test_default.pdf' and
91
+ not File.exist?(TMPDIR + '/pdfmd.log')
92
+ result = 'OK'
93
+ else
94
+ result = 'failed'
95
+ end
96
+ $testResults.store('006',{:result => result, :command => commandparameter })
97
+
metadata CHANGED
@@ -1,69 +1,93 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdfmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Roos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-25 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 0.19.1
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - '='
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.19'
30
+ - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 0.19.1
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: highline
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
- - - '='
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ - - ">="
32
41
  - !ruby/object:Gem::Version
33
42
  version: 1.7.1
34
43
  type: :runtime
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
- - - '='
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.7'
50
+ - - ">="
39
51
  - !ruby/object:Gem::Version
40
52
  version: 1.7.1
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: fileutils
43
55
  requirement: !ruby/object:Gem::Requirement
44
56
  requirements:
45
- - - '='
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.7'
60
+ - - ">="
46
61
  - !ruby/object:Gem::Version
47
62
  version: '0.7'
48
63
  type: :runtime
49
64
  prerelease: false
50
65
  version_requirements: !ruby/object:Gem::Requirement
51
66
  requirements:
52
- - - '='
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.7'
70
+ - - ">="
53
71
  - !ruby/object:Gem::Version
54
72
  version: '0.7'
55
73
  - !ruby/object:Gem::Dependency
56
74
  name: i18n
57
75
  requirement: !ruby/object:Gem::Requirement
58
76
  requirements:
59
- - - '='
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.6'
80
+ - - ">="
60
81
  - !ruby/object:Gem::Version
61
82
  version: 0.6.11
62
83
  type: :runtime
63
84
  prerelease: false
64
85
  version_requirements: !ruby/object:Gem::Requirement
65
86
  requirements:
66
- - - '='
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ - - ">="
67
91
  - !ruby/object:Gem::Version
68
92
  version: 0.6.11
69
93
  description: |2
@@ -80,6 +104,7 @@ files:
80
104
  - CHANGELOG.md
81
105
  - LICENSE
82
106
  - README.md
107
+ - Rakefile
83
108
  - bin/pdfmd
84
109
  - lib/pdfmd.rb
85
110
  - lib/pdfmd/check.rb
@@ -95,7 +120,12 @@ files:
95
120
  - lib/pdfmd/rename.rb
96
121
  - lib/pdfmd/show.rb
97
122
  - lib/pdfmd/sort.rb
123
+ - lib/string_extend.rb
98
124
  - pdfmd.gemspec
125
+ - test/test_default.pdf
126
+ - test/test_rename.rb
127
+ - test/test_show.rb
128
+ - test/test_sort.rb
99
129
  homepage: https://github.com/Micronarrativ/ruby-pmd
100
130
  licenses:
101
131
  - MIT