From 261c545476a4fa4a1b18524929dc0ec22a355bf1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 26 Feb 2018 15:04:20 +0100 Subject: [PATCH] [libcalamaresui] Refactor loading of YAML to QVariantMap --- src/libcalamares/utils/YamlUtils.cpp | 46 +++++++++++++++++++++++++++- src/libcalamares/utils/YamlUtils.h | 12 +++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/libcalamares/utils/YamlUtils.cpp b/src/libcalamares/utils/YamlUtils.cpp index 962cbd1da..3bc9c36d2 100644 --- a/src/libcalamares/utils/YamlUtils.cpp +++ b/src/libcalamares/utils/YamlUtils.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Teo Mrnjavac - * Copyright 2017, Adriaan de Groot + * Copyright 2017-2018, Adriaan de Groot * * 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 #include +#include +#include #include 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 diff --git a/src/libcalamares/utils/YamlUtils.h b/src/libcalamares/utils/YamlUtils.h index 33ee8dca0..ad8de7d57 100644 --- a/src/libcalamares/utils/YamlUtils.h +++ b/src/libcalamares/utils/YamlUtils.h @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Teo Mrnjavac - * Copyright 2017, Adriaan de Groot + * Copyright 2017-2018, Adriaan de Groot * * 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 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 );