forked from SvenSommer/structure_comparer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
283 lines (261 loc) · 7.54 KB
/
server.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import argparse
from pathlib import Path
from flask import Flask, jsonify, request
from flask_swagger import swagger
from flask_cors import CORS
from structure_comparer.serve import (
get_mapping_fields_int,
get_mapping_int,
get_mappings_int,
init_project,
post_mapping_field_int,
)
def create_app(project_dir: Path):
# create the app
app = Flask(__name__)
CORS(app, origins="http://localhost:4200")
# project config
project = init_project(project_dir)
setattr(app, "project", project)
@app.route("/", methods=["GET"])
def hello_world():
return "<p>Hello, World!</p>"
@app.route("/mappings", methods=["GET"])
def get_mappings():
"""
Get the available mappings
Returns a list with all mappings, including the name and the url to access it.
---
produces:
- application/json
definitions:
- schema:
id: OverviewMapping
type: object
required:
- id
- name
- url
properties:
id:
type: string
name:
type: string
url:
type: string
responses:
200:
description: Available mappings
schema:
required:
- mappings
properties:
mappings:
type: array
items:
$ref: "#/definitions/OverviewMapping"
"""
return get_mappings_int(app.project)
@app.route("/mapping/<id>", methods=["GET"])
def get_mapping(id: str):
"""
Get a specific mapping
Returns the mapping with the given id. This includes all details like classifications, presences in profiles, etc.
---
produces:
- application/json
definitions:
- schema:
id: MappingFieldProfile
type: object
required:
- name
- present
properties:
name:
type: string
present:
type: boolean
- schema:
id: MappingField
required:
- id
- name
- classification
- profiles
- remark
type: object
properties:
id:
type: string
name:
type: string
classification:
type: string
extension:
type: string
extra:
type: string
profiles:
type: array
items:
$ref: "#/definitions/MappingFieldProfile"
remark:
type: string
- schema:
id: Mapping
type: object
required:
- id
- name
- source_profiles
- target_profile
- fields
properties:
fields:
type: array
items:
$ref: "#/definitions/MappingField"
id:
type: string
name:
type: string
source_profiles:
type: array
items:
type: string
target_profile:
type: string
parameters:
- in: path
name: id
type: string
required: true
description: The id of the mapping
responses:
200:
description: The mapping with the given id
schema:
$ref: "#/definitions/Mapping"
404:
description: Mapping not found
"""
mapping = get_mapping_int(app.project, id)
if mapping:
return mapping
else:
return "", 404
@app.route("/mapping/<id>/fields", methods=["GET"])
def get_mapping_fields(id: str):
"""
Get the fields of a mapping
Returns a brief list of the fields
---
produces:
- application/json
definitions:
- schema:
id: MappingFieldShort
type: object
reuqired:
- id
- name
properties:
id:
type: string
name:
type: string
- schema:
id: MappingShort
type: object
required:
- id
- fields
properties:
fields:
type: array
items:
$ref: "#/definitions/MappingFieldShort"
id:
type: string
parameters:
- in: path
name: id
type: string
required: true
description: The id of the mapping
responses:
200:
description: The fields of the mapping
schema:
$ref: "#/definitions/MappingShort"
404:
description: Mapping not found
"""
fields = get_mapping_fields_int(app.project, id)
if fields:
return fields
else:
return "", 404
@app.route("/mapping/<mapping_id>/field/<field_id>", methods=["POST"])
def post_mapping_field(mapping_id: str, field_id: str):
"""
Post a manual entry for a field
Overrides the default classification of a field. A field can target a field with the same name or a different one to map the field, can point to 'null' to ignore it or can be set to a fixed value
---
consumes:
- application/json
parameters:
- in: path
name: mapping_id
type: string
required: true
description: The id of the mapping
- in: path
name: field_id
type: string
required: true
description: The id of the field
- in: body
name: body
schema:
properties:
target:
type: string
description: The target field
fixed:
type: string
description: Fixed value to assign to the field
responses:
200:
description: The field was updated
404:
description: Mapping or field not found
"""
result = post_mapping_field_int(
app.project, mapping_id, field_id, request.get_json()
)
if result is None:
return "", 404
return "", 200
@app.route("/spec", methods=["GET"])
def spec():
swag = swagger(app)
swag["info"]["version"] = "1.0"
swag["info"]["title"] = "Structure Comparer"
return jsonify(swag)
return app
def get_args():
parser = argparse.ArgumentParser(
description="Compare profiles and generate mapping"
)
parser.add_argument(
"--project-dir",
type=Path,
help="The project directory containing the profiles and config",
)
return parser.parse_args()
if __name__ == "__main__":
args = get_args()
app = create_app(project_dir=args.project_dir)
app.run()