[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:
parent
c1151cbcfa
commit
9408601074
@ -27,11 +27,6 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
|
||||||
#ifdef WITH_PYTHON
|
|
||||||
#include "PythonHelper.h"
|
|
||||||
namespace bp = boost::python;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using CalamaresUtils::operator""_MiB;
|
using CalamaresUtils::operator""_MiB;
|
||||||
|
|
||||||
namespace Calamares
|
namespace Calamares
|
||||||
@ -161,75 +156,3 @@ GlobalStorage::loadYaml( const QString& filename )
|
|||||||
|
|
||||||
|
|
||||||
} // namespace Calamares
|
} // 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
|
|
||||||
|
@ -26,20 +26,6 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVariantMap>
|
#include <QVariantMap>
|
||||||
|
|
||||||
#ifdef WITH_PYTHON
|
|
||||||
namespace boost
|
|
||||||
{
|
|
||||||
namespace python
|
|
||||||
{
|
|
||||||
namespace api
|
|
||||||
{
|
|
||||||
class object;
|
|
||||||
}
|
|
||||||
class list;
|
|
||||||
} // namespace python
|
|
||||||
} // namespace boost
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Calamares
|
namespace Calamares
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -106,33 +92,4 @@ private:
|
|||||||
|
|
||||||
} // namespace Calamares
|
} // 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
|
#endif // CALAMARES_GLOBALSTORAGE_H
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include "PythonHelper.h"
|
#include "PythonHelper.h"
|
||||||
|
|
||||||
|
#include "GlobalStorage.h"
|
||||||
#include "utils/Dirs.h"
|
#include "utils/Dirs.h"
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
@ -390,5 +391,67 @@ Helper::handleLastError()
|
|||||||
return QString( "<div>%1</div>" ).arg( msgList.join( "</div><div>" ) );
|
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
|
} // namespace CalamaresPython
|
||||||
|
@ -25,6 +25,11 @@
|
|||||||
|
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
|
namespace Calamares
|
||||||
|
{
|
||||||
|
class GlobalStorage;
|
||||||
|
}
|
||||||
|
|
||||||
namespace CalamaresPython
|
namespace CalamaresPython
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -62,6 +67,28 @@ private:
|
|||||||
QStringList m_pythonPaths;
|
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
|
} // namespace CalamaresPython
|
||||||
|
|
||||||
#endif // CALAMARES_PYTHONJOBHELPER_H
|
#endif // CALAMARES_PYTHONJOBHELPER_H
|
||||||
|
Loading…
Reference in New Issue
Block a user