New GlobalStoragePythonWrapper is passed to the Python API.

This allows us to make GlobalStorage a QObject, with disabled copy
constructor. And thanks to this change, GlobalStorage now emits
changed() every time an insert or remove is performed.
This commit is contained in:
Teo Mrnjavac 2015-03-05 19:35:26 +01:00
parent 060983279f
commit edb21e05af
3 changed files with 70 additions and 32 deletions

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,6 +31,7 @@ namespace bp = boost::python;
namespace Calamares {
GlobalStorage::GlobalStorage()
: QObject( nullptr )
{
}
@ -53,6 +54,7 @@ void
GlobalStorage::insert( const QString& key, const QVariant& value )
{
m.insert( key, value );
emit changed();
}
@ -67,6 +69,7 @@ int
GlobalStorage::remove( const QString& key )
{
return m.remove( key );
emit changed();
}
@ -76,47 +79,63 @@ GlobalStorage::value( const QString& key ) const
return m.value( key );
}
} // namespace Calamares
#ifdef WITH_PYTHON
bool
GlobalStorage::python_contains( const std::string& key ) const
namespace CalamaresPython
{
return contains( QString::fromStdString( key ) );
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
: m_gs( gs )
{}
bool
GlobalStoragePythonWrapper::contains( const std::string& key ) const
{
return m_gs->contains( QString::fromStdString( key ) );
}
int
GlobalStoragePythonWrapper::count() const
{
return m_gs->count();
}
void
GlobalStorage::python_insert( const std::string& key,
GlobalStoragePythonWrapper::insert( const std::string& key,
const bp::object& value )
{
insert( QString::fromStdString( key ),
m_gs->insert( QString::fromStdString( key ),
CalamaresPython::variantFromPyObject( value ) );
}
bp::list
GlobalStorage::python_keys() const
GlobalStoragePythonWrapper::keys() const
{
bp::list pyList;
foreach( const QString& key, keys() )
foreach( const QString& key, m_gs->keys() )
pyList.append( key.toStdString() );
return pyList;
}
int
GlobalStorage::python_remove( const std::string& key )
GlobalStoragePythonWrapper::remove( const std::string& key )
{
return remove( QString::fromStdString( key ) );
return m_gs->remove( QString::fromStdString( key ) );
}
bp::object
GlobalStorage::python_value( const std::string& key ) const
GlobalStoragePythonWrapper::value( const std::string& key ) const
{
return CalamaresPython::variantToPyObject( value( QString::fromStdString( key ) ) );
return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) );
}
#endif // WITH_PYTHON
} // namespace CalamaresPython
} // namespace Calamares
#endif // WITH_PYTHON

View File

@ -42,8 +42,9 @@ namespace Calamares
class DebugWindow;
class GlobalStorage
class GlobalStorage : public QObject
{
Q_OBJECT
public:
explicit GlobalStorage();
@ -56,13 +57,8 @@ public:
int remove( const QString& key );
QVariant value( const QString& key ) const;
#ifdef WITH_PYTHON
bool python_contains( const std::string& key ) const;
void python_insert( const std::string& key, const boost::python::api::object& value );
boost::python::list python_keys() const;
int python_remove( const std::string& key );
boost::python::api::object python_value( const std::string& key ) const;
#endif
signals:
void changed();
private:
QVariantMap m;
@ -72,4 +68,26 @@ private:
} // namespace Calamares
#ifdef WITH_PYTHON
namespace CalamaresPython
{
class GlobalStoragePythonWrapper
{
public:
explicit GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs );
bool contains( const std::string& key ) const;
int count() const;
void insert( const std::string& key, const boost::python::api::object& value );
boost::python::list keys() const;
int remove( const std::string& key );
boost::python::api::object value( const std::string& key ) const;
private:
Calamares::GlobalStorage* m_gs;
};
} // namespace CalamaresPython
#endif
#endif // CALAMARES_GLOBALSTORAGE_H

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -79,13 +79,13 @@ BOOST_PYTHON_MODULE( libcalamares )
"as a real number between 0 and 1."
);
bp::class_< Calamares::GlobalStorage >( "GlobalStorage", bp::init<>() )
.def( "contains", &Calamares::GlobalStorage::python_contains )
.def( "count", &Calamares::GlobalStorage::count )
.def( "insert", &Calamares::GlobalStorage::python_insert )
.def( "keys", &Calamares::GlobalStorage::python_keys )
.def( "remove", &Calamares::GlobalStorage::python_remove )
.def( "value", &Calamares::GlobalStorage::python_value );
bp::class_< CalamaresPython::GlobalStoragePythonWrapper >( "GlobalStorage", bp::init< Calamares::GlobalStorage* >() )
.def( "contains", &CalamaresPython::GlobalStoragePythonWrapper::contains )
.def( "count", &CalamaresPython::GlobalStoragePythonWrapper::count )
.def( "insert", &CalamaresPython::GlobalStoragePythonWrapper::insert )
.def( "keys", &CalamaresPython::GlobalStoragePythonWrapper::keys )
.def( "remove", &CalamaresPython::GlobalStoragePythonWrapper::remove )
.def( "value", &CalamaresPython::GlobalStoragePythonWrapper::value );
// libcalamares.utils submodule starts here
bp::object utilsModule( bp::handle<>( bp::borrowed( PyImport_AddModule( "libcalamares.utils" ) ) ) );
@ -271,7 +271,8 @@ PythonJob::exec()
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
calamaresNamespace[ "job" ] = CalamaresPython::PythonJobInterface( this );
calamaresNamespace[ "globalstorage" ] = bp::ptr( JobQueue::instance()->globalStorage() );
calamaresNamespace[ "globalstorage" ] = CalamaresPython::GlobalStoragePythonWrapper(
JobQueue::instance()->globalStorage() );
bp::object execResult = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(),
scriptNamespace,