CommandBankUtils.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 "CommandBank.h"
42 #include "CommandSections.h"
43 #include "drain/image/Image.h"
44 
45 
46 namespace drain {
47 
48 
49 // Static
50 /*
51 class CommandBankUtils : public CommandBank {
52 public:
53 };
54 */
55 
56 
57 
58 
59 
60 class CmdVerbosity : public SimpleCommand<int> {
61 
62 public:
63 
64  CmdVerbosity() : SimpleCommand<int>(__FUNCTION__, "Logging verbosity", "level", drain::getLog().getVerbosity()) {
65  };
66 
67 
68  inline
69  void exec() const {
70  // NEW
71  Context & ctx = getContext<Context>();
72  ctx.log.setVerbosity(value);
73 
74  // OLD
75  drain::getLog().setVerbosity(value);
76  drain::image::getImgLog().setVerbosity(value-1);
77  }
78 
79 };
80 
81 class CmdDebug : public BasicCommand {
82 
83 public:
84 
85  inline
86  CmdDebug() : BasicCommand(__FUNCTION__, "Set verbosity to LOG_DEBUG") { // TODO
87  };
88 
89  inline
90  void exec() const {
91  Context & ctx = getContext<Context>();
92  ctx.log.setVerbosity(LOG_DEBUG);
93  drain::getLog().setVerbosity(LOG_DEBUG);
94  drain::image::getImgLog().setVerbosity(LOG_DEBUG);
95  //r.run("verbose","8"); // FIXME r.setVerbosity();
96  };
97 
98 };
99 
100 //class CmdLog : public SimpleCommand<> {
101 class CmdLog : public BasicCommand {
102 
103 public:
104 
105  CmdLog(CommandBank & cmdBank);
106 
107  CmdLog(const CmdLog & cmd);
108 
109  void exec() const;
110 
111 protected:
112 
113  CommandBank & bank;
114 
115  std::string filename;
116 
117  // For numeric or string keys
118  std::string level;
119 
120  bool timing;
121 
122 };
123 
124 
125 
126 
127 
129 
130 public:
131 
132  inline
133  CmdStatus() : drain::BasicCommand(__FUNCTION__, "Dump information on current images.") {
134  };
135 
136  void exec() const;
137 
138 };
139 
140 
142 
143 public:
144 
145  inline
146  CmdExpandVariables() : BasicCommand(__FUNCTION__, "Toggle variable expansions on/off") {
147  };
148 
149  inline
150  void exec() const {
151  SmartContext & ctx = getContext<SmartContext>();
152  //CommandRegistry & r = getRegistry();
153  ctx.expandVariables = !ctx.expandVariables;
154  };
155 
156 };
157 
158 
160 
164 class CmdScript : public SimpleCommand<std::string> {
165 
166 public:
167 
168  inline
169  CmdScript(CommandBank & cmdBank) :
170  SimpleCommand<std::string>(__FUNCTION__, "Define script.", "script"),
171  bank(cmdBank){
172  cmdBank.scriptCmd = getName(); // mark me special
173  };
174 
175  inline
176  CmdScript(const CmdScript & cmd) : SimpleCommand<std::string>(cmd), bank(cmd.bank){
177  }
178 
179 protected:
180 
181  // Copy constructor should copy this as well.
182  // FUture versions may store the script in Context?
183  CommandBank & bank;
184 
185 };
186 
187 
189 
193 class CmdExecScript : public BasicCommand {
194 
195 public:
196 
197  inline
198  CmdExecScript() :
199  BasicCommand(__FUNCTION__, "Execute script.") {
200  // const drain::Flagger::ivalue_t
201  cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
202  this->section |= TRIGGER;
203  /*
204  drain::Logger mout(__FILE__, __FUNCTION__);
205  mout.experimental("constr: TRIGGER=" , TRIGGER );
206  mout.experimental("My sections: " , this->section );
207  */
208  };
209 
210  /*
211  inline
212  CmdExecScript(const CmdExecScript & cmd) : BasicCommand(cmd){
213  drain::Logger mout(__FILE__, __FUNCTION__);
214  mout.experimental("Copy constr, their sections: " , cmd.section );
215  mout.experimental("Copy constr, my sections: " , this->section );
216  };
217  */
218  /*
219  CmdExecScript(CommandBank & cmdBank) :
220  BasicCommand(__FUNCTION__, "Execute script.") {
221  //cmdBank.setScriptExecCmd(getName());
222  const drain::Flagger::value_t TRIGGER = drain::Static::get<drain::TriggerSection>().index;
223  cmdBank.setScriptTriggerFlag(TRIGGER);
224  this->section |= TRIGGER;
225 
226  drain::Logger mout(__FILE__, __FUNCTION__);
227  mout.experimental("constr: TRIGGER=" , TRIGGER );
228  mout.experimental(*this );
229  mout.experimental("My sections: '" , this->section , "' ." );
230 
231  };
232  */
233 
234  inline
235  void exec() const {
236  SmartContext & ctx = getContext<SmartContext>();
237  drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
238  mout.debug("Executing script with '" , getName() , "' ." );
239  //mout.warn("My sections: '" , this->section , "' ." );
240  //mout.error("This command '" , getName() , "' cannot be run independently." );
241  };
242 
243  // reconsider exec();
244 
245 protected:
246 
247  // Copy constructor should copy this as well.
248  // FUture versions may store the script in Context!
249  //CommandBank & bank;
250 
251 };
252 
253 
254 
256 
260 class CmdExecFile : public SimpleCommand<std::string> {
261 
262 public:
263 
264  CmdExecFile(CommandBank & cmdBank) :
265  SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
266  bank(cmdBank){
267  cmdBank.execFileCmd = getName(); // mark me special
268  };
269 
270 protected:
271 
272  // Copy constructor should copy this as well.
273  CommandBank & bank;
274 
275 };
276 
277 
279 class HelpCmd : public SimpleCommand<std::string> {
280 
281 public:
282 
283 
284  HelpCmd(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
285  };
286 
287  inline
288  void exec() const {
289  if (value.empty()){
290  if (cmdBank.has("general")){
291  cmdBank.help("general");
292  exit(0);
293  }
294  }
295  cmdBank.help(value);
296  // TODO: "see-also" commands as a list, which is checked.
297  exit(0);
298  }
299 
300 protected:
301 
302  // Copy constructor should copy this as well.
303  CommandBank & cmdBank;
304 
305 };
306 
307 
308 
309 
310 //template <class C=Context>
311 class CmdFormat : public SimpleCommand<std::string> {
312 
313 public:
314 
315  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.") {
316  };
317 
318  inline
319  void exec() const {
320  SmartContext &ctx = getContext<SmartContext>();
321  ctx.formatStr = value;
322  }
323 
324 
325 };
326 
327 
328 
329 
330 template <class C=Context>
331 class CmdFormatFile : public SimpleCommand<std::string> {
332 
333 public:
334 
335 
336  CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
337  };
338 
339  void exec() const;
340 
341 };
342 
343 
344 template <class C>
346 
347  C &ctx = getContext<C>();
348 
349  drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
350 
351  //drain::Input ifstr(value);
352  std::ifstream ifstr;
353  ifstr.open(value.c_str(), std::ios::in);
354  if (ifstr.good()){
355  std::stringstream sstr;
356  sstr << ifstr.rdbuf();
357  /*
358  for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
359  sstr << (char)c;
360  }
361  */
362  ifstr.close();
363  //Context &ctx = getContext<>();
364  ctx.formatStr = sstr.str(); // SmartContext ?
365 
366  }
367  else
368  mout.error(getName() , ": opening file '" , value , "' failed." );
369 
370 };
371 
372 
374 class CmdNotFound : public SimpleCommand<> {
375 
376 public:
377 
378  CmdNotFound(CommandBank & cmdBank) :
379  SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
380  //cmdBank(cmdBank)
381  {
382  section = 0; // hidden
383  cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
384  };
385 
386  void exec() const {
387  Context & ctx = getContext<>();
388 
389  drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
390 
391  mout.error("Command '" , value , "' not found." );
393  }
394 
395 };
396 
397 
398 
399 } /* namespace drain */
400 
401 #endif
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition: Bank.h:141
Simple implementation of Command: adds name , description and parameters .
Definition: Command.h:385
Definition: CommandBankUtils.h:81
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:90
Load script file and execute the commands immediately using current Context.
Definition: CommandBankUtils.h:260
Executes the defined script.
Definition: CommandBankUtils.h:193
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:235
Definition: CommandBankUtils.h:141
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:150
Definition: CommandBankUtils.h:331
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:345
Definition: CommandBankUtils.h:311
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:319
Definition: CommandBankUtils.h:101
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.cpp:52
Special command for handling undefined commands.
Definition: CommandBankUtils.h:374
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:386
Load script file and executes the commands immediately.
Definition: CommandBankUtils.h:164
Definition: CommandBankUtils.h:128
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.cpp:73
Definition: CommandBankUtils.h:60
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:69
Container and execution tools for commands derived from Command.
Definition: CommandBank.h:56
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition: CommandBank.h:225
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition: CommandBank.h:271
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition: CommandBank.cpp:856
std::string scriptCmd
Command for storing a routine.
Definition: CommandBank.h:237
int cmd_section_type
Definition: Command.h:173
std::string formatStr
Definition: Context.h:205
Definition: Context.h:57
CommandBank-dependent.
Definition: CommandBankUtils.h:279
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:288
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:414
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:678
A single-parameter command.
Definition: Command.h:409
Definition: Context.h:211
Definition: DataSelector.cpp:1277