summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Seigo <aseigo@kde.org>2014-12-07 16:06:23 +0100
committerAaron Seigo <aseigo@kde.org>2014-12-11 00:55:20 +0100
commit323f60b25f0872ddee04f52aec374b741dbf9d17 (patch)
tree26833e06f090b4a43bdc04051fe37e35cbba267b
parentfdd74a41929b4343902f1d1dfcd5116534a79f4f (diff)
downloadsink-323f60b25f0872ddee04f52aec374b741dbf9d17.tar.gz
sink-323f60b25f0872ddee04f52aec374b741dbf9d17.zip
How Are We Doing (hawd) framework
-rw-r--r--CMakeLists.txt2
-rw-r--r--hawd.conf4
-rw-r--r--hawd_defs/buffer_creation7
-rw-r--r--tests/CMakeLists.txt2
-rw-r--r--tests/hawd/CMakeLists.txt16
-rw-r--r--tests/hawd/datasetdefinition.cpp195
-rw-r--r--tests/hawd/datasetdefinition.h83
-rw-r--r--tests/hawd/main.cpp56
-rw-r--r--tests/hawd/module.cpp170
-rw-r--r--tests/hawd/module.h66
-rw-r--r--tests/hawd/modules/list.cpp79
-rw-r--r--tests/hawd/modules/list.h37
-rw-r--r--tests/hawd/state.cpp99
-rw-r--r--tests/hawd/state.h47
14 files changed, 863 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0ead220..c5b2eb4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -38,6 +38,8 @@ set(CMAKE_AUTOMOC ON)
38add_definitions("-Wall -std=c++0x") 38add_definitions("-Wall -std=c++0x")
39include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) 39include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
40 40
41configure_file(hawd.conf hawd.conf)
42
41# common, eventually a lib but right now just the command buffers 43# common, eventually a lib but right now just the command buffers
42add_subdirectory(common) 44add_subdirectory(common)
43 45
diff --git a/hawd.conf b/hawd.conf
new file mode 100644
index 0000000..169a1d4
--- /dev/null
+++ b/hawd.conf
@@ -0,0 +1,4 @@
1{
2 "results": "~/hawd",
3 "project": "@CMAKE_SOURCE_DIR@/hawd_defs"
4}
diff --git a/hawd_defs/buffer_creation b/hawd_defs/buffer_creation
new file mode 100644
index 0000000..6d9e383
--- /dev/null
+++ b/hawd_defs/buffer_creation
@@ -0,0 +1,7 @@
1{
2 "name": "Buffer Creation",
3 "columns": {
4 "numBuffers": { "type": "int" },
5 "time": { "type": "int", "unit": "ms", "min": 0, "max": 100 }
6 }
7}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 95aba64..e63bc1c 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,3 +1,5 @@
1add_subdirectory(hawd)
2
1set(CMAKE_AUTOMOC ON) 3set(CMAKE_AUTOMOC ON)
2include_directories(${CMAKE_CURRENT_BINARY_DIR}) 4include_directories(${CMAKE_CURRENT_BINARY_DIR})
3 5
diff --git a/tests/hawd/CMakeLists.txt b/tests/hawd/CMakeLists.txt
new file mode 100644
index 0000000..8c677c0
--- /dev/null
+++ b/tests/hawd/CMakeLists.txt
@@ -0,0 +1,16 @@
1project(hawd)
2
3include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
4
5set(SRCS
6 datasetdefinition.cpp
7 main.cpp
8 module.cpp
9 state.cpp
10 modules/list.cpp
11)
12
13add_executable(${PROJECT_NAME} ${SRCS})
14qt5_use_modules(${PROJECT_NAME} Core)
15install(TARGETS ${PROJECT_NAME} DESTINATION bin)
16
diff --git a/tests/hawd/datasetdefinition.cpp b/tests/hawd/datasetdefinition.cpp
new file mode 100644
index 0000000..550c4fb
--- /dev/null
+++ b/tests/hawd/datasetdefinition.cpp
@@ -0,0 +1,195 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "datasetdefinition.h"
21
22#include <QDebug>
23#include <QFile>
24#include <QJsonDocument>
25#include <QJsonObject>
26
27#include <iostream>
28
29namespace HAWD
30{
31
32static QHash<QString, QMetaType::Type> s_types;
33
34DataDefinition::DataDefinition(const QString &name, QMetaType::Type type, const QString &unit, int min, int max)
35 : m_name(name),
36 m_type(type),
37 m_unit(unit),
38 m_min(min),
39 m_max(max)
40{
41}
42
43DataDefinition::DataDefinition(const QJsonObject &json)
44 : m_name(json.value("name").toString()),
45 m_type(QMetaType::Int),
46 m_unit(json.value("unit").toString()),
47 m_min(json.value("min").toInt()),
48 m_max(json.value("max").toInt())
49{
50 if (s_types.isEmpty()) {
51 s_types.insert("date", QMetaType::QDate);
52 s_types.insert("time", QMetaType::QTime);
53 s_types.insert("int", QMetaType::Int);
54 s_types.insert("uint", QMetaType::UInt);
55 s_types.insert("bool", QMetaType::Bool);
56 s_types.insert("char", QMetaType::QChar);
57 s_types.insert("string", QMetaType::QString);
58 s_types.insert("datetime", QMetaType::QDateTime);
59 }
60
61 const QString typeString = json.value("type").toString().toLower();
62 if (s_types.contains(typeString)) {
63 m_type = s_types.value(typeString);
64 }
65}
66
67QString DataDefinition::name() const
68{
69 return m_name;
70}
71
72QString DataDefinition::typeString() const
73{
74 return QMetaType::typeName(m_type);
75}
76
77QMetaType::Type DataDefinition::type() const
78{
79 return m_type;
80}
81
82QString DataDefinition::unit() const
83{
84 return m_unit;
85}
86
87int DataDefinition::min() const
88{
89 return m_min;
90}
91
92int DataDefinition::max() const
93{
94 return m_max;
95}
96
97DatasetRow::DatasetRow(const QHash<QString, DataDefinition> &columns)
98 : m_columns(columns)
99{
100 QHashIterator<QString, DataDefinition> it(columns);
101 while (it.hasNext()) {
102 it.next();
103 m_data.insert(it.key(), QVariant());
104 }
105}
106
107void DatasetRow::setValue(const QString &column, const QVariant &value)
108{
109 if (!m_columns.contains(column) || !value.canConvert(m_columns[column].type())) {
110 return;
111 }
112
113 m_data[column] = value;
114}
115
116void DatasetRow::annotate(const QString &note)
117{
118 m_annotation = note;
119}
120
121QString DatasetRow::toString() const
122{
123 QString string;
124 QHashIterator<QString, QVariant> it(m_data);
125 while (it.hasNext()) {
126 it.next();
127 if (!string.isEmpty()) {
128 string.append('\t');
129 }
130
131 string.append(it.value().toString());
132 }
133
134 if (!m_annotation.isEmpty()) {
135 string.append('\t').append(m_annotation);
136 }
137
138 return string;
139}
140
141DatasetDefinition::DatasetDefinition(const QString &path)
142 : m_valid(false)
143{
144 QFile file(path);
145 m_name = file.fileName();
146
147 if (file.open(QIODevice::ReadOnly)) {
148 QJsonParseError error;
149 QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &error);
150
151 if (jsonDoc.isNull()) {
152 std::cerr << QObject::tr("Dataset definition file malformed at character %1: %2").arg(error.offset).arg(error.errorString()).toStdString() << std::endl;
153 } else {
154 m_valid = true;
155 QJsonObject json = jsonDoc.object();
156 const QString name = json.value("name").toString();
157 if (!name.isEmpty()) {
158 m_name = name;
159 }
160
161 m_description = json.value("description").toString();
162 QJsonObject cols = json.value("columns").toObject();
163 for (const QString &key: cols.keys()) {
164 QJsonObject def = cols.value(key).toObject();
165 if (!def.isEmpty()) {
166 m_columns.insert(key, DataDefinition(def));
167 }
168 }
169 }
170 }
171}
172
173
174bool DatasetDefinition::isValid() const
175{
176 return m_valid;
177}
178
179QString DatasetDefinition::name() const
180{
181 return m_name;
182}
183
184QString DatasetDefinition::description() const
185{
186 return m_description;
187}
188
189QHash<QString, DataDefinition> DatasetDefinition::columns() const
190{
191 return m_columns;
192}
193
194} // namespace HAWD
195
diff --git a/tests/hawd/datasetdefinition.h b/tests/hawd/datasetdefinition.h
new file mode 100644
index 0000000..0593f39
--- /dev/null
+++ b/tests/hawd/datasetdefinition.h
@@ -0,0 +1,83 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QHash>
23#include <QJsonObject>
24#include <QVariant>
25
26namespace HAWD
27{
28
29class DataDefinition
30{
31public:
32 DataDefinition(const QString &name = QString(), QMetaType::Type type = QMetaType::Void, const QString &unit = QString(), int min = 0, int max = 0);
33 DataDefinition(const QJsonObject &object);
34
35 QString name() const;
36 QMetaType::Type type() const;
37 QString typeString() const;
38 QString unit() const;
39 int min() const;
40 int max() const;
41
42private:
43 QString m_name;
44 QMetaType::Type m_type;
45 QString m_unit;
46 int m_min;
47 int m_max;
48};
49
50class DatasetRow
51{
52public:
53 DatasetRow(const QHash<QString, DataDefinition> &columns);
54 void setValue(const QString &column, const QVariant &value);
55 void annotate(const QString &note);
56 QString toString() const;
57
58private:
59 QHash<QString, DataDefinition> m_columns;
60 QHash<QString, QVariant> m_data;
61 QString m_annotation;
62};
63
64class DatasetDefinition
65{
66public:
67 DatasetDefinition(const QString &path);
68
69 bool isValid() const;
70
71 QString name() const;
72 QString description() const;
73 QHash<QString, DataDefinition> columns() const;
74
75private:
76 bool m_valid;
77 QString m_name;
78 QString m_description;
79 QHash<QString, DataDefinition> m_columns;
80};
81
82} // namespace HAWD
83
diff --git a/tests/hawd/main.cpp b/tests/hawd/main.cpp
new file mode 100644
index 0000000..fb07b08
--- /dev/null
+++ b/tests/hawd/main.cpp
@@ -0,0 +1,56 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <unistd.h>
21
22#include "module.h"
23
24#include <QCoreApplication>
25#include <QDebug>
26
27#include <iostream>
28
29void printHelp()
30{
31 std::cout << QCoreApplication::translate("main", "Usage of the How Are We Doing (hawd) command line tool:").toStdString() << std::endl;
32 HAWD::Module::printCommands();
33}
34
35int main(int argc, char *argv[])
36{
37 // load all modules
38 HAWD::Module::loadModules();
39 HAWD::State state;
40
41 if (!state.isValid()) {
42 exit(1);
43 }
44
45 QCoreApplication app(argc, argv);
46 app.setApplicationName("hawd");
47
48 QStringList commands = app.arguments();
49 commands.removeFirst();
50
51 if (commands.isEmpty()) {
52 printHelp();
53 }
54
55 return HAWD::Module::match(commands, state);
56}
diff --git a/tests/hawd/module.cpp b/tests/hawd/module.cpp
new file mode 100644
index 0000000..c020e06
--- /dev/null
+++ b/tests/hawd/module.cpp
@@ -0,0 +1,170 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "module.h"
21
22#include "modules/list.h"
23
24#include <QCoreApplication>
25
26#include <iostream>
27
28namespace HAWD
29{
30
31QVector<Module> Module::s_modules;
32
33Module::Syntax::Syntax()
34{
35}
36
37Module::Syntax::Syntax(const QString &k, std::function<bool(const QStringList &, State &)> l, bool e)
38 : keyword(k),
39 lambda(l),
40 eventDriven(e)
41{
42}
43
44Module::Module()
45{
46}
47
48void Module::loadModules()
49{
50 addModule(List());
51}
52
53void Module::printCommands()
54{
55 for (const Module &module: s_modules) {
56 printSyntax(1, module.syntax(), module.description());
57 }
58}
59
60void Module::printSyntax(uint indent, const Syntax &syntax, const QString &description)
61{
62 const std::string indentation(indent, '\t');
63 std::cout << indentation;
64
65 if (indent < 2) {
66 std::cout << "hawd ";
67 }
68
69 std::cout << syntax.keyword.toStdString();
70
71 if (!description.isEmpty()) {
72 std::cout << ": " << description.toStdString();
73 }
74
75 std::cout << std::endl;
76
77 for (const Syntax &child: syntax.children) {
78 printSyntax(indent + 1, child);
79 }
80}
81
82void Module::addModule(const Module &module)
83{
84 s_modules.append(module);
85}
86
87QVector<Module> Module::modules()
88{
89 return s_modules;
90}
91
92bool Module::match(const QStringList &commands, State &state)
93{
94 for (const Module &module: s_modules) {
95 if (module.matches(commands, state)) {
96 return true;
97 }
98 }
99
100 return false;
101}
102
103Module::Syntax Module::syntax() const
104{
105 return m_syntax;
106}
107
108void Module::setSyntax(const Syntax &syntax)
109{
110 m_syntax = syntax;
111}
112
113QString Module::description() const
114{
115 return m_description;
116}
117
118void Module::setDescription(const QString &description)
119{
120 m_description = description;
121}
122
123bool Module::matches(const QStringList &commands, State &state) const
124{
125 if (commands.isEmpty()) {
126 return false;
127 }
128
129 QStringListIterator commandIt(commands);
130
131 if (commandIt.next() != m_syntax.keyword) {
132 return false;
133 }
134
135 QListIterator<Syntax> syntaxIt(m_syntax.children);
136 const Syntax *syntax = &m_syntax;
137 QStringList tailCommands;
138 while (commandIt.hasNext() && syntaxIt.hasNext()) {
139 const QString word = commandIt.next();
140 while (syntaxIt.hasNext()) {
141 const Syntax &child = syntaxIt.next();
142 if (word == child.keyword) {
143 syntax = &child;
144 syntaxIt = child.children;
145 }
146 }
147
148 if (!syntaxIt.hasNext()) {
149 tailCommands << word;
150 break;
151 }
152 }
153
154 if (syntax && syntax->lambda) {
155 while (commandIt.hasNext()) {
156 tailCommands << commandIt.next();
157 }
158
159 bool rv = syntax->lambda(tailCommands, state);
160 if (rv && syntax->eventDriven) {
161 return QCoreApplication::instance()->exec();
162 }
163
164 return rv;
165 }
166
167 return false;
168}
169
170} // namespace HAWD
diff --git a/tests/hawd/module.h b/tests/hawd/module.h
new file mode 100644
index 0000000..8f94707
--- /dev/null
+++ b/tests/hawd/module.h
@@ -0,0 +1,66 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "state.h"
23
24#include <QStringList>
25#include <QVector>
26
27namespace HAWD
28{
29
30class Module
31{
32public:
33 struct Syntax
34 {
35 Syntax();
36 Syntax(const QString &keyword, std::function<bool(const QStringList &, State &)> lambda = std::function<bool(const QStringList &, State &)>(), bool eventDriven = false);
37 QString keyword;
38 std::function<bool(const QStringList &, State &)> lambda;
39 QList<Syntax> children;
40 bool eventDriven;
41 };
42
43 static void addModule(const Module &module);
44 static QVector<Module> modules();
45 static bool match(const QStringList &commands, State &state);
46 static void loadModules();
47 static void printCommands();
48
49 Module();
50
51 Module::Syntax syntax() const;
52 void setSyntax(const Syntax &syntax);
53
54 QString description() const;
55 void setDescription(const QString &description);
56
57 bool matches(const QStringList &commands, State &state) const;
58
59private:
60 static void printSyntax(uint indent, const Syntax &syntax, const QString &description = QString());
61 Syntax m_syntax;
62 QString m_description;
63 static QVector<Module> s_modules;
64};
65
66} // namespace HAWD
diff --git a/tests/hawd/modules/list.cpp b/tests/hawd/modules/list.cpp
new file mode 100644
index 0000000..3310217
--- /dev/null
+++ b/tests/hawd/modules/list.cpp
@@ -0,0 +1,79 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "list.h"
21
22#include "../datasetdefinition.h"
23
24#include <QDebug>
25#include <QDir>
26#include <QObject>
27
28#include <iostream>
29
30namespace HAWD
31{
32
33List::List()
34 : Module()
35{
36 Syntax top("list", &List::list);
37 //top.children << Syntax("create", &List::create);
38 setSyntax(top);
39}
40
41bool List::list(const QStringList &commands, State &state)
42{
43 QDir project(state.projectPath());
44
45 if (commands.isEmpty()) {
46 project.setFilter(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot | QDir::NoSymLinks);
47 const QStringList entryList = project.entryList();
48
49 if (entryList.isEmpty()) {
50 std::cout << QObject::tr("No data sets in this project").toStdString() << std::endl;
51 } else {
52 std::cout << QObject::tr("Data sets in this project:").toStdString() << std::endl;
53 for (const QString &file: project.entryList()) {
54 std::cout << '\t' << file.toStdString() << std::endl;
55 }
56 }
57 } else {
58 for (const QString &file: commands) {
59 DatasetDefinition dataset(project.absoluteFilePath(file));
60 if (dataset.isValid()) {
61 DatasetDefinition dataset(project.absoluteFilePath(file));
62 std::cout << '\t' << QObject::tr("Dataset: %1").arg(dataset.name()).toStdString() << std::endl;
63 QHashIterator<QString, DataDefinition> it(dataset.columns());
64 while (it.hasNext()) {
65 it.next();
66 std::cout << "\t\t" << it.value().typeString().toStdString() << ' ' << it.key().toStdString() << std::endl;
67 }
68
69 } else {
70 std::cout << QObject::tr("Invalid or non-existent dataset definition at %1").arg(project.absoluteFilePath(file)).toStdString() << std::endl;
71 }
72 }
73 }
74
75 return true;
76}
77
78} // namespace HAWD
79
diff --git a/tests/hawd/modules/list.h b/tests/hawd/modules/list.h
new file mode 100644
index 0000000..3bd81e2
--- /dev/null
+++ b/tests/hawd/modules/list.h
@@ -0,0 +1,37 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "module.h"
23
24namespace HAWD
25{
26
27class List : public Module
28{
29public:
30 List();
31
32private:
33 static bool list(const QStringList &commands, State &state);
34};
35
36} // namespace HAWD
37
diff --git a/tests/hawd/state.cpp b/tests/hawd/state.cpp
new file mode 100644
index 0000000..cfc4326
--- /dev/null
+++ b/tests/hawd/state.cpp
@@ -0,0 +1,99 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "state.h"
21
22#include <QDebug>
23#include <QDir>
24#include <QJsonDocument>
25#include <QObject>
26
27#include <iostream>
28
29static const QString configFileName("hawd.conf");
30
31namespace HAWD
32{
33
34State::State()
35 : m_valid(true)
36{
37 QDir dir;
38 QString configPath;
39
40 while (!dir.exists(configFileName) && dir.cdUp()) { }
41
42 if (dir.exists(configFileName)) {
43 configPath = dir.absoluteFilePath(configFileName);
44 }
45
46 if (configPath.isEmpty()) {
47 std::cerr << QObject::tr("Could not find hawd configuration. A hawd.conf file must be in the current directory or in a directory above it.").toStdString() << std::endl;
48 m_valid = false;
49 return;
50 }
51
52 QFile configFile(configPath);
53 if (configFile.open(QIODevice::ReadOnly)) {
54 QJsonParseError error;
55 QJsonDocument config = QJsonDocument::fromJson(configFile.readAll(), &error);
56 if (config.isNull()) {
57 std::cerr << QObject::tr("Error parsing config file at %1").arg(configPath).toStdString() << std::endl;
58 std::cerr << '\t' << error.errorString().toStdString();
59 } else {
60 m_configData = config.object();
61 }
62 }
63}
64
65bool State::isValid() const
66{
67 return m_valid;
68}
69
70QString tildeExpand(QString path)
71{
72 if (path.isEmpty() || path.at(0) != '~') {
73 return path;
74 }
75
76 return path.replace('~', QDir::homePath());
77}
78
79QString State::resultsPath() const
80{
81 return tildeExpand(configValue("results").toString());
82}
83
84QString State::projectPath() const
85{
86 return tildeExpand(configValue("project").toString());
87}
88
89DatasetDefinition State::datasetDefinition(const QString &name) const
90{
91 return DatasetDefinition(projectPath() + '/' + name);
92}
93
94QVariant State::configValue(const QString &key) const
95{
96 return m_configData.value(key).toVariant();
97}
98
99} // namespace HAWD
diff --git a/tests/hawd/state.h b/tests/hawd/state.h
new file mode 100644
index 0000000..5e4d3c2
--- /dev/null
+++ b/tests/hawd/state.h
@@ -0,0 +1,47 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QStringList>
23#include <QJsonObject>
24
25#include "datasetdefinition.h"
26
27namespace HAWD
28{
29
30class State
31{
32public:
33 State();
34
35 bool isValid() const;
36 QVariant configValue(const QString &key) const;
37 QString resultsPath() const;
38 QString projectPath() const;
39 DatasetDefinition datasetDefinition(const QString &name) const;
40
41private:
42 bool m_valid;
43 QJsonObject m_configData;
44};
45
46} // namespace HAWD
47