-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
71 lines (61 loc) · 1.61 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
import json
import sys
import jsonpath_ng
# ==================== This file contains testing code (mode: 'test')
# ==================== Test mode argument definition
def arguments(parser):
"""
Defines arguments specific to testing.
"""
parser.add_argument(
"--input",
type=str,
nargs="?",
default="",
help="path to the file to test",
)
parser.add_argument(
"--jpath",
type=str,
nargs="?",
default="",
required=True,
help="JSON path to test (XPATH like,"
+ " see https://goessner.net/articles/JsonPath/,"
+ ' example: "state.clusters[*].points")',
)
parser.add_argument(
"--stats",
action="store_true",
help="plots some statistics about tested matching",
)
# ==================== Test specific functionality
def test_filter(
input: str,
jpath: str,
stats: bool,
):
"""
Simply filters a file using the given path and prints the result.
"""
# Load json data and extract information
content = ""
if len(input) > 0:
with open(input) as jsonFile:
content = jsonFile.read()
else:
content = "".join(sys.stdin.readlines())
data = json.loads(content)
try:
expression = jsonpath_ng.parse(jpath)
except Exception:
print(f'error in path syntax: "{jpath}"')
return
# Find and print all matching results
matches = 0
for match in expression.find(data):
print(match.value)
matches += 1
if stats:
print("Statistics:")
print(f"{matches} matches")