jgrep 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/jgrep +37 -78
- data/jgrep.gemspec +2 -2
- data/lib/jgrep.rb +10 -27
- metadata +12 -5
data/bin/jgrep
CHANGED
@@ -3,68 +3,17 @@
|
|
3
3
|
require 'jgrep'
|
4
4
|
require 'optparse'
|
5
5
|
|
6
|
-
|
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
|
6
|
+
options = {:flat => false, :start => nil, :field => []}
|
46
7
|
|
47
8
|
begin
|
48
9
|
OptionParser.new do |opts|
|
49
10
|
opts.banner = "Usage: jgrep [options] \"expression\""
|
50
11
|
opts.on("-s", "--simple [FIELDS]", "Display only one or more fields from each of the resulting json documents") do |field|
|
51
|
-
|
52
|
-
if (field.split(" ")).size > 1
|
53
|
-
@options[:field] = field.split(" ")
|
54
|
-
else
|
55
|
-
@options[:field] = field
|
56
|
-
end
|
57
|
-
else
|
58
|
-
raise "-s flag requires a field value"
|
59
|
-
end
|
12
|
+
options[:field].concat(field.split(" "))
|
60
13
|
end
|
61
14
|
|
62
15
|
opts.on("-c", "--compact", "Display non pretty json") do
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
opts.on("-n", "--stream", "Display continuous output from continuous input") do
|
67
|
-
@options[:stream] = true
|
16
|
+
options[:flat] = true
|
68
17
|
end
|
69
18
|
|
70
19
|
opts.on("-f", "--flatten", "Makes output as flat as possible") do
|
@@ -72,11 +21,11 @@ begin
|
|
72
21
|
end
|
73
22
|
|
74
23
|
opts.on("-i", "--input [FILENAME]", "Specify input file to parse") do |filename|
|
75
|
-
|
24
|
+
options[:file] = filename
|
76
25
|
end
|
77
26
|
|
78
27
|
opts.on("-q", "--quiet", "Quiet; don't write to stdout. Exit with zero status if match found.") do
|
79
|
-
|
28
|
+
options[:quiet] = true
|
80
29
|
end
|
81
30
|
|
82
31
|
opts.on("-v", "--verbose", "Verbose output") do
|
@@ -84,16 +33,12 @@ begin
|
|
84
33
|
end
|
85
34
|
|
86
35
|
opts.on("--start [FIELD]", "Where in the data to start from") do |field|
|
87
|
-
|
36
|
+
options[:start] = field
|
88
37
|
end
|
89
38
|
end.parse!
|
90
39
|
rescue OptionParser::InvalidOption => e
|
91
40
|
puts e.to_s.capitalize
|
92
41
|
exit 1
|
93
|
-
|
94
|
-
rescue Exception => e
|
95
|
-
puts e
|
96
|
-
exit 1
|
97
42
|
end
|
98
43
|
|
99
44
|
begin
|
@@ -109,35 +54,49 @@ begin
|
|
109
54
|
|
110
55
|
expression = "" if expression.nil?
|
111
56
|
|
112
|
-
#Continuously gets if inputstream in constant
|
113
57
|
#Load json from standard input if tty is false
|
114
58
|
#else find and load file from command line arugments
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
end
|
59
|
+
unless STDIN.tty?
|
60
|
+
json = STDIN.read
|
61
|
+
else
|
62
|
+
if options[:file]
|
63
|
+
json = File.read(options[:file])
|
121
64
|
else
|
122
65
|
raise "No json input specified"
|
123
66
|
end
|
67
|
+
end
|
68
|
+
|
69
|
+
if options[:field].empty?
|
70
|
+
result = JGrep::jgrep((json), expression, nil, options[:start])
|
71
|
+
unless options[:quiet] == true
|
72
|
+
(options[:flat] == false) ? puts(JSON.pretty_generate(result)) : puts(result.to_json)
|
73
|
+
end
|
124
74
|
else
|
125
|
-
|
126
|
-
|
127
|
-
|
75
|
+
if options[:field].size > 1
|
76
|
+
JGrep::validate_filters(options[:field])
|
77
|
+
result = JGrep::jgrep((json), expression, options[:field], options[:start])
|
78
|
+
unless result == [] or options[:quiet] == true
|
79
|
+
puts(JSON.pretty_generate(result))
|
80
|
+
end
|
81
|
+
|
128
82
|
else
|
129
|
-
|
130
|
-
|
131
|
-
|
83
|
+
JGrep::validate_filters(options[:field][0])
|
84
|
+
result = JGrep::jgrep((json), expression, options[:field][0], options[:start])
|
85
|
+
unless result.first.is_a?(Hash) || result.flatten.first.is_a?(Hash)
|
86
|
+
unless options[:quiet] == true
|
87
|
+
result.map{|x| puts x unless x.nil?}
|
88
|
+
end
|
132
89
|
else
|
133
|
-
|
90
|
+
unless options[:quiet] == true
|
91
|
+
puts(JSON.pretty_generate(result))
|
92
|
+
end
|
93
|
+
end
|
94
|
+
unless result.length > 0
|
95
|
+
exit 1
|
134
96
|
end
|
135
97
|
end
|
136
98
|
end
|
137
99
|
|
138
|
-
rescue Interrupt
|
139
|
-
STDERR.puts "Exiting..."
|
140
|
-
exit 1
|
141
100
|
rescue Exception => e
|
142
101
|
if e.is_a?(SystemExit)
|
143
102
|
exit e.status
|
data/jgrep.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "jgrep"
|
3
|
-
s.version = "1.3.
|
3
|
+
s.version = "1.3.3"
|
4
4
|
|
5
5
|
s.authors = ["P Loubser"]
|
6
|
-
s.date = %q{
|
6
|
+
s.date = %q{2012-09-26}
|
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"
|
data/lib/jgrep.rb
CHANGED
@@ -4,28 +4,12 @@ require 'parser/parser.rb'
|
|
4
4
|
require 'parser/scanner.rb'
|
5
5
|
require 'rubygems'
|
6
6
|
require 'json'
|
7
|
+
#require 'yajl/json_gem'
|
7
8
|
|
8
9
|
module JGrep
|
9
10
|
@verbose = false
|
10
11
|
@flatten = false
|
11
12
|
|
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
|
-
|
29
13
|
def self.verbose_on
|
30
14
|
@verbose = true
|
31
15
|
end
|
@@ -42,11 +26,7 @@ module JGrep
|
|
42
26
|
errors = ""
|
43
27
|
begin
|
44
28
|
JSON.create_id = nil
|
45
|
-
|
46
|
-
if json.class == String
|
47
|
-
json = JSON.parse(json)
|
48
|
-
end
|
49
|
-
|
29
|
+
json = JSON.parse(json)
|
50
30
|
if json.is_a? Hash
|
51
31
|
json = [json]
|
52
32
|
end
|
@@ -68,7 +48,7 @@ module JGrep
|
|
68
48
|
pp document
|
69
49
|
STDERR.puts "Error - #{e} \n\n"
|
70
50
|
else
|
71
|
-
errors = "One or more the json documents could not be parsed. Run jgrep -v to display documents"
|
51
|
+
errors = "One or more the json documents could not be parsed. Run jgrep -v for to display documents"
|
72
52
|
end
|
73
53
|
end
|
74
54
|
end
|
@@ -88,11 +68,9 @@ module JGrep
|
|
88
68
|
|
89
69
|
rescue JSON::ParserError => e
|
90
70
|
STDERR.puts "Error. Invalid JSON given"
|
91
|
-
exit 1
|
92
71
|
end
|
93
72
|
end
|
94
73
|
|
95
|
-
|
96
74
|
#Convert a specific hash inside a JSON document to an array
|
97
75
|
#Mark is a string in the format foo.bar.baz that points to
|
98
76
|
#the array in the document.
|
@@ -393,9 +371,14 @@ module JGrep
|
|
393
371
|
|
394
372
|
#Digs to a specific path in the json document and returns the value
|
395
373
|
def self.dig_path(json, path)
|
396
|
-
|
374
|
+
index = nil
|
397
375
|
path = path.gsub(/^\./, "")
|
398
376
|
|
377
|
+
if path =~ /(.*)\[(.*)\]/
|
378
|
+
path = $1
|
379
|
+
index = $2
|
380
|
+
end
|
381
|
+
|
399
382
|
if path == ""
|
400
383
|
return json
|
401
384
|
end
|
@@ -440,7 +423,7 @@ module JGrep
|
|
440
423
|
end
|
441
424
|
end
|
442
425
|
unless tmp.empty?
|
443
|
-
return tmp
|
426
|
+
(index) ? (return tmp.flatten[index.to_i]) : (return tmp)
|
444
427
|
end
|
445
428
|
end
|
446
429
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jgrep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 1
|
7
8
|
- 3
|
8
|
-
-
|
9
|
-
version: 1.3.
|
9
|
+
- 3
|
10
|
+
version: 1.3.3
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- P Loubser
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2012-09-26 00:00:00 +01:00
|
18
19
|
default_executable: jgrep
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: json
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
27
30
|
segments:
|
28
31
|
- 0
|
29
32
|
version: "0"
|
@@ -42,8 +45,8 @@ files:
|
|
42
45
|
- jgrep.gemspec
|
43
46
|
- bin/jgrep
|
44
47
|
- lib/jgrep.rb
|
45
|
-
- lib/parser/parser.rb
|
46
48
|
- lib/parser/scanner.rb
|
49
|
+
- lib/parser/parser.rb
|
47
50
|
has_rdoc: true
|
48
51
|
homepage: https://github.com/psy1337/JSON-Grep
|
49
52
|
licenses: []
|
@@ -54,23 +57,27 @@ rdoc_options: []
|
|
54
57
|
require_paths:
|
55
58
|
- lib
|
56
59
|
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
57
61
|
requirements:
|
58
62
|
- - ">="
|
59
63
|
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
60
65
|
segments:
|
61
66
|
- 0
|
62
67
|
version: "0"
|
63
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
64
70
|
requirements:
|
65
71
|
- - ">="
|
66
72
|
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
67
74
|
segments:
|
68
75
|
- 0
|
69
76
|
version: "0"
|
70
77
|
requirements: []
|
71
78
|
|
72
79
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.3.
|
80
|
+
rubygems_version: 1.3.7
|
74
81
|
signing_key:
|
75
82
|
specification_version: 3
|
76
83
|
summary: Compare a list of json documents to a simple logical language and returns matches as output
|