[libcalamares] Stub of saveYaml

- This belongs alongside loadYaml, so place it in libcalamares
 - Doesn't actually save anything yet (it isn't used yet)
This commit is contained in:
Adriaan de Groot 2019-01-28 07:50:30 -05:00
parent 5efbf51ed3
commit 5e1bcd9b4a
3 changed files with 55 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#include "JobQueue.h"
#include "utils/Logger.h"
#include "utils/YamlUtils.h"
#include <QFile>
#include <QJsonDocument>
@ -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

View File

@ -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

View File

@ -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.