MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / sLoadFile

Method sLoadFile

src/App/ApplicationPy.cpp:287–340  ·  view source on GitHub ↗

NOLINTBEGIN(cppcoreguidelines-pro-type-*)

Source from the content-addressed store, hash-verified

285
286// NOLINTBEGIN(cppcoreguidelines-pro-type-*)
287PyObject* 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
342PyObject* ApplicationPy::sIsRestoring(PyObject* /*self*/, PyObject* args)
343{

Callers

nothing calls this directly

Calls 14

isFileMethod · 0.80
getImportModulesMethod · 0.80
runStringMethod · 0.80
StringClass · 0.70
existsMethod · 0.45
emptyMethod · 0.45
extensionMethod · 0.45
c_strMethod · 0.45
frontMethod · 0.45
reprMethod · 0.45
hasExtensionMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected