| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2018 Zheng Lei (realthunder) <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
****************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include "StringHasher.h" |
| 25 |
|
| 26 |
#include "StringHasherPy.h" |
| 27 |
#include "StringHasherPy.cpp" |
| 28 |
#include <Base/PyWrapParseTupleAndKeywords.h> |
| 29 |
|
| 30 |
using namespace App; |
| 31 |
|
| 32 |
// returns a string which represent the object e.g. when printed in python |
| 33 |
std::string StringHasherPy::representation() const |
| 34 |
{ |
| 35 |
std::ostringstream str; |
| 36 |
str << "<StringHasher at " << getStringHasherPtr() << ">"; |
| 37 |
return str.str(); |
| 38 |
} |
| 39 |
|
| 40 |
PyObject* StringHasherPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper |
| 41 |
{ |
| 42 |
return new StringHasherPy(new StringHasher); |
| 43 |
} |
| 44 |
|
| 45 |
// constructor method |
| 46 |
int StringHasherPy::PyInit(PyObject* args, PyObject* kwds) |
| 47 |
{ |
| 48 |
static const std::array<const char*, 1> kwlist {nullptr}; |
| 49 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", kwlist)) { |
| 50 |
return -1; |
| 51 |
} |
| 52 |
|
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
PyObject* StringHasherPy::isSame(PyObject* args) const |
| 57 |
{ |
| 58 |
PyObject* other; |
| 59 |
if (!PyArg_ParseTuple(args, "O!", &StringHasherPy::Type, &other)) { |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
|
| 63 |
auto otherHasher = static_cast<StringHasherPy*>(other)->getStringHasherPtr(); |
| 64 |
bool same = getStringHasherPtr() == otherHasher; |
| 65 |
|
| 66 |
return PyBool_FromLong(same ? 1 : 0); |
| 67 |
} |
| 68 |
|
| 69 |
PyObject* StringHasherPy::getID(PyObject* args) |
| 70 |
{ |
| 71 |
long id; |
| 72 |
int index = 0; |
| 73 |
if (PyArg_ParseTuple(args, "l|i", &id, &index)) { |
| 74 |
if (id > 0) { |
| 75 |
PY_TRY |
| 76 |
{ |
| 77 |
auto sid = getStringHasherPtr()->getID(id, index); |
| 78 |
if (!sid) { |
| 79 |
Py_Return; |
| 80 |
} |
| 81 |
|
| 82 |
return sid.getPyObject(); |
| 83 |
} |
| 84 |
PY_CATCH; |
| 85 |
} |
| 86 |
else { |
| 87 |
PyErr_SetString(PyExc_ValueError, "Id must be positive integer"); |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
PyErr_Clear(); |
| 93 |
PyObject* value = nullptr; |
| 94 |
PyObject* base64 = Py_False; |
| 95 |
if (PyArg_ParseTuple(args, "O!|O!", &PyUnicode_Type, &value, &PyBool_Type, &base64)) { |
| 96 |
PY_TRY |
| 97 |
{ |
| 98 |
std::string txt = PyUnicode_AsUTF8(value); |
| 99 |
QByteArray data; |
| 100 |
StringIDRef sid; |
| 101 |
if (PyObject_IsTrue(base64)) { |
| 102 |
data = QByteArray::fromBase64(QByteArray::fromRawData(txt.c_str(), txt.size())); |
| 103 |
sid = getStringHasherPtr()->getID(data, true); |
| 104 |
} |
| 105 |
else { |
| 106 |
sid = getStringHasherPtr()->getID(txt.c_str(), txt.size()); |
| 107 |
} |
| 108 |
|
| 109 |
return sid.getPyObject(); |
| 110 |
} |
| 111 |
PY_CATCH; |
| 112 |
} |
| 113 |
|
| 114 |
PyErr_SetString(PyExc_TypeError, |
| 115 |
"Positive integer and optional integer or " |
| 116 |
"string and optional boolean is required"); |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
Py::Long StringHasherPy::getCount() const |
| 121 |
{ |
| 122 |
return Py::Long(PyLong_FromSize_t(getStringHasherPtr()->count()), true); |
| 123 |
} |
| 124 |
|
| 125 |
Py::Long StringHasherPy::getSize() const |
| 126 |
{ |
| 127 |
return Py::Long(PyLong_FromSize_t(getStringHasherPtr()->size()), true); |
| 128 |
} |
| 129 |
|
| 130 |
Py::Boolean StringHasherPy::getSaveAll() const |
| 131 |
{ |
| 132 |
return {getStringHasherPtr()->getSaveAll()}; |
| 133 |
} |
| 134 |
|
| 135 |
void StringHasherPy::setSaveAll(Py::Boolean value) |
| 136 |
{ |
| 137 |
getStringHasherPtr()->setSaveAll(value); |
| 138 |
} |
| 139 |
|
| 140 |
Py::Long StringHasherPy::getThreshold() const |
| 141 |
{ |
| 142 |
return Py::Long(getStringHasherPtr()->getThreshold()); |
| 143 |
} |
| 144 |
|
| 145 |
void StringHasherPy::setThreshold(Py::Long value) |
| 146 |
{ |
| 147 |
getStringHasherPtr()->setThreshold(value); |
| 148 |
} |
| 149 |
|
| 150 |
Py::Dict StringHasherPy::getTable() const |
| 151 |
{ |
| 152 |
Py::Dict dict; |
| 153 |
for (const auto& v : getStringHasherPtr()->getIDMap()) { |
| 154 |
dict.setItem(Py::Long(v.first), Py::String(v.second.dataToText())); |
| 155 |
} |
| 156 |
|
| 157 |
return dict; |
| 158 |
} |
| 159 |
|
| 160 |
PyObject* StringHasherPy::getCustomAttributes(const char* /*attr*/) const |
| 161 |
{ |
| 162 |
return nullptr; |
| 163 |
} |
| 164 |
|
| 165 |
int StringHasherPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) |
| 166 |
{ |
| 167 |
return 0; |
| 168 |
} |