From 252089e372a2200fe9f27fb17b15058a649bb207 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 9 Mar 2020 15:01:07 -0500 Subject: [PATCH] [libcalamares] Refactor pretty_name() call - Split out a general method-that-returns-string caller. --- src/libcalamares/PythonJob.cpp | 39 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index dd0ea47c1..37859b349 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -215,6 +215,18 @@ PythonJob::prettyStatusMessage() const } } +static QString +pythonStringMethod( bp::dict& script, const char* funcName ) +{ + bp::object func = script.get( funcName, bp::object() ); + if ( !func.is_none() ) + { + bp::extract< std::string > result( func() ); + return result.check() ? QString::fromStdString( result() ).trimmed() : QString(); + } + return QString(); +} + JobResult PythonJob::exec() @@ -248,27 +260,12 @@ PythonJob::exec() calamaresNamespace[ "globalstorage" ] = CalamaresPython::GlobalStoragePythonWrapper( JobQueue::instance()->globalStorage() ); + cDebug() << "Job file" << scriptFI.absoluteFilePath(); bp::object execResult = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), scriptNamespace, scriptNamespace ); - bp::object entryPoint = scriptNamespace[ "run" ]; - bp::object prettyNameFunc = scriptNamespace.get( "pretty_name", bp::object() ); - - cDebug() << "Job file" << scriptFI.absoluteFilePath(); - if ( !prettyNameFunc.is_none() ) - { - bp::extract< std::string > prettyNameResult( prettyNameFunc() ); - if ( prettyNameResult.check() ) - { - m_description = QString::fromStdString( prettyNameResult() ).trimmed(); - } - if ( !m_description.isEmpty() ) - { - cDebug() << "Job description from pretty_name" << prettyName() << "=" << m_description; - emit progress( 0 ); - } - } + m_description = pythonStringMethod( scriptNamespace, "pretty_name" ); if ( m_description.isEmpty() ) { bp::extract< std::string > entryPoint_doc_attr( entryPoint.attr( "__doc__" ) ); @@ -281,10 +278,14 @@ PythonJob::exec() { m_description.truncate( i_newline ); } - cDebug() << "Job description from __doc__" << prettyName() << "=" << m_description; - emit progress( 0 ); + cDebug() << "Job description from __doc__" << prettyName() << '=' << m_description; } } + else + { + cDebug() << "Job description from pretty_name" << prettyName() << '=' << m_description; + } + emit progress( 0 ); bp::object runResult = entryPoint();