forked from immodded/PyNestServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (58 loc) · 2.53 KB
/
main.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
import json
import os
import sys
from flask import Flask, jsonify
app = Flask(__name__)
def load_data_from_file(file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
return data
except FileNotFoundError:
return None
def get_value_at_path(data_dict, keys):
try:
for key in keys:
if "::" in key:
operator, threshold = key.split('::')
if "from:" in operator or "to:" in threshold:
from_value = int(operator.split("from:")[1]) if "from:" in operator else None
to_value = int(threshold.split("to:")[1]) + 1 if "to:" in threshold else None
data_dict = data_dict[from_value:to_value]
elif "et:" in key:
key_name, Qfilter = operator, threshold.split("et:")[1]
data_dict = [item for item in data_dict if str(item.get(key_name)) == Qfilter]
elif "gt:" in key:
key_name, threshold_value = operator, int(threshold.split("gt:")[1])
data_dict = [item for item in data_dict if item.get(key_name) > threshold_value]
elif "lt:" in key:
key_name, threshold_value = operator, int(threshold.split("lt:")[1])
data_dict = [item for item in data_dict if item.get(key_name) < threshold_value]
else:
data_dict = data_dict[int(key)]
except (KeyError, IndexError, ValueError) as e:
return None
return data_dict
@app.route('/', methods=['GET'])
@app.route('/<path:keys>', methods=['GET'])
def nested_data(keys=""):
if keys == "":
return jsonify(data)
try:
keys_list = keys.split('/')
data_result = get_value_at_path(data, keys_list)
return jsonify(data_result) if data_result is not None else jsonify({"error": "Invalid path, index out of range, or remove trailing slash."}), 200
except ValueError as e:
return jsonify({"error": str(e)}), 400
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python main.py <json_file_path> [port]")
sys.exit(1)
json_file_path = sys.argv[1]
port = int(sys.argv[2]) if len(sys.argv) > 2 else 7070
absolute_path = os.path.abspath(os.path.join(os.path.dirname(__file__), json_file_path))
data = load_data_from_file(absolute_path)
if data is None:
print(f"Error: Unable to load data from file: {absolute_path}")
sys.exit(1)
app.run(port=port, debug=False)