NOLINTBEGIN(cppcoreguidelines-pro-type-*)
| 285 | |
| 286 | // NOLINTBEGIN(cppcoreguidelines-pro-type-*) |
| 287 | PyObject* ApplicationPy::sLoadFile(PyObject* /*self*/, PyObject* args) |
| 288 | { |
| 289 | const char* path = ""; |
| 290 | const char* doc = ""; |
| 291 | const char* mod = ""; |
| 292 | if (!PyArg_ParseTuple(args, "s|ss", &path, &doc, &mod)) { |
| 293 | return nullptr; |
| 294 | } |
| 295 | try { |
| 296 | Base::FileInfo fi(path); |
| 297 | if (!fi.isFile() || !fi.exists()) { |
| 298 | PyErr_Format(PyExc_IOError, "File %s doesn't exist.", path); |
| 299 | return nullptr; |
| 300 | } |
| 301 | |
| 302 | std::string module = mod; |
| 303 | if (module.empty()) { |
| 304 | std::string ext = fi.extension(); |
| 305 | std::vector<std::string> modules = GetApplication().getImportModules(ext.c_str()); |
| 306 | if (modules.empty()) { |
| 307 | PyErr_Format(PyExc_IOError, "Filetype %s is not supported.", ext.c_str()); |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | module = modules.front(); |
| 312 | } |
| 313 | |
| 314 | // path and doc could contain characters that need escaping, such as quote signs |
| 315 | // therefore use their repr() in the Python code string |
| 316 | auto pathRepr = static_cast<std::string>(Py::String(path).repr()); |
| 317 | auto docRepr = static_cast<std::string>(Py::String(doc).repr()); |
| 318 | |
| 319 | std::stringstream str; |
| 320 | str << "import " << module << std::endl; |
| 321 | if (fi.hasExtension("FCStd")) { |
| 322 | str << module << ".openDocument(" << pathRepr << ")" << std::endl; |
| 323 | } |
| 324 | else { |
| 325 | str << module << ".insert(" << pathRepr << "," << docRepr << ")" << std::endl; |
| 326 | } |
| 327 | |
| 328 | Base::Interpreter().runString(str.str().c_str()); |
| 329 | Py_Return; |
| 330 | } |
| 331 | catch (const Base::Exception& e) { |
| 332 | e.setPyException(); |
| 333 | return nullptr; |
| 334 | } |
| 335 | catch (const std::exception& e) { |
| 336 | // might be subclass from zipios |
| 337 | PyErr_Format(PyExc_IOError, "Invalid project file %s: %s", path, e.what()); |
| 338 | return nullptr; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | PyObject* ApplicationPy::sIsRestoring(PyObject* /*self*/, PyObject* args) |
| 343 | { |
nothing calls this directly
no test coverage detected