|
12 | 12 | import yaml |
13 | 13 | from fourcipp import CONFIG |
14 | 14 | from fourcipp.fourc_input import FourCInput, ValidationError |
| 15 | +from fourcipp.utils.yaml_io import load_yaml |
15 | 16 | from trame.app import get_server |
16 | 17 | from trame.decorators import TrameApp, change, controller |
17 | 18 |
|
@@ -1011,41 +1012,58 @@ def init_funct_state_and_server_vars(self): |
1011 | 1012 | 6 # precision for the user input of the values defined above: x, y, z and t_max |
1012 | 1013 | ) |
1013 | 1014 |
|
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. |
1018 | 1017 |
|
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( |
1027 | 1024 | self._server_vars["fourc_yaml_file_dir"], |
1028 | | - exo_file_name, |
| 1025 | + include_name, |
1029 | 1026 | ) |
1030 | | - exo_temp_path = Path( |
| 1027 | + include_temp_path = Path( |
1031 | 1028 | self._server_vars["temp_dir_object"].name, |
1032 | | - exo_file_name, |
| 1029 | + include_name, |
1033 | 1030 | ) |
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: |
1037 | 1034 | fw.write(fr.read()) |
1038 | | - elif not exo_temp_path.is_file(): |
| 1035 | + elif not include_temp_path.is_file(): |
1039 | 1036 | included_files.append( |
1040 | 1037 | { |
1041 | | - "name": exo_file_name, |
| 1038 | + "name": include_name, |
1042 | 1039 | "uploaded": False, |
1043 | 1040 | "error": None, |
1044 | 1041 | "content": None, |
1045 | 1042 | } |
1046 | 1043 | ) |
1047 | | - |
1048 | 1044 | 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 | + |
1049 | 1067 | if self.state.included_files: |
1050 | 1068 | self.state.include_upload_open = True |
1051 | 1069 | else: |
@@ -1168,6 +1186,15 @@ def on_upload_include_file(self, uploaded_file, index, **kwargs): |
1168 | 1186 | """ |
1169 | 1187 | self.state.included_files[index]["content"] = uploaded_file |
1170 | 1188 |
|
| 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 | + |
1171 | 1198 | try: |
1172 | 1199 | if self.state.included_files[index]["name"] != uploaded_file["name"]: |
1173 | 1200 | self.state.included_files[index]["error"] = ( |
|
0 commit comments