[libcalamares] Expand GlobalStorage with load()

- Counterpart to save(), for JSON-style dumps
This commit is contained in:
Adriaan de Groot 2018-07-02 06:11:57 -04:00
parent 20cf0c8f3d
commit c19ce26e5f
2 changed files with 35 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include "utils/Logger.h"
#include "utils/Yaml.h"
#include "utils/Units.h"
#include <QFile>
#include <QJsonDocument>
@ -37,6 +38,8 @@
namespace bp = boost::python;
#endif
using CalamaresUtils::operator""_MiB;
namespace Calamares {
GlobalStorage::GlobalStorage()
@ -110,6 +113,30 @@ GlobalStorage::save(const QString& filename)
return true;
}
bool
GlobalStorage::load( const QString& filename )
{
QFile f( filename );
if ( !f.open( QFile::ReadOnly ) )
return false;
QJsonParseError e;
QJsonDocument d = QJsonDocument::fromJson( f.read( 1_MiB ), &e );
if ( d.isNull() )
cWarning() << filename << e.errorString();
else if ( !d.isObject() )
cWarning() << filename << "Not suitable JSON.";
else
{
auto map = d.toVariant().toMap();
for( auto i = map.constBegin() ; i != map.constEnd() ; ++i )
{
insert( i.key(), *i );
}
return true;
}
return false;
}
bool
GlobalStorage::saveYaml( const QString& filename )

View File

@ -70,6 +70,14 @@ public:
*/
bool save( const QString& filename );
/** @brief Adds the keys from the given JSON file
*
* No tidying, sanitization, or censoring is done.
* The JSON file is read and each key is added as a value to
* the global storage.
*/
bool load( const QString& filename );
/** @brief write as YAML to the given filename
*
* See also save(), above.