Loading...
Searching...
No Matches
PythonUtils.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//#include "PythonSerializer.h"
34
35#ifndef DRAIN_PYTHON_UTILS
36#define DRAIN_PYTHON_UTILS
37
38#include <list>
39//#include <drain/prog/Command.h>
40#include <drain/prog/CommandBank.h>
41
42namespace drain {
43
44
45
46
48
49public:
50
51 static
52 const std::string TRIPLE_HYPHEN;
53
54 // Whitespace
55 std::string indentStr = std::string(4, ' ');
56
57 static
58 const std::string & getType(const drain::Castable & c);
59
60 // std::string content = "pass";
61 std::list<std::string> content = {"return self._make_cmd(locals())"};
62
63 mutable
64 int counter = 100;
65
66 template <class T>
67 void exportImports(const std::initializer_list<T> & container, std::ostream & ostr=std::cout) const;
68
69 void exportCommands(const std::string &name, const drain::CommandBank & commandBank, std::ostream & ostr=std::cout, int indentLevel=0) const;
70
71 void exportCommand(const std::string & name, const drain::Command & command, std::ostream & ostr=std::cout, int indentLevel=1, bool implicit=false) const;
72
73
107};
108
109template <class T>
110void PythonConverter::exportImports(const std::initializer_list<T> & container, std::ostream & ostr) const {
111 for (const auto & entry: container){
112 ostr << "import " << entry << '\n';
113 }
114}
115
117
118public:
119
120 std::ostream & ostr = std::cout;
121 int indentLevel = 0;
122 std::string indentStr = " ";
123
124 PythonIndent(std::ostream & ostr=std::cout, int indentLevel=0, std::string indentStr=" ") : ostr(ostr), indentLevel(indentLevel), indentStr(indentStr) {
125 };
126
127 inline
128 void setIndent(int indentLevel){
129 if (indentLevel < 0)
130 indentLevel = 0;
131 this->indentLevel = indentLevel;
132 }
133
134 inline
135 void operator ++(){
136 ++this->indentLevel;
137 }
138
139 inline
140 void operator --(){
141 --this->indentLevel;
142 if (this->indentLevel < 0)
143 this->indentLevel = 0;
144 }
145
146 inline
147 void indent() const {
148 for (int i=0; i<this->indentLevel; ++i){
149 ostr << this->indentStr;
150 }
151 }
152
153 template <typename ... TT>
154 void write(TT... args) const {
155 this->indent();
156 this->flush(args...);
157 };
158
162 void writeTitle(const std::string & title) const;
163
164
165protected:
166
167 template <typename T, typename ... TT>
168 void flush(const T & arg, TT... args) const {
169 this->ostr << arg;
170 this->flush(args...);
171 };
172
173 inline
174 void flush() const {
175 this->ostr << '\n';
176 }
177
178};
179
180
181}
182
183#endif
Definition Castable.h:76
Container and execution tools for commands derived from Command.
Definition CommandBank.h:56
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition Command.h:55
Definition PythonUtils.h:47
Definition PythonUtils.h:116
void writeTitle(const std::string &title) const
Definition PythonUtils.cpp:41
Definition DataSelector.cpp:1277