[libcalamares] Apply coding style to geoip/
This commit is contained in:
parent
a2ab91474f
commit
ec073ee188
@ -30,7 +30,7 @@ namespace CalamaresUtils
|
||||
namespace GeoIP
|
||||
{
|
||||
|
||||
GeoIPJSON::GeoIPJSON(const QString& attribute)
|
||||
GeoIPJSON::GeoIPJSON( const QString& attribute )
|
||||
: Interface( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute )
|
||||
{
|
||||
}
|
||||
@ -42,19 +42,25 @@ GeoIPJSON::GeoIPJSON(const QString& attribute)
|
||||
* "foo" of @p m, like a regular JSON lookup would.
|
||||
*/
|
||||
static QString
|
||||
selectMap( const QVariantMap& m, const QStringList& l, int index)
|
||||
selectMap( const QVariantMap& m, const QStringList& l, int index )
|
||||
{
|
||||
if ( index >= l.count() )
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString attributeName = l[index];
|
||||
QString attributeName = l[ index ];
|
||||
if ( index == l.count() - 1 )
|
||||
{
|
||||
return CalamaresUtils::getString( m, attributeName );
|
||||
}
|
||||
else
|
||||
{
|
||||
bool success = false; // bogus
|
||||
if ( m.contains( attributeName ) )
|
||||
return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index+1 );
|
||||
{
|
||||
return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index + 1 );
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
}
|
||||
@ -67,18 +73,18 @@ GeoIPJSON::rawReply( const QByteArray& data )
|
||||
YAML::Node doc = YAML::Load( data );
|
||||
|
||||
QVariant var = CalamaresUtils::yamlToVariant( doc );
|
||||
if ( !var.isNull() &&
|
||||
var.isValid() &&
|
||||
var.type() == QVariant::Map )
|
||||
if ( !var.isNull() && var.isValid() && var.type() == QVariant::Map )
|
||||
{
|
||||
return selectMap( var.toMap(), m_element.split('.'), 0 );
|
||||
return selectMap( var.toMap(), m_element.split( '.' ), 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
cWarning() << "Invalid YAML data for GeoIPJSON";
|
||||
}
|
||||
}
|
||||
catch ( YAML::Exception& e )
|
||||
{
|
||||
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
|
||||
CalamaresUtils::explainYamlException( e, data, "GeoIP data" );
|
||||
}
|
||||
|
||||
return QString();
|
||||
@ -90,5 +96,5 @@ GeoIPJSON::processReply( const QByteArray& data )
|
||||
return splitTZString( rawReply( data ) );
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -45,9 +45,9 @@ public:
|
||||
explicit GeoIPJSON( const QString& attribute = QString() );
|
||||
|
||||
virtual RegionZonePair processReply( const QByteArray& ) override;
|
||||
virtual QString rawReply(const QByteArray & ) override;
|
||||
} ;
|
||||
virtual QString rawReply( const QByteArray& ) override;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
#endif
|
||||
|
@ -48,10 +48,12 @@ getElementTexts( const QByteArray& data, const QString& tag )
|
||||
cDebug() << "GeoIP found" << tzElements.length() << "elements";
|
||||
for ( int it = 0; it < tzElements.length(); ++it )
|
||||
{
|
||||
auto e = tzElements.at(it).toElement();
|
||||
auto e = tzElements.at( it ).toElement();
|
||||
auto e_text = e.text();
|
||||
if ( !e_text.isEmpty() )
|
||||
{
|
||||
elements.append( e_text );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -60,7 +62,9 @@ getElementTexts( const QByteArray& data, const QString& tag )
|
||||
}
|
||||
|
||||
if ( elements.count() < 1 )
|
||||
{
|
||||
cWarning() << "GeopIP XML had no non-empty elements" << tag;
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
@ -71,7 +75,9 @@ GeoIPXML::rawReply( const QByteArray& data )
|
||||
{
|
||||
for ( const auto& e : getElementTexts( data, m_element ) )
|
||||
if ( !e.isEmpty() )
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
@ -83,11 +89,13 @@ GeoIPXML::processReply( const QByteArray& data )
|
||||
{
|
||||
auto tz = splitTZString( e );
|
||||
if ( !tz.first.isEmpty() )
|
||||
{
|
||||
return tz;
|
||||
}
|
||||
}
|
||||
|
||||
return RegionZonePair();
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -45,9 +45,9 @@ public:
|
||||
explicit GeoIPXML( const QString& element = QString() );
|
||||
|
||||
virtual RegionZonePair processReply( const QByteArray& ) override;
|
||||
virtual QString rawReply(const QByteArray & ) override;
|
||||
} ;
|
||||
virtual QString rawReply( const QByteArray& ) override;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
#endif
|
||||
|
@ -19,7 +19,7 @@
|
||||
#include "Handler.h"
|
||||
|
||||
#include "GeoIPJSON.h"
|
||||
#if defined(QT_XML_LIB)
|
||||
#if defined( QT_XML_LIB )
|
||||
#include "GeoIPXML.h"
|
||||
#endif
|
||||
|
||||
@ -28,8 +28,8 @@
|
||||
#include "utils/Variant.h"
|
||||
|
||||
#include <QEventLoop>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
#include <memory>
|
||||
|
||||
@ -76,7 +76,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
|
||||
{
|
||||
cWarning() << "GeoIP style *none* does not do anything.";
|
||||
}
|
||||
#if !defined(QT_XML_LIB)
|
||||
#if !defined( QT_XML_LIB )
|
||||
else if ( m_type == Type::XML )
|
||||
{
|
||||
m_type = Type::None;
|
||||
@ -85,9 +85,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
|
||||
#endif
|
||||
}
|
||||
|
||||
Handler::~Handler()
|
||||
{
|
||||
}
|
||||
Handler::~Handler() {}
|
||||
|
||||
static QByteArray
|
||||
synchronous_get( const QString& urlstring )
|
||||
@ -108,17 +106,17 @@ synchronous_get( const QString& urlstring )
|
||||
static std::unique_ptr< Interface >
|
||||
create_interface( Handler::Type t, const QString& selector )
|
||||
{
|
||||
switch( t )
|
||||
switch ( t )
|
||||
{
|
||||
case Handler::Type::None:
|
||||
return nullptr;
|
||||
case Handler::Type::JSON:
|
||||
return std::make_unique< GeoIPJSON >( selector );
|
||||
case Handler::Type::XML:
|
||||
#if defined(QT_XML_LIB)
|
||||
return std::make_unique< GeoIPXML >( selector );
|
||||
case Handler::Type::None:
|
||||
return nullptr;
|
||||
case Handler::Type::JSON:
|
||||
return std::make_unique< GeoIPJSON >( selector );
|
||||
case Handler::Type::XML:
|
||||
#if defined( QT_XML_LIB )
|
||||
return std::make_unique< GeoIPXML >( selector );
|
||||
#else
|
||||
return nullptr;
|
||||
return nullptr;
|
||||
#endif
|
||||
}
|
||||
NOTREACHED return nullptr;
|
||||
@ -129,7 +127,9 @@ do_query( Handler::Type type, const QString& url, const QString& selector )
|
||||
{
|
||||
const auto interface = create_interface( type, selector );
|
||||
if ( !interface )
|
||||
{
|
||||
return RegionZonePair();
|
||||
}
|
||||
|
||||
return interface->processReply( synchronous_get( url ) );
|
||||
}
|
||||
@ -139,7 +139,9 @@ do_raw_query( Handler::Type type, const QString& url, const QString& selector )
|
||||
{
|
||||
const auto interface = create_interface( type, selector );
|
||||
if ( !interface )
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
|
||||
return interface->rawReply( synchronous_get( url ) );
|
||||
}
|
||||
@ -148,7 +150,9 @@ RegionZonePair
|
||||
Handler::get() const
|
||||
{
|
||||
if ( !isValid() )
|
||||
{
|
||||
return RegionZonePair();
|
||||
}
|
||||
return do_query( m_type, m_url, m_selector );
|
||||
}
|
||||
|
||||
@ -160,17 +164,16 @@ Handler::query() const
|
||||
QString url = m_url;
|
||||
QString selector = m_selector;
|
||||
|
||||
return QtConcurrent::run( [=]
|
||||
{
|
||||
return do_query( type, url, selector );
|
||||
} );
|
||||
return QtConcurrent::run( [=] { return do_query( type, url, selector ); } );
|
||||
}
|
||||
|
||||
QString
|
||||
Handler::getRaw() const
|
||||
{
|
||||
if ( !isValid() )
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
return do_raw_query( m_type, m_url, m_selector );
|
||||
}
|
||||
|
||||
@ -182,11 +185,8 @@ Handler::queryRaw() const
|
||||
QString url = m_url;
|
||||
QString selector = m_selector;
|
||||
|
||||
return QtConcurrent::run( [=]
|
||||
{
|
||||
return do_raw_query( type, url, selector );
|
||||
} );
|
||||
return QtConcurrent::run( [=] { return do_raw_query( type, url, selector ); } );
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -21,9 +21,9 @@
|
||||
|
||||
#include "Interface.h"
|
||||
|
||||
#include <QtConcurrent/QtConcurrentRun>
|
||||
#include <QString>
|
||||
#include <QVariantMap>
|
||||
#include <QtConcurrent/QtConcurrentRun>
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
@ -46,7 +46,7 @@ public:
|
||||
None,
|
||||
JSON,
|
||||
XML
|
||||
} ;
|
||||
};
|
||||
|
||||
/** @brief An unconfigured handler; this always returns errors. */
|
||||
Handler();
|
||||
@ -89,7 +89,6 @@ private:
|
||||
const QString m_selector;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
#endif
|
||||
|
||||
|
@ -25,14 +25,12 @@ namespace CalamaresUtils
|
||||
namespace GeoIP
|
||||
{
|
||||
|
||||
Interface::Interface(const QString& e)
|
||||
Interface::Interface( const QString& e )
|
||||
: m_element( e )
|
||||
{
|
||||
}
|
||||
|
||||
Interface::~Interface()
|
||||
{
|
||||
}
|
||||
Interface::~Interface() {}
|
||||
|
||||
RegionZonePair
|
||||
splitTZString( const QString& tz )
|
||||
@ -53,5 +51,5 @@ splitTZString( const QString& tz )
|
||||
return RegionZonePair( QString(), QString() );
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -27,7 +27,7 @@
|
||||
|
||||
class QByteArray;
|
||||
|
||||
namespace CalamaresUtils
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
namespace GeoIP
|
||||
{
|
||||
@ -38,18 +38,27 @@ namespace GeoIP
|
||||
* pasting the strings back together with a "/" is the right thing to
|
||||
* do. The Zone **may** contain a "/" (e.g. "Kentucky/Monticello").
|
||||
*/
|
||||
class DLLEXPORT RegionZonePair : public QPair<QString, QString>
|
||||
class DLLEXPORT RegionZonePair : public QPair< QString, QString >
|
||||
{
|
||||
public:
|
||||
/** @brief Construct from an existing pair. */
|
||||
explicit RegionZonePair( const QPair& p ) : QPair(p) { }
|
||||
explicit RegionZonePair( const QPair& p )
|
||||
: QPair( p )
|
||||
{
|
||||
}
|
||||
/** @brief Construct from two strings, like qMakePair(). */
|
||||
RegionZonePair( const QString& region, const QString& zone ) : QPair( region, zone ) { }
|
||||
RegionZonePair( const QString& region, const QString& zone )
|
||||
: QPair( region, zone )
|
||||
{
|
||||
}
|
||||
/** @brief An invalid zone pair (empty strings). */
|
||||
RegionZonePair() : QPair( QString(), QString() ) { }
|
||||
RegionZonePair()
|
||||
: QPair( QString(), QString() )
|
||||
{
|
||||
}
|
||||
|
||||
bool isValid() const { return !first.isEmpty(); }
|
||||
} ;
|
||||
};
|
||||
|
||||
/** @brief Splits a region/zone string into a pair.
|
||||
*
|
||||
@ -60,8 +69,7 @@ public:
|
||||
* pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah
|
||||
* will return "America", "North_Dakota/Beulah").
|
||||
*/
|
||||
DLLEXPORT RegionZonePair
|
||||
splitTZString( const QString& s );
|
||||
DLLEXPORT RegionZonePair splitTZString( const QString& s );
|
||||
|
||||
/**
|
||||
* @brief Interface for GeoIP retrievers.
|
||||
@ -93,8 +101,8 @@ protected:
|
||||
Interface( const QString& e = QString() );
|
||||
|
||||
QString m_element; // string for selecting from data
|
||||
} ;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace
|
||||
} // namespace GeoIP
|
||||
} // namespace CalamaresUtils
|
||||
#endif
|
||||
|
@ -30,34 +30,40 @@
|
||||
using std::cerr;
|
||||
using namespace CalamaresUtils::GeoIP;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
int
|
||||
main( int argc, char** argv )
|
||||
{
|
||||
if (argc != 2)
|
||||
if ( argc != 2 )
|
||||
{
|
||||
cerr << "Usage: curl url | test_geoip <format>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Interface* handler = nullptr;
|
||||
if ( QStringLiteral( "json" ) == argv[1] )
|
||||
if ( QStringLiteral( "json" ) == argv[ 1 ] )
|
||||
{
|
||||
handler = new GeoIPJSON;
|
||||
}
|
||||
#ifdef QT_XML_LIB
|
||||
else if ( QStringLiteral( "xml" ) == argv[1] )
|
||||
else if ( QStringLiteral( "xml" ) == argv[ 1 ] )
|
||||
{
|
||||
handler = new GeoIPXML;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ( !handler )
|
||||
{
|
||||
cerr << "Unknown format '" << argv[1] << "'\n";
|
||||
cerr << "Unknown format '" << argv[ 1 ] << "'\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
QByteArray ba;
|
||||
while( !std::cin.eof() ) {
|
||||
char arr[1024];
|
||||
std::cin.read(arr, sizeof(arr));
|
||||
int s = static_cast<int>( std::cin.gcount() );
|
||||
ba.append(arr, s);
|
||||
while ( !std::cin.eof() )
|
||||
{
|
||||
char arr[ 1024 ];
|
||||
std::cin.read( arr, sizeof( arr ) );
|
||||
int s = static_cast< int >( std::cin.gcount() );
|
||||
ba.append( arr, s );
|
||||
}
|
||||
|
||||
auto tz = handler->processReply( ba );
|
||||
@ -67,7 +73,8 @@ int main(int argc, char** argv)
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "TimeZone Region=" << tz.first.toLatin1().constData() << "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n';
|
||||
std::cout << "TimeZone Region=" << tz.first.toLatin1().constData()
|
||||
<< "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user