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