forked from devicetree-org/lopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlopper_rest.py
100 lines (79 loc) · 2.65 KB
/
lopper_rest.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#/*
# * Copyright (c) 2020 Xilinx Inc. All rights reserved.
# *
# * Author:
# * Bruce Ashfield <bruce.ashfield@xilinx.com>
# *
# * SPDX-License-Identifier: BSD-3-Clause
# */
from flask import Flask
from flask import jsonify
from flask import send_file
from flask_restful import Resource, Api, reqparse
import pandas as pd
import ast
import tempfile
import json
from collections import OrderedDict
import lopper
app = Flask(__name__)
api = Api(app)
sdt = None
class Domains(Resource):
def get(self):
try:
domains = sdt.tree.nodes( "/domains/[^/]*$" )
except:
domains = []
domain_names = ""
if domains:
# this just dumps the domain names comma separated. We loop
# up to the last element, adding the ",". We then add the
# last element after the loop, no "," .. we could also just
# remove the trailing "," after the loop, but the list slicing
# seems a bit faster.
for d in domains[:-1]:
domain_names += d.abs_path + ","
domain_names = domain_names + domains[-1].abs_path
return domain_names, 200
class Tree(Resource):
def get(self):
fpp = tempfile.NamedTemporaryFile( delete=True )
if sdt:
lp = lopper.LopperTreePrinter( sdt.tree.fdt, False, fpp.name )
lp.exec()
with open(fpp.name, 'r') as f:
filecontents = f.read()
# json = json.dumps(filecontents, ensure_ascii=False)
return filecontents, 200
else:
return "", 204
class Nodes(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('path', required=True)
parser.add_argument('details', required=False)
args = parser.parse_args()
try:
details = args['details']
if details == "True":
details = True
else:
details = False
except:
details = False
node_data = OrderedDict()
node_list = sdt.tree.nodes( args['path'] )
if not details:
for n in node_list:
node_data[n.abs_path] = None
else:
for n in node_list:
prop_dict = OrderedDict()
for p in n.__props__:
prop_dict[p] = n.__props__[p].string_val
node_data[n.abs_path] = prop_dict
return node_data, 200
api.add_resource(Domains, '/domains') # '/domains' is an entry point
api.add_resource(Tree, '/tree') # '/tree' is an entry point
api.add_resource(Nodes, '/nodes') # '/tree' is an entry point