Class: Pdfmdsort

Inherits:
Pdfmd show all
Defined in:
lib/pdfmd/pdfmdsort.rb

Overview

Class: pdfmdsort

TODO: Author values with a slave One/two should be sorted into one/two/yyyymmdd-one_to-xxx.pdf

Instance Attribute Summary collapse

Attributes inherited from Pdfmd

#logfile, #logstatus

Instance Method Summary collapse

Methods inherited from Pdfmd

#check_metatags, #metadata, #readUserInput, #read_metatags

Methods included from Pdfmdmethods

#determineValidSetting, #log, #queryHiera

Constructor Details

#initialize(input) ⇒ Pdfmdsort

Initialize



11
12
13
14
15
16
17
18
19
20
# File 'lib/pdfmd/pdfmdsort.rb', line 11

def initialize(input)
    super input
    @stringSimBorder = 0.8  # Defines the value of the typo check
    @destination     = '.'
    @interactive     = false
    @copy            = false
    @dryrun          = false
    @overwrite       = false
    @typo            = false
end

Instance Attribute Details

#copyObject

Returns the value of attribute copy.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def copy
  @copy
end

#destinationObject

Returns the value of attribute destination.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def destination
  @destination
end

#dryrunObject

Returns the value of attribute dryrun.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def dryrun
  @dryrun
end

#filenameObject

Returns the value of attribute filename.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def filename
  @filename
end

#interactiveObject

Returns the value of attribute interactive.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def interactive
  @interactive
end

#overwriteObject

Returns the value of attribute overwrite.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def overwrite
  @overwrite
end

#typoObject

Returns the value of attribute typo.



8
9
10
# File 'lib/pdfmd/pdfmdsort.rb', line 8

def typo
  @typo
end

Instance Method Details

#checkDestinationObject

Check if the destination is valid



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pdfmd/pdfmdsort.rb', line 25

def checkDestination

  log('debug', "Checking destination parameter '#{@destination}'.")

  if File.file?(@destination)
    log('error', "Destination '#{@destination}' is a file.")
    false
  else
    log('debug', "Destination '#{@destination}' as directory confirmed.")
    true
  end

end

#findSimilarTargetdir(targetdir) ⇒ Object

Method compares string from ‘targetdir’ with all subfolders in the targetdir

in order to find similarities in writing.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pdfmd/pdfmdsort.rb', line 53

def findSimilarTargetdir( targetdir )

  self.log('debug', "Running method 'findSimilarTarget' with parameter '#{targetdir}'.")

  fuzzy       = FuzzyStringMatch::JaroWinkler.create( :native )
  returnValue = false

  # Get all subfolders 
  subDirectories = Dir[@destination + '/*']
  subDirectories.each do |fullPathFolder|
     stringSimilarity = fuzzy.getDistance( 
       fullPathFolder.gsub(@destination + '/', ''),
       targetdir.gsub(@destination + '/', '')
       )
     if stringSimilarity > @stringSimBorder
       self.log('debug', "findSimilarTargetdir: Found String value #{stringSimilarity.to_s} for target '#{fullPathFolder}'.")
       returnValue = fullPathFolder
     end
  end
  returnValue
end

#get_authorObject

Get the author Return ‘false’ if no author is being found.



42
43
44
45
46
47
48
49
# File 'lib/pdfmd/pdfmdsort.rb', line 42

def get_author()
  if not self.check_metatags('author')
    return false
  end
  author = @@metadata['author'].gsub(/\./,'_').gsub(/\&/,'').gsub(/\-/,'_').gsub(/\s/,'_').gsub(/\,/,'_').gsub(/\_\_/,'_')
  I18n.enforce_available_locales = false
  I18n.transliterate(author).downcase # Normalising
end

#sortObject

Sort the file away



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/pdfmd/pdfmdsort.rb', line 78

def sort
  if self.checkDestination

    if @interactive
      answer = readUserInput("Process '#{@filename}' ([y]/n): ")
      answer = answer.empty? ? 'y' : answer
      self.log('info', "User Answer for file '#{@filename}': #{answer}")
      if !answer.match(/y/)  
        self.log('info',"Skipping file '#{@filename}' due to user answer: '#{answer}'.")
        return
      else
        self.log('info',"Processing file '#{@filename}' due to user answer: '#{answer}'.")
      end
    end

    if not author = get_author() or author.empty?
      self.log('error', "File '#{@filename}' has not value for author set. Cannot sort file. Abort.")
      exit 1
    end
    targetdir   = @destination.chomp + '/' + author
    targetfile  = targetdir + '/' + Pathname.new(@filename).basename.to_s

    # Create the target directory, if it does not exist yet.
    if !File.exists? targetdir

      # Check for similiar directory names which might indicate a typo in the
      #   current directory name.
      if @typo and foundDir = self.findSimilarTargetdir(targetdir)

        self.log('info', "Similar target found ('" + foundDir + "'). Request user input.")
        puts 'Similar target directory detected:'
        puts 'Found : ' + foundDir
        puts 'Target: ' + targetdir
        while answer = readUserInput('Abort? ([y]/n): ')
          if answer.match(/(y|yes|j|ja|^$)/i)
            self.log('info','User chose to abort sorting.')
            puts 'Abort.'
            exit 0
          elsif answer.match(/(n|no)/i)
            self.log('info', 'User chose to continue sorting.')
            break
          end
        end
      end

      if @dryrun
        self.log('info', "Dryrun: Created Directory '#{targetdir}'.")
      else
        self.log('info', "Created directory '#{targetdir}'.")
        puts 'Created: ' + targetdir
        FileUtils.mkdir_p(targetdir)
      end
    end

    # Check if the file already exists
    # This does nothing so far
    if File.exists?(targetfile) and @overwrite
      self.log('info', "File '#{@filename}' already exists. Overwrite active: replacing file.")
    elsif File.exists?(targetfile) and !@overwrite
      self.log('info', "File '#{@filename}' already exists, overwrite disabled: not replacing file.")
      return true
    end

    if @copy

      if @dryrun
        self.log('info', "Dryrun: Copy file '#{@filename}' to '#{targetdir}'.")
      else
        self.log('info', "Copy file '#{@filename}' to '#{targetdir}'.")
        FileUtils.cp(@filename, targetdir)
      end

    else

      if @dryrun
        self.log('info', "Dryrun: Move file '#{@filename}' to '#{targetdir}'.")
      else
        self.log('info', "Move file '#{@filename}' to '#{targetdir}'.")
        FileUtils.mv(@filename, targetdir)
      end

    end

  end
end