Skip to content

Commit af62f1f

Browse files
committed
Refactor
1 parent f5ee8e7 commit af62f1f

8 files changed

Lines changed: 317 additions & 263 deletions

File tree

Features/snippets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void Snippets::LoadSnippets(bool& success)
5454
"write_output(\"Hi You,\\n\")\n"
5555
"\n"
5656
"# get_apppath() -> get exe path\n"
57-
"print (\"PyRun.exe is at :\", get_apppath())\n\n"));
57+
"print (\"expressPython.exe is at :\", get_apppath())\n\n"));
5858
}
5959

6060
m_Snippets = new QMap<QString, QString>(data);

PyRun.pro

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ QT += core gui
99
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
1010

1111

12-
TARGET = PyRun
12+
TARGET = expressPython
1313
TEMPLATE = app
1414

1515
SOURCES += main.cpp\
1616
UI/mainview.cpp \
1717
CodeEditor/pythonsyntaxhighlighter.cpp \
1818
CodeEditor/codeeditor.cpp \
19-
Features/snippets.cpp
19+
Features/snippets.cpp \
20+
PythonAccess/emb.cpp \
21+
PythonAccess/pythonworker.cpp
2022

2123
HEADERS += UI/mainview.h \
2224
CodeEditor/pythonsyntaxhighlighter.h \
2325
CodeEditor/codeeditor.h \
2426
Features/snippets.h \
25-
PythonAccess/emb.h
27+
PythonAccess/emb.h \
28+
PythonAccess/pythonworker.h
2629

2730
FORMS += UI/mainview.ui
2831

29-
QMAKE_CXXFLAGS += -std=c++11
32+
QMAKE_CXXFLAGS += -std=c++11 -Wpedantic
3033

3134
RESOURCES += \
3235
PyRunResources.qrc

