Stabilizing commit

* move request timeout settings to the configuration header
* initial support of requiredby dictionary
* add AWPatternFunctions namespace
* improve components communication
* update UI to recent abilities
* rewrite qCDebug messages and update CONTRIBUTING.md accordingly
This commit is contained in:
2016-02-01 00:25:28 +07:00
parent c1cf8185a3
commit beb2682b04
31 changed files with 326 additions and 244 deletions

View File

@ -179,8 +179,7 @@ QVariantMap AWActions::getFont(const QVariantMap defaultFont) const
// to avoid additional object definition this method is static
void AWActions::sendNotification(const QString eventId, const QString message)
{
qCDebug(LOG_AW) << "Event" << eventId;
qCDebug(LOG_AW) << "Message" << message;
qCDebug(LOG_AW) << "Event" << eventId << "with message" << message;
KNotification *notification = KNotification::event(
eventId, QString("Awesome Widget ::: %1").arg(eventId), message);
@ -229,9 +228,8 @@ void AWActions::showUpdates(const QString version) const
void AWActions::versionReplyRecieved(QNetworkReply *reply,
const bool showAnyway) const
{
qCDebug(LOG_AW) << "Return code" << reply->error();
qCDebug(LOG_AW) << "Reply error message" << reply->errorString();
qCDebug(LOG_AW) << "Show anyway" << showAnyway;
qCDebug(LOG_AW) << "Return code" << reply->error() << "with message"
<< reply->errorString() << "and show anyway" << showAnyway;
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);

View File

@ -216,9 +216,8 @@ void AWConfigHelper::copyExtensions(const QString item, const QString type,
QSettings &settings,
const bool inverse) const
{
qCDebug(LOG_AW) << "Extension" << item;
qCDebug(LOG_AW) << "Type" << type;
qCDebug(LOG_AW) << "Inverse" << inverse;
qCDebug(LOG_AW) << "Extension" << item << "has type" << type
<< "inverse copying" << inverse;
settings.beginGroup(item);
QSettings itemSettings(
@ -247,8 +246,7 @@ void AWConfigHelper::copySettings(QSettings &from, QSettings &to) const
void AWConfigHelper::readFile(QSettings &settings, const QString key,
const QString fileName) const
{
qCDebug(LOG_AW) << "Key" << key;
qCDebug(LOG_AW) << "File" << fileName;
qCDebug(LOG_AW) << "Key" << key << "from file" << fileName;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
@ -264,8 +262,7 @@ void AWConfigHelper::readFile(QSettings &settings, const QString key,
void AWConfigHelper::writeFile(QSettings &settings, const QString key,
const QString fileName) const
{
qCDebug(LOG_AW) << "Key" << key;
qCDebug(LOG_AW) << "File" << fileName;
qCDebug(LOG_AW) << "Key" << key << "to file" << fileName;
if (!settings.contains(key))
return;

View File

@ -90,7 +90,7 @@ void AWDataAggregator::setParameters(QVariantMap settings)
counts += configuration[QString("downTooltip")].toInt();
counts += configuration[QString("batTooltip")].toInt();
// resize tooltip image
toolTipView->resize(100.0 * counts, 105.0);
toolTipView->resize(100 * counts, 105);
boundaries[QString("cpuTooltip")] = 100.0;
boundaries[QString("cpuclTooltip")] = 4000.0;
@ -179,9 +179,8 @@ void AWDataAggregator::dataUpdate(const QHash<QString, QString> &values)
void AWDataAggregator::checkValue(const QString source, const float value,
const float extremum) const
{
qCDebug(LOG_AW) << "Notification source" << source;
qCDebug(LOG_AW) << "Value" << value;
qCDebug(LOG_AW) << "Called with extremum" << extremum;
qCDebug(LOG_AW) << "Notification source" << source << "with value" << value
<< "called with extremum" << extremum;
if (value >= 0.0) {
if ((m_enablePopup) && (value > extremum)
@ -200,9 +199,8 @@ void AWDataAggregator::checkValue(const QString source, const float value,
void AWDataAggregator::checkValue(const QString source, const QString current,
const QString received) const
{
qCDebug(LOG_AW) << "Notification source" << source;
qCDebug(LOG_AW) << "Current value" << current;
qCDebug(LOG_AW) << "Received value" << received;
qCDebug(LOG_AW) << "Notification source" << source << "with current value"
<< current << "and received one" << received;
if ((m_enablePopup) && (current != received) && (!received.isEmpty()))
return AWActions::sendNotification(QString("event"),
@ -225,8 +223,7 @@ void AWDataAggregator::initScene()
QString AWDataAggregator::notificationText(const QString source,
const float value) const
{
qCDebug(LOG_AW) << "Notification source" << source;
qCDebug(LOG_AW) << "Value" << value;
qCDebug(LOG_AW) << "Notification source" << source << "with value" << value;
QString output;
if (source == QString("batTooltip"))
@ -247,8 +244,7 @@ QString AWDataAggregator::notificationText(const QString source,
QString AWDataAggregator::notificationText(const QString source,
const QString value) const
{
qCDebug(LOG_AW) << "Notification source" << source;
qCDebug(LOG_AW) << "Value" << value;
qCDebug(LOG_AW) << "Notification source" << source << "with value" << value;
QString output;
if (source == QString("netdev"))
@ -287,9 +283,8 @@ void AWDataAggregator::setData(const QHash<QString, QString> &values)
void AWDataAggregator::setData(const QString &source, float value,
const float extremum)
{
qCDebug(LOG_AW) << "Source" << source;
qCDebug(LOG_AW) << "Value" << value;
qCDebug(LOG_AW) << "Called with extremum" << extremum;
qCDebug(LOG_AW) << "Source" << source << "to value" << value
<< "with extremum" << extremum;
if (data[source].count() == 0)
data[source].append(0.0);
@ -316,9 +311,8 @@ void AWDataAggregator::setData(const QString &source, float value,
void AWDataAggregator::setData(const bool dontInvert, const QString &source,
float value)
{
qCDebug(LOG_AW) << "Do not invert value" << dontInvert;
qCDebug(LOG_AW) << "Source" << source;
qCDebug(LOG_AW) << "Value" << value;
qCDebug(LOG_AW) << "Do not invert" << dontInvert << "value" << value
<< "for source" << source;
// invert values for different battery colours
value = dontInvert ? value : -value;

View File

@ -45,9 +45,9 @@ AWDataEngineAggregator::~AWDataEngineAggregator()
void AWDataEngineAggregator::disconnectSources()
{
for (auto dataengine : m_dataEngines.keys())
for (auto source : m_dataEngines[dataengine]->sources())
m_dataEngines[dataengine]->disconnectSource(source, parent());
for (auto dataengine : m_dataEngines.values())
for (auto source : dataengine->sources())
dataengine->disconnectSource(source, parent());
}
@ -93,7 +93,7 @@ void AWDataEngineAggregator::initDataEngines()
// additional method required by systemmonitor structure
connect(m_dataEngines[QString("systemmonitor")],
&Plasma::DataEngine::sourceAdded, [this](const QString source) {
static_cast<AWKeys *>(parent())->addDevice(source);
emit(deviceAdded(source));
m_dataEngines[QString("systemmonitor")]->connectSource(
source, parent(), m_interval);
});

View File

@ -38,6 +38,9 @@ public:
// properties
void setInterval(const int _interval);
signals:
void deviceAdded(const QString &source);
public slots:
void dropSource(const QString source);
void reconnectSources();

View File

@ -21,6 +21,7 @@
#include "awactions.h"
#include "awconfighelper.h"
#include "awdataengineaggregator.h"
#include "awkeys.h"

View File

@ -26,10 +26,9 @@
#include "awdebug.h"
void AWKeyCache::addKeyToCache(const QString type, const QString key)
bool AWKeyCache::addKeyToCache(const QString type, const QString key)
{
qCDebug(LOG_AW) << "Key type" << type;
qCDebug(LOG_AW) << "Key" << key;
qCDebug(LOG_AW) << "Key" << key << "with type" << type;
QString fileName = QString("%1/awesomewidgets.ndx")
.arg(QStandardPaths::writableLocation(
@ -70,7 +69,7 @@ void AWKeyCache::addKeyToCache(const QString type, const QString key)
}
} else {
if (cachedValues.contains(key))
return;
return false;
qCInfo(LOG_AW) << "Found new key" << key << "for type" << type;
cache.setValue(
QString("%1").arg(cache.allKeys().count(), 3, 10, QChar('0')), key);
@ -78,6 +77,7 @@ void AWKeyCache::addKeyToCache(const QString type, const QString key)
cache.endGroup();
cache.sync();
return true;
}

View File

@ -25,7 +25,7 @@
namespace AWKeyCache
{
void addKeyToCache(const QString type, const QString key = QString(""));
bool addKeyToCache(const QString type, const QString key = QString(""));
QHash<QString, QStringList> loadKeysFromCache();
};

View File

@ -54,28 +54,6 @@ AWKeyOperations::~AWKeyOperations()
}
void AWKeyOperations::addDevice(const QString source)
{
qCDebug(LOG_AW) << "Source" << source;
QRegExp diskRegexp
= QRegExp(QString("disk/(?:md|sd|hd)[a-z|0-9]_.*/Rate/(?:rblk)"));
QRegExp mountRegexp = QRegExp(QString("partitions/.*/filllevel"));
if (source.contains(diskRegexp)) {
QString device = source;
device.remove(QString("/Rate/rblk"));
addKeyToCache(QString("disk"), device);
} else if (source.contains(mountRegexp)) {
QString device = source;
device.remove(QString("partitions")).remove(QString("/filllevel"));
addKeyToCache(QString("mount"), device);
} else if (source.startsWith(QString("lmsensors"))) {
addKeyToCache(QString("temp"), source);
}
}
QStringList AWKeyOperations::devices(const QString type) const
{
qCDebug(LOG_AW) << "Looking for type" << type;
@ -259,6 +237,8 @@ QStringList AWKeyOperations::dictKeys() const
}
// this method is required to provide GraphicalItem functions (e.g. paint()) to
// parent classes
GraphicalItem *AWKeyOperations::giByKey(const QString key) const
{
qCDebug(LOG_AW) << "Looking for item" << key;
@ -355,14 +335,36 @@ void AWKeyOperations::editItem(const QString type)
}
void AWKeyOperations::addDevice(const QString &source)
{
qCDebug(LOG_AW) << "Source" << source;
QRegExp diskRegexp
= QRegExp(QString("disk/(?:md|sd|hd)[a-z|0-9]_.*/Rate/(?:rblk)"));
QRegExp mountRegexp = QRegExp(QString("partitions/.*/filllevel"));
if (source.contains(diskRegexp)) {
QString device = source;
device.remove(QString("/Rate/rblk"));
addKeyToCache(QString("disk"), device);
} else if (source.contains(mountRegexp)) {
QString device = source;
device.remove(QString("partitions")).remove(QString("/filllevel"));
addKeyToCache(QString("mount"), device);
} else if (source.startsWith(QString("lmsensors"))) {
addKeyToCache(QString("temp"), source);
}
}
void AWKeyOperations::addKeyToCache(const QString type, const QString key)
{
qCDebug(LOG_AW) << "Key type" << type;
qCDebug(LOG_AW) << "Key" << key;
qCDebug(LOG_AW) << "Key" << key << "with type" << type;
AWKeyCache::addKeyToCache(type, key);
m_devices = AWKeyCache::loadKeysFromCache();
reinitKeys();
if (AWKeyCache::addKeyToCache(type, key)) {
m_devices = AWKeyCache::loadKeysFromCache();
reinitKeys();
}
}
@ -391,17 +393,8 @@ void AWKeyOperations::reinitKeys()
// init
QStringList allKeys = dictKeys();
#ifdef BUILD_TESTING
// not documented feature - place all available tags
m_pattern = m_pattern.replace(QString("$ALL"), [allKeys]() {
QStringList strings;
for (auto tag : allKeys)
strings.append(QString("%1: $%1").arg(tag));
return strings.join(QString(" | "));
}());
#endif /* BUILD_TESTING */
// apply aw_* functions
m_pattern = AWPatternFunctions::insertAllKeys(m_pattern, allKeys);
m_pattern = AWPatternFunctions::insertKeyCount(m_pattern, allKeys);
m_pattern = AWPatternFunctions::insertKeyNames(m_pattern, allKeys);
m_pattern = AWPatternFunctions::insertKeys(m_pattern, allKeys);

View File

@ -45,7 +45,6 @@ class AWKeyOperations : public QObject
public:
explicit AWKeyOperations(QObject *parent = nullptr);
virtual ~AWKeyOperations();
void addDevice(const QString source);
QStringList devices(const QString type) const;
QHash<QString, QStringList> devices() const;
void updateCache();
@ -62,6 +61,9 @@ public:
signals:
void updateKeys(const QStringList currentKeys);
public slots:
void addDevice(const QString &source);
private:
// methods
void addKeyToCache(const QString type, const QString key = QString(""));

View File

@ -27,6 +27,7 @@
#include "awdebug.h"
#include "awkeyoperations.h"
#include "awkeysaggregator.h"
#include "awpatternfunctions.h"
#include "graphicalitem.h"
#include "version.h"
@ -79,9 +80,8 @@ void AWKeys::initDataAggregator(const QVariantMap tooltipParams)
void AWKeys::initKeys(const QString currentPattern, const int interval,
const int limit)
{
qCDebug(LOG_AW) << "Pattern" << currentPattern;
qCDebug(LOG_AW) << "Interval" << interval;
qCDebug(LOG_AW) << "Queue limit" << limit;
qCDebug(LOG_AW) << "Pattern" << currentPattern << "with interval"
<< interval << "and queue limit" << limit;
// init
keyOperator->setPattern(currentPattern);
@ -89,6 +89,9 @@ void AWKeys::initKeys(const QString currentPattern, const int interval,
dataEngineAggregator = new AWDataEngineAggregator(this, interval);
connect(this, SIGNAL(dropSourceFromDataengine(QString)),
dataEngineAggregator, SLOT(dropSource(QString)));
// transfer signal from dataengine to update source list
connect(dataEngineAggregator, SIGNAL(deviceAdded(const QString &)),
keyOperator, SLOT(addDevice(const QString &)));
} else
dataEngineAggregator->setInterval(interval);
m_threadPool->setMaxThreadCount(limit == 0 ? QThread::idealThreadCount()
@ -101,8 +104,7 @@ void AWKeys::initKeys(const QString currentPattern, const int interval,
void AWKeys::setAggregatorProperty(const QString key, const QVariant value)
{
qCDebug(LOG_AW) << "Key" << key;
qCDebug(LOG_AW) << "Value" << value;
qCDebug(LOG_AW) << "Key" << key << "with value" << value;
aggregator->setProperty(key.toUtf8().constData(), value);
}
@ -124,8 +126,8 @@ void AWKeys::updateCache()
QStringList AWKeys::dictKeys(const bool sorted, const QString regexp) const
{
qCDebug(LOG_AW) << "Should be sorted" << sorted;
qCDebug(LOG_AW) << "Filter" << regexp;
qCDebug(LOG_AW) << "Should be sorted" << sorted << "and filter applied"
<< regexp;
QStringList allKeys = keyOperator->dictKeys();
// sort if required
@ -149,7 +151,7 @@ QStringList AWKeys::getHddDevices() const
QString AWKeys::infoByKey(QString key) const
{
qCDebug(LOG_AW) << "Requested key" << key;
qCDebug(LOG_AW) << "Requested info for key" << key;
return keyOperator->infoByKey(key);
}
@ -158,7 +160,7 @@ QString AWKeys::infoByKey(QString key) const
// HACK this method requires to define tag value from bar from UI interface
QString AWKeys::valueByKey(QString key) const
{
qCDebug(LOG_AW) << "Requested key" << key;
qCDebug(LOG_AW) << "Requested value for key" << key;
return values.value(key.remove(QRegExp(QString("^bar[0-9]{1,}"))),
QString(""));
@ -173,14 +175,6 @@ void AWKeys::editItem(const QString type)
}
void AWKeys::addDevice(const QString source)
{
qCDebug(LOG_AW) << "Source" << source;
return keyOperator->addDevice(source);
}
void AWKeys::dataUpdated(const QString &sourceName,
const Plasma::DataEngine::Data &data)
{
@ -201,58 +195,11 @@ void AWKeys::reinitKeys(const QStringList currentKeys)
qCDebug(LOG_AW) << "Update found keys by using list" << currentKeys;
// append lists
// bars
m_foundBars = [currentKeys](const QString pattern) {
QStringList selectedKeys;
for (auto key : currentKeys)
if ((key.startsWith(QString("bar")))
&& (pattern.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found bar" << key;
selectedKeys.append(key);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No bars found";
return selectedKeys;
}(keyOperator->pattern());
// main key list
m_foundKeys = [currentKeys](const QString pattern) {
QStringList selectedKeys;
for (auto key : currentKeys)
if ((!key.startsWith(QString("bar")))
&& (pattern.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found key" << key;
selectedKeys.append(key);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No keys found";
return selectedKeys;
}(keyOperator->pattern());
// lambdas
m_foundLambdas = [](const QString pattern) {
QStringList selectedKeys;
// substring inside ${{ }} (with brackets) which should not contain ${{
QRegularExpression lambdaRegexp(
QString("\\$\\{\\{((?!\\$\\{\\{).)*?\\}\\}"));
lambdaRegexp.setPatternOptions(
QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator it = lambdaRegexp.globalMatch(pattern);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString lambda = match.captured();
// drop brackets
lambda.remove(QRegExp(QString("^\\$\\{\\{")));
lambda.remove(QRegExp(QString("\\}\\}$")));
// append
qCInfo(LOG_AW) << "Found lambda" << lambda;
selectedKeys.append(lambda);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No lambdas found";
return selectedKeys;
}(keyOperator->pattern());
m_foundBars
= AWPatternFunctions::findBars(keyOperator->pattern(), currentKeys);
m_foundKeys
= AWPatternFunctions::findKeys(keyOperator->pattern(), currentKeys);
m_foundLambdas = AWPatternFunctions::findLambdas(keyOperator->pattern());
// set key data to aggregator
aggregator->setDevices(keyOperator->devices());
@ -354,7 +301,7 @@ void AWKeys::calculateValues()
QString AWKeys::parsePattern(QString pattern) const
{
// screen sign
pattern.replace(QString("$$"), QString("$\\$\\"));
pattern.replace(QString("$$"), QString(0x1d));
// lambdas
for (auto key : m_foundLambdas)
@ -385,7 +332,7 @@ QString AWKeys::parsePattern(QString pattern) const
}
// prepare strings
pattern.replace(QString("$\\$\\"), QString("$$"));
pattern.replace(QString(0x1d), QString("$"));
if (m_wrapNewLines)
pattern.replace(QString("\n"), QString("<br>"));
@ -395,8 +342,7 @@ QString AWKeys::parsePattern(QString pattern) const
void AWKeys::setDataBySource(const QString &sourceName, const QVariantMap &data)
{
qCDebug(LOG_AW) << "Source" << sourceName;
qCDebug(LOG_AW) << "Data" << data;
qCDebug(LOG_AW) << "Source" << sourceName << "with data" << data;
// first list init
QStringList tags = aggregator->keysFromSource(sourceName);
@ -404,21 +350,18 @@ void AWKeys::setDataBySource(const QString &sourceName, const QVariantMap &data)
tags = aggregator->registerSource(sourceName,
data[QString("units")].toString());
// update data or drop source if there are no matches
// update data or drop source if there are no matches and exit
if (tags.isEmpty()) {
qCDebug(LOG_AW) << "Source" << sourceName << "not found";
emit(dropSourceFromDataengine(sourceName));
} else {
m_mutex.lock();
// HACK workaround for time values which are stored in the different
// path
QVariant value = sourceName == QString("Local")
? data[QString("DateTime")]
: data[QString("value")];
std::for_each(tags.cbegin(), tags.cend(),
[this, value](const QString tag) {
values[tag] = aggregator->formater(value, tag);
});
m_mutex.unlock();
qCInfo(LOG_AW) << "Source" << sourceName << "not found";
return emit(dropSourceFromDataengine(sourceName));
}
m_mutex.lock();
// HACK workaround for time values which are stored in the different path
QVariant value = sourceName == QString("Local") ? data[QString("DateTime")]
: data[QString("value")];
std::for_each(tags.cbegin(), tags.cend(), [this, value](const QString tag) {
values[tag] = aggregator->formater(value, tag);
});
m_mutex.unlock();
}

View File

@ -58,7 +58,6 @@ public:
Q_INVOKABLE void editItem(const QString type);
public slots:
void addDevice(const QString source);
void dataUpdated(const QString &sourceName,
const Plasma::DataEngine::Data &data);
// dummy method required by DataEngine connections

View File

@ -42,8 +42,7 @@ AWKeysAggregator::~AWKeysAggregator()
QString AWKeysAggregator::formater(const QVariant &data,
const QString &key) const
{
qCDebug(LOG_AW) << "Data" << data;
qCDebug(LOG_AW) << "Key" << key;
qCDebug(LOG_AW) << "Data" << data << "for key" << key;
QString output;
QLocale loc = m_translate ? QLocale::system() : QLocale::c();
@ -135,7 +134,7 @@ QString AWKeysAggregator::formater(const QVariant &data,
return source;
}(m_formater[key] == Uptime ? QString("$ddd$hhh$mmm")
: m_customUptime,
data.toFloat());
static_cast<int>(data.toFloat()));
break;
case NoFormat:
default:
@ -155,6 +154,15 @@ QStringList AWKeysAggregator::keysFromSource(const QString &source) const
}
QStringList
AWKeysAggregator::requiredByKeysFromSource(const QString &source) const
{
qCDebug(LOG_AW) << "Search for source" << source;
return m_requiredByMap.values(source);
}
void AWKeysAggregator::setAcOffline(const QString inactive)
{
qCDebug(LOG_AW) << "Inactive AC string" << inactive;
@ -216,8 +224,7 @@ void AWKeysAggregator::setTranslate(const bool translate)
QStringList AWKeysAggregator::registerSource(const QString &source,
const QString &units)
{
qCDebug(LOG_AW) << "Source" << source;
qCDebug(LOG_AW) << "Units" << units;
qCDebug(LOG_AW) << "Source" << source << "with units" << units;
// regular expressions
QRegExp cpuRegExp = QRegExp(QString("cpu/cpu.*/TotalLoad"));
@ -331,6 +338,11 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
key = QString("hddfreegb%1").arg(index);
m_map.insertMulti(source, key);
m_formater[key] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source,
QString("hddtotmb%1").arg(index));
m_requiredByMap.insertMulti(source,
QString("hddtotgb%1").arg(index));
}
} else if (source.contains(mountUsedRegExp)) {
// used
@ -346,6 +358,11 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
key = QString("hddgb%1").arg(index);
m_map.insertMulti(source, key);
m_formater[key] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source,
QString("hddtotmb%1").arg(index));
m_requiredByMap.insertMulti(source,
QString("hddtotgb%1").arg(index));
}
} else if (source.startsWith(QString("hdd/temperature"))) {
// hdd temperature
@ -372,6 +389,8 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
// gb
m_map.insertMulti(source, QString("memgb"));
m_formater[QString("memgb")] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source, QString("mem"));
} else if (source == QString("mem/physical/free")) {
// free memory
// mb
@ -380,6 +399,10 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
// gb
m_map.insertMulti(source, QString("memfreegb"));
m_formater[QString("memfreegb")] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source, QString("memtotmb"));
m_requiredByMap.insertMulti(source, QString("memtotgb"));
m_requiredByMap.insertMulti(source, QString("mem"));
} else if (source == QString("mem/physical/used")) {
// used memory
// mb
@ -388,10 +411,21 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
// gb
m_map.insertMulti(source, QString("memusedgb"));
m_formater[QString("memusedgb")] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source, QString("memtotmb"));
m_requiredByMap.insertMulti(source, QString("memtotgb"));
m_requiredByMap.insertMulti(source, QString("mem"));
} else if (source == QString("network/current/name")) {
// network device
m_map[source] = QString("netdev");
m_formater[QString("netdev")] = NoFormat;
// fill required by list
m_requiredByMap.insertMulti(source, QString("down"));
m_requiredByMap.insertMulti(source, QString("downkb"));
m_requiredByMap.insertMulti(source, QString("downunits"));
m_requiredByMap.insertMulti(source, QString("up"));
m_requiredByMap.insertMulti(source, QString("upkb"));
m_requiredByMap.insertMulti(source, QString("upunits"));
} else if (source.contains(netRegExp)) {
// network speed
QString type = source.contains(QString("receiver")) ? QString("down")
@ -412,6 +446,13 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
m_map.insertMulti(source, key);
m_formater[key] = NetSmartUnits;
}
// fill required by list
m_requiredByMap.insertMulti(source, QString("%1").arg(type));
m_requiredByMap.insertMulti(source, QString("%1kb").arg(type));
m_requiredByMap.insertMulti(source, QString("%1units").arg(type));
m_requiredByMap.insertMulti(source, QString("%1").arg(type));
m_requiredByMap.insertMulti(source, QString("%1kb").arg(type));
m_requiredByMap.insertMulti(source, QString("%1units").arg(type));
} else if (source.startsWith(QString("upgrade"))) {
// package manager
QString key = source;
@ -450,6 +491,10 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
// gb
m_map.insertMulti(source, QString("swapfreegb"));
m_formater[QString("swapfreegb")] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source, QString("swaptotmb"));
m_requiredByMap.insertMulti(source, QString("swaptotgb"));
m_requiredByMap.insertMulti(source, QString("swap"));
} else if (source == QString("mem/swap/used")) {
// used swap
// mb
@ -458,6 +503,10 @@ QStringList AWKeysAggregator::registerSource(const QString &source,
// gb
m_map.insertMulti(source, QString("swapgb"));
m_formater[QString("swapgb")] = MemGBFormat;
// fill required by list
m_requiredByMap.insertMulti(source, QString("swaptotmb"));
m_requiredByMap.insertMulti(source, QString("swaptotgb"));
m_requiredByMap.insertMulti(source, QString("swap"));
} else if (source.startsWith(QString("lmsensors/"))) {
// temperature
int index = m_devices[QString("temp")].indexOf(source);

View File

@ -67,6 +67,7 @@ public:
// get methods
QString formater(const QVariant &data, const QString &key) const;
QStringList keysFromSource(const QString &source) const;
QStringList requiredByKeysFromSource(const QString &source) const;
// set methods
void setAcOffline(const QString inactive);
void setAcOnline(const QString active);
@ -90,6 +91,7 @@ private:
QHash<QString, QStringList> m_devices;
QHash<QString, FormaterType> m_formater;
QHash<QString, QString> m_map;
QHash<QString, QString> m_requiredByMap;
QString m_tempUnits;
bool m_translate = false;
};

View File

@ -28,8 +28,15 @@ QVariantList AWPatternFunctions::findFunctionCalls(const QString function,
{
qCDebug(LOG_AW) << "Looking for function" << function << "in" << code;
// I suggest the following regex for the internal functions
// $aw_function_name<some args here if any>{{function body}}
// * args should be always comma separated (e.g. commas are not supported
// in this field if they are not screened by $, i.e. '$,'
// * body depends on the function name, double brackets (i.e. {{ or }}) are
// not supported
QRegularExpression regex(
QString("%1\\<(?<args>.*?)\\>\\((?<body>.*?)\\)").arg(function));
QString("\\$%1\\<(?<args>.*?)\\>\\{\\{(?<body>.*?)\\}\\}")
.arg(function));
regex.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QVariantList foundFunctions;
@ -38,8 +45,20 @@ QVariantList AWPatternFunctions::findFunctionCalls(const QString function,
QRegularExpressionMatch match = it.next();
QVariantHash metadata;
metadata[QString("args")]
= match.captured(QString("args")).split(QChar(','));
// work with args
QString argsString = match.captured(QString("args"));
if (argsString.isEmpty()) {
metadata[QString("args")] = QStringList();
} else {
// replace '$,' to 0x1d
argsString.replace(QString("$,"), QString(0x1d));
QStringList args = argsString.split(QChar(','));
std::for_each(args.begin(), args.end(), [](QString &arg) {
arg.replace(QString(0x1d), QString(","));
});
metadata[QString("args")] = args;
}
// other variables
metadata[QString("body")] = match.captured(QString("body"));
metadata[QString("what")] = match.captured();
foundFunctions.append(metadata);
@ -51,27 +70,22 @@ QVariantList AWPatternFunctions::findFunctionCalls(const QString function,
QString AWPatternFunctions::expandTemplates(QString code)
{
qCDebug(LOG_AW) << "Expand tempaltes in" << code;
qCDebug(LOG_AW) << "Expand templates in" << code;
// match the following construction $template{{some code here}}
QRegularExpression templatesRegexp(
QString("\\$template\\{\\{((?!\\$template\\{\\{).)*?\\}\\}"));
QString("\\$template\\{\\{(?<body>.*?)\\}\\}"));
templatesRegexp.setPatternOptions(
QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator it = templatesRegexp.globalMatch(code);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString fullTemplate = match.captured();
// drop additional markers
QString templ = fullTemplate;
templ.remove(QRegExp(QString("^\\$template\\{\\{")));
templ.remove(QRegExp(QString("\\}\\}$")));
QString body = match.captured(QString("body"));
QJSEngine engine;
qCInfo(LOG_AW) << "Expression" << templ;
QJSValue result = engine.evaluate(templ);
qCInfo(LOG_AW) << "Expression" << body;
QJSValue result = engine.evaluate(body);
QString templateResult = QString("");
if (result.isError()) {
qCWarning(LOG_AW) << "Uncaught exception at line"
@ -82,7 +96,34 @@ QString AWPatternFunctions::expandTemplates(QString code)
}
// replace template
code.replace(fullTemplate, templateResult);
code.replace(match.captured(), templateResult);
}
return code;
}
QString AWPatternFunctions::insertAllKeys(QString code, const QStringList keys)
{
qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list"
<< keys;
QVariantList found
= AWPatternFunctions::findFunctionCalls(QString("aw_all"), code);
for (auto function : found) {
QVariantHash metadata = function.toHash();
QString separator
= metadata[QString("args")].toStringList().isEmpty()
? QString(",")
: metadata[QString("args")].toStringList().at(0);
QStringList required
= keys.filter(QRegExp(metadata[QString("body")].toString()));
std::for_each(required.begin(), required.end(), [](QString &value) {
value = QString("%1: $%1").arg(value);
});
code.replace(metadata[QString("what")].toString(),
required.join(separator));
}
return code;
@ -111,7 +152,7 @@ QString AWPatternFunctions::insertKeyCount(QString code, const QStringList keys)
QString AWPatternFunctions::insertKeyNames(QString code, const QStringList keys)
{
qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list"
qCDebug(LOG_AW) << "Looking for key names in code" << code << "using list"
<< keys;
QVariantList found
@ -158,3 +199,69 @@ QString AWPatternFunctions::insertKeys(QString code, const QStringList keys)
return code;
}
QStringList AWPatternFunctions::findBars(const QString code,
const QStringList keys)
{
qCDebug(LOG_AW) << "Looking for bars in code" << code << "using list"
<< keys;
QStringList selectedKeys;
for (auto key : keys)
if ((key.startsWith(QString("bar")))
&& (code.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found bar" << key;
selectedKeys.append(key);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No bars found";
return selectedKeys;
}
QStringList AWPatternFunctions::findKeys(const QString code,
const QStringList keys)
{
qCDebug(LOG_AW) << "Looking for keys in code" << code << "using list"
<< keys;
QStringList selectedKeys;
for (auto key : keys)
if ((!key.startsWith(QString("bar")))
&& (code.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found key" << key;
selectedKeys.append(key);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No keys found";
return selectedKeys;
}
QStringList AWPatternFunctions::findLambdas(const QString code)
{
qCDebug(LOG_AW) << "Looking for lambdas in code" << code;
QStringList selectedKeys;
// match the following construction ${{some code here}}
QRegularExpression lambdaRegexp(QString("\\$\\{\\{(?<body>.*?)\\}\\}"));
lambdaRegexp.setPatternOptions(
QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator it = lambdaRegexp.globalMatch(code);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString lambda = match.captured(QString("body"));
// append
qCInfo(LOG_AW) << "Found lambda" << lambda;
selectedKeys.append(lambda);
}
if (selectedKeys.isEmpty())
qCWarning(LOG_AW) << "No lambdas found";
return selectedKeys;
}

View File

@ -25,11 +25,17 @@
namespace AWPatternFunctions
{
// insert methods
QString expandTemplates(QString code);
QVariantList findFunctionCalls(const QString function, const QString code);
QString insertAllKeys(QString code, const QStringList keys);
QString insertKeyCount(QString code, const QStringList keys);
QString insertKeyNames(QString code, const QStringList keys);
QString insertKeys(QString code, const QStringList keys);
// find methods
QStringList findBars(const QString code, const QStringList keys);
QStringList findKeys(const QString code, const QStringList keys);
QStringList findLambdas(const QString code);
};