interactive_grep 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .ignore
2
+ .rvmrc
3
+ *.gem
4
+ *.tmp
5
+ .bundle
6
+ Gemfile.lock
7
+ coverage/*
8
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in interactive_grep.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Jonathan Thomas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.txt ADDED
@@ -0,0 +1,41 @@
1
+ Interactively Grep for patterns.
2
+
3
+ This works particularly well for files with muli-line patterns (like
4
+ weblogs) and it handles zipped files.
5
+
6
+ The code: horrible. This is my very first ruby program!
7
+ I keep it simply because it's useful.
8
+ One day I'll actually fix the code ;-)
9
+
10
+ Basic use-case(s):
11
+ gem install interactive_grep
12
+ # from the commandline:
13
+ igrep -h
14
+
15
+ # in your code:
16
+ irb
17
+ # standard 'grep' (for optionally gzipped files)
18
+ ig = InteractiveGrep::Grepper.new("/path/to/my/weblog.gz", search_pattern_string)
19
+ results = ig.run
20
+ puts results
21
+ # => [matching_line1, matching_line2, matching_line3, etc...]
22
+
23
+ # standard 'grep -c' (for optionally gzipped files)
24
+ ig = InteractiveGrep::Grepper.new("/path/to/my/weblog.gz", search_pattern_string, true)
25
+ ig.run
26
+ # => 20 <-- yes, that third 'magic' initialization param caused
27
+ the grepper to merely "count" the number of matches
28
+
29
+ # *** interactive grep (for optionally gzipped files) ***
30
+ ig = InteractiveGrep::Grepper.new("/path/to/my/weblog.gz", search_pattern_string)
31
+ ig.interactive = true
32
+ ig.run
33
+ # every time your *current* pattern is matched, the output is
34
+ # displayed ane you are prompted to:
35
+ # repeat the pattern search (just hit return)
36
+ # update the pattern (i.e. to find the next level of detail)
37
+
38
+ (See specs for more detailed use-cases.)
39
+ rake rspec
40
+
41
+ # TODO: re-write this TDD-style
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ gemspec = eval(File.read("interactive_grep.gemspec"))
7
+
8
+ task :build => "#{gemspec.full_name}.gem"
9
+
10
+ file "#{gemspec.full_name}.gem" => gemspec.files + ["interactive_grep.gemspec"] do
11
+ system "gem build interactive_grep.gemspec"
12
+ system "gem install interactive_grep-#{InteractiveGrep::VERSION}.gem"
13
+ end
data/bin/igrep ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # the "i" is for interactive
4
+ require 'interactive_grep'
5
+
6
+ if __FILE__ == $0
7
+ if '-h' == ARGV[0] || 0 == ARGV.size
8
+ puts "usage $0 -h"
9
+ puts "\tproduces this help-message"
10
+ puts ""
11
+
12
+ puts "usage $0 <input_file> [<pattern>] [<display count-only>] [<interactive-mode=on>]"
13
+ puts ""
14
+ puts "e.g."
15
+ puts "./igrep.rb big_test.txt.gz"
16
+ puts './igrep.rb "/reporting/access*.gz" cvsfa=63'
17
+ puts ""
18
+ exit
19
+ end
20
+
21
+ in_file = ARGV[0]
22
+ pattern = nil
23
+ interactive = nil
24
+ just_count = false
25
+ prompt = nil
26
+ if 2 <= ARGV.size
27
+ pattern = ARGV[1]
28
+ end
29
+ if 3 <= ARGV.size
30
+ # ARGV[2]
31
+ just_count = true
32
+ puts "true count: #{ARGV[2]}"
33
+ end
34
+ if 4 <= ARGV.size
35
+ interactive = ARGV[3]
36
+ puts "interactive mode: #{interactive}"
37
+ end
38
+
39
+ prompt = 'ready to continue' if interactive
40
+ igrep = InteractiveGrep::Grepper.new(in_file,pattern,just_count,interactive)
41
+ puts "#{prompt}\n\n" if interactive
42
+ igrep.run()
43
+ exit
44
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "interactive_grep/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "interactive_grep"
7
+ s.version = InteractiveGrep::VERSION
8
+ s.authors = ["JayTeeSr"]
9
+ s.email = ["jaytee_sr_at_his-service_dot_net"]
10
+ s.homepage = "https://github.com/JayTeeSF/interactive_grep"
11
+ s.summary = %q{My very first Ruby program, which helped me interactively grep for multi-line patterns in gzipped weblogs}
12
+ s.description = %q{Interactively grep for stuff in (optionally gzipped) files}
13
+ s.rubyforge_project = "interactive_grep"
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.bindir = 'bin'
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ s.add_development_dependency "rspec", "~> 2.11.0"
22
+ end
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,3 @@
1
+ require 'interactive_grep/version'
2
+ require 'interactive_grep/grepper'
3
+ module InteractiveGrep; end
Binary file
@@ -0,0 +1,133 @@
1
+ module InteractiveGrep
2
+ class Grepper
3
+ require 'zlib'
4
+
5
+ @@DEBUG = false
6
+ attr_accessor :in_file, :ending, :def_pattern, :interactive, :counter, :just_count, :gz
7
+
8
+ def initialize(in_file,def_pattern=".",just_count=false,interactive=nil)
9
+ @counter = 0
10
+ @file_index = 0
11
+ @in_file = [ in_file.chomp ]
12
+ m = /(\.[^\.]+)$/.match(@in_file[0])
13
+
14
+ @ending = m[1] || ''
15
+
16
+ # handle glob...
17
+ if /\*/.match(@in_file[0])
18
+ @in_file = Dir.glob(@in_file[0]).map{|fname| fname}
19
+ end
20
+
21
+ @def_pattern = /#{def_pattern}/; #%r{ #{def_pattern} }
22
+ if interactive && just_count
23
+ just_count = false
24
+ end
25
+
26
+ @just_count = just_count
27
+ @interactive = interactive
28
+ @gz = false
29
+ if '.gz' == @ending
30
+ @gz = true
31
+ end
32
+
33
+ if @@DEBUG
34
+ print "using:\n\tin_file: #{@in_file}\n\tw/ ending:"
35
+ print "#{@ending}\n\tdef_pattern: #{@def_pattern}\n"
36
+ end
37
+ end
38
+
39
+ def prompt(pattern, msg, display=true)
40
+ if display && msg
41
+ @counter += 1
42
+ if !@just_count
43
+ if @@DEBUG
44
+ puts ""
45
+ puts ">>"
46
+ end
47
+ print msg
48
+ if @@DEBUG
49
+ puts "<<"
50
+ end
51
+ puts ""
52
+ end
53
+ end
54
+
55
+ if @interactive.nil?
56
+ if @@DEBUG
57
+ puts "interactive mode is off"
58
+ end
59
+ pattern
60
+ else
61
+ puts "S | O | <pattern: [#{pattern}]> ? "
62
+ user_input = STDIN.gets.chomp
63
+ puts ""
64
+ if @@DEBUG
65
+ puts "user_input: >>#{user_input}<<"
66
+ end
67
+
68
+ case
69
+ when /^\s*$/.match(user_input) #corresponds w/ <enter> (or <return>)
70
+ if @@DEBUG
71
+ puts "user hit <return> (cont w/ same pattern)"
72
+ end
73
+ pattern
74
+ when ( (user_input == "o") || (user_input == "O") ) #interactive mode Off
75
+ @interactive = nil
76
+ if @@DEBUG
77
+ puts "turned interactive mode off"
78
+ end
79
+ pattern
80
+ when ( (user_input == "s") || (user_input == "S") ) #Show every line
81
+ if @@DEBUG
82
+ puts "show next line"
83
+ end
84
+ /./
85
+ else #use whatever was entered
86
+ if @@DEBUG
87
+ puts "use whatever was entered"
88
+ end
89
+ %r{#{user_input}}
90
+ end
91
+ end
92
+ end
93
+
94
+ def run(display_results=true)
95
+ results = []
96
+ while (1 + @file_index) < @in_file.count
97
+ myfh = get_file_handle()
98
+ pattern = @def_pattern
99
+ while line = myfh.gets
100
+ if pattern.match(line)
101
+ if @@DEBUG
102
+ puts ""
103
+ end
104
+ if line
105
+ line.strip!
106
+ results << line
107
+ end
108
+ pattern = prompt( pattern, line, display_results )
109
+ if @@DEBUG
110
+ puts ""
111
+ end
112
+ end
113
+ if @@DEBUG
114
+ puts "."
115
+ end
116
+ end
117
+ end
118
+ puts "count: #{@counter}" if @just_count
119
+ results
120
+ end
121
+
122
+ def get_file_handle
123
+ handle = nil
124
+ if @gz
125
+ handle = Zlib::GzipReader.open(@in_file[@file_index]);
126
+ else
127
+ handle = File.open(@in_file[@file_index], 'r')
128
+ end
129
+ @file_index += 1
130
+ handle
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,3 @@
1
+ module InteractiveGrep
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+ describe InteractiveGrep::Grepper do
3
+ context "initialization" do
4
+ context "with invalid params" do
5
+ it "should raise an error" do
6
+ expect { InteractiveGrep::Grepper.new }.to raise_error(ArgumentError)
7
+ expect { InteractiveGrep::Grepper.new(:one) }.to raise_error
8
+ end
9
+ end
10
+ context "with valid params" do
11
+ it "should not raise an error" do
12
+ expect { InteractiveGrep::Grepper.new( "./*.gz" ) }.not_to raise_error
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,5 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ require "rspec"
5
+ require "interactive_grep"
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interactive_grep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - JayTeeSr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70239385100440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70239385100440
25
+ description: Interactively grep for stuff in (optionally gzipped) files
26
+ email:
27
+ - jaytee_sr_at_his-service_dot_net
28
+ executables:
29
+ - igrep
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - MIT-LICENSE
36
+ - README.txt
37
+ - Rakefile
38
+ - bin/igrep
39
+ - interactive_grep.gemspec
40
+ - lib/.DS_Store
41
+ - lib/interactive_grep.rb
42
+ - lib/interactive_grep/.DS_Store
43
+ - lib/interactive_grep/grepper.rb
44
+ - lib/interactive_grep/version.rb
45
+ - spec/lib/interactive_grep/grepper_spec.rb
46
+ - spec/spec_helper.rb
47
+ homepage: https://github.com/JayTeeSF/interactive_grep
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project: interactive_grep
67
+ rubygems_version: 1.8.17
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: My very first Ruby program, which helped me interactively grep for multi-line
71
+ patterns in gzipped weblogs
72
+ test_files:
73
+ - spec/lib/interactive_grep/grepper_spec.rb
74
+ - spec/spec_helper.rb