Class: Icersplicer::FileProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/icersplicer.rb

Constant Summary collapse

COLOURS =
{"black" => 0,
"red" => 1, 
"green" => 2, 
"yellow" => 3, 
"blue" => 4,
"purple" => 5,
"cyan" => 6,
"white" => 7}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileProcessor

Returns a new instance of FileProcessor.



29
30
31
32
33
34
# File 'lib/icersplicer.rb', line 29

def initialize
  @fileopen = 0
  @keywordsfile = "keywords.ice"
  @debug = 0
  @nolinenumbers = false
end

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



18
19
20
# File 'lib/icersplicer.rb', line 18

def debug=(value)
  @debug = value
end

#keywordsfile=(value) ⇒ Object (writeonly)

Sets the attribute keywordsfile

Parameters:

  • value

    the value to set the attribute keywordsfile to.



18
19
20
# File 'lib/icersplicer.rb', line 18

def keywordsfile=(value)
  @keywordsfile = value
end

#nohighlighter=(value) ⇒ Object (writeonly)

Sets the attribute nohighlighter

Parameters:

  • value

    the value to set the attribute nohighlighter to.



18
19
20
# File 'lib/icersplicer.rb', line 18

def nohighlighter=(value)
  @nohighlighter = value
end

#nolinenumbers=(value) ⇒ Object (writeonly)

Sets the attribute nolinenumbers

Parameters:

  • value

    the value to set the attribute nolinenumbers to.



18
19
20
# File 'lib/icersplicer.rb', line 18

def nolinenumbers=(value)
  @nolinenumbers = value
end

#skip_lines=(value) ⇒ Object (writeonly)

Sets the attribute skip_lines

Parameters:

  • value

    the value to set the attribute skip_lines to.



18
19
20
# File 'lib/icersplicer.rb', line 18

def skip_lines=(value)
  @skip_lines = value
end

Instance Method Details

#closefileObject



179
180
181
182
183
184
185
# File 'lib/icersplicer.rb', line 179

def closefile
  begin
    @export.close
    puts "Closing file"
  rescue NoMethodError
  end
end

#countlines(inputfile) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/icersplicer.rb', line 107

def countlines(inputfile)
  lines = 0
  unless inputfile == nil
    if File.exist?(inputfile)
      File.open(inputfile) {|n|
        n.each_line {
          lines += 1
        }
      }
      puts "Filename: #{inputfile} Total Line Count: #{lines}"
    end
  end
  return lines
end

#filterlinestats(filterlines) ⇒ Object



40
41
42
# File 'lib/icersplicer.rb', line 40

def filterlinestats(filterlines)
  puts "\nLines Displayed by Filter: #{filterlines}"
end

#followtail(filename, number) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/icersplicer.rb', line 44

def followtail(filename, number)
  File::Tail::Logfile.open(filename) do |log|
    log.interval = 3
    log.backward(10)
    log.backward(number).tail { |line| puts line }
  end
  exit
end

#load_keywords(file) ⇒ Object



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

def load_keywords(file)
  keys = Hash.new
  linenum = 0
  unless Dir.exist?("#{Dir.home}/.icersplicer")
    Dir.mkdir("#{Dir.home}/.icersplicer")
  end
  if File.exists?("#{Dir.home}/.icersplicer/#{file}")
    File.open("#{Dir.home}/.icersplicer/#{file}") {|n|
      n.each_line {|l|
        keys.update({linenum => "#{l.strip}"}) unless l.strip == ""
        puts "L: #{l.strip}" if @debug >= 1
        linenum += 1
      }
    }
    return keys
  else
    return false
  end
end

#openfile(outputfile) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/icersplicer.rb', line 166

def openfile(outputfile)
  begin
    puts "Openfile: #{outputfile}" if @debug >= 1
    @export = File.open("#{outputfile}", 'w')
  rescue Errno::EACCES
    raise IOError, "Can't create file please check file / directory permissions"
  end
end


159
160
161
162
163
164
# File 'lib/icersplicer.rb', line 159

def print_to_screen(linenum, text, quiet)
  unless @nolinenumbers == true
    print "\e[1;33mLn: #{linenum}:\e[0m\ "
  end
  print "#{text}" unless quiet == true
end

#processdata(data, outputfile, quietmode) ⇒ Object



187
188
189
190
191
# File 'lib/icersplicer.rb', line 187

def processdata(data, outputfile, quietmode)
  openfile(outputfile) if @fileopen == 0
  writefile(data)
  @fileopen += 1
end

#reset_screenObject



36
37
38
# File 'lib/icersplicer.rb', line 36

def reset_screen
  puts "\e[0m\ "
end

#skip(line) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/icersplicer.rb', line 145

def skip(line)
  begin
    if instance_variable_defined?("@skip_lines")
      line_element = @skip_lines.key(line)
      if line_element != nil
        skiper = @skip_lines[line_element]
      end
    end
  rescue NoMethodError
    return nil
  end
  return skiper
end

#skip_processor(filter) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/icersplicer.rb', line 122

def skip_processor(filter)
  skip_lines = Hash.new
  skipcounter = 0
  filter.to_s.split(",").each {|n| 
    skip_lines.update({skipcounter => n.to_i}) 
    # Allow line ranges 
    min = n.split("-")[0].to_i
    max = n.split("-")[1].to_i
    puts "Min: #{min} Max: #{max}" if @debug >= 2
    unless n.split("-")[1] == nil
      if min > max
        raise RangeError, "Range Error: Minimun value can't be more than Maxiumun Range value"              
      end
      min.upto(max) {|s|
        skip_lines.update({skipcounter => s}) unless skip_lines[skip_lines.size - 1] == s
        skipcounter += 1
      }
    end
    skipcounter += 1
  }
  return skip_lines
end

#stats(inputfile, outputfile) ⇒ Object



193
194
195
196
197
# File 'lib/icersplicer.rb', line 193

def stats(inputfile, outputfile)
  puts "Skip Lines #{@skip_lines}" if @debug >= 1
  print "Inputfile lines: "
  countlines(inputfile)
end

#text_highlighter(text) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/icersplicer.rb', line 82

def text_highlighter(text)
  keys ||= load_keywords("#{@keywordsfile}")
  unless keys.class == Hash
    keys = {0 => "Ln:", 
             1 => "SELECT", 
             2 => "CREATE TABLE", 
             3 => "UPDATE", 
             4 => "DELETE", 
             5 => "INSERT"}
  end
  cpicker = [2,3,4,1,7,5,6] # Just a selection of colours
  keys.each {|n|
    if n[1].split("##")[1] == nil
      text.gsub!("#{n[1]}", "\e[4;3#{cpicker[rand(cpicker.size)]}m#{n[1]}\e[0m\ \e[0;32m")
    else
      name = n[1].split("##")[1].split("=")[1]; puts "Name: #{name}" if @debug >= 3
      cnum = COLOURS[name].to_i; puts "Colour Number: #{cnum}" if @debug >= 3
      nval = n[1].split("##")[0]; puts "Value: #{nval}" if @debug >= 3
      text.gsub!("#{nval}", "\e[4;3#{cnum}m#{nval}\e[0m\ \e[0;32m")
    end
    text.gsub!(" \e[0;32m", "\e[0;32m")
  }
  return text
end

#text_processor(data) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/icersplicer.rb', line 73

def text_processor(data)
  unless @nohighlighter == "OFF"
    data = text_highlighter(data)
    return data
  else
    return data
  end
end

#writefile(data) ⇒ Object



175
176
177
# File 'lib/icersplicer.rb', line 175

def writefile(data)
  @export.write(data)
end