FreeCAD

Форк
0
/
StringHasherPyImp.cpp 
167 строк · 5.2 Кб
1
/****************************************************************************
2
*   Copyright (c) 2018 Zheng Lei (realthunder) <realthunder.dev@gmail.com> *
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
#include "PreCompiled.h"
24

25
#include "StringHasher.h"
26

27
#include "StringHasherPy.h"
28
#include "StringHasherPy.cpp"
29
#include <Base/PyWrapParseTupleAndKeywords.h>
30

31
using namespace App;
32

33
// returns a string which represent the object e.g. when printed in python
34
std::string StringHasherPy::representation() const
35
{
36
    std::ostringstream str;
37
    str << "<StringHasher at " << getStringHasherPtr() << ">";
38
    return str.str();
39
}
40

41
PyObject *StringHasherPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
42
{
43
    return new StringHasherPy(new StringHasher);
44
}
45

46
// constructor method
47
int StringHasherPy::PyInit(PyObject* args, PyObject* kwds)
48
{
49
    static const std::array<const char *, 1> kwlist {nullptr};
50
    if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", kwlist)) {
51
        return -1;
52
    }
53

54
    return 0;
55
}
56

57

58
PyObject* StringHasherPy::isSame(PyObject *args)
59
{
60
    PyObject *other;
61
    if (!PyArg_ParseTuple(args, "O!", &StringHasherPy::Type, &other)) {
62
        return nullptr;
63
    }
64

65
    auto otherHasher = static_cast<StringHasherPy*>(other)->getStringHasherPtr();
66
    bool same = getStringHasherPtr() == otherHasher;
67

68
    return  PyBool_FromLong(same ? 1 : 0);
69
}
70

71
PyObject* StringHasherPy::getID(PyObject *args)
72
{
73
    long id;
74
    int index = 0;
75
    if (PyArg_ParseTuple(args, "l|i", &id, &index)) {
76
        if (id > 0) {
77
            PY_TRY {
78
                auto sid = getStringHasherPtr()->getID(id, index);
79
                if (!sid) {
80
                    Py_Return;
81
                }
82

83
                return sid.getPyObject();
84
            }
85
            PY_CATCH;
86
        }
87
        else {
88
            PyErr_SetString(PyExc_ValueError, "Id must be positive integer");
89
            return nullptr;
90
        }
91
    }
92

93
    PyErr_Clear();
94
    PyObject *value = nullptr;
95
    PyObject *base64 = Py_False;
96
    if (PyArg_ParseTuple(args, "O!|O!", &PyUnicode_Type, &value, &PyBool_Type, &base64)) {
97
        PY_TRY {
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, "Positive integer and optional integer or "
115
        "string and optional boolean is required");
116
    return nullptr;
117
}
118

119
Py::Long StringHasherPy::getCount() const
120
{
121
    return Py::Long(PyLong_FromSize_t(getStringHasherPtr()->count()), true);
122
}
123

124
Py::Long StringHasherPy::getSize() const
125
{
126
    return Py::Long(PyLong_FromSize_t(getStringHasherPtr()->size()), true);
127
}
128

129
Py::Boolean StringHasherPy::getSaveAll() const
130
{
131
    return {getStringHasherPtr()->getSaveAll()};
132
}
133

134
void StringHasherPy::setSaveAll(Py::Boolean value)
135
{
136
    getStringHasherPtr()->setSaveAll(value);
137
}
138

139
Py::Long StringHasherPy::getThreshold() const
140
{
141
    return Py::Long(getStringHasherPtr()->getThreshold());
142
}
143

144
void StringHasherPy::setThreshold(Py::Long value)
145
{
146
    getStringHasherPtr()->setThreshold(value);
147
}
148

149
Py::Dict StringHasherPy::getTable() const
150
{
151
    Py::Dict dict;
152
    for (const auto &v : getStringHasherPtr()->getIDMap()) {
153
        dict.setItem(Py::Long(v.first), Py::String(v.second.dataToText()));
154
    }
155

156
    return dict;
157
}
158

159
PyObject *StringHasherPy::getCustomAttributes(const char* /*attr*/) const
160
{
161
    return nullptr;
162
}
163

164
int StringHasherPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
165
{
166
    return 0;
167
}
168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.