Add support for config map to Python job API.
Rename PythonJobHelper ==> CalamaresPython::Helper. Add QVariant to boost::python::object conversion functions. Rename some constants in the libcalamares Python API.
This commit is contained in:
parent
2f03dfa43b
commit
d9f4c74537
@ -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})
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
#include "CalamaresConfig.h"
|
||||
#ifdef WITH_PYTHON
|
||||
#include "PythonJobHelper.h"
|
||||
#include "PythonHelper.h"
|
||||
#endif
|
||||
|
||||
#include <QThread>
|
||||
|
@ -16,7 +16,7 @@
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
|
@ -24,10 +24,17 @@
|
||||
#include <QStringList>
|
||||
|
||||
#undef slots
|
||||
#include <boost/python/dict.hpp>
|
||||
#include <boost/python/list.hpp>
|
||||
#include <boost/python/object.hpp>
|
||||
|
||||
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
|
@ -18,7 +18,7 @@
|
||||
|
||||
#include "PythonJob.h"
|
||||
|
||||
#include "PythonJobHelper.h"
|
||||
#include "PythonHelper.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QDir>
|
||||
@ -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 )
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,8 @@
|
||||
|
||||
#include "Job.h"
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
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
|
||||
|
@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,9 @@
|
||||
|
||||
#include "PythonJob.h"
|
||||
|
||||
#undef slots
|
||||
#include <boost/python/dict.hpp>
|
||||
|
||||
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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user