jgrep 1.3.0.1 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/jgrep +76 -42
  2. data/jgrep.gemspec +2 -2
  3. data/lib/jgrep.rb +24 -3
  4. metadata +2 -3
data/bin/jgrep CHANGED
@@ -3,21 +3,68 @@
3
3
  require 'jgrep'
4
4
  require 'optparse'
5
5
 
6
- options = {:flat => false, :start => nil}
6
+ @options = {:flat => false, :start => nil}
7
+
8
+ def print_json(result)
9
+ unless @options[:flat]
10
+ result = result.first if @options[:stream]
11
+ puts(JSON.pretty_generate(result))
12
+ else
13
+ puts(result.first.to_json)
14
+ end
15
+ end
16
+
17
+ def do_grep(json, expression)
18
+ unless @options[:field]
19
+ result = JGrep::jgrep((json), expression, nil, @options[:start])
20
+ unless result == [] or @options[:quiet] == true
21
+ print_json(result)
22
+ end
23
+ else
24
+ if @options[:field].is_a? Array
25
+ JGrep::validate_filters(@options[:field])
26
+ result = JGrep::jgrep((json), expression, @options[:field], @options[:start])
27
+ unless result == [] or @options[:quiet] == true
28
+ print_json(result)
29
+ end
30
+
31
+ else
32
+ JGrep::validate_filters(@options[:field])
33
+ result = JGrep::jgrep((json), expression, @options[:field], @options[:start])
34
+ unless result.first.is_a?(Hash) || result.flatten.first.is_a?(Hash)
35
+ unless @options[:quiet] == true
36
+ result.map{|x| puts x unless x.nil?}
37
+ end
38
+ else
39
+ unless @options[:quiet] == true
40
+ print_json(result)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
7
46
 
8
47
  begin
9
48
  OptionParser.new do |opts|
10
49
  opts.banner = "Usage: jgrep [options] \"expression\""
11
50
  opts.on("-s", "--simple [FIELDS]", "Display only one or more fields from each of the resulting json documents") do |field|
12
- if (field.split(" ")).size > 1
13
- options[:field] = field.split(" ")
51
+ unless field.nil?
52
+ if (field.split(" ")).size > 1
53
+ @options[:field] = field.split(" ")
54
+ else
55
+ @options[:field] = field
56
+ end
14
57
  else
15
- options[:field] = field
58
+ raise "-s flag requires a field value"
16
59
  end
17
60
  end
18
61
 
19
62
  opts.on("-c", "--compact", "Display non pretty json") do
20
- options[:flat] = true
63
+ @options[:flat] = true
64
+ end
65
+
66
+ opts.on("-n", "--stream", "Display continuous output from continuous input") do
67
+ @options[:stream] = true
21
68
  end
22
69
 
23
70
  opts.on("-f", "--flatten", "Makes output as flat as possible") do
@@ -25,11 +72,11 @@ begin
25
72
  end
26
73
 
27
74
  opts.on("-i", "--input [FILENAME]", "Specify input file to parse") do |filename|
28
- options[:file] = filename
75
+ @options[:file] = filename
29
76
  end
30
77
 
31
78
  opts.on("-q", "--quiet", "Quiet; don't write to stdout. Exit with zero status if match found.") do
32
- options[:quiet] = true
79
+ @options[:quiet] = true
33
80
  end
34
81
 
35
82
  opts.on("-v", "--verbose", "Verbose output") do
@@ -37,12 +84,16 @@ begin
37
84
  end
38
85
 
39
86
  opts.on("--start [FIELD]", "Where in the data to start from") do |field|
40
- options[:start] = field
87
+ @options[:start] = field
41
88
  end
42
89
  end.parse!
43
90
  rescue OptionParser::InvalidOption => e
44
91
  puts e.to_s.capitalize
45
92
  exit 1
93
+
94
+ rescue Exception => e
95
+ puts e
96
+ exit 1
46
97
  end
47
98
 
48
99
  begin
@@ -58,52 +109,35 @@ begin
58
109
 
59
110
  expression = "" if expression.nil?
60
111
 
112
+ #Continuously gets if inputstream in constant
61
113
  #Load json from standard input if tty is false
62
114
  #else find and load file from command line arugments
63
- unless STDIN.tty?
64
- json = STDIN.read
65
- else
66
- if options[:file]
67
- json = File.read(options[:file])
115
+
116
+ if @options[:stream]
117
+ unless STDIN.tty?
118
+ while json = gets
119
+ do_grep(json, expression)
120
+ end
68
121
  else
