FreeCAD

Форк
0
/
PythonConsolePy.cpp 
317 строк · 9.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 "PythonConsolePy.h"
26
#include "PythonConsole.h"
27

28

29
using namespace Gui;
30

31
void PythonStdout::init_type()
32
{
33
    behaviors().name("PythonStdout");
34
    behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
35
    // you must have overwritten the virtual functions
36
    behaviors().supportRepr();
37
    add_varargs_method("write",&PythonStdout::write,"write()");
38
    add_varargs_method("flush",&PythonStdout::flush,"flush()");
39
    add_noargs_method("isatty",&PythonStdout::isatty,"isatty()");
40
}
41

42
PythonStdout::PythonStdout(PythonConsole *pc)
43
  : pyConsole(pc)
44
{
45
}
46

47
PythonStdout::~PythonStdout() = default;
48

49
Py::Object PythonStdout::getattr(const char *name)
50
{
51
    if (strcmp(name, "softspace") == 0) {
52
        int i=0;
53
        return Py::Int(i);
54
    }
55
    return getattr_methods(name);
56
}
57

58
Py::Object PythonStdout::repr()
59
{
60
    std::string s;
61
    std::ostringstream s_out;
62
    s_out << "PythonStdout";
63
    return Py::String(s_out.str());
64
}
65

66
Py::Object PythonStdout::write(const Py::Tuple& args)
67
{
68
    PyObject* output;
69
    if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
70
        throw Py::TypeError("PythonStdout.write() takes exactly one argument of type str");
71

72
    PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
73
    if (unicode) {
74
        const char* string = PyBytes_AsString(unicode);
75
        int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
76
        pyConsole->insertPythonOutput(QString::fromUtf8(string, maxlen));
77
        Py_DECREF(unicode);
78
    }
79

80
    return Py::None();
81
}
82

83
Py::Object PythonStdout::flush(const Py::Tuple&)
84
{
85
    return Py::None();
86
}
87

88
Py::Object PythonStdout::isatty()
89
{
90
    return Py::False();
91
}
92

93
// -------------------------------------------------------------------------
94

95
void PythonStderr::init_type()
96
{
97
    behaviors().name("PythonStderr");
98
    behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
99
    // you must have overwritten the virtual functions
100
    behaviors().supportRepr();
101
    add_varargs_method("write",&PythonStderr::write,"write()");
102
    add_varargs_method("flush",&PythonStderr::flush,"flush()");
103
    add_noargs_method("isatty",&PythonStderr::isatty,"isatty()");
104
}
105

106
PythonStderr::PythonStderr(PythonConsole *pc)
107
  : pyConsole(pc)
108
{
109
}
110

111
PythonStderr::~PythonStderr() = default;
112

113
Py::Object PythonStderr::getattr(const char *name)
114
{
115
    if (strcmp(name, "softspace") == 0) {
116
        int i=0;
117
        return Py::Int(i);
118
    }
119
    return getattr_methods(name);
120
}
121

122
Py::Object PythonStderr::repr()
123
{
124
    std::string s;
125
    std::ostringstream s_out;
126
    s_out << "PythonStderr";
127
    return Py::String(s_out.str());
128
}
129

130
Py::Object PythonStderr::write(const Py::Tuple& args)
131
{
132
    PyObject* output;
133
    if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
134
        throw Py::TypeError("PythonStderr.write() takes exactly one argument of type str");
135

136
    PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
137
    if (unicode) {
138
        const char* string = PyBytes_AsString(unicode);
139
        int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
140
        pyConsole->insertPythonError(QString::fromUtf8(string, maxlen));
141
        Py_DECREF(unicode);
142
    }
143

144
    return Py::None();
145
}
146

147
Py::Object PythonStderr::flush(const Py::Tuple&)
148
{
149
    return Py::None();
150
}
151

152
Py::Object PythonStderr::isatty()
153
{
154
    return Py::False();
155
}
156

157
// -------------------------------------------------------------------------
158

159
void OutputStdout::init_type()
160
{
161
    behaviors().name("OutputStdout");
162
    behaviors().doc("Redirection of stdout to FreeCAD's report view");
163
    // you must have overwritten the virtual functions
164
    behaviors().supportRepr();
165
    add_varargs_method("write",&OutputStdout::write,"write()");
166
    add_varargs_method("flush",&OutputStdout::flush,"flush()");
167
    add_noargs_method("isatty",&OutputStdout::isatty,"isatty()");
168
}
169

