Commit 6ce0133b authored by Christoph Cullmann's avatar Christoph Cullmann 🍨
Browse files

compiles pythonutils but doesn't link yet

parent 952c2a55
......@@ -62,11 +62,6 @@ ecm_optional_add_subdirectory(apps)
# our documentation
ecm_optional_add_subdirectory(doc)
# python bindings
if (BUILD_PYTHON_BINDINDS)
ecm_optional_add_subdirectory(python)
endif()
ki18n_install(po)
if(KF5DocTools_FOUND)
kdoctools_install(po)
......
#
# Kate Python bindings
#
remove_definitions(-DQT_STRICT_ITERATORS)
set(PYTHON_BINDING_NAMESPACE "PyKate")
# Just to fix warnings with --warn-uninitialized
if(NOT DEFINED SHIBOKEN_CUSTOM_PREFIX) #look for shiboken in a custom location
set(SHIBOKEN_CUSTOM_PREFIX "")
endif()
if(NOT DEFINED PYSIDE_CUSTOM_PREFIX) #look for pyside in a custom location
set(PYSIDE_CUSTOM_PREFIX "")
endif()
if(${PROJECT_NAME}_QT6)
set(PYSIDE_MAJOR_VERSION "6")
set(PYTHON_BINDING_NAMESPACE "${PYTHON_BINDING_NAMESPACE}Qt${PYSIDE_MAJOR_VERSION}")
set(QtWidgets_VERSION ${Qt6Widgets_VERSION})
else()
set(PYSIDE_MAJOR_VERSION "2")
set(QtWidgets_VERSION ${Qt5Widgets_VERSION})
endif()
find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development)
find_package(Shiboken${PYSIDE_MAJOR_VERSION} REQUIRED)
find_package(PySide${PYSIDE_MAJOR_VERSION} REQUIRED)
if(NOT PYTHON_BINDINGS_INSTALL_PREFIX)
set(PYTHON_BINDINGS_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
endif()
set(Python3_VERSION_MAJORMINOR "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}")
set(BINDINGS_DIR "${INSTALL_LIBRARY_DIR}/python${Python3_VERSION_MAJORMINOR}/site-packages/${PYTHON_BINDING_NAMESPACE}")
set(PYTHON_BINDINGS_INSTALL_PREFIX "${PYTHON_BINDINGS_INSTALL_PREFIX}/${BINDINGS_DIR}")
include(PySide${PYSIDE_MAJOR_VERSION}ModuleBuild)
add_subdirectory(PyKate)
#
# Kate Python plugin
#
kate_add_plugin(katepythonplugin)
target_compile_definitions(katepythonplugin PRIVATE TRANSLATION_DOMAIN="katepythonplugin")
......@@ -6,11 +48,14 @@ target_link_libraries(
PRIVATE
KF5::I18n
KF5::TextEditor
Shiboken2::libshiboken
${Python3_LIBRARIES}
)
target_sources(
katepythonplugin
PRIVATE
katepythonplugin.cpp
pythonutils.cpp
plugin.qrc
)
......@@ -71,6 +71,6 @@ create_python_bindings(
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/__init__.py.cmake ${CMAKE_CURRENT_BINARY_DIR}/__init__.py @ONLY)
# install
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/__init__.py $<TARGET_FILE:Kate>
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/__init__.py $<TARGET_FILE:PyKate>
DESTINATION ${PYTHON_BINDINGS_INSTALL_PREFIX}
)
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "pythonutils.h"
#include <QtCore/QByteArray>
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QOperatingSystemVersion>
#include <QtCore/QStringList>
#include <QtCore/QTemporaryFile>
#include <sbkconverter.h>
#include <sbkmodule.h>
#include <sbkpython.h>
/* from AppLib bindings */
extern "C" PyObject *PyInit_AppLib();
static const char moduleName[] = "AppLib";
// This variable stores all Python types exported by this module.
extern PyTypeObject **SbkAppLibTypes;
// This variable stores all type converters exported by this module.
extern SbkConverter **SbkAppLibTypeConverters;
namespace PythonUtils
{
static State state = PythonUninitialized;
static void cleanup()
{
if (state > PythonUninitialized) {
Py_Finalize();
state = PythonUninitialized;
}
}
static const char virtualEnvVar[] = "VIRTUAL_ENV";
// If there is an active python virtual environment, use that environment's
// packages location.
static void initVirtualEnvironment()
{
// As of Python 3.8, Python is no longer able to run stand-alone in a
// virtualenv due to missing libraries. Add the path to the modules instead.
if (QOperatingSystemVersion::currentType() == QOperatingSystemVersion::Windows
&& (PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8))) {
const QByteArray virtualEnvPath = qgetenv(virtualEnvVar);
qputenv("PYTHONPATH", virtualEnvPath + "\\Lib\\site-packages");
}
}
State init()
{
if (state > PythonUninitialized)
return state;
if (qEnvironmentVariableIsSet(virtualEnvVar))
initVirtualEnvironment();
if (PyImport_AppendInittab(moduleName, PyInit_AppLib) == -1) {
qWarning("Failed to add the module '%s' to the table of built-in modules.", moduleName);
return state;
}
Py_Initialize();
qAddPostRoutine(cleanup);
state = PythonInitialized;
const bool pythonInitialized = PyInit_AppLib() != nullptr;
const bool pyErrorOccurred = PyErr_Occurred() != nullptr;
if (pythonInitialized && !pyErrorOccurred) {
state = AppModuleLoaded;
} else {
if (pyErrorOccurred)
PyErr_Print();
qWarning("Failed to initialize the module.");
}
return state;
}
bool runScript(const QStringList &script)
{
if (init() == PythonUninitialized)
return false;
// Concatenating all the lines
QString content;
QTextStream ss(&content);
for (const QString &line : script)
ss << line << "\n";
// Executing the whole script as one line
bool result = true;
const QByteArray line = content.toUtf8();
if (PyRun_SimpleString(line.constData()) == -1) {
if (PyErr_Occurred())
PyErr_Print();
result = false;
}
return result;
}
} // namespace PythonUtils
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef PYTHONUTILS_H
#define PYTHONUTILS_H
#include <QtCore/QStringList>
class QObject;
namespace PythonUtils
{
enum AppLibTypes {
MainWindowType = 0 // SBK_MAINWINDOW_IDX
};
enum State { PythonUninitialized, PythonInitialized, AppModuleLoaded };
State init();
bool runScript(const QStringList &script);
} // namespace PythonUtils
#endif // PYTHONUTILS_H
remove_definitions(-DQT_STRICT_ITERATORS)
set(PYTHON_BINDING_NAMESPACE "PyKate")
# Just to fix warnings with --warn-uninitialized
if(NOT DEFINED SHIBOKEN_CUSTOM_PREFIX) #look for shiboken in a custom location
set(SHIBOKEN_CUSTOM_PREFIX "")
endif()
if(NOT DEFINED PYSIDE_CUSTOM_PREFIX) #look for pyside in a custom location
set(PYSIDE_CUSTOM_PREFIX "")
endif()
if(${PROJECT_NAME}_QT6)
set(PYSIDE_MAJOR_VERSION "6")
set(PYTHON_BINDING_NAMESPACE "${PYTHON_BINDING_NAMESPACE}Qt${PYSIDE_MAJOR_VERSION}")
set(QtWidgets_VERSION ${Qt6Widgets_VERSION})
else()
set(PYSIDE_MAJOR_VERSION "2")
set(QtWidgets_VERSION ${Qt5Widgets_VERSION})
endif()
find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development)
find_package(Shiboken${PYSIDE_MAJOR_VERSION} REQUIRED)
find_package(PySide${PYSIDE_MAJOR_VERSION} ${QtWidgets_VERSION} EXACT REQUIRED)
if(NOT PYTHON_BINDINGS_INSTALL_PREFIX)
set(PYTHON_BINDINGS_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
endif()
set(Python3_VERSION_MAJORMINOR "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}")
set(BINDINGS_DIR "${INSTALL_LIBRARY_DIR}/python${Python3_VERSION_MAJORMINOR}/site-packages/${PYTHON_BINDING_NAMESPACE}")
set(PYTHON_BINDINGS_INSTALL_PREFIX "${PYTHON_BINDINGS_INSTALL_PREFIX}/${BINDINGS_DIR}")
include(PySide${PYSIDE_MAJOR_VERSION}ModuleBuild)
add_subdirectory(PyKate)
# if(${PROJECT_NAME}_TESTS)
# add_subdirectory(tests)
# endif()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment