From 940860107445dbdc499a4a7f737ba84e40948674 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 12:37:43 +0100 Subject: [PATCH] [libcalamares] Move Python wrapper - Take the Python wrapper for GlobalStorage out of the GlobalStorage.h header and add it to PythonHelper instead, saving some work in all the cases that only GS is interesting, not the Python bits. --- src/libcalamares/GlobalStorage.cpp | 77 ------------------------------ src/libcalamares/GlobalStorage.h | 43 ----------------- src/libcalamares/PythonHelper.cpp | 63 ++++++++++++++++++++++++ src/libcalamares/PythonHelper.h | 27 +++++++++++ 4 files changed, 90 insertions(+), 120 deletions(-) diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index 2ccaf79b4..428b01103 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -27,11 +27,6 @@ #include #include -#ifdef WITH_PYTHON -#include "PythonHelper.h" -namespace bp = boost::python; -#endif - using CalamaresUtils::operator""_MiB; namespace Calamares @@ -161,75 +156,3 @@ GlobalStorage::loadYaml( const QString& filename ) } // namespace Calamares - -#ifdef WITH_PYTHON - -namespace CalamaresPython -{ - -Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr; - -// The special handling for nullptr is only for the testing -// script for the python bindings, which passes in None; -// normal use will have a GlobalStorage from JobQueue::instance() -// passed in. Testing use will leak the allocated GlobalStorage -// object, but that's OK for testing. -GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ) - : m_gs( gs ? gs : s_gs_instance ) -{ - if ( !m_gs ) - { - s_gs_instance = new Calamares::GlobalStorage; - m_gs = s_gs_instance; - } -} - -bool -GlobalStoragePythonWrapper::contains( const std::string& key ) const -{ - return m_gs->contains( QString::fromStdString( key ) ); -} - - -int -GlobalStoragePythonWrapper::count() const -{ - return m_gs->count(); -} - - -void -GlobalStoragePythonWrapper::insert( const std::string& key, const bp::object& value ) -{ - m_gs->insert( QString::fromStdString( key ), CalamaresPython::variantFromPyObject( value ) ); -} - -bp::list -GlobalStoragePythonWrapper::keys() const -{ - bp::list pyList; - const auto keys = m_gs->keys(); - for ( const QString& key : keys ) - { - pyList.append( key.toStdString() ); - } - return pyList; -} - - -int -GlobalStoragePythonWrapper::remove( const std::string& key ) -{ - return m_gs->remove( QString::fromStdString( key ) ); -} - - -bp::object -GlobalStoragePythonWrapper::value( const std::string& key ) const -{ - return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) ); -} - -} // namespace CalamaresPython - -#endif // WITH_PYTHON diff --git a/src/libcalamares/GlobalStorage.h b/src/libcalamares/GlobalStorage.h index b070e23f6..bef9ec1cc 100644 --- a/src/libcalamares/GlobalStorage.h +++ b/src/libcalamares/GlobalStorage.h @@ -26,20 +26,6 @@ #include #include -#ifdef WITH_PYTHON -namespace boost -{ -namespace python -{ -namespace api -{ -class object; -} -class list; -} // namespace python -} // namespace boost -#endif - namespace Calamares { @@ -106,33 +92,4 @@ 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; - - // This is a helper for scripts that do not go through - // the JobQueue (i.e. the module testpython script), - // which allocate their own (singleton) GlobalStorage. - static Calamares::GlobalStorage* globalStorageInstance() { return s_gs_instance; } - -private: - Calamares::GlobalStorage* m_gs; - static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance() -}; - -} // namespace CalamaresPython -#endif - #endif // CALAMARES_GLOBALSTORAGE_H diff --git a/src/libcalamares/PythonHelper.cpp b/src/libcalamares/PythonHelper.cpp index 88008692e..d9db8581e 100644 --- a/src/libcalamares/PythonHelper.cpp +++ b/src/libcalamares/PythonHelper.cpp @@ -19,6 +19,7 @@ #include "PythonHelper.h" +#include "GlobalStorage.h" #include "utils/Dirs.h" #include "utils/Logger.h" @@ -390,5 +391,67 @@ Helper::handleLastError() return QString( "
%1
" ).arg( msgList.join( "
" ) ); } +Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr; + +// The special handling for nullptr is only for the testing +// script for the python bindings, which passes in None; +// normal use will have a GlobalStorage from JobQueue::instance() +// passed in. Testing use will leak the allocated GlobalStorage +// object, but that's OK for testing. +GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ) + : m_gs( gs ? gs : s_gs_instance ) +{ + if ( !m_gs ) + { + s_gs_instance = new Calamares::GlobalStorage; + m_gs = s_gs_instance; + } +} + +bool +GlobalStoragePythonWrapper::contains( const std::string& key ) const +{ + return m_gs->contains( QString::fromStdString( key ) ); +} + + +int +GlobalStoragePythonWrapper::count() const +{ + return m_gs->count(); +} + + +void +GlobalStoragePythonWrapper::insert( const std::string& key, const bp::object& value ) +{ + m_gs->insert( QString::fromStdString( key ), CalamaresPython::variantFromPyObject( value ) ); +} + +bp::list +GlobalStoragePythonWrapper::keys() const +{ + bp::list pyList; + const auto keys = m_gs->keys(); + for ( const QString& key : keys ) + { + pyList.append( key.toStdString() ); + } + return pyList; +} + + +int +GlobalStoragePythonWrapper::remove( const std::string& key ) +{ + return m_gs->remove( QString::fromStdString( key ) ); +} + + +bp::object +GlobalStoragePythonWrapper::value( const std::string& key ) const +{ + return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) ); +} } // namespace CalamaresPython diff --git a/src/libcalamares/PythonHelper.h b/src/libcalamares/PythonHelper.h index bb37eb868..418c75e5f 100644 --- a/src/libcalamares/PythonHelper.h +++ b/src/libcalamares/PythonHelper.h @@ -25,6 +25,11 @@ #include +namespace Calamares +{ +class GlobalStorage; +} + namespace CalamaresPython { @@ -62,6 +67,28 @@ private: QStringList m_pythonPaths; }; +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; + + // This is a helper for scripts that do not go through + // the JobQueue (i.e. the module testpython script), + // which allocate their own (singleton) GlobalStorage. + static Calamares::GlobalStorage* globalStorageInstance() { return s_gs_instance; } + +private: + Calamares::GlobalStorage* m_gs; + static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance() +}; + } // namespace CalamaresPython #endif // CALAMARES_PYTHONJOBHELPER_H