mirror of
https://github.com/parchlinux/calamares.git
synced 2025-06-28 09:55:37 -04:00
[libcalamaresui] Refactor loading of YAML to QVariantMap
This commit is contained in:
parent
f26ac63c07
commit
261c545476
2 changed files with 56 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
||||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
*
|
*
|
||||||
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
* 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
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -23,6 +23,8 @@
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
|
|
||||||
void
|
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
|
} // namespace
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
*
|
*
|
||||||
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
* 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
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -24,6 +24,7 @@
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
class QByteArray;
|
class QByteArray;
|
||||||
|
class QFileInfo;
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
|
@ -35,6 +36,15 @@ void operator>>( const YAML::Node& node, QStringList& v );
|
||||||
|
|
||||||
namespace CalamaresUtils
|
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 yamlToVariant( const YAML::Node& node );
|
||||||
QVariant yamlScalarToVariant( const YAML::Node& scalarNode );
|
QVariant yamlScalarToVariant( const YAML::Node& scalarNode );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue