[libcalamaresui] Refactor loading of YAML to QVariantMap
This commit is contained in:
parent
f26ac63c07
commit
261c545476
@ -1,7 +1,7 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2017, Adriaan de Groot <groot@kde.org>
|
||||
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -23,6 +23,8 @@
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QRegExp>
|
||||
|
||||
void
|
||||
@ -146,4 +148,46 @@ explainYamlException( const YAML::Exception& e, const QByteArray& yamlData, cons
|
||||
}
|
||||
}
|
||||
|
||||
QVariantMap
|
||||
loadYaml(const QFileInfo& fi, bool* ok)
|
||||
{
|
||||
return loadYaml( fi.absoluteFilePath(), ok );
|
||||
}
|
||||
|
||||
QVariantMap
|
||||
loadYaml(const QString& filename, bool* ok)
|
||||
{
|
||||
if ( ok )
|
||||
*ok = false;
|
||||
|
||||
QFile descriptorFile( filename );
|
||||
QVariant moduleDescriptor;
|
||||
if ( descriptorFile.exists() && descriptorFile.open( QFile::ReadOnly | QFile::Text ) )
|
||||
{
|
||||
QByteArray ba = descriptorFile.readAll();
|
||||
try
|
||||
{
|
||||
YAML::Node doc = YAML::Load( ba.constData() );
|
||||
moduleDescriptor = CalamaresUtils::yamlToVariant( doc );
|
||||
}
|
||||
catch ( YAML::Exception& e )
|
||||
{
|
||||
explainYamlException( e, ba, filename.toLatin1().constData() );
|
||||
return QVariantMap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( moduleDescriptor.isValid() &&
|
||||
!moduleDescriptor.isNull() &&
|
||||
moduleDescriptor.type() == QVariant::Map )
|
||||
{
|
||||
if ( ok )
|
||||
*ok = true;
|
||||
return moduleDescriptor.toMap();
|
||||
}
|
||||
|
||||
return QVariantMap();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2017, Adriaan de Groot <groot@kde.org>
|
||||
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -24,6 +24,7 @@
|
||||
#include <QVariant>
|
||||
|
||||
class QByteArray;
|
||||
class QFileInfo;
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
@ -35,6 +36,15 @@ void operator>>( const YAML::Node& node, QStringList& v );
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
/**
|
||||
* Loads a given @p filename and returns the YAML data
|
||||
* as a QVariantMap. If filename doesn't exist, or is
|
||||
* malformed in some way, returns an empty map and sets
|
||||
* @p *ok to false. Otherwise sets @p *ok to true.
|
||||
*/
|
||||
QVariantMap loadYaml( const QString& filename, bool* ok = nullptr );
|
||||
/** Convenience overload. */
|
||||
QVariantMap loadYaml( const QFileInfo&, bool* ok = nullptr );
|
||||
|
||||
QVariant yamlToVariant( const YAML::Node& node );
|
||||
QVariant yamlScalarToVariant( const YAML::Node& scalarNode );
|
||||
|
Loading…
Reference in New Issue
Block a user