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 TRIGGER = drain::Static::get<drain::TriggerSection>().index;
201  this->section |= TRIGGER;
202  /*
203  drain::Logger mout(__FILE__, __FUNCTION__);
204  mout.experimental("constr: TRIGGER=" , TRIGGER );
205  mout.experimental("My sections: " , this->section );
206  */
207  };
208 
209  /*
210  inline
211  CmdExecScript(const CmdExecScript & cmd) : BasicCommand(cmd){
212  drain::Logger mout(__FILE__, __FUNCTION__);
213  mout.experimental("Copy constr, their sections: " , cmd.section );
214  mout.experimental("Copy constr, my sections: " , this->section );
215  };
216  */
217  /*
218  CmdExecScript(CommandBank & cmdBank) :
219  BasicCommand(__FUNCTION__, "Execute script.") {
220  //cmdBank.setScriptExecCmd(getName());
221  const drain::Flagger::value_t TRIGGER = drain::Static::get<drain::TriggerSection>().index;
222  cmdBank.setScriptTriggerFlag(TRIGGER);
223  this->section |= TRIGGER;
224 
225  drain::Logger mout(__FILE__, __FUNCTION__);
226  mout.experimental("constr: TRIGGER=" , TRIGGER );
227  mout.experimental(*this );
228  mout.experimental("My sections: '" , this->section , "' ." );
229 
230  };
231  */
232 
233  inline
234  void exec() const {
235  SmartContext & ctx = getContext<SmartContext>();
236  drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
237  mout.debug("Executing script with '" , getName() , "' ." );
238  //mout.warn("My sections: '" , this->section , "' ." );
239  //mout.error("This command '" , getName() , "' cannot be run independently." );
240  };
241 
242  // reconsider exec();
243 
244 protected:
245 
246  // Copy constructor should copy this as well.
247  // FUture versions may store the script in Context!
248  //CommandBank & bank;
249 
250 };
251 
252 
253 
255 
259 class CmdExecFile : public SimpleCommand<std::string> {
260 
261 public:
262 
263  CmdExecFile(CommandBank & cmdBank) :
264  SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
265  bank(cmdBank){
266  cmdBank.execFileCmd = getName(); // mark me special
267  };
268 
269 protected:
270 
271  // Copy constructor should copy this as well.
272  CommandBank & bank;
273 
274 };
275 
276 
278 class HelpCmd : public SimpleCommand<std::string> {
279 
280 public:
281 
282 
283  HelpCmd(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
284  };
285 
286  inline
287  void exec() const {
288  if (value.empty()){
289  if (cmdBank.has("general")){
290  cmdBank.help("general");
291  exit(0);
292  }
293  }
294  cmdBank.help(value);
295  // TODO: "see-also" commands as a list, which is checked.
296  exit(0);
297  }
298 
299 protected:
300 
301  // Copy constructor should copy this as well.
302  CommandBank & cmdBank;
303 
304 };
305 
306 
307 
308 
309 //template <class C=Context>
310 class CmdFormat : public SimpleCommand<std::string> {
311 
312 public:
313 
314  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.") {
315  };
316 
317  inline
318  void exec() const {
319  SmartContext &ctx = getContext<SmartContext>();
320  ctx.formatStr = value;
321  }
322 
323 };
324 
326 class CmdNotFound : public SimpleCommand<> {
327 
328 public:
329 
330  CmdNotFound(CommandBank & cmdBank) :
331  SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
332  //cmdBank(cmdBank)
333  {
334  section = 0; // hidden
335  cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
336  };
337 
338  void exec() const {
339  Context & ctx = getContext<>();
340 
341  drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
342 
343  mout.error("Command '" , value , "' not found." );
345  }
346 
347 };
348 
349 
350 
351 
352 template <class C=Context>
353 class CmdFormatFile : public SimpleCommand<std::string> {
354 
355 public:
356 
357 
358  CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
359  };
360 
361  void exec() const;
362 
363 };
364 
365 
366 template <class C>
368 
369  C &ctx = getContext<C>();
370 
371  drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
372 
373  //drain::Input ifstr(value);
374  std::ifstream ifstr;
375  ifstr.open(value.c_str(), std::ios::in);
376  if (ifstr.good()){
377  std::stringstream sstr;
378  sstr << ifstr.rdbuf();
379  /*
380  for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
381  sstr << (char)c;
382  }
383  */
384  ifstr.close();
385  //Context &ctx = getContext<>();
386  ctx.formatStr = sstr.str(); // SmartContext ?
387 
388  }
389  else
390  mout.error(getName() , ": opening file '" , value , "' failed." );
391 
392 };
393 
394 
395 } /* namespace drain */
396 
397 #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:417
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:259
Executes the defined script.
Definition: CommandBankUtils.h:193
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:234
Definition: CommandBankUtils.h:141
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:150
Definition: CommandBankUtils.h:353
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:367
Definition: CommandBankUtils.h:310
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:318
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:326
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:338
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:54
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition: CommandBank.h:222
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition: CommandBank.h:268
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition: CommandBank.cpp:850
std::string scriptCmd
Command for storing a routine.
Definition: CommandBank.h:234
int section
Definition: Command.h:213
std::string formatStr
Definition: Context.h:198
Definition: Context.h:57
CommandBank-dependent.
Definition: CommandBankUtils.h:278
void exec() const
Run the command with current parameter values.
Definition: CommandBankUtils.h:287
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
A single-parameter command.
Definition: Command.h:501
Definition: Context.h:204
Definition: DataSelector.cpp:1277