From fcbe8d3a3e6dae3de9c08eaf292b5c9093aac395 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 29 Nov 2021 12:46:12 +0100 Subject: [PATCH] [libcalamares] API for YAML-loading from Python --- src/libcalamares/PythonJob.cpp | 11 ++++++++++- src/libcalamares/PythonJobApi.cpp | 8 ++++++++ src/libcalamares/PythonJobApi.h | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index 6176f0312..291adbc54 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -79,6 +79,7 @@ BOOST_PYTHON_MODULE( libcalamares ) bp::scope utilsScope = utilsModule; Q_UNUSED( utilsScope ) + // .. Logging functions bp::def( "debug", &CalamaresPython::debug, bp::args( "s" ), "Writes the given string to the Calamares debug stream." ); bp::def( "warning", @@ -92,6 +93,11 @@ BOOST_PYTHON_MODULE( libcalamares ) bp::def( "error", &CalamaresPython::warning, bp::args( "s" ), "Writes the given string to the Calamares error stream." ); + + // .. YAML functions + bp::def( "load_yaml", &CalamaresPython::load_yaml, bp::args( "path" ), "Loads YAML from a file." ); + + // .. Filesystem functions bp::def( "mount", &CalamaresPython::mount, mount_overloads( bp::args( "device_path", "mount_point", "filesystem_name", "options" ), @@ -100,6 +106,8 @@ BOOST_PYTHON_MODULE( libcalamares ) "-1 = QProcess crash\n" "-2 = QProcess cannot start\n" "-3 = bad arguments" ) ); + + // .. Process functions bp::def( "target_env_call", static_cast< int ( * )( const std::string&, const std::string&, int ) >( &CalamaresPython::target_env_call ), @@ -158,6 +166,7 @@ BOOST_PYTHON_MODULE( libcalamares ) host_env_process_output_overloads( bp::args( "command", "callback", "stdin", "timeout" ), "Runs the specified command in the host system." ) ); + // .. String functions bp::def( "obscure", &CalamaresPython::obscure, bp::args( "s" ), @@ -166,7 +175,7 @@ BOOST_PYTHON_MODULE( libcalamares ) "Applying the function to a string obscured by this function will result " "in the original string." ); - + // .. Translation functions bp::def( "gettext_languages", &CalamaresPython::gettext_languages, "Returns list of languages (most to least-specific) for gettext." ); diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index b1ff536be..869bda81e 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -19,6 +19,7 @@ #include "utils/RAII.h" #include "utils/Runner.h" #include "utils/String.h" +#include "utils/Yaml.h" #include #include @@ -163,6 +164,13 @@ error( const std::string& s ) log_action( Logger::LOGERROR, s ); } +boost::python::dict +load_yaml( const std::string& path ) +{ + return variantMapToPyDict( CalamaresUtils::loadYaml( QString::fromStdString( path ) ) ); +} + + PythonJobInterface::PythonJobInterface( Calamares::PythonJob* parent ) : m_parent( parent ) { diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 0ecc43abd..62346ceda 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -62,6 +62,11 @@ void debug( const std::string& s ); void warning( const std::string& s ); void error( const std::string& s ); +/** @brief Loads YAML and returns (nested) dicts representing it + * + */ +boost::python::dict load_yaml( const std::string& path ); + class PythonJobInterface { public: