[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.
This commit is contained in:
Adriaan de Groot 2020-02-12 12:37:43 +01:00
parent c1151cbcfa
commit 9408601074
4 changed files with 90 additions and 120 deletions

View file

@ -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( "<div>%1</div>" ).arg( msgList.join( "</div><div>" ) );
}
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