Loading...
Searching...
No Matches
CommandBankUtils.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32
33// New design (2020)
34
35#ifndef COMMAND_BANK_UTILS_H_
36#define COMMAND_BANK_UTILS_H_
37
38#include <iostream>
39#include <fstream>
40
41#include "drain/image/Image.h"
42#include "drain/util/JSON.h"
43
44#include "CommandBank.h"
45#include "CommandSections.h"
46
47
48namespace drain {
49
50
51// Static
52/*
53class CommandBankUtils : public CommandBank {
54public:
55};
56*/
57
58// Rename CommandUtils?
59
60
61
62
63class CmdVerbosity : public SimpleCommand<int> {
64
65public:
66
67 CmdVerbosity() : SimpleCommand<int>(__FUNCTION__, "Logging verbosity", "level", drain::getLog().getVerbosity()) {
68 };
69
70
71 inline
72 void exec() const {
73 // NEW
74 Context & ctx = getContext<Context>();
75 ctx.log.setVerbosity(value);
76
77 // OLD
78 drain::getLog().setVerbosity(value);
79 drain::image::getImgLog().setVerbosity(value-1);
80 }
81
82};
83
84class CmdDebug : public BasicCommand {
85
86public:
87
88 inline
89 CmdDebug() : BasicCommand(__FUNCTION__, "Set verbosity to LOG_DEBUG") { // TODO
90 };
91
92 inline
93 void exec() const {
94 Context & ctx = getContext<Context>();
95 ctx.log.setVerbosity(LOG_DEBUG);
96 drain::getLog().setVerbosity(LOG_DEBUG);
97 drain::image::getImgLog().setVerbosity(LOG_DEBUG);
98 //r.run("verbose","8"); // FIXME r.setVerbosity();
99 };
100
101};
102
103class CmdForEach : public drain::SimpleCommand<std::string> {
104
105public:
106
107 CmdForEach() : drain::SimpleCommand<std::string>(__FUNCTION__, "Iterate a list of values, calling a script for each", "key","val,val2,...","comma-separated list") {
108 };
109
110 void exec() const override;
111
112};
113
114
116
125class CmdJSON : public drain::SimpleCommand<std::string> { // drain::BasicCommand {
126public:
127
128 CmdJSON() : drain::SimpleCommand<std::string>(__FUNCTION__, "Export a command to JSON.", "property", "", ""){
129 };
130
131
132
133 template <class T>
134 static
135 void commandsToJSON(const drain::CommandBank & cmdBank, const std::string & cmds, drain::JSONtree & dst, const T & separator){
136 std::list<std::string> cmdList;
137 drain::StringTools::split(cmds, cmdList, separator);
138 commandsToJSON(cmdBank, cmdList, dst);
139 }
140
141 static
142 void commandsToJSON(const drain::CommandBank & cmdBank, const std::list<std::string> & cmds, drain::JSONtree & dst);
143
144
145 static
146 void commandToJSON(const drain::Command & cmd, drain::JSONtree & dst);
147
148 static
149 void commandToJSON_old(const drain::Command & cmd, drain::JSONtree & dst);
150
151 static
152 const std::map<std::string,std::string> & getVariables();
153
154
155 virtual
156 void exec() const {
157
158 //RackContext & ctx = getContext<RackContext>();
159 drain::Context & ctx = getContext<>();
160
161 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
162
163 //drain::CommandRegistry & reg = drain::getRegistry();
165
166 std::ostream & ostr = std::cout;
167
168 if (value.empty()){
169 mout.unimplemented("drain::JSONwriter");
170 }
171 else {
172
173 if (!cmdBank.has(value)){
174 mout.error("no such key: " , value );
175 return;
176 }
177
178 drain::JSONtree jsonRoot;
179 //drain::JSONtree & json = jsonRoot[value];
180 commandToJSON_old(cmdBank.get(value), jsonRoot[value]);
182 }
183
184 //std::cout << std::setw(10) << "Voila!" << std::endl;
185 };
186
187};
188
189
190/*
191class CmdForEach : public drain::SimpleCommand<std::string> {
192
193public:
194
195 CmdForEach() : drain::SimpleCommand<std::string>(__FUNCTION__, "Iterate values of status variable ${key}, calling a script for each", "key") {
196 };
197
198 void exec() const override;
199
200};
201*/
202//class CmdLog : public SimpleCommand<> {
203class CmdLog : public BasicCommand {
204
205public:
206
207 CmdLog(CommandBank & cmdBank);
208
209 CmdLog(const CmdLog & cmd);
210
211 void exec() const;
212
213protected:
214
215 CommandBank & bank;
216
217 std::string filename;
218
219 // For numeric or string keys
220 std::string level;
221
222 bool timing;
223
224};
225
226
227
228
229
231
232public:
233
234 inline
235 CmdStatus() : drain::BasicCommand(__FUNCTION__, "Dump information on current images.") {
236 };
237
238 void exec() const;
239
240};
241
242
249
250public:
251
252 inline
253 CmdExpandVariables() : BasicCommand(__FUNCTION__, "Toggle variable expansions on/off") {
254 };
255
256 inline
257 void exec() const {
258 SmartContext & ctx = getContext<SmartContext>();
259 ctx.expandVariables = !ctx.expandVariables;
260 };
261
262};
263
264
266
270class CmdScript : public SimpleCommand<std::string> {
271
272public:
273
274 inline
275 CmdScript(CommandBank & cmdBank) :
276 SimpleCommand<std::string>(__FUNCTION__, "Define script.", "script"),
277 bank(cmdBank){
278 cmdBank.scriptCmd = getName(); // mark me special
279 };
280
281 inline
282 CmdScript(const CmdScript & cmd) : SimpleCommand<std::string>(cmd), bank(cmd.bank){
283 }
284
285protected:
286
287 // Copy constructor should copy this as well.
288 // FUture versions may store the script in Context?
289 CommandBank & bank;
290
291};
292
294
298class CmdScriptFile : public SimpleCommand<std::string> {
299
300public:
301
302 inline
303 CmdScriptFile() : // CommandBank & cmdBank
304 SimpleCommand<std::string>(__FUNCTION__, "Define script.", "filename"){
305 //bank(cmdBank){
306 //cmdBank.scriptCmd = getName(); // mark me special
307 };
308
309 inline
311 }
312
313// protected:
314
315 //inline
316 void exec() const override;
317 /*
318 {
319 SmartContext & ctx = getContext<SmartContext>();
320 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
321 mout.debug("Storing script with '" , getName() , "' ." );
322 };
323 */
324
325
326};
327
328
330
337
338public:
339
340 inline
341 CmdExecScript() :
342 BasicCommand(__FUNCTION__, "Execute script.") {
343 // const drain::Flagger::ivalue_t
344 cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
345 this->section |= TRIGGER;
346 };
347
348
349 inline
350 void exec() const {
351 SmartContext & ctx = getContext<SmartContext>();
352 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
353 mout.debug("Storing script with '" , getName() , "' ." );
354 };
355
356
357};
358
359
360
361
363
367class CmdExecFile : public SimpleCommand<std::string> {
368
369public:
370
371 CmdExecFile(CommandBank & cmdBank) :
372 SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
373 bank(cmdBank){
374 cmdBank.execFileCmd = getName(); // mark me special
375 };
376
377protected:
378
379 // Copy constructor should copy this as well.
380 CommandBank & bank;
381
382};
383
384
386class CmdHelp : public SimpleCommand<std::string> {
387
388public:
389
390
391 CmdHelp(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
392 };
393
394 inline
395 void exec() const {
396 if (value.empty()){
397 if (cmdBank.has("general")){
398 cmdBank.help("general");
399 exit(0);
400 }
401 }
402 cmdBank.help(value);
403 // TODO: "see-also" commands as a list, which is checked.
404 exit(0);
405 }
406
407protected:
408
409 // Copy constructor should copy this as well.
410 CommandBank & cmdBank;
411
412};
413
414
415
416
417//template <class C=Context>
418class CmdFormat : public SimpleCommand<std::string> {
419
420public:
421
422 CmdFormat() : SimpleCommand<std::string>(__FUNCTION__,"Set format for data dumps (see --sample or --outputFile)", "format","") { // SimpleCommand<std::string>(getResources().generalCommands, name, alias, "Sets a format std::string.") {
423 };
424
425 inline
426 void exec() const {
427 SmartContext &ctx = getContext<SmartContext>();
428 ctx.formatStr = value;
429 }
430
431
432};
433
434
435
436
437template <class C=Context>
438class CmdFormatFile : public SimpleCommand<std::string> {
439
440public:
441
442
443 CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
444 };
445
446 void exec() const;
447
448};
449
450
451template <class C>
453
454 C &ctx = getContext<C>();
455
456 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
457
458 //drain::Input ifstr(value);
459 std::ifstream ifstr;
460 ifstr.open(value.c_str(), std::ios::in);
461 if (ifstr.good()){
462 std::stringstream sstr;
463 sstr << ifstr.rdbuf();
464 /*
465 for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
466 sstr << (char)c;
467 }
468 */
469 ifstr.close();
470 //Context &ctx = getContext<>();
471 ctx.formatStr = sstr.str(); // SmartContext ?
472
473 }
474 else
475 mout.error(getName() , ": opening file '" , value , "' failed." );
476
477}
478
479
481class CmdNotFound : public SimpleCommand<> {
482
483public:
484
485 CmdNotFound(CommandBank & cmdBank) :
486 SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
487 //cmdBank(cmdBank)
488 {
489 section = 0; // hidden
490 cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
491 };
492
493 void exec() const {
494 Context & ctx = getContext<>();
495
496 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
497
498 mout.error("Command '" , value , "' not found." );
500 }
501
502};
503
504
505
506} /* namespace drain */
507
508#endif
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition Bank.h:140
const T & get(const K &key) const
Returns the base instance.
Definition Bank.h:158
Simple implementation of Command: adds name , description and parameters .
Definition Command.h:461
const std::string & getName() const final
Returns the command name.
Definition Command.h:384
Definition CommandBankUtils.h:84
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:93
Load script file and execute the commands immediately using current Context.
Definition CommandBankUtils.h:367
Executes the defined script.
Definition CommandBankUtils.h:336
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:350
Definition CommandBankUtils.h:248
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:257
Definition CommandBankUtils.h:103
void exec() const override
Run the command with current parameter values.
Definition CommandBankUtils.cpp:101
Definition CommandBankUtils.h:438
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:452
Definition CommandBankUtils.h:418
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:426
CommandBank-dependent.
Definition CommandBankUtils.h:386
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:395
Explain..
Definition CommandBankUtils.h:125
virtual void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:156
static const std::map< std::string, std::string > & getVariables()
Definition CommandBankUtils.cpp:279
Definition CommandBankUtils.h:203
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:52
Special command for handling undefined commands.
Definition CommandBankUtils.h:481
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:493
Define a script.
Definition CommandBankUtils.h:298
void exec() const override
Run the command with current parameter values.
Definition CommandBankUtils.cpp:299
Define a script.
Definition CommandBankUtils.h:270
Definition CommandBankUtils.h:230
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:73
Definition CommandBankUtils.h:63
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:72
Container and execution tools for commands derived from Command.
Definition CommandBank.h:67
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:278
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:321
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition CommandBank.cpp:949
std::string scriptCmd
Command for storing a routine.
Definition CommandBank.h:290
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition Command.h:53
int cmd_section_type
Definition Command.h:187
cmd_section_type section
Section bitmask; zero means hidden from help.
Definition Command.h:188
std::string formatStr
Definition Context.h:214
Definition Context.h:55
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:676
Logger & error(const TT &... args)
Echoes.
Definition Log.h:426
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:521
A single-parameter command.
Definition Command.h:487
Definition Context.h:220
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:423
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition Sprinter.h:224
static void split(const std::string &s, T &sequence, const C &separators, const std::string &trimChars=" \t\n")
Splits and trims a given std::string to a std Sequence.
Definition StringTools.h:516
Definition Data.h:1473
CommandBank & getCommandBank()
Global program command registry. Optional utility.
Definition CommandBank.cpp:52