170
OutputStdout::OutputStdout() = default;
171

172
OutputStdout::~OutputStdout() = default;
173

174
Py::Object OutputStdout::getattr(const char *name)
175
{
176
    if (strcmp(name, "softspace") == 0) {
177
        int i=0;
178
        return Py::Int(i);
179
    }
180
    return getattr_methods(name);
181
}
182

183
Py::Object OutputStdout::repr()
184
{
185
    std::string s;
186
    std::ostringstream s_out;
187
    s_out << "OutputStdout";
188
    return Py::String(s_out.str());
189
}
190

191
Py::Object OutputStdout::write(const Py::Tuple& args)
192
{
193
    PyObject* output;
194
    if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
195
        throw Py::TypeError("OutputStdout.write() takes exactly one argument of type str");
196

197
    PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
198
    if (unicode) {
199
        const char* string = PyBytes_AsString(unicode);
200
        Base::Console().Message("%s",string);
201
        Py_DECREF(unicode);
202
    }
203

204
    return Py::None();
205
}
206

207
Py::Object OutputStdout::flush(const Py::Tuple&)
208
{
209
    return Py::None();
210
}
211

212
Py::Object OutputStdout::isatty()
213
{
214
    return Py::False();
215
}
216

217

218
// -------------------------------------------------------------------------
219

220
void OutputStderr::init_type()
221
{
222
    behaviors().name("OutputStderr");
223
    behaviors().doc("Redirection of stdout to FreeCAD's report view");
224
    // you must have overwritten the virtual functions
225
    behaviors().supportRepr();
226
    add_varargs_method("write",&OutputStderr::write,"write()");
227
    add_varargs_method("flush",&OutputStderr::flush,"flush()");
228
    add_noargs_method("isatty",&OutputStderr::isatty,"isatty()");
229
}
230

231
OutputStderr::OutputStderr() = default;
232

233
OutputStderr::~OutputStderr() = default;
234

235
Py::Object OutputStderr::getattr(const char *name)
236
{
237
    if (strcmp(name, "softspace") == 0) {
238
        int i=0;
239
        return Py::Int(i);
240
    }
241
    return getattr_methods(name);
242
}
243

244
Py::Object OutputStderr::repr()
245
{
246
    std::string s;
247
    std::ostringstream s_out;
248
    s_out << "OutputStderr";
249
    return Py::String(s_out.str());
250
}
251

252
Py::Object OutputStderr::write(const Py::Tuple& args)
253
{
254
    PyObject* output;
255
    if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
256
        throw Py::TypeError("OutputStderr.write() takes exactly one argument of type str");
257

258
    PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
259
    if (unicode) {
260
        const char* string = PyBytes_AsString(unicode);
261
        Base::Console().Error("%s",string);
262
        Py_DECREF(unicode);
263
    }
264

265
    return Py::None();
266
}
267

268
Py::Object OutputStderr::flush(const Py::Tuple&)
269
{
270
    return Py::None();
271
}
272

273
Py::Object OutputStderr::isatty()
274
{
275
    return Py::False();
276
}
277

278

279
// -------------------------------------------------------------------------
280

281
void PythonStdin::init_type()
282
{
283
    behaviors().name("PythonStdin");
284
    behaviors().doc("Redirection of stdin to FreeCAD to open an input dialog");
285
    // you must have overwritten the virtual functions
286
    behaviors().supportRepr();
287
    behaviors().supportGetattr();
288
    add_varargs_method("readline",&PythonStdin::readline,"readline()");
289
}
290

291
PythonStdin::PythonStdin(PythonConsole *pc)
292
  : pyConsole(pc)
293
{
294
}
295

296
PythonStdin::~PythonStdin() = default;
297

298
Py::Object PythonStdin::repr()
299
{
300
    std::string s;
301
    std::ostringstream s_out;
302
    s_out << "PythonStdin";
303
    return Py::String(s_out.str());
304
}
305

306
Py::Object PythonStdin::getattr(const char *name)
307
{
308
    if (strcmp(name, "closed") == 0) {
309
        return Py::Boolean(false);
310
    }
311
    return getattr_methods(name);
312
}
313

314
Py::Object PythonStdin::readline(const Py::Tuple& /*args*/)
315
{
316
    return Py::String( (const char *)pyConsole->readline().toLatin1() );
317
}
318

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

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

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

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