Class: Filecamo::TextMucker

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

Constant Summary collapse

MAX_FILE_SIZE =
128 * 1024
LANG_MARKS =
{
  csharp: '//',
  python: '#',
  ruby: '#',
  shell: '#',
  js: '#',
  plain: '#',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment_prefix, logger: Logger.new($stdout)) ⇒ TextMucker

Returns a new instance of TextMucker.



20
21
22
23
24
25
26
27
# File 'lib/filecamo/text_mucker.rb', line 20

def initialize(comment_prefix, logger: Logger.new($stdout))
  @marks = LANG_MARKS.clone
  @marks.each_value{|m| m << comment_prefix}
  @logger = logger
  @magic = FileMagic.new
  @mime = FileMagic.mime
  @stats = {files_selected: 0, lines_added: 0}
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



29
30
31
# File 'lib/filecamo/text_mucker.rb', line 29

def stats
  @stats
end

Instance Method Details

#muck(percent_select, percent_lines, paths) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
# File 'lib/filecamo/text_mucker.rb', line 31

def muck(percent_select, percent_lines, paths)
  select_chance = percent_select.to_f / 100
  lines_chance = percent_lines.to_f / 100

  paths.each do |path|
    path[0] == '.' and next
    if File.directory?(path)
      paths.concat(Dir.entries(path).map{|e| e[0] == '.' ? nil : File.join(path,e)}.compact)
      next
    end

    fn = path
    fn_size = File.size(fn)

    # todo: support working with large files by reading next line
    if fn_size > MAX_FILE_SIZE
      @logger.debug "Skipping #{fn} by size: #{file.size}"
      break
    end

    lang = case File.extname(fn)
           when '.cs' then :csharp
           when '.py' then :python
           when '.js' then :js
           when '.json' then :json
           when '.yaml','meta' then :yaml
           when '.html' then :html
           when '.txt' then :plain
           else
             case m = @mime.file(fn)
             when /python/ then :python
             when /ruby/ then :ruby
             when /shell/ then :shell
             when /plain/
               case g = @magic.file(fn)
               when /python/ then :python
               when /ruby/ then :ruby
               when /node/ then :js
               else
                 :plain
               end
             else
               @logger.debug "Skipping #{fn} by mime type: #{m}"
               next
             end
           end

    if Random.rand > select_chance
      @logger.debug "Skipping #{fn} by chance"
      next
    end

    @stats[:files_selected] += 1

    new_lines = {}
    new_bytes_needed = (fn_size * lines_chance).floor
    while new_bytes_needed > 0
      offset = Random.rand(fn_size)
      new_line = get_line_for(lang)
      new_lines[offset] = new_line
      new_bytes_needed -= new_line.bytesize
    end
    new_lines = new_lines.sort
    @stats[:lines_added] += new_lines.size

    body = ''
    line_nums = []

    File.open(fn) do |file|
      line_num = 0
      while !file.eof? && line = file.readline
        body << line
        line_num += 1
        new_lines.empty? and next # read remainder of file
        offset = new_lines[0][0]
        if file.pos >= offset # add a line as soon as passed the offset
          offset, new_line = new_lines.shift
          line_nums << (line_num+=1)
          body << new_line
        end
      end

      # concat any remaining lines
      if !new_lines.empty?
        body[-1] == $/ or body << $/
        new_lines.each do |offset, new_line|
          line_num += 1
          line_nums << line_num
          body << new_line
        end
      end
    end

    # todo: use same charset as mime type indicates when writing!
    File.open(fn, 'wb') {|f| f.write(body)}

    block_given? and yield(fn, lang, line_nums)
  end
end