mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-07-09 11:55:52 +00:00
Replace yahoo finance to stooq (#131)
Old code is dropped to separated provider
This commit is contained in:
@ -24,8 +24,7 @@
|
||||
#include "awkeysaggregator.h"
|
||||
|
||||
|
||||
QString AWPatternFunctions::expandLambdas(QString _code,
|
||||
AWKeysAggregator *_aggregator,
|
||||
QString AWPatternFunctions::expandLambdas(QString _code, AWKeysAggregator *_aggregator,
|
||||
const QVariantHash &_metadata,
|
||||
const QStringList &_usedKeys)
|
||||
{
|
||||
@ -41,9 +40,8 @@ QString AWPatternFunctions::expandLambdas(QString _code,
|
||||
qCInfo(LOG_AW) << "Expression" << _code;
|
||||
QJSValue result = engine.evaluate(_code);
|
||||
if (result.isError()) {
|
||||
qCWarning(LOG_AW) << "Uncaught exception at line"
|
||||
<< result.property("lineNumber").toInt() << ":"
|
||||
<< result.toString();
|
||||
qCWarning(LOG_AW) << "Uncaught exception at line" << result.property("lineNumber").toInt()
|
||||
<< ":" << result.toString();
|
||||
return "";
|
||||
} else {
|
||||
return result.toString();
|
||||
@ -57,8 +55,7 @@ QString AWPatternFunctions::expandTemplates(QString _code)
|
||||
|
||||
// match the following construction $template{{some code here}}
|
||||
QRegularExpression templatesRegexp("\\$template\\{\\{(?<body>.*?)\\}\\}");
|
||||
templatesRegexp.setPatternOptions(
|
||||
QRegularExpression::DotMatchesEverythingOption);
|
||||
templatesRegexp.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
|
||||
|
||||
QRegularExpressionMatchIterator it = templatesRegexp.globalMatch(_code);
|
||||
while (it.hasNext()) {
|
||||
@ -71,8 +68,7 @@ QString AWPatternFunctions::expandTemplates(QString _code)
|
||||
QString templateResult = "";
|
||||
if (result.isError()) {
|
||||
qCWarning(LOG_AW) << "Uncaught exception at line"
|
||||
<< result.property("lineNumber").toInt() << ":"
|
||||
<< result.toString();
|
||||
<< result.property("lineNumber").toInt() << ":" << result.toString();
|
||||
} else {
|
||||
templateResult = result.toString();
|
||||
}
|
||||
@ -86,8 +82,7 @@ QString AWPatternFunctions::expandTemplates(QString _code)
|
||||
|
||||
|
||||
QList<AWPatternFunctions::AWFunction>
|
||||
AWPatternFunctions::findFunctionCalls(const QString &_function,
|
||||
const QString &_code)
|
||||
AWPatternFunctions::findFunctionCalls(const QString &_function, const QString &_code)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for function" << _function << "in" << _code;
|
||||
|
||||
@ -98,8 +93,7 @@ AWPatternFunctions::findFunctionCalls(const QString &_function,
|
||||
// * body depends on the function name, double brackets should be screened
|
||||
// by using $, e.g. ${
|
||||
QRegularExpression regex(
|
||||
QString("\\$%1\\<(?<args>.*?)\\>\\{\\{(?<body>.*?)\\}\\}")
|
||||
.arg(_function));
|
||||
QString("\\$%1\\<(?<args>.*?)\\>\\{\\{(?<body>.*?)\\}\\}").arg(_function));
|
||||
regex.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
|
||||
|
||||
QList<AWPatternFunctions::AWFunction> foundFunctions;
|
||||
@ -116,9 +110,8 @@ AWPatternFunctions::findFunctionCalls(const QString &_function,
|
||||
// replace '$,' to 0x1d
|
||||
argsString.replace("$,", QString(0x1d));
|
||||
QStringList args = argsString.split(',');
|
||||
std::for_each(args.begin(), args.end(), [](QString &arg) {
|
||||
arg.replace(QString(0x1d), ",");
|
||||
});
|
||||
std::for_each(args.begin(), args.end(),
|
||||
[](QString &arg) { arg.replace(QString(0x1d), ","); });
|
||||
metadata.args = args;
|
||||
}
|
||||
// other variables
|
||||
@ -134,20 +127,17 @@ AWPatternFunctions::findFunctionCalls(const QString &_function,
|
||||
}
|
||||
|
||||
|
||||
QString AWPatternFunctions::insertAllKeys(QString _code,
|
||||
const QStringList &_keys)
|
||||
QString AWPatternFunctions::insertAllKeys(QString _code, const QStringList &_keys)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list"
|
||||
<< _keys;
|
||||
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list" << _keys;
|
||||
|
||||
QList<AWPatternFunctions::AWFunction> found
|
||||
= AWPatternFunctions::findFunctionCalls("aw_all", _code);
|
||||
for (auto &function : found) {
|
||||
QString separator = function.args.isEmpty() ? "," : function.args.at(0);
|
||||
QStringList required = _keys.filter(QRegExp(function.body));
|
||||
std::for_each(required.begin(), required.end(), [](QString &value) {
|
||||
value = QString("%1: $%1").arg(value);
|
||||
});
|
||||
std::for_each(required.begin(), required.end(),
|
||||
[](QString &value) { value = QString("%1: $%1").arg(value); });
|
||||
|
||||
_code.replace(function.what, required.join(separator));
|
||||
}
|
||||
@ -156,11 +146,9 @@ QString AWPatternFunctions::insertAllKeys(QString _code,
|
||||
}
|
||||
|
||||
|
||||
QString AWPatternFunctions::insertKeyCount(QString _code,
|
||||
const QStringList &_keys)
|
||||
QString AWPatternFunctions::insertKeyCount(QString _code, const QStringList &_keys)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for count in code" << _code << "using list"
|
||||
<< _keys;
|
||||
qCDebug(LOG_AW) << "Looking for count in code" << _code << "using list" << _keys;
|
||||
|
||||
QList<AWPatternFunctions::AWFunction> found
|
||||
= AWPatternFunctions::findFunctionCalls("aw_count", _code);
|
||||
@ -174,11 +162,9 @@ QString AWPatternFunctions::insertKeyCount(QString _code,
|
||||
}
|
||||
|
||||
|
||||
QString AWPatternFunctions::insertKeyNames(QString _code,
|
||||
const QStringList &_keys)
|
||||
QString AWPatternFunctions::insertKeyNames(QString _code, const QStringList &_keys)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for key names in code" << _code << "using list"
|
||||
<< _keys;
|
||||
qCDebug(LOG_AW) << "Looking for key names in code" << _code << "using list" << _keys;
|
||||
|
||||
QList<AWPatternFunctions::AWFunction> found
|
||||
= AWPatternFunctions::findFunctionCalls("aw_names", _code);
|
||||
@ -195,17 +181,15 @@ QString AWPatternFunctions::insertKeyNames(QString _code,
|
||||
|
||||
QString AWPatternFunctions::insertKeys(QString _code, const QStringList &_keys)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list"
|
||||
<< _keys;
|
||||
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list" << _keys;
|
||||
|
||||
QList<AWPatternFunctions::AWFunction> found
|
||||
= AWPatternFunctions::findFunctionCalls("aw_keys", _code);
|
||||
for (auto &function : found) {
|
||||
QString separator = function.args.isEmpty() ? "," : function.args.at(0);
|
||||
QStringList required = _keys.filter(QRegExp(function.body));
|
||||
std::for_each(required.begin(), required.end(), [](QString &value) {
|
||||
value = QString("$%1").arg(value);
|
||||
});
|
||||
std::for_each(required.begin(), required.end(),
|
||||
[](QString &value) { value = QString("$%1").arg(value); });
|
||||
|
||||
_code.replace(function.what, required.join(separator));
|
||||
}
|
||||
@ -229,13 +213,11 @@ QString AWPatternFunctions::insertMacros(QString _code)
|
||||
QString name = macro.args.takeFirst();
|
||||
// find macro usage
|
||||
QList<AWPatternFunctions::AWFunction> macroUsage
|
||||
= AWPatternFunctions::findFunctionCalls(
|
||||
QString("aw_macro_%1").arg(name), _code);
|
||||
= AWPatternFunctions::findFunctionCalls(QString("aw_macro_%1").arg(name), _code);
|
||||
for (auto &function : macroUsage) {
|
||||
if (function.args.count() != macro.args.count()) {
|
||||
qCWarning(LOG_AW)
|
||||
<< "Invalid args count found for call" << function.what
|
||||
<< "with macro" << macro.what;
|
||||
qCWarning(LOG_AW) << "Invalid args count found for call" << function.what
|
||||
<< "with macro" << macro.what;
|
||||
continue;
|
||||
}
|
||||
// generate body to replace
|
||||
@ -243,8 +225,7 @@ QString AWPatternFunctions::insertMacros(QString _code)
|
||||
std::for_each(macro.args.cbegin(), macro.args.cend(),
|
||||
[&result, macro, function](const QString &arg) {
|
||||
int index = macro.args.indexOf(arg);
|
||||
result.replace(QString("$%1").arg(arg),
|
||||
function.args.at(index));
|
||||
result.replace(QString("$%1").arg(arg), function.args.at(index));
|
||||
});
|
||||
// do replace
|
||||
_code.replace(function.what, result);
|
||||
@ -258,20 +239,17 @@ QString AWPatternFunctions::insertMacros(QString _code)
|
||||
}
|
||||
|
||||
|
||||
QStringList AWPatternFunctions::findKeys(const QString &_code,
|
||||
const QStringList &_keys,
|
||||
QStringList AWPatternFunctions::findKeys(const QString &_code, const QStringList &_keys,
|
||||
const bool _isBars)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list"
|
||||
<< _keys;
|
||||
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list" << _keys;
|
||||
|
||||
QStringList selectedKeys;
|
||||
QString replacedCode = _code;
|
||||
for (auto &key : _keys)
|
||||
if ((key.startsWith("bar") == _isBars)
|
||||
&& (replacedCode.contains(QString("$%1").arg(key)))) {
|
||||
qCInfo(LOG_AW) << "Found key" << key << "with bar enabled"
|
||||
<< _isBars;
|
||||
qCInfo(LOG_AW) << "Found key" << key << "with bar enabled" << _isBars;
|
||||
selectedKeys.append(key);
|
||||
replacedCode.replace(QString("$%1").arg(key), "");
|
||||
}
|
||||
@ -289,8 +267,7 @@ QStringList AWPatternFunctions::findLambdas(const QString &_code)
|
||||
QStringList selectedKeys;
|
||||
// match the following construction ${{some code here}}
|
||||
QRegularExpression lambdaRegexp("\\$\\{\\{(?<body>.*?)\\}\\}");
|
||||
lambdaRegexp.setPatternOptions(
|
||||
QRegularExpression::DotMatchesEverythingOption);
|
||||
lambdaRegexp.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
|
||||
|
||||
QRegularExpressionMatchIterator it = lambdaRegexp.globalMatch(_code);
|
||||
while (it.hasNext()) {
|
||||
|
Reference in New Issue
Block a user