codegen.py
Go to the documentation of this file.
1 
2 schema_files = dict(
3  object_ids = 'NodeIds.csv',
4  object_ids_tostring = 'NodeIds.csv',
5  status_codes = 'StatusCode.csv',
6  status_codes_tostring = 'StatusCode.csv',
7  attribute_ids = 'AttributeIds.csv',
8  attribute_ids_getoptionvalue = 'AttributeIds.csv',
9  attribute_ids_tostring = 'AttributeIds.csv',
10 )
11 
12 import csv
13 import re
14 
15 def usage(msg=None):
16  if msg: print ('Error {0}'.format(msg))
17  print ('''Usage: {0} [cxx|py] [{1}]'''.format(sys.argv[0],','.join(schema_files.keys())))
18  raise SystemExit
19 
20 
21 first_cap_re = re.compile('(.)([A-Z][a-z]+)')
22 all_cap_re = re.compile('([a-z0-9])([A-Z])')
24  s1 = first_cap_re.sub(r'\1 \2', s)
25  return all_cap_re.sub(r'\1 \2', s1).lower()
26 
27 
28 def cxx_object_ids(fname):
29  print('''//
30 // DO NOT EDIT THIS FILE!
31 // It is automatically generated from opcfoundation.org schemas.
32 //
33 
34 #pragma once
35 
36 #include <stdint.h>
37 
38 namespace OpcUa
39 {
40 enum class ObjectId : uint32_t
41 {
42  Null = 0,''')
43 
44  with open(fname) as fd:
45  for e in csv.reader(fd, delimiter=','):
46  print (' {0} = {1},'.format(*e[:2]))
47 
48  print ('''};
49 }
50 ''')
51 
53  print('''//
54 // DO NOT EDIT THIS FILE!
55 // It is automatically generated from opcfoundation.org schemas.
56 //
57 
58 #include <sstream>
59 #include <string>
60 
61 #include "opc/ua/protocol/object_ids.h"
62 
63 namespace OpcUa
64 {
65 
66 std::string ToString(const ObjectId & value)
67 {
68  switch (value)
69  {''')
70 
71  with open(fname) as fd:
72  for e in csv.reader(fd, delimiter=','):
73  print (''' case ObjectId::{0}:
74  return "{1}";'''.format(e[0],e[0]))
75 
76  print (''' default:
77  {
78  std::stringstream result;
79  result << "unknown(" << static_cast<int>(value) << ")";
80  return result.str();
81  }
82  }
83 }
84 
85 } // namespace OpcUa
86 ''')
87 
88 def cxx_status_codes(fname):
89  print('''//
90 // DO NOT EDIT THIS FILE!
91 // It is automatically generated from opcfoundation.org schemas.
92 //
93 
94 #pragma once
95 
96 #include <stdint.h>
97 
98 namespace OpcUa
99 {
100 enum class StatusCode : uint32_t
101 {
102  Good = 0,''')
103 
104  with open(fname) as fd:
105  for e in csv.reader(fd, delimiter=','):
106  print (' {0} = {1},'.format(*e[:2]))
107 
108  print ('''};
109 
110 //raise appropriate exception if StatusCode is not Good
111 void CheckStatusCode(StatusCode code);
112 
113 }
114 ''')
115 
117  print('''//
118 // DO NOT EDIT THIS FILE!
119 // It is automatically generated from opcfoundation.org schemas.
120 //
121 
122 #include <string>
123 #include <sstream>
124 #include <iomanip>
125 
126 #include "opc/ua/protocol/status_codes.h"
127 
128 namespace OpcUa
129 {
130 
131 std::string ToString(const StatusCode & code)
132 {
133  if (code == StatusCode::Good)
134  {
135  return std::string();
136  }
137 
138  std::stringstream stream;
139 
140  switch (code)
141  {''')
142 
143  with open(fname) as fd:
144  for e in csv.reader(fd, delimiter=','):
145  print (''' case StatusCode::{0}:
146  stream << "{1}";
147  break;
148 '''.format(e[0],e[2]))
149 
150  print (''' default:
151  stream << "Unknown StatusCode?";
152  break;
153  }
154 
155  stream << " (0x" << std::setfill('0') << std::setw(8) << std::hex << (unsigned)code << ")";
156 
157  return stream.str();
158 }
159 
160 } // namespace OpcUa
161 ''')
162 
163 def cxx_attribute_ids(fname):
164  print('''//
165 // DO NOT EDIT THIS FILE!
166 // It is automatically generated from opcfoundation.org schemas.
167 //
168 
169 #pragma once
170 
171 #include <stdint.h>
172 
173 namespace OpcUa
174 {
175 enum class AttributeId : uint32_t
176 {''')
177 
178  with open(fname) as fd:
179  for e in csv.reader(fd, delimiter=','):
180  print (' {0} = {1},'.format(*e))
181 
182  print (''' Unknown = ~uint32_t(),
183 };
184 } // namespace OpcUa
185 ''')
186 
188  print('''//
189 // DO NOT EDIT THIS FILE!
190 // It is automatically generated from opcfoundation.org schemas.
191 //
192 
193 #pragma once
194 
195 inline AttributeId GetAttributeIdOptionValue(const po::variables_map & vm)
196 {
197  const std::string name = vm[OPTION_ATTRIBUTE].as<std::string>();
198 ''')
199 
200  with open(fname) as fd:
201  for e in csv.reader(fd, delimiter=','):
202  print (''' if (name == "{0}")
203  {{
204  return AttributeId::{1};
205  }}
206 '''.format(camel_to_spacedstring(e[0]), e[0]))
207 
208  print (''' throw std::logic_error(std::string("Unknown AttributeId: ") + name);
209 }
210 ''')
211 
213  print('''//
214 // DO NOT EDIT THIS FILE!
215 // It is automatically generated from opcfoundation.org schemas.
216 //
217 
218 #include <sstream>
219 #include <string>
220 
221 #include "opc/ua/protocol/attribute_ids.h"
222 
223 namespace OpcUa
224 {
225 
226 std::string ToString(const AttributeId & value)
227 {
228  switch (value)
229  {''')
230 
231  with open(fname) as fd:
232  for e in csv.reader(fd, delimiter=','):
233  print (''' case AttributeId::{0}:
234  return "{1}";'''.format(e[0],e[0]))
235 
236  print (''' default:
237  {
238  std::stringstream result;
239  result << "unknown(" << static_cast<int>(value) << ")";
240  return result.str();
241  }
242  }
243 }
244 
245 } // namespace OpcUa
246 ''')
247 
248 def py_object_ids(fname):
249  print('''//
250 // DO NOT EDIT THIS FILE!
251 // It is automatically generated from opcfoundation.org schemas.
252 //
253 
254 #include <boost/python.hpp>
255 
256 #include "opc/ua/protocol/object_ids.h"
257 
258 using namespace boost::python;
259 using namespace OpcUa;
260 
261 void py_opcua_enums_ObjectId()
262 {
263  enum_<ObjectId>("ObjectId")
264 #define _value(X) value(#X, ObjectId:: X)''')
265 
266  with open(fname) as fd:
267  for e in csv.reader(fd, delimiter=','):
268  print (' ._value({0})'.format(e[0]))
269 
270  print ('''#undef _value
271  ;
272 }
273 ''')
274 
275 def py_status_codes(fname):
276  print('''//
277 // DO NOT EDIT THIS FILE!
278 // It is automatically generated from opcfoundation.org schemas.
279 //
280 
281 #include <boost/python.hpp>
282 
283 #include "opc/ua/protocol/status_codes.h"
284 
285 using namespace boost::python;
286 using namespace OpcUa;
287 
288 void py_opcua_enums_StatusCode()
289 {
290  enum_<StatusCode>("StatusCode")
291 #define _value(X) value(#X, StatusCode:: X)''')
292 
293  with open(fname) as fd:
294  for e in csv.reader(fd, delimiter=','):
295  print (' ._value({0})'.format(e[0]))
296 
297  print ('''#undef _value
298  ;
299 }
300 ''')
301 
303  raise Exception('py_status_codes_tostring not implemented')
304 
305 def py_attribute_ids(fname):
306  print('''//
307 // DO NOT EDIT THIS FILE!
308 // It is automatically generated from opcfoundation.org schemas.
309 //
310 
311 #include <boost/python.hpp>
312 
313 #include "opc/ua/protocol/attribute_ids.h"
314 
315 using namespace boost::python;
316 using namespace OpcUa;
317 
318 void py_opcua_enums_AttributeId()
319 {
320  enum_<AttributeId>("AttributeId")
321 #define _value(X) value(#X, AttributeId:: X)''')
322 
323  with open(fname) as fd:
324  for e in csv.reader(fd, delimiter=','):
325  print (' ._value({0})'.format(e[0]))
326 
327  print ('''#undef _value
328  ;
329 }
330 ''')
331 
333  raise Exception('py_attribute_ids_getoptionvalue not implemented')
334 
335 if __name__ == '__main__':
336  import sys
337  import os
338  if len(sys.argv) != 3: usage()
339  target, what = sys.argv[1:]
340  if target not in ('cxx','py',): usage()
341  if what not in schema_files.keys(): usage()
342  call='{0}_{1}'.format(target,what)
343  try:
344  relpath = os.path.dirname(__file__)
345  locals()[call](os.path.join(relpath,schema_files[what]))
346  except Exception as e:
347  usage(e)
348 
349 
def cxx_status_codes(fname)
Definition: codegen.py:88
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:873
def usage(msg=None)
Definition: codegen.py:15
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3686
def cxx_attribute_ids(fname)
Definition: codegen.py:163
def py_attribute_ids_getoptionvalue(fname)
Definition: codegen.py:332
def py_attribute_ids(fname)
Definition: codegen.py:305
def camel_to_spacedstring(s)
Definition: codegen.py:23
def cxx_object_ids_tostring(fname)
Definition: codegen.py:52
def py_status_codes_tostring(fname)
Definition: codegen.py:302
def cxx_object_ids(fname)
Definition: codegen.py:28
def cxx_attribute_ids_tostring(fname)
Definition: codegen.py:212
def cxx_status_codes_tostring(fname)
Definition: codegen.py:116
def py_status_codes(fname)
Definition: codegen.py:275
def py_object_ids(fname)
Definition: codegen.py:248
def cxx_attribute_ids_getoptionvalue(fname)
Definition: codegen.py:187


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04