Add conversion utilities from QHash to py dict. When translating from pydict, however, translate into a QMap as before to keep current behaviour.

This commit is contained in:
shainer 2016-07-04 22:30:45 +01:00
parent 058c7d8913
commit 2349f7125e
2 changed files with 45 additions and 13 deletions

View File

@ -29,7 +29,8 @@
namespace bp = boost::python; namespace bp = boost::python;
namespace CalamaresPython { namespace CalamaresPython
{
boost::python::object boost::python::object
@ -40,6 +41,9 @@ variantToPyObject( const QVariant& variant )
case QVariant::Map: case QVariant::Map:
return variantMapToPyDict( variant.toMap() ); return variantMapToPyDict( variant.toMap() );
case QVariant::Hash:
return variantHashToPyDict( variant.toHash() );
case QVariant::List: case QVariant::List:
case QVariant::StringList: case QVariant::StringList:
return variantListToPyList( variant.toList() ); return variantListToPyList( variant.toList() );
@ -94,9 +98,7 @@ variantListToPyList( const QVariantList& variantList )
{ {
bp::list pyList; bp::list pyList;
foreach ( const QVariant& variant, variantList ) foreach ( const QVariant& variant, variantList )
{
pyList.append( variantToPyObject( variant ) ); pyList.append( variantToPyObject( variant ) );
}
return pyList; return pyList;
} }
@ -106,9 +108,7 @@ variantListFromPyList( const boost::python::list& pyList )
{ {
QVariantList list; QVariantList list;
for ( int i = 0; i < bp::len( pyList ); ++i ) for ( int i = 0; i < bp::len( pyList ); ++i )
{
list.append( variantFromPyObject( pyList[ i ] ) ); list.append( variantFromPyObject( pyList[ i ] ) );
}
return list; return list;
} }
@ -118,9 +118,7 @@ variantMapToPyDict( const QVariantMap& variantMap )
{ {
bp::dict pyDict; bp::dict pyDict;
for ( auto it = variantMap.constBegin(); it != variantMap.constEnd(); ++it ) for ( auto it = variantMap.constBegin(); it != variantMap.constEnd(); ++it )
{
pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() ); pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() );
}
return pyDict; return pyDict;
} }
@ -148,6 +146,40 @@ variantMapFromPyDict( const boost::python::dict& pyDict )
return map; return map;
} }
boost::python::dict
variantHashToPyDict( const QVariantHash& variantHash )
{
bp::dict pyDict;
for ( auto it = variantHash.constBegin(); it != variantHash.constEnd(); ++it )
pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() );
return pyDict;
}
QVariantHash
variantHashFromPyDict( const boost::python::dict& pyDict )
{
QVariantHash hash;
bp::list keys = pyDict.keys();
for ( int i = 0; i < bp::len( keys ); ++i )
{
bp::extract< std::string > extracted_key( keys[ i ] );
if ( !extracted_key.check() )
{
cDebug() << "Key invalid, map might be incomplete.";
continue;
}
std::string key = extracted_key;
bp::object obj = pyDict[ key ];
hash.insert( QString::fromStdString( key ), variantFromPyObject( obj ) );
}
return hash;
}
Helper* Helper::s_instance = nullptr; Helper* Helper::s_instance = nullptr;
@ -165,20 +197,16 @@ Helper::Helper( QObject* parent )
// If we're running from the build dir // If we're running from the build dir
QFileInfo fi( QDir::current().absoluteFilePath( "libcalamares.so" ) ); QFileInfo fi( QDir::current().absoluteFilePath( "libcalamares.so" ) );
if ( fi.exists() && fi.isReadable() ) if ( fi.exists() && fi.isReadable() )
{
m_pythonPaths.append( fi.dir().absolutePath() ); m_pythonPaths.append( fi.dir().absolutePath() );
}
QDir calaPythonPath( CalamaresUtils::systemLibDir().absolutePath() + QDir calaPythonPath( CalamaresUtils::systemLibDir().absolutePath() +
QDir::separator() + "calamares" ); QDir::separator() + "calamares" );
if ( calaPythonPath.exists() && if ( calaPythonPath.exists() &&
calaPythonPath.isReadable() ) calaPythonPath.isReadable() )
{ {
QFileInfo fi( calaPythonPath.absoluteFilePath( "libcalamares.so" ) ); QFileInfo fi( calaPythonPath.absoluteFilePath( "libcalamares.so" ) );
if ( fi.exists() && fi.isReadable() ) if ( fi.exists() && fi.isReadable() )
{
m_pythonPaths.append( fi.dir().absolutePath() ); m_pythonPaths.append( fi.dir().absolutePath() );
}
} }

View File

@ -28,7 +28,8 @@
#include <boost/python/list.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::object variantToPyObject( const QVariant& variant );
QVariant variantFromPyObject( const boost::python::object& pyObject ); QVariant variantFromPyObject( const boost::python::object& pyObject );
@ -39,6 +40,9 @@ QVariantList variantListFromPyList( const boost::python::list& pyList
boost::python::dict variantMapToPyDict( const QVariantMap& variantMap ); boost::python::dict variantMapToPyDict( const QVariantMap& variantMap );
QVariantMap variantMapFromPyDict( const boost::python::dict& pyDict ); QVariantMap variantMapFromPyDict( const boost::python::dict& pyDict );
boost::python::dict variantHashToPyDict( const QVariantHash& variantHash );
QVariantHash variantHashFromPyDict( const boost::python::dict& pyDict );
class Helper : public QObject class Helper : public QObject
{ {