Run python
(&self, figure_path: &S, show: bool)
| 1173 | |
| 1174 | /// Run python |
| 1175 | fn run<S>(&self, figure_path: &S, show: bool) -> Result<(), StrError> |
| 1176 | where |
| 1177 | S: AsRef<OsStr> + ?Sized, |
| 1178 | { |
| 1179 | // update commands |
| 1180 | let fig_path = Path::new(figure_path); |
| 1181 | let header = if show { |
| 1182 | "#### >>>> file generated by plotpy <<<< ####\n\n".to_string() + PYTHON_HEADER |
| 1183 | } else { |
| 1184 | "#### >>>> file generated by plotpy <<<< ####\n\nimport matplotlib\nmatplotlib.use('Agg')\n".to_string() |
| 1185 | + PYTHON_HEADER |
| 1186 | }; |
| 1187 | let mut txt = "plt.savefig(fn".to_string(); |
| 1188 | if self.save_tight { |
| 1189 | txt.push_str(",bbox_inches='tight',bbox_extra_artists=EXTRA_ARTISTS"); |
| 1190 | } |
| 1191 | if let Some(pad) = self.save_pad_inches { |
| 1192 | txt.push_str(format!(",pad_inches={}", pad).as_str()); |
| 1193 | } |
| 1194 | if let Some(transparent) = self.save_transparent { |
| 1195 | if transparent { |
| 1196 | txt.push_str(",transparent=True"); |
| 1197 | } |
| 1198 | } |
| 1199 | txt.push_str(")\n"); |
| 1200 | if show { |
| 1201 | txt.push_str("\nplt.show()\n"); |
| 1202 | }; |
| 1203 | let commands = format!( |
| 1204 | "{}{}\nfn=r'{}'\n{}", |
| 1205 | header, |
| 1206 | self.buffer, |
| 1207 | fig_path.to_string_lossy(), |
| 1208 | txt |
| 1209 | ); |
| 1210 | |
| 1211 | // call python |
| 1212 | let mut path = Path::new(figure_path).to_path_buf(); |
| 1213 | path.set_extension("py"); |
| 1214 | let output = call_python3(&self.python_exe, &commands, &path)?; |
| 1215 | |
| 1216 | // handle error => write log file |
| 1217 | if output != "" { |
| 1218 | let mut log_path = Path::new(figure_path).to_path_buf(); |
| 1219 | log_path.set_extension("log"); |
| 1220 | let mut log_file = File::create(log_path).map_err(|_| "cannot create log file")?; |
| 1221 | log_file |
| 1222 | .write_all(output.as_bytes()) |
| 1223 | .map_err(|_| "cannot write to log file")?; |
| 1224 | if self.show_errors { |
| 1225 | println!("{}", output); |
| 1226 | } |
| 1227 | return Err("python3 failed; please see the log file"); |
| 1228 | } |
| 1229 | Ok(()) |
| 1230 | } |
| 1231 | } |
| 1232 |
no test coverage detected