69
122
  raise "No json input specified"
70
123
  end
71
- end
72
-
73
- unless options[:field]
74
- result = JGrep::jgrep((json), expression, nil, options[:start])
75
- unless result == [] or options[:quiet] == true
76
- (options[:flat] == false) ? puts(JSON.pretty_generate(result)) : puts(result.to_json)
77
- end
78
- if result.length == 0
79
- exit 1
80
- end
81
124
  else
82
- if options[:field].is_a? Array
83
- JGrep::validate_filters(options[:field])
84
- result = JGrep::jgrep((json), expression, options[:field], options[:start])
85
- unless result == [] or options[:quiet] == true
86
- puts(JSON.pretty_generate(result))
87
- end
88
-
125
+ unless STDIN.tty?
126
+ json = STDIN.read
127
+ do_grep(json, expression)
89
128
  else
90
- JGrep::validate_filters(options[:field])
91
- result = JGrep::jgrep((json), expression, options[:field], options[:start])
92
- unless result.first.is_a?(Hash) || result.flatten.first.is_a?(Hash)
93
- unless options[:quiet] == true
94
- result.map{|x| puts x unless x.nil?}
95
- end
129
+ if @options[:file]
130
+ json = File.read(@options[:file])
131
+ do_grep(json, expression)
96
132
  else
97
- unless options[:quiet] == true
98
- puts(JSON.pretty_generate(result))
99
- end
100
- end
101
- unless result.length > 0
102
- exit 1
133
+ raise "No json input specified"
103
134
  end
104
135
  end
105
136
  end
106
137
 
138
+ rescue Interrupt
139
+ STDERR.puts "Exiting..."
140
+ exit 1
107
141
  rescue Exception => e
108
142
  if e.is_a?(SystemExit)
109
143
  exit e.status
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "jgrep"
3
- s.version = "1.3.0.1"
3
+ s.version = "1.3.1"
4
4
 
5
5
  s.authors = ["P Loubser"]
6
- s.date = %q{2011-08-05}
6
+ s.date = %q{2011-12-15}
7
7
  s.default_executable = "jgrep"
8
8
  s.add_dependency('json')
9
9
  s.description = "Compare a list of json documents to a simple logical language and returns matches as output"
@@ -4,12 +4,28 @@ require 'parser/parser.rb'
4
4
  require 'parser/scanner.rb'
5
5
  require 'rubygems'
6
6
  require 'json'
7
- #require 'yajl/json_gem'
8
7
 
9
8
  module JGrep
10
9
  @verbose = false
11
10
  @flatten = false
12
11
 
12
+ class JGrep
13
+ attr_accessor :value, :expression
14
+
15
+ def initialize()
16
+ @value = ""
17
+ @expression = ""
18
+ end
19
+
20
+ def match_expression(expression)
21
+ return !::JGrep.jgrep(@value, expression).empty?
22
+ end
23
+
24
+ def match_value(value)
25
+ return !::JGrep.jgrep(value, @expression).empty?
26
+ end
27
+ end
28
+
13
29
  def self.verbose_on
14
30
  @verbose = true
15
31
  end
@@ -26,7 +42,11 @@ module JGrep
26
42
  errors = ""
27
43
  begin
28
44
  JSON.create_id = nil
29
- json = JSON.parse(json)
45
+
46
+ if json.class == String
47
+ json = JSON.parse(json)
48
+ end
49
+
30
50
  if json.is_a? Hash
31
51
  json = [json]
32
52
  end
@@ -48,7 +68,7 @@ module JGrep
48
68
  pp document
49
69
  STDERR.puts "Error - #{e} \n\n"
50
70
  else
51
- errors = "One or more the json documents could not be parsed. Run jgrep -v for to display documents"
71
+ errors = "One or more the json documents could not be parsed. Run jgrep -v to display documents"
52
72
  end
53
73
  end
54
74
  end
@@ -72,6 +92,7 @@ module JGrep
72
92
  end
73
93
  end
74
94
 
95
+
75
96
  #Convert a specific hash inside a JSON document to an array
76
97
  #Mark is a string in the format foo.bar.baz that points to
77
98
  #the array in the document.
metadata CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 3
8
- - 0
9
8
  - 1
10
- version: 1.3.0.1
9
+ version: 1.3.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - P Loubser
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-08-05 00:00:00 +01:00
17
+ date: 2011-12-15 00:00:00 +00:00
19
18
  default_executable: jgrep
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency