| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 The FreeCAD project association AISBL * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it under the terms of the * |
| 9 |
* GNU Lesser General Public License as published by the Free Software Foundation, either * |
| 10 |
* version 2.1 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* FreeCAD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * |
| 13 |
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public License along with FreeCAD. * |
| 17 |
* If not, see <https://www.gnu.org/licenses/>. * |
| 18 |
* * |
| 19 |
**************************************************************************************************/ |
| 20 |
|
| 21 |
#include <Python.h> |
| 22 |
#include <filesystem> |
| 23 |
#include <string> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
|
| 27 |
#include "Application.h" |
| 28 |
|
| 29 |
// inclusion of the generated files (generated out of ApplicationDirectories.pyi) |
| 30 |
#include <App/ApplicationDirectoriesPy.h> |
| 31 |
#include <App/ApplicationDirectoriesPy.cpp> // NOLINT |
| 32 |
|
| 33 |
namespace fs = std::filesystem; |
| 34 |
|
| 35 |
using namespace App; |
| 36 |
|
| 37 |
// NOLINTBEGIN(cppcoreguidelines-pro-type-vararg) |
| 38 |
|
| 39 |
// returns a string which represent the object e.g. when printed in python |
| 40 |
std::string ApplicationDirectoriesPy::representation() const |
| 41 |
{ |
| 42 |
return {"<ApplicationDirectoriesPy object>"}; |
| 43 |
} |
| 44 |
|
| 45 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::usingCurrentVersionConfig(PyObject* args) |
| 46 |
{ |
| 47 |
char *path = nullptr; |
| 48 |
if (!PyArg_ParseTuple(args, "s", &path)) { |
| 49 |
return nullptr; |
| 50 |
} |
| 51 |
bool result = App::Application::directories()->usingCurrentVersionConfig(Base::FileInfo::stringToPath(path)); |
| 52 |
return Py::new_reference_to(Py::Boolean(result)); |
| 53 |
} |
| 54 |
|
| 55 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::migrateAllPaths(PyObject* args) |
| 56 |
{ |
| 57 |
PyObject* object {nullptr}; |
| 58 |
if (!PyArg_ParseTuple(args, "O", &object)) { |
| 59 |
return nullptr; |
| 60 |
} |
| 61 |
|
| 62 |
if (PyTuple_Check(object) || PyList_Check(object)) { |
| 63 |
Py::Sequence seq(object); |
| 64 |
Py::Sequence::size_type size = seq.size(); |
| 65 |
std::vector<fs::path> paths; |
| 66 |
paths.resize(size); |
| 67 |
|
| 68 |
for (Py::Sequence::size_type i = 0; i < size; i++) { |
| 69 |
Py::Object item = seq[i]; |
| 70 |
|
| 71 |
if (!PyUnicode_Check(item.ptr())) { |
| 72 |
PyErr_SetString(PyExc_RuntimeError, "path was not a string"); |
| 73 |
return nullptr; |
| 74 |
} |
| 75 |
const char* s = PyUnicode_AsUTF8(item.ptr()); |
| 76 |
if (!s) { |
| 77 |
return nullptr; // PyUnicode_AsUTF8 sets an error |
| 78 |
} |
| 79 |
paths[i] = Base::FileInfo::stringToPath(s); |
| 80 |
} |
| 81 |
auto result = App::Application::directories()->migrateAllPaths(paths); |
| 82 |
Py::List failedList; |
| 83 |
for (const auto& path : result.failedPaths) { |
| 84 |
failedList.append(Py::String(Base::FileInfo::pathToString(path))); |
| 85 |
} |
| 86 |
return Py::new_reference_to(failedList); |
| 87 |
} |
| 88 |
Py_Return; |
| 89 |
} |
| 90 |
|
| 91 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::versionStringForPath(PyObject* args) |
| 92 |
{ |
| 93 |
int major {0}; |
| 94 |
int minor {0}; |
| 95 |
if (!PyArg_ParseTuple(args, "ii", &major, &minor)) { |
| 96 |
return nullptr; |
| 97 |
} |
| 98 |
auto result = App::ApplicationDirectories::versionStringForPath(major, minor); |
| 99 |
return Py::new_reference_to(Py::String(result)); |
| 100 |
} |
| 101 |
|
| 102 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::isVersionedPath(PyObject* args) |
| 103 |
{ |
| 104 |
char *path = nullptr; |
| 105 |
if (!PyArg_ParseTuple(args, "s", &path)) { |
| 106 |
return nullptr; |
| 107 |
} |
| 108 |
bool result = App::Application::directories()->isVersionedPath(Base::FileInfo::stringToPath(path)); |
| 109 |
return Py::new_reference_to(Py::Boolean(result)); |
| 110 |
} |
| 111 |
|
| 112 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::mostRecentAvailableConfigVersion(PyObject* args) |
| 113 |
{ |
| 114 |
char *path = nullptr; |
| 115 |
if (!PyArg_ParseTuple(args, "s", &path)) { |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
std::string result = App::Application::directories()->mostRecentAvailableConfigVersion(Base::FileInfo::stringToPath(path)); |
| 119 |
return Py::new_reference_to(Py::String(result)); |
| 120 |
} |
| 121 |
|
| 122 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::mostRecentConfigFromBase(PyObject* args) |
| 123 |
{ |
| 124 |
char *path = nullptr; |
| 125 |
if (!PyArg_ParseTuple(args, "s", &path)) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
fs::path result = App::Application::directories()->mostRecentConfigFromBase(Base::FileInfo::stringToPath(path)); |
| 129 |
return Py::new_reference_to(Py::String(Base::FileInfo::pathToString(result))); |
| 130 |
} |
| 131 |
|
| 132 |
[[maybe_unused]] PyObject* ApplicationDirectoriesPy::migrateConfig(PyObject* args) |
| 133 |
{ |
| 134 |
char *oldPath = nullptr; |
| 135 |
char *newPath = nullptr; |
| 136 |
if (!PyArg_ParseTuple(args, "ss", &oldPath, &newPath)) { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
auto result = App::ApplicationDirectories::migrateConfig( |
| 140 |
Base::FileInfo::stringToPath(oldPath), |
| 141 |
Base::FileInfo::stringToPath(newPath)); |
| 142 |
Py::List failedList; |
| 143 |
for (const auto& path : result.failedPaths) { |
| 144 |
failedList.append(Py::String(Base::FileInfo::pathToString(path))); |
| 145 |
} |
| 146 |
return Py::new_reference_to(failedList); |
| 147 |
} |
| 148 |
|
| 149 |
PyObject* ApplicationDirectoriesPy::getCustomAttributes([[maybe_unused]] const char* attr) const |
| 150 |
{ |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
int ApplicationDirectoriesPy::setCustomAttributes([[maybe_unused]] const char* attr, [[maybe_unused]] PyObject* obj) |
| 155 |
{ |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
// NOLINTEND(cppcoreguidelines-pro-type-vararg) |