| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2007 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <boost/iostreams/stream.hpp> |
| 26 |
|
| 27 |
#include "Persistence.h" |
| 28 |
#include "PyWrapParseTupleAndKeywords.h" |
| 29 |
#include "Writer.h" |
| 30 |
|
| 31 |
// generated out of Persistence.pyi |
| 32 |
#include "PersistencePy.h" |
| 33 |
#include "PersistencePy.cpp" |
| 34 |
|
| 35 |
using namespace Base; |
| 36 |
|
| 37 |
// returns a string which represent the object e.g. when printed in python |
| 38 |
std::string PersistencePy::representation() const |
| 39 |
{ |
| 40 |
return {"<persistence object>"}; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
Py::String PersistencePy::getContent() const |
| 45 |
{ |
| 46 |
StringWriter writer; |
| 47 |
// force all objects to write pure XML without files |
| 48 |
writer.setForceXML(true); |
| 49 |
getPersistencePtr()->Save(writer); |
| 50 |
|
| 51 |
return {writer.getString()}; |
| 52 |
} |
| 53 |
|
| 54 |
Py::Long PersistencePy::getMemSize() const |
| 55 |
{ |
| 56 |
return Py::Long((long)getPersistencePtr()->getMemSize()); |
| 57 |
} |
| 58 |
|
| 59 |
PyObject* PersistencePy::dumpContent(PyObject* args, PyObject* kwds) const |
| 60 |
{ |
| 61 |
int compression = 3; |
| 62 |
static const std::array<const char*, 2> kwds_def {"Compression", nullptr}; |
| 63 |
PyErr_Clear(); |
| 64 |
if (!Wrapped_ParseTupleAndKeywords(args, kwds, "|i", kwds_def, &compression)) { |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
// setup the stream. the in flag is needed to make "read" work |
| 69 |
std::stringstream stream( |
| 70 |
std::stringstream::out | std::stringstream::in | std::stringstream::binary |
| 71 |
); |
| 72 |
try { |
| 73 |
getPersistencePtr()->dumpToStream(stream, compression); |
| 74 |
} |
| 75 |
catch (NotImplementedError&) { |
| 76 |
PyErr_SetString( |
| 77 |
PyExc_NotImplementedError, |
| 78 |
"Dumping content of this object type is not implemented" |
| 79 |
); |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
catch (...) { |
| 83 |
PyErr_SetString(PyExc_IOError, "Unable to parse content into binary representation"); |
| 84 |
return nullptr; |
| 85 |
} |
| 86 |
|
| 87 |
// build the byte array with correct size |
| 88 |
if (!stream.seekp(0, std::stringstream::end)) { |
| 89 |
PyErr_SetString(PyExc_IOError, "Unable to find end of stream"); |
| 90 |
return nullptr; |
| 91 |
} |
| 92 |
|
| 93 |
std::stringstream::pos_type offset = stream.tellp(); |
| 94 |
if (!stream.seekg(0, std::stringstream::beg)) { |
| 95 |
PyErr_SetString(PyExc_IOError, "Unable to find begin of stream"); |
| 96 |
return nullptr; |
| 97 |
} |
| 98 |
|
| 99 |
PyObject* ba = PyByteArray_FromStringAndSize(nullptr, offset); |
| 100 |
|
| 101 |
// use the buffer protocol to access the underlying array and write into it |
| 102 |
Py_buffer buf = Py_buffer(); |
| 103 |
PyObject_GetBuffer(ba, &buf, PyBUF_WRITABLE); |
| 104 |
try { |
| 105 |
if (!stream.read((char*)buf.buf, offset)) { |
| 106 |
PyErr_SetString(PyExc_IOError, "Error copying data into byte array"); |
| 107 |
return nullptr; |
| 108 |
} |
| 109 |
PyBuffer_Release(&buf); |
| 110 |
} |
| 111 |
catch (...) { |
| 112 |
PyBuffer_Release(&buf); |
| 113 |
PyErr_SetString(PyExc_IOError, "Error copying data into byte array"); |
| 114 |
return nullptr; |
| 115 |
} |
| 116 |
|
| 117 |
return ba; |
| 118 |
} |
| 119 |
|
| 120 |
PyObject* PersistencePy::restoreContent(PyObject* args) |
| 121 |
{ |
| 122 |
PyObject* buffer = nullptr; |
| 123 |
if (!PyArg_ParseTuple(args, "O", &buffer)) { |
| 124 |
return nullptr; |
| 125 |
} |
| 126 |
|
| 127 |
// check if it really is a buffer |
| 128 |
if (!PyObject_CheckBuffer(buffer)) { |
| 129 |
PyErr_SetString(PyExc_TypeError, "Must be a buffer object"); |
| 130 |
return nullptr; |
| 131 |
} |
| 132 |
|
| 133 |
Py_buffer buf; |
| 134 |
if (PyObject_GetBuffer(buffer, &buf, PyBUF_SIMPLE) < 0) { |
| 135 |
return nullptr; |
| 136 |
} |
| 137 |
|
| 138 |
if (!PyBuffer_IsContiguous(&buf, 'C')) { |
| 139 |
PyErr_SetString(PyExc_TypeError, "Buffer must be contiguous"); |
| 140 |
return nullptr; |
| 141 |
} |
| 142 |
|
| 143 |
// check if it really is a buffer |
| 144 |
try { |
| 145 |
using Device = boost::iostreams::basic_array_source<char>; |
| 146 |
boost::iostreams::stream<Device> stream((char*)buf.buf, buf.len); |
| 147 |
getPersistencePtr()->restoreFromStream(stream); |
| 148 |
} |
| 149 |
catch (...) { |
| 150 |
PyErr_SetString(PyExc_IOError, "Unable to restore content"); |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
Py_Return; |
| 155 |
} |
| 156 |
|
| 157 |
PyObject* PersistencePy::getCustomAttributes(const char*) const |
| 158 |
{ |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
int PersistencePy::setCustomAttributes(const char*, PyObject*) |
| 163 |
{ |
| 164 |
return 0; |
| 165 |
} |