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:
Teo Mrnjavac 2014-07-18 14:27:59 +02:00
parent 2f03dfa43b
commit d9f4c74537
9 changed files with 91 additions and 27 deletions

View File

@ -35,9 +35,9 @@ include_directories(
if( WITH_PYTHON ) if( WITH_PYTHON )
set( libSources set( libSources
${libSources} ${libSources}
PythonHelper.cpp
PythonJob.cpp PythonJob.cpp
PythonJobApi.cpp PythonJobApi.cpp
PythonJobHelper.cpp
) )
include_directories(${PYTHON_INCLUDE_DIRS}) include_directories(${PYTHON_INCLUDE_DIRS})

View File

@ -22,7 +22,7 @@
#include "CalamaresConfig.h" #include "CalamaresConfig.h"
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
#include "PythonJobHelper.h" #include "PythonHelper.h"
#endif #endif
#include <QThread> #include <QThread>

View File

@ -16,7 +16,7 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "PythonJobHelper.h" #include "PythonHelper.h"
#include "utils/CalamaresUtils.h" #include "utils/CalamaresUtils.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@ -31,6 +31,59 @@ namespace bp = boost::python;
namespace CalamaresPython { 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::s_instance = nullptr;
Helper::Helper( QObject* parent ) Helper::Helper( QObject* parent )
@ -74,7 +127,7 @@ Helper::Helper( QObject* parent )
} }
else else
{ {
cDebug() << "WARNING: creating PythonJobHelper more than once. This is very bad."; cDebug() << "WARNING: creating PythonHelper more than once. This is very bad.";
return; return;
} }

View File

@ -24,10 +24,17 @@
#include <QStringList> #include <QStringList>
#undef slots #undef slots
#include <boost/python/dict.hpp>
#include <boost/python/list.hpp>
#include <boost/python/object.hpp> #include <boost/python/object.hpp>
namespace CalamaresPython { 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 class Helper : public QObject
{ {
Q_OBJECT Q_OBJECT

View File

@ -18,7 +18,7 @@
#include "PythonJob.h" #include "PythonJob.h"
#include "PythonJobHelper.h" #include "PythonHelper.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QDir> #include <QDir>
@ -34,15 +34,16 @@ namespace bp = boost::python;
BOOST_PYTHON_MODULE( libcalamares ) BOOST_PYTHON_MODULE( libcalamares )
{ {
bp::scope().attr( "ORGANIZATION_NAME" ) = CALAMARES_ORGANIZATION_NAME; bp::scope().attr( "organizationName" ) = CALAMARES_ORGANIZATION_NAME;
bp::scope().attr( "ORGANIZATION_DOMAIN" ) = CALAMARES_ORGANIZATION_DOMAIN; bp::scope().attr( "organizationDomain" ) = CALAMARES_ORGANIZATION_DOMAIN;
bp::scope().attr( "APPLICATION_NAME" ) = CALAMARES_APPLICATION_NAME; bp::scope().attr( "applicationName" ) = CALAMARES_APPLICATION_NAME;
bp::scope().attr( "VERSION" ) = CALAMARES_VERSION; bp::scope().attr( "version" ) = CALAMARES_VERSION;
bp::scope().attr( "VERSION_SHORT" ) = CALAMARES_VERSION_SHORT; bp::scope().attr( "shortVersion" ) = CALAMARES_VERSION_SHORT;
bp::class_< CalamaresPython::PythonJobInterface >( "job", bp::init< const Calamares::PythonJob* >() ) bp::class_< CalamaresPython::PythonJobInterface >( "job", bp::init< const Calamares::PythonJob* >() )
.def( "prettyName", &CalamaresPython::PythonJobInterface::prettyName ) .def_readonly( "prettyName", &CalamaresPython::PythonJobInterface::prettyName )
.def( "workingPath", &CalamaresPython::PythonJobInterface::workingPath ); .def_readonly( "workingPath", &CalamaresPython::PythonJobInterface::workingPath )
.def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration );
} }
@ -51,10 +52,12 @@ namespace Calamares {
PythonJob::PythonJob( const QString& scriptFile, PythonJob::PythonJob( const QString& scriptFile,
const QString& workingPath, const QString& workingPath,
const QVariantMap& moduleConfiguration,
QObject* parent ) QObject* parent )
: Job( parent ) : Job( parent )
, m_scriptFile( scriptFile ) , m_scriptFile( scriptFile )
, m_workingPath( workingPath ) , m_workingPath( workingPath )
, m_configurationMap( moduleConfiguration )
{ {
} }

View File

@ -21,6 +21,8 @@
#include "Job.h" #include "Job.h"
#include <QVariant>
namespace CalamaresPython namespace CalamaresPython
{ {
class PythonJobInterface; class PythonJobInterface;
@ -35,6 +37,7 @@ class PythonJob : public Job
public: public:
explicit PythonJob( const QString& scriptFile, explicit PythonJob( const QString& scriptFile,
const QString& workingPath, const QString& workingPath,
const QVariantMap& moduleConfiguration = QVariantMap(),
QObject* parent = nullptr ); QObject* parent = nullptr );
virtual ~PythonJob(); virtual ~PythonJob();
@ -47,6 +50,7 @@ private:
CalamaresPython::Helper* helper(); CalamaresPython::Helper* helper();
QString m_scriptFile; QString m_scriptFile;
QString m_workingPath; QString m_workingPath;
QVariantMap m_configurationMap;
}; };
} // namespace Calamares } // namespace Calamares

View File

@ -18,25 +18,17 @@
#include "PythonJobApi.h" #include "PythonJobApi.h"
#include "PythonHelper.h"
namespace CalamaresPython namespace CalamaresPython
{ {
PythonJobInterface::PythonJobInterface( const Calamares::PythonJob* parent ) PythonJobInterface::PythonJobInterface( const Calamares::PythonJob* parent )
: m_parent( parent ) : m_parent( parent )
{}
std::string
PythonJobInterface::prettyName() const
{ {
return m_parent->prettyName().toStdString(); prettyName = m_parent->prettyName().toStdString();
} workingPath = m_parent->m_workingPath.toStdString();
configuration = CalamaresPython::variantMapToPyDict( m_parent->m_configurationMap );
std::string
PythonJobInterface::workingPath() const
{
return m_parent->m_workingPath.toStdString();
} }
} }

View File

@ -23,6 +23,9 @@
#include "PythonJob.h" #include "PythonJob.h"
#undef slots
#include <boost/python/dict.hpp>
namespace CalamaresPython namespace CalamaresPython
{ {
@ -31,9 +34,10 @@ class PythonJobInterface
public: public:
explicit PythonJobInterface( const Calamares::PythonJob* parent ); 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: private:
const Calamares::PythonJob* m_parent; const Calamares::PythonJob* m_parent;

View File

@ -50,7 +50,8 @@ PythonJobModule::loadSelf()
return; return;
Calamares::job_ptr j = Calamares::job_ptr( new PythonJob( m_scriptFileName, Calamares::job_ptr j = Calamares::job_ptr( new PythonJob( m_scriptFileName,
m_workingPath ) ); m_workingPath,
m_configurationMap ) );
JobQueue::instance()->enqueue( j ); JobQueue::instance()->enqueue( j );
m_loaded = true; m_loaded = true;
} }