diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index 4f98ea2eb..54331eb3e 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -21,6 +21,7 @@ #include "JobQueue.h" #include "utils/Logger.h" +#include "utils/YamlUtils.h" #include #include @@ -110,6 +111,23 @@ GlobalStorage::save(const QString& filename) } +bool +GlobalStorage::saveYaml( const QString& filename ) +{ + return CalamaresUtils::saveYaml( filename, m ); +} + +bool +GlobalStorage::loadYaml( const QString& filename ) +{ + bool ok = false; + auto gs = CalamaresUtils::loadYaml( filename, &ok ); + if ( ok ) + m = gs; + return ok; +} + + } // namespace Calamares #ifdef WITH_PYTHON diff --git a/src/libcalamares/utils/YamlUtils.cpp b/src/libcalamares/utils/YamlUtils.cpp index e54bdfc94..80e2a316a 100644 --- a/src/libcalamares/utils/YamlUtils.cpp +++ b/src/libcalamares/utils/YamlUtils.cpp @@ -203,4 +203,38 @@ loadYaml(const QString& filename, bool* ok) return QVariantMap(); } +/// @brief Convenience function writes @p indent times four spaces +static void +writeIndent( QFile& f, int indent ) +{ + while ( indent > 0 ) + f.write( " " ); +} + +/// @brief Recursive helper to dump @p map to file +static bool +dumpYaml( QFile& f, const QVariantMap& map, int indent ) +{ + for ( auto it = map.cbegin(); it != map.cend(); ++it ) + { + writeIndent( f, indent ); + f.write( it.key().toUtf8() ); + f.write( "\n" ); + } + return true; +} + +bool +saveYaml( const QString& filename, const QVariantMap& map ) +{ + QFile f( filename ); + if ( !f.open( QFile::WriteOnly ) ) + return false; + + f.write( "# YAML dump\n---\n" ); + return dumpYaml( f, map, 0 ); +} + + + } // namespace diff --git a/src/libcalamares/utils/YamlUtils.h b/src/libcalamares/utils/YamlUtils.h index 268c0bdc7..49c8d6613 100644 --- a/src/libcalamares/utils/YamlUtils.h +++ b/src/libcalamares/utils/YamlUtils.h @@ -51,6 +51,9 @@ QVariant yamlScalarToVariant( const YAML::Node& scalarNode ); QVariant yamlSequenceToVariant( const YAML::Node& sequenceNode ); QVariant yamlMapToVariant( const YAML::Node& mapNode ); +/// @brief Save a @p map to @p filename as YAML +bool saveYaml( const QString& filename, const QVariantMap& map ); + /** * Given an exception from the YAML parser library, explain * what is going on in terms of the data passed to the parser.