Skip to content

Commit 918ac2e

Browse files
committed
Add yaml includes recursive
1 parent 871db10 commit 918ac2e

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

src/fourc_webviewer/fourc_webserver.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import yaml
1313
from fourcipp import CONFIG
1414
from fourcipp.fourc_input import FourCInput, ValidationError
15+
from fourcipp.utils.yaml_io import load_yaml
1516
from trame.app import get_server
1617
from trame.decorators import TrameApp, change, controller
1718

@@ -1011,41 +1012,58 @@ def init_funct_state_and_server_vars(self):
10111012
6 # precision for the user input of the values defined above: x, y, z and t_max
10121013
)
10131014

1014-
def request_included_files(self):
1015-
"""Requests the included files from the user by opening a the include
1016-
files dialog and setting up the state variable accordingly."""
1017-
included_files = []
1015+
def append_include_files(self, file_paths):
1016+
"""Appends list of files to the included files input field.
10181017
1019-
exo_file_name = Path(
1020-
self._server_vars.get("fourc_yaml_content")
1021-
.sections.get("STRUCTURE GEOMETRY", {})
1022-
.get("FILE")
1023-
or ""
1024-
).name
1025-
if exo_file_name:
1026-
exo_file_server = Path(
1018+
They will be uploaded before the user can edit or view the file.
1019+
"""
1020+
yaml_include_names = [Path(file_path).name for file_path in file_paths]
1021+
included_files = copy.deepcopy(self.state.included_files)
1022+
for include_name in yaml_include_names:
1023+
include_file_server = Path(
10271024
self._server_vars["fourc_yaml_file_dir"],
1028-
exo_file_name,
1025+
include_name,
10291026
)
1030-
exo_temp_path = Path(
1027+
include_temp_path = Path(
10311028
self._server_vars["temp_dir_object"].name,
1032-
exo_file_name,
1029+
include_name,
10331030
)
1034-
if exo_file_server.is_file():
1035-
with open(exo_file_server, "rb") as fr:
1036-
with open(exo_temp_path, "wb") as fw:
1031+
if include_file_server.is_file():
1032+
with open(include_file_server, "rb") as fr:
1033+
with open(include_temp_path, "wb") as fw:
10371034
fw.write(fr.read())
1038-
elif not exo_temp_path.is_file():
1035+
elif not include_temp_path.is_file():
10391036
included_files.append(
10401037
{
1041-
"name": exo_file_name,
1038+
"name": include_name,
10421039
"uploaded": False,
10431040
"error": None,
10441041
"content": None,
10451042
}
10461043
)
1047-
10481044
self.state.included_files = included_files
1045+
1046+
def request_included_files(self):
1047+
"""Requests the included files from the user by opening a the include
1048+
files dialog and setting up the state variable accordingly."""
1049+
1050+
self.append_include_files(
1051+
[
1052+
self._server_vars.get("fourc_yaml_content")
1053+
.sections.get("STRUCTURE GEOMETRY", {})
1054+
.get("FILE")
1055+
or ""
1056+
]
1057+
)
1058+
# add yaml includes
1059+
yaml_include_names = [
1060+
Path(file_path).name
1061+
for file_path in self._server_vars.get("fourc_yaml_content").sections.get(
1062+
"INCLUDES", []
1063+
)
1064+
]
1065+
self.append_include_files(yaml_include_names)
1066+
10491067
if self.state.included_files:
10501068
self.state.include_upload_open = True
10511069
else:
@@ -1168,6 +1186,15 @@ def on_upload_include_file(self, uploaded_file, index, **kwargs):
11681186
"""
11691187
self.state.included_files[index]["content"] = uploaded_file
11701188

1189+
if uploaded_file["name"].split(".")[-1] in ["yaml", "yml"]:
1190+
content = (
1191+
load_yaml(uploaded_file.get("content", {}).get("content", "")) or {}
1192+
)
1193+
yaml_include_names = [
1194+
Path(file_path).name for file_path in content.get("INCLUDES", [])
1195+
]
1196+
self.append_include_files(yaml_include_names)
1197+
11711198
try:
11721199
if self.state.included_files[index]["name"] != uploaded_file["name"]:
11731200
self.state.included_files[index]["error"] = (

0 commit comments

Comments
 (0)