[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

@ -27,11 +27,6 @@
#include <QFile>
#include <QJsonDocument>
#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

View File

@ -26,20 +26,6 @@
#include <QString>
#include <QVariantMap>
#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

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

View File

@ -25,6 +25,11 @@
#include <QStringList>
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