1
/****************************************************************************
2
* Copyright (c) 2018 Zheng Lei (realthunder) <realthunder.dev@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
****************************************************************************/
23
#include "PreCompiled.h"
25
#include "StringHasher.h"
27
#include "StringHasherPy.h"
28
#include "StringHasherPy.cpp"
29
#include <Base/PyWrapParseTupleAndKeywords.h>
33
// returns a string which represent the object e.g. when printed in python
34
std::string StringHasherPy::representation() const
36
std::ostringstream str;
37
str << "<StringHasher at " << getStringHasherPtr() << ">";
41
PyObject *StringHasherPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
43
return new StringHasherPy(new StringHasher);
47
int StringHasherPy::PyInit(PyObject* args, PyObject* kwds)
49
static const std::array<const char *, 1> kwlist {nullptr};
50
if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", kwlist)) {
58
PyObject* StringHasherPy::isSame(PyObject *args)
61
if (!PyArg_ParseTuple(args, "O!", &StringHasherPy::Type, &other)) {
65
auto otherHasher = static_cast<StringHasherPy*>(other)->getStringHasherPtr();
66
bool same = getStringHasherPtr() == otherHasher;
68
return PyBool_FromLong(same ? 1 : 0);
71
PyObject* StringHasherPy::getID(PyObject *args)
75
if (PyArg_ParseTuple(args, "l|i", &id, &index)) {
78
auto sid = getStringHasherPtr()->getID(id, index);
83
return sid.getPyObject();
88
PyErr_SetString(PyExc_ValueError, "Id must be positive integer");
94
PyObject *value = nullptr;
95
PyObject *base64 = Py_False;
96
if (PyArg_ParseTuple(args, "O!|O!", &PyUnicode_Type, &value, &PyBool_Type, &base64)) {
98
std::string txt = PyUnicode_AsUTF8(value);
101
if (PyObject_IsTrue(base64)) {
102
data = QByteArray::fromBase64(QByteArray::fromRawData(txt.c_str(),txt.size()));
103
sid = getStringHasherPtr()->getID(data,true);
106
sid = getStringHasherPtr()->getID(txt.c_str(),txt.size());
109
return sid.getPyObject();
114
PyErr_SetString(PyExc_TypeError, "Positive integer and optional integer or "
115
"string and optional boolean is required");
119
Py::Long StringHasherPy::getCount() const
121
return Py::Long(PyLong_FromSize_t(getStringHasherPtr()->count()), true);
124
Py::Long StringHasherPy::getSize() const
126
return Py::Long(PyLong_FromSize_t(getStringHasherPtr()->size()), true);
129
Py::Boolean StringHasherPy::getSaveAll() const
131
return {getStringHasherPtr()->getSaveAll()};
134
void StringHasherPy::setSaveAll(Py::Boolean value)
136
getStringHasherPtr()->setSaveAll(value);
139
Py::Long StringHasherPy::getThreshold() const
141
return Py::Long(getStringHasherPtr()->getThreshold());
144
void StringHasherPy::setThreshold(Py::Long value)
146
getStringHasherPtr()->setThreshold(value);
149
Py::Dict StringHasherPy::getTable() const
152
for (const auto &v : getStringHasherPtr()->getIDMap()) {
153
dict.setItem(Py::Long(v.first), Py::String(v.second.dataToText()));
159
PyObject *StringHasherPy::getCustomAttributes(const char* /*attr*/) const
164
int StringHasherPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)