-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.py
187 lines (165 loc) · 7.09 KB
/
config.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
from trame.widgets import vuetify, markdown
from datetime import date
# functions to update GUI after user enters a new key-value pair in jsonData
from fileio import set_json_fileio
from initialization import set_json_initialization
from materials import set_json_materials
from numerics import set_json_numerics
from physics import set_json_physics
from solver import set_json_solver
from su2_json import updateBCDictListfromJSON
from su2_io import createjsonMarkers
from uicard import server
from logger import log
# for JSON schema validation
# imports for json schema
# import json
# import jsonschema
# Extract state and controller from the server
state, ctrl = server.state, server.controller
state.cofig_str = ""
state.config_desc = f"""%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% SU2 configuration file %
% Case description: %
% Author: %
% Date: {str(date.today()) } %
% SU2 version: %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
def add_new_property():
# prepare a valid schema for using the function given below
# validate_dict_against_schema()
if state.new_config_key==None or state.new_config_value==None:
return
value = state.new_config_value
# trying to convert new_config_value to a bool or float
try:
value = float(value)
except:
if value.lower() in ("yes", "true", "no", "false"):
value = value.lower() == "yes" or value.lower() == "true"
elif ',' in value or ' ' in value or ((value.startswith('(') and value.endswith(')')) or (value.startswith('[') and value.endswith(']')) or (value.startswith('{') and value.endswith('}'))) :
# Remove parentheses and split by comma
if value[0]=='(' or value[-1]==')' or value[0]=='[' or value[-1]==']' or value[0]=='{' or value[-1]=='}':
value = value[1:-1]
if ',' in value:
value = value.split(',')
else:
value = value.split()
# Convert each item to an appropriate type
value = [v.strip() for v in value]
value = [int(v) if v.isdigit() else v for v in value]
state.new_config_key = state.new_config_key.upper().strip()
state.jsonData[state.new_config_key] = value
state.dirty('jsonData')
update_config_str()
set_json_physics()
set_json_initialization()
set_json_numerics()
set_json_solver()
set_json_fileio()
set_json_materials()
updateBCDictListfromJSON()
log("info", f"Added {state.new_config_key} : {state.new_config_value}({type(state.new_config_value)})")
def update_config_str():
createjsonMarkers()
max_key_len = max(len(key) for key in state.jsonData.keys())
state.config_str = " \n".join([f"\t{key:{max_key_len}} : {value}" for key, value in state.jsonData.items()])
@state.change("key")
def update_new_config_key(key, **kwargs):
state.new_config_key = key
@state.change("value")
def update_new_config_value(value, **kwargs):
state.new_config_value = value
@state.change("config_desc")
def update_config_desc(config_desc, **kwargs):
new_config_desc = ""
for line in config_desc.splitlines():
if not line.startswith("%"):
line = '%' + line
new_config_desc += line + "\n"
state.config_desc = new_config_desc
#################### JSON SCHEMA VALIDATION ####################
# Validate the dictionary against the JSON schema
# this will be used later when the JSON schema is ready
# def validate_dict_against_schema():
# log("info", state.jsonData)
# # Load the JSON schema from the file
# try:
# with open("./user/JsonSchema.json", 'r') as file:
# schema = json.load(file)
# except FileNotFoundError:
# log("error", "Schema file not found.")
# return False
# except json.JSONDecodeError:
# log("error", "Invalid JSON schema file.")
# return False
# # Validate the dictionary against the schema
# try:
# validate(instance=state.jsonData, schema=schema)
# log("info", "The dictionary is valid.")
# return True
# except jsonschema.exceptions.ValidationError as ve:
# log("error", f"Validation error: {ve.message}")
# return False
# except jsonschema.exceptions.SchemaError as se:
# log("error", f"Schema error: {se.message}")
# return False
############### CONFIG TAB GUI ####################
def config_tab():
with vuetify.VTabItem(
value=(2,), style="width: 100%; height: 100%; padding: 3rem"
):
markdown.Markdown(
content = ('config_tab_heading', "Add Properties Manually \n"),
style = "font-weight:bolder;background-color: white; color:black; font-size: larger; "
)
with vuetify.VRow(
style= "margin:1rem;"
):
with vuetify.VCol(
style = "width:30%"):
vuetify.VTextField(
label="Key",
v_model = ("key", None),
outlined=True,
dense=True,
hide_details=True,
)
with vuetify.VCol(
style = "width:70%"):
vuetify.VTextField(
label="Value",
v_model = ("value", None),
outlined=True,
dense=True,
hide_details=True,
)
vuetify.VBtn("Add",click=(add_new_property),
style = "background-color: #3a76de; margin-left: 2rem; color: white; margin-bottom: 1rem;"
)
markdown.Markdown(
content = ('config_tab_desc_heading', "Edit description for Configuration file: \n"),
style = "font-weight:bolder;background-color: white; color:black;"
)
vuetify.VTextarea(
v_model=("config_desc", ""),
style="font-family: monospace; margin-bottom: 2rem;",
hide_details=True,
)
with vuetify.VRow(
style = "justify-content: space-between; margin:.5rem;"
):
markdown.Markdown(
content = ('config_data_heading', "Configuration File Data \n"),
style = "font-weight:bolder;background-color: white; color:black;"
)
vuetify.VBtn("Reload",click=(update_config_str),
style = "background-color: #3a76de; margin-left: 2rem; color: white;"
)
markdown.Markdown(
content = ('config_str', state.confing_str),
style = "background-color: white;"
)