PythonAccess/emb.cpp

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
// --------------------------------------------------------------------------
2+
// Copyright (C) 2011 Mateusz Loskot <mateusz@loskot.net>
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Blog article: http://mateusz.loskot.net/?p=2819
8+
// --------------------------------------------------------------------------
9+
10+
#pragma GCC diagnostic ignored "-Wunused-parameter"
11+
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
12+
13+
#include <functional>
14+
#include <iostream>
15+
#include <string>
16+
#include "PythonAccess/emb.h"
17+
18+
namespace emb {
19+
MainView* mainView;
20+
void setMainView(MainView* _mainView)
21+
{
22+
mainView = _mainView;
23+
}
24+
MainView* getMainView()
25+
{
26+
return mainView;
27+
}
28+
struct StdOut {
29+
PyObject_HEAD
30+
StdOutWriteType write;
31+
};
32+
PyObject* StdOutWrite(PyObject* self, PyObject* args)
33+
{
34+
std::size_t written(0);
35+
StdOut* selfimpl = reinterpret_cast<StdOut*>(self);
36+
if (selfimpl->write) {
37+
char* data;
38+
if (!PyArg_ParseTuple(args, "s", &data))
39+
return 0;
40+
std::string str(data);
41+
selfimpl->write(str);
42+
written = str.size();
43+
}
44+
return PyLong_FromSize_t(written);
45+
}
46+
PyObject* StdOutFlush(PyObject* self, PyObject* args)
47+
{
48+
// no-op
49+
return Py_BuildValue("");
50+
}
51+
PyMethodDef stdOutMethods[] = {
52+
{ "write", StdOutWrite, METH_VARARGS, "sys.stdout.write" },
53+
{ "flush", StdOutFlush, METH_VARARGS, "sys.stdout.flush" },
54+
{ 0, 0, 0, 0 } // sentinel
55+
};
56+
PyTypeObject StdoutType = {
57+
PyVarObject_HEAD_INIT(0, 0) "emb.StdoutType", /* tp_name */
58+
sizeof(StdOut), /* tp_basicsize */
59+
0, /* tp_itemsize */
60+
0, /* tp_dealloc */
61+
0, /* tp_print */
62+
0, /* tp_getattr */
63+
0, /* tp_setattr */
64+
0, /* tp_reserved */
65+
0, /* tp_repr */
66+
0, /* tp_as_number */
67+
0, /* tp_as_sequence */
68+
0, /* tp_as_mapping */
69+
0, /* tp_hash */
70+
0, /* tp_call */
71+
0, /* tp_str */
72+
0, /* tp_getattro */
73+
0, /* tp_setattro */
74+
0, /* tp_as_buffer */
75+
Py_TPFLAGS_DEFAULT, /* tp_flags */
76+
"emb.Stdout objects", /* tp_doc */
77+
0, /* tp_traverse */
78+
0, /* tp_clear */
79+
0, /* tp_richcompare */
80+
0, /* tp_weaklistoffset */
81+
0, /* tp_iter */
82+
0, /* tp_iternext */
83+
stdOutMethods, /* tp_methods */
84+
0, /* tp_members */
85+
0, /* tp_getset */
86+
0, /* tp_base */
87+
0, /* tp_dict */
88+
0, /* tp_descr_get */
89+
0, /* tp_descr_set */
90+
0, /* tp_dictoffset */
91+
0, /* tp_init */
92+
0, /* tp_alloc */
93+
0, /* tp_new */
94+
};
95+
PyModuleDef embModule = {
96+
PyModuleDef_HEAD_INIT,
97+
"emb", 0, -1, 0,
98+
};
99+
// Internal state
100+
PyObject* gStdOut;
101+
PyObject* gStdOutSaved;
102+
PyObject* gStdErrSaved;
103+
PyMODINIT_FUNC PyInitEmbConnect(void)
104+
{
105+
gStdOut = 0;
106+
gStdOutSaved = 0;
107+
gStdErrSaved = 0;
108+
StdoutType.tp_new = PyType_GenericNew;
109+
if (PyType_Ready(&StdoutType) < 0)
110+
return 0;
111+
PyObject* m = PyModule_Create(&embModule);
112+
if (m) {
113+
Py_INCREF(&StdoutType);
114+
PyModule_AddObject(m, "Stdout", reinterpret_cast<PyObject*>(&StdoutType));
115+
}
116+
return m;
117+
}
118+
void SetStdout(StdOutWriteType write)
119+
{
120+
if (!gStdOut) {
121+
gStdOutSaved = PySys_GetObject("stdout");
122+
gStdErrSaved = PySys_GetObject("stderr");
123+
gStdOut = StdoutType.tp_new(&StdoutType, 0, 0);
124+
}
125+
StdOut* impl = reinterpret_cast<StdOut*>(gStdOut);
126+
impl->write = write;
127+
PySys_SetObject("stdout", gStdOut);
128+
PySys_SetObject("stderr", gStdOut);
129+
}
130+
void ResetStdOut()
131+
{
132+
if (gStdOutSaved)
133+
PySys_SetObject("stdout", gStdOutSaved);
134+
if (gStdErrSaved)
135+
PySys_SetObject("stderr", gStdErrSaved);
136+
137+
Py_XDECREF(gStdOut);
138+
gStdOut = 0;
139+
}
140+
//--------------------------------------------------------------------
141+
// Embedded APIs
142+
//--------------------------------------------------------------------
143+
PyObject* ApiGetInput(PyObject* self, PyObject* args)
144+
{
145+
if (!PyArg_ParseTuple(args, ":numargs"))
146+
return NULL;
147+
148+
return Py_BuildValue("s", mainView->GetInput().toStdString().c_str());
149+
}
150+
PyObject* ApiSetInput(PyObject* self, PyObject* args)
151+
{
152+
char* data;
153+
if (!PyArg_ParseTuple(args, "s", &data))
154+
return NULL;
155+
156+
mainView->SetInput(QString(data));
157+
158+
return Py_BuildValue("i", 0);
159+
}
160+
PyObject* ApiGetAppPath(PyObject* self, PyObject* args)
161+
{
162+
if (!PyArg_ParseTuple(args, ":numargs"))
163+
return NULL;
164+
165+
return Py_BuildValue("s", QCoreApplication::applicationDirPath().toStdString().c_str());
166+
}
167+
PyObject* ApiGetOutput(PyObject* self, PyObject* args)
168+
{
169+
if (!PyArg_ParseTuple(args, ":numargs"))
170+
return NULL;
171+
172+
return Py_BuildValue("s", mainView->GetOutput().toStdString().c_str());
173+
}
174+
175+
PyObject* ApiSetOutput(PyObject* self, PyObject* args)
176+
{
177+
char* data;
178+
if (!PyArg_ParseTuple(args, "s", &data))
179+
return NULL;
180+
181+
mainView->SetOutput(QString(data));
182+
183+
return Py_BuildValue("i", 0);
184+
}
185+
PyObject* ApiGetCode(PyObject* self, PyObject* args)
186+
{
187+
if (!PyArg_ParseTuple(args, ":numargs"))
188+
return NULL;
189+
190+
return Py_BuildValue("s", mainView->GetCode().toStdString().c_str());
191+
}
192+
193+
PyObject* ApiSetCode(PyObject* self, PyObject* args)
194+
{
195+
char* data;
196+
if (!PyArg_ParseTuple(args, "s", &data))
197+
return NULL;
198+
199+
mainView->SetCode(QString(data));
200+
201+
return Py_BuildValue("i", 0);
202+
}
203+
PyObject* ApiWriteOutput(PyObject* self, PyObject* args)
204+
{
205+
char* data;
206+
if (!PyArg_ParseTuple(args, "s", &data))
207+
return NULL;
208+
209+
mainView->WriteOutput(QString(data));
210+
211+
return Py_BuildValue("i", 0);
212+
}
213+
214+
PyMethodDef apiMethods[] = {
215+
{ "get_input", ApiGetInput, METH_VARARGS,
216+
"Get input textbox's content" },
217+
{ "set_input", ApiSetInput, METH_VARARGS,
218+
"Set input textbox's content" },
219+
220+
{ "get_apppath", ApiGetAppPath, METH_VARARGS,
221+
"Get application path" },
222+
223+
{ "get_output", ApiGetOutput, METH_VARARGS,
224+
"Get output textbox's content" },
225+
{ "set_output", ApiSetOutput, METH_VARARGS,
226+
"Set output textbox's content" },
227+
228+
{ "get_code", ApiGetCode, METH_VARARGS,
229+
"Get code textbox's content" },
230+
{ "set_code", ApiSetCode, METH_VARARGS,
231+
"Get code textbox's content" },
232+
233+
{ "write_output", ApiWriteOutput, METH_VARARGS,
234+
"Append to output, It does not automatically add a newline" },
235+
// end of method definitions
236+
{ NULL, NULL, 0, NULL }
237+
};
238+
239+
PyModuleDef apiModule = {
240+
PyModuleDef_HEAD_INIT, "expressApi", NULL, -1, apiMethods,
241+
NULL, NULL, NULL, NULL
242+
};
243+
PyObject* PyInitApiConnection(void)
244+
{
245+
return PyModule_Create(&apiModule);
246+
}
247+
}
248+
249+
#pragma GCC diagnostic warning "-Wunused-parameter"
250+
#pragma GCC diagnostic warning "-Wmissing-field-initializers"

0 commit comments

Comments
 (0)