|
12 | 12 | */ |
13 | 13 |
|
14 | 14 | #include "../src/PyRunner.hpp" |
15 | | -#include <Python.h> |
16 | | -#include <vector> |
17 | | -#include <string> |
18 | | -#include <iostream> |
19 | | -#include <filesystem> |
| 15 | +#include <QDebug> |
| 16 | +#include <QString> |
| 17 | +#include <QVector> |
| 18 | +#include <QFile> |
20 | 19 | #include <cstdio> |
21 | 20 |
|
22 | | -namespace fs = std::filesystem; |
| 21 | +#undef slots |
| 22 | +#undef signals |
| 23 | +#include <Python.h> |
| 24 | +PyThreadState *g_mainState = nullptr; |
23 | 25 |
|
24 | 26 | PyRunner::PyRunner() : m_status(0) { |
25 | 27 | Py_Initialize(); |
| 28 | + g_mainState = PyEval_SaveThread(); |
26 | 29 | } |
27 | 30 |
|
28 | | -PyRunner::PyRunner(const std::string& cwd) : m_cwd(cwd), m_status(0) { |
| 31 | +PyRunner::PyRunner(const QString& cwd) : m_cwd(cwd), m_status(0) { |
29 | 32 | Py_Initialize(); |
| 33 | + g_mainState = PyEval_SaveThread(); |
30 | 34 | } |
31 | 35 |
|
32 | 36 | PyRunner::~PyRunner() { |
33 | | - Py_Finalize(); |
| 37 | + PyEval_RestoreThread(g_mainState); |
| 38 | + Py_FinalizeEx(); |
34 | 39 | } |
35 | 40 |
|
36 | | -bool PyRunner::setUpCamp(const std::string& level) { |
37 | | - if (!fs::exists(level)) return false; |
| 41 | +bool PyRunner::setUpCamp(const QString& level) { |
| 42 | + bool status = true; |
| 43 | + if (!QFile::exists(level)) { |
| 44 | + status = false; |
| 45 | + } |
38 | 46 | m_cwd = level; |
39 | | - return true; |
| 47 | + return status; |
40 | 48 | } |
41 | 49 |
|
42 | | -void PyRunner::run(const std::string& script, |
43 | | - const std::vector<std::string>& args) { |
| 50 | +void PyRunner::run(const QString& script, |
| 51 | + const QVector<QString>& args) { |
44 | 52 | if (!Py_IsInitialized()) { |
45 | | - std::cerr << "Python not initialized!" << std::endl; |
| 53 | + qDebug() << "Python not initialized!"; |
46 | 54 | m_status = 1; |
47 | 55 | return; |
48 | 56 | } |
49 | 57 |
|
50 | | - std::cerr << "[PyRunner] Executing script file: " << script << std::endl; |
| 58 | + PyGILState_STATE gil = PyGILState_Ensure(); |
| 59 | + qDebug() << "[PyRunner] Executing script file: " << script << Qt::endl; |
51 | 60 |
|
52 | 61 | // Add working dir to sys.path |
53 | | - if (!m_cwd.empty()) { |
| 62 | + if (!m_cwd.isEmpty()) { |
54 | 63 | PyObject *sysPath = PySys_GetObject("path"); |
55 | | - PyObject *cwdPath = PyUnicode_FromString(m_cwd.c_str()); |
| 64 | + PyObject *cwdPath = PyUnicode_FromString(m_cwd.toStdString().c_str()); |
56 | 65 | PyList_Insert(sysPath, 0, cwdPath); |
57 | 66 | Py_DECREF(cwdPath); |
58 | 67 | } |
59 | 68 |
|
60 | 69 | // Set sys.argv |
61 | 70 | PyObject *argvList = PyList_New(args.size()); |
62 | 71 | for (size_t i = 0; i < args.size(); ++i) { |
63 | | - PyObject *arg = PyUnicode_FromString(args[i].c_str()); |
| 72 | + PyObject *arg = PyUnicode_FromString(args[i].toStdString().c_str()); |
64 | 73 | PyList_SET_ITEM(argvList, i, arg); // steals ref |
65 | 74 | } |
66 | 75 | PySys_SetObject("argv", argvList); |
67 | 76 | Py_DECREF(argvList); |
68 | 77 |
|
69 | | - std::string fullpath = |
70 | | - m_cwd.empty() ? script : (m_cwd + "/" + script + ".py"); |
71 | | - FILE* fp = fopen(fullpath.c_str(), "r"); |
| 78 | + QString fullpath = |
| 79 | + m_cwd.isEmpty() ? script : (m_cwd + "/" + script + ".py"); |
| 80 | + FILE* fp = fopen(fullpath.toStdString().c_str(), "r"); |
72 | 81 | if (!fp) { |
73 | | - std::cerr << "[PyRunner] Failed to open script file: " |
74 | | - << fullpath << std::endl; |
| 82 | + qDebug() << "[PyRunner] Failed to open script file: " |
| 83 | + << fullpath << Qt::endl; |
75 | 84 | m_status = 1; |
76 | 85 | return; |
77 | 86 | } |
78 | 87 |
|
79 | | - int result = PyRun_SimpleFile(fp, fullpath.c_str()); |
| 88 | + int result = PyRun_SimpleFile(fp, fullpath.toStdString().c_str()); |
80 | 89 | fclose(fp); |
| 90 | + PyGILState_Release(gil); |
81 | 91 |
|
82 | 92 | if (result != 0) { |
83 | | - std::cerr << "[PyRunner] Python script returned error.\n"; |
| 93 | + qDebug() << "[PyRunner] Python script returned error.\n"; |
84 | 94 | m_status = 1; |
85 | 95 | } else { |
86 | | - std::cerr << "[PyRunner] Script executed successfully.\n"; |
| 96 | + qDebug() << "[PyRunner] Script executed successfully.\n"; |
87 | 97 | m_status = 0; |
88 | 98 | } |
89 | 99 | } |
90 | 100 |
|
91 | | -int64_t PyRunner::updateLevel(int64_t lid) { |
| 101 | +qint64 PyRunner::updateLevel(qint64 lid) { |
92 | 102 | run("tombll_manage_data", |
93 | | - {"tombll_manage_data.py", "-u", std::to_string(lid)}); |
| 103 | + {"tombll_manage_data.py", "-u", QString("%1").arg(lid)}); |
94 | 104 | return m_status; |
95 | 105 | } |
96 | 106 |
|
97 | | -int64_t PyRunner::syncCards(int64_t lid) { |
| 107 | +qint64 PyRunner::syncCards() { |
98 | 108 | run("tombll_manage_data", {"tombll_manage_data.py", "-sc"}); |
99 | 109 | return m_status; |
100 | 110 | } |
101 | 111 |
|
102 | | -int64_t PyRunner::getStatus() const { |
| 112 | +qint64 PyRunner::getStatus() const { |
103 | 113 | return m_status; |
104 | 114 | } |
105 | 115 |
|
0 commit comments