diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 125e300e0..f88e34ddc 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -35,9 +35,9 @@ include_directories( if( WITH_PYTHON ) set( libSources ${libSources} + PythonHelper.cpp PythonJob.cpp PythonJobApi.cpp - PythonJobHelper.cpp ) include_directories(${PYTHON_INCLUDE_DIRS}) diff --git a/src/libcalamares/JobQueue.cpp b/src/libcalamares/JobQueue.cpp index 13162a603..e6de2f3df 100644 --- a/src/libcalamares/JobQueue.cpp +++ b/src/libcalamares/JobQueue.cpp @@ -22,7 +22,7 @@ #include "CalamaresConfig.h" #ifdef WITH_PYTHON -#include "PythonJobHelper.h" +#include "PythonHelper.h" #endif #include diff --git a/src/libcalamares/PythonJobHelper.cpp b/src/libcalamares/PythonHelper.cpp similarity index 73% rename from src/libcalamares/PythonJobHelper.cpp rename to src/libcalamares/PythonHelper.cpp index 5edc9a5a9..693b61e81 100644 --- a/src/libcalamares/PythonJobHelper.cpp +++ b/src/libcalamares/PythonHelper.cpp @@ -16,7 +16,7 @@ * along with Calamares. If not, see . */ -#include "PythonJobHelper.h" +#include "PythonHelper.h" #include "utils/CalamaresUtils.h" #include "utils/Logger.h" @@ -31,6 +31,59 @@ namespace bp = boost::python; namespace CalamaresPython { + +boost::python::object +variantToPyObject( const QVariant& variant ) +{ + switch ( variant.type() ) + { + case QVariant::List: + case QVariant::StringList: + return variantListToPyList( variant.toList() ); + + case QVariant::Map: + return variantMapToPyDict( variant.toMap() ); + + case QVariant::Int: + return bp::object( variant.toInt() ); + + case QVariant::Double: + return bp::object( variant.toDouble() ); + + case QVariant::String: + case QVariant::Char: + return bp::object( variant.toString().toStdString() ); + + default: + return bp::object(); + } +} + + +boost::python::list +variantListToPyList( const QVariantList& variantList ) +{ + bp::list pyList; + foreach ( const QVariant& variant, variantList ) + { + pyList.append( variantToPyObject( variant ) ); + } + return pyList; +} + + +boost::python::dict +variantMapToPyDict( const QVariantMap& variantMap ) +{ + bp::dict pyDict; + for ( auto it = variantMap.constBegin(); it != variantMap.constEnd(); ++it ) + { + pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() ); + } + return pyDict; +} + + Helper* Helper::s_instance = nullptr; Helper::Helper( QObject* parent ) @@ -74,7 +127,7 @@ Helper::Helper( QObject* parent ) } else { - cDebug() << "WARNING: creating PythonJobHelper more than once. This is very bad."; + cDebug() << "WARNING: creating PythonHelper more than once. This is very bad."; return; } diff --git a/src/libcalamares/PythonJobHelper.h b/src/libcalamares/PythonHelper.h similarity index 83% rename from src/libcalamares/PythonJobHelper.h rename to src/libcalamares/PythonHelper.h index 75752301b..3049eef59 100644 --- a/src/libcalamares/PythonJobHelper.h +++ b/src/libcalamares/PythonHelper.h @@ -24,10 +24,17 @@ #include #undef slots +#include +#include #include namespace CalamaresPython { +boost::python::object variantToPyObject( const QVariant& variant ); +boost::python::list variantListToPyList( const QVariantList& variantList ); +boost::python::dict variantMapToPyDict( const QVariantMap& variantMap ); + + class Helper : public QObject { Q_OBJECT diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index d38bf1d5f..1c45b0033 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -18,7 +18,7 @@ #include "PythonJob.h" -#include "PythonJobHelper.h" +#include "PythonHelper.h" #include "utils/Logger.h" #include @@ -34,15 +34,16 @@ namespace bp = boost::python; BOOST_PYTHON_MODULE( libcalamares ) { - bp::scope().attr( "ORGANIZATION_NAME" ) = CALAMARES_ORGANIZATION_NAME; - bp::scope().attr( "ORGANIZATION_DOMAIN" ) = CALAMARES_ORGANIZATION_DOMAIN; - bp::scope().attr( "APPLICATION_NAME" ) = CALAMARES_APPLICATION_NAME; - bp::scope().attr( "VERSION" ) = CALAMARES_VERSION; - bp::scope().attr( "VERSION_SHORT" ) = CALAMARES_VERSION_SHORT; + bp::scope().attr( "organizationName" ) = CALAMARES_ORGANIZATION_NAME; + bp::scope().attr( "organizationDomain" ) = CALAMARES_ORGANIZATION_DOMAIN; + bp::scope().attr( "applicationName" ) = CALAMARES_APPLICATION_NAME; + bp::scope().attr( "version" ) = CALAMARES_VERSION; + bp::scope().attr( "shortVersion" ) = CALAMARES_VERSION_SHORT; bp::class_< CalamaresPython::PythonJobInterface >( "job", bp::init< const Calamares::PythonJob* >() ) - .def( "prettyName", &CalamaresPython::PythonJobInterface::prettyName ) - .def( "workingPath", &CalamaresPython::PythonJobInterface::workingPath ); + .def_readonly( "prettyName", &CalamaresPython::PythonJobInterface::prettyName ) + .def_readonly( "workingPath", &CalamaresPython::PythonJobInterface::workingPath ) + .def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration ); } @@ -51,10 +52,12 @@ namespace Calamares { PythonJob::PythonJob( const QString& scriptFile, const QString& workingPath, + const QVariantMap& moduleConfiguration, QObject* parent ) : Job( parent ) , m_scriptFile( scriptFile ) , m_workingPath( workingPath ) + , m_configurationMap( moduleConfiguration ) { } diff --git a/src/libcalamares/PythonJob.h b/src/libcalamares/PythonJob.h index 38500bc0e..4202746f9 100644 --- a/src/libcalamares/PythonJob.h +++ b/src/libcalamares/PythonJob.h @@ -21,6 +21,8 @@ #include "Job.h" +#include + namespace CalamaresPython { class PythonJobInterface; @@ -35,6 +37,7 @@ class PythonJob : public Job public: explicit PythonJob( const QString& scriptFile, const QString& workingPath, + const QVariantMap& moduleConfiguration = QVariantMap(), QObject* parent = nullptr ); virtual ~PythonJob(); @@ -47,6 +50,7 @@ private: CalamaresPython::Helper* helper(); QString m_scriptFile; QString m_workingPath; + QVariantMap m_configurationMap; }; } // namespace Calamares diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index f3e09cae7..7255195dc 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -18,25 +18,17 @@ #include "PythonJobApi.h" +#include "PythonHelper.h" namespace CalamaresPython { PythonJobInterface::PythonJobInterface( const Calamares::PythonJob* parent ) : m_parent( parent ) -{} - - -std::string -PythonJobInterface::prettyName() const { - return m_parent->prettyName().toStdString(); -} - -std::string -PythonJobInterface::workingPath() const -{ - return m_parent->m_workingPath.toStdString(); + prettyName = m_parent->prettyName().toStdString(); + workingPath = m_parent->m_workingPath.toStdString(); + configuration = CalamaresPython::variantMapToPyDict( m_parent->m_configurationMap ); } } diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index fd3bf7b72..cf77709a7 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -23,6 +23,9 @@ #include "PythonJob.h" +#undef slots +#include + namespace CalamaresPython { @@ -31,9 +34,10 @@ class PythonJobInterface public: explicit PythonJobInterface( const Calamares::PythonJob* parent ); - std::string prettyName() const; + std::string prettyName; + std::string workingPath; - std::string workingPath() const; + boost::python::dict configuration; private: const Calamares::PythonJob* m_parent; diff --git a/src/libcalamaresui/modulesystem/PythonJobModule.cpp b/src/libcalamaresui/modulesystem/PythonJobModule.cpp index 2e01e6a51..c9e8522e7 100644 --- a/src/libcalamaresui/modulesystem/PythonJobModule.cpp +++ b/src/libcalamaresui/modulesystem/PythonJobModule.cpp @@ -50,7 +50,8 @@ PythonJobModule::loadSelf() return; Calamares::job_ptr j = Calamares::job_ptr( new PythonJob( m_scriptFileName, - m_workingPath ) ); + m_workingPath, + m_configurationMap ) ); JobQueue::instance()->enqueue( j ); m_loaded = true; }