codegen.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 schema_files = dict(
00004   object_ids = 'NodeIds.csv',
00005   status_codes = 'StatusCode.csv',
00006   status_codes_tostring = 'StatusCode.csv',
00007   attribute_ids = 'AttributeIds.csv',
00008   attribute_ids_getoptionvalue = 'AttributeIds.csv',
00009 )
00010 
00011 import csv
00012 import re
00013 
00014 def usage(msg=None):
00015   if msg: print ('Error {0}'.format(msg))
00016   print ('''Usage: {0} [cxx|py] [{1}]'''.format(sys.argv[0],','.join(schema_files.keys())))
00017   raise SystemExit
00018 
00019 
00020 first_cap_re = re.compile('(.)([A-Z][a-z]+)')
00021 all_cap_re = re.compile('([a-z0-9])([A-Z])')
00022 def camel_to_spacedstring(s):
00023   s1 = first_cap_re.sub(r'\1 \2', s)
00024   return all_cap_re.sub(r'\1 \2', s1).lower()
00025 
00026 
00027 def cxx_object_ids(fname):
00028   print('''//
00029 // DO NOT EDIT THIS FILE!
00030 // It is automatically generated from opcfoundation.org schemas.
00031 //
00032 
00033 #pragma once
00034 
00035 #include <stdint.h>
00036 
00037 namespace OpcUa
00038 {
00039   enum class ObjectId : uint32_t
00040   {
00041     Null = 0,''')
00042 
00043   with open(fname) as fd:
00044     for e in csv.reader(fd, delimiter=','):
00045       print ('    {0} = {1},'.format(*e[:2]))
00046 
00047   print ('''  };
00048 }
00049 ''')
00050 
00051 def cxx_status_codes(fname):
00052   print('''//
00053 // DO NOT EDIT THIS FILE!
00054 // It is automatically generated from opcfoundation.org schemas.
00055 //
00056 
00057 #pragma once
00058 
00059 #include <stdint.h>
00060 
00061 namespace OpcUa
00062 {
00063   enum class StatusCode : uint32_t
00064   {
00065     Good = 0,''')
00066 
00067   with open(fname) as fd:
00068     for e in csv.reader(fd, delimiter=','):
00069       print ('    {0} = {1},'.format(*e[:2]))
00070 
00071   print ('''  };
00072 
00073   //raise appropriate exception if StatusCode is not Good
00074   void CheckStatusCode(StatusCode code);
00075 
00076 }
00077 ''')
00078 
00079 def cxx_status_codes_tostring(fname):
00080   print('''//
00081 // DO NOT EDIT THIS FILE!
00082 // It is automatically generated from opcfoundation.org schemas.
00083 //
00084 
00085 #include <string>
00086 #include <sstream>
00087 #include <iomanip>
00088 
00089 #include "opc/ua/protocol/status_codes.h"
00090 
00091 namespace OpcUa
00092 {
00093 
00094 std::string ToString(const StatusCode& code)
00095 {
00096   if (code == StatusCode::Good)
00097   {
00098     return std::string();
00099   }
00100 
00101   std::stringstream stream;
00102   switch (code)
00103   {''')
00104 
00105   with open(fname) as fd:
00106     for e in csv.reader(fd, delimiter=','):
00107       print ('''    case StatusCode::{0}:
00108       stream << "{1}";
00109       break;'''.format(e[0],e[2]))
00110 
00111   print ('''    default:
00112       stream << "Unknown StatusCode?";
00113       break;
00114   }
00115 
00116   stream << " (0x" << std::setfill('0') << std::setw(8) << std::hex << (unsigned)code << ")";
00117 
00118   return stream.str();
00119 }
00120 
00121 } // namespace OpcUa
00122 ''')
00123 
00124 def cxx_attribute_ids(fname):
00125   print('''//
00126 // DO NOT EDIT THIS FILE!
00127 // It is automatically generated from opcfoundation.org schemas.
00128 //
00129 
00130 #pragma once
00131 
00132 #include <stdint.h>
00133 
00134 namespace OpcUa
00135 {
00136   enum class AttributeId : uint32_t
00137   {''')
00138 
00139   with open(fname) as fd:
00140     for e in csv.reader(fd, delimiter=','):
00141       print ('    {0} = {1},'.format(*e))
00142 
00143   print ('''    Unknown = ~uint32_t(),
00144   };
00145 } // namespace OpcUa
00146 ''')
00147 
00148 def cxx_attribute_ids_getoptionvalue(fname):
00149   print('''//
00150 // DO NOT EDIT THIS FILE!
00151 // It is automatically generated from opcfoundation.org schemas.
00152 //
00153 
00154 #pragma once
00155 
00156 inline AttributeId GetAttributeIdOptionValue(const po::variables_map& vm)
00157   {
00158     const std::string name = vm[OPTION_ATTRIBUTE].as<std::string>();''')
00159 
00160   with open(fname) as fd:
00161     for e in csv.reader(fd, delimiter=','):
00162       print ('''    if (name == "{0}")
00163     {{
00164       return AttributeId::{1};
00165     }}'''.format(camel_to_spacedstring(e[0]), e[0]))
00166 
00167   print ('''
00168     throw std::logic_error(std::string("Unknown AttributeId: ") + name);
00169   };
00170 ''')
00171 
00172 
00173 def py_object_ids(fname):
00174   print('''//
00175 // DO NOT EDIT THIS FILE!
00176 // It is automatically generated from opcfoundation.org schemas.
00177 //
00178 
00179 #include <boost/python.hpp>
00180 
00181 #include "opc/ua/protocol/object_ids.h"
00182 
00183 using namespace boost::python;
00184 using namespace OpcUa;
00185 
00186 void py_opcua_enums_ObjectId()
00187 {
00188   enum_<ObjectId>("ObjectId")
00189 #define _value(X) value(#X, ObjectId:: X)''')
00190 
00191   with open(fname) as fd:
00192     for e in csv.reader(fd, delimiter=','):
00193       print ('  ._value({0})'.format(e[0]))
00194 
00195   print ('''#undef _value
00196   ;
00197 }
00198 ''')
00199 
00200 def py_status_codes(fname):
00201   print('''//
00202 // DO NOT EDIT THIS FILE!
00203 // It is automatically generated from opcfoundation.org schemas.
00204 //
00205 
00206 #include <boost/python.hpp>
00207 
00208 #include "opc/ua/protocol/status_codes.h"
00209 
00210 using namespace boost::python;
00211 using namespace OpcUa;
00212 
00213 void py_opcua_enums_StatusCode()
00214 {
00215   enum_<StatusCode>("StatusCode")
00216 #define _value(X) value(#X, StatusCode:: X)''')
00217 
00218   with open(fname) as fd:
00219     for e in csv.reader(fd, delimiter=','):
00220       print ('  ._value({0})'.format(e[0]))
00221 
00222   print ('''#undef _value
00223   ;
00224 }
00225 ''')
00226 
00227 def py_status_codes_tostring(fname):
00228   raise Exception('py_status_codes_tostring not implemented')
00229 
00230 def py_attribute_ids(fname):
00231   print('''//
00232 // DO NOT EDIT THIS FILE!
00233 // It is automatically generated from opcfoundation.org schemas.
00234 //
00235 
00236 #include <boost/python.hpp>
00237 
00238 #include "opc/ua/protocol/attribute_ids.h"
00239 
00240 using namespace boost::python;
00241 using namespace OpcUa;
00242 
00243 void py_opcua_enums_AttributeId()
00244 {
00245   enum_<AttributeId>("AttributeId")
00246 #define _value(X) value(#X, AttributeId:: X)''')
00247 
00248   with open(fname) as fd:
00249     for e in csv.reader(fd, delimiter=','):
00250       print ('  ._value({0})'.format(e[0]))
00251 
00252   print ('''#undef _value
00253   ;
00254 }
00255 ''')
00256 
00257 def py_attribute_ids_getoptionvalue(fname):
00258   raise Exception('py_attribute_ids_getoptionvalue not implemented')
00259 
00260 if __name__ == '__main__':
00261   import sys
00262   import os
00263   if len(sys.argv) != 3: usage()
00264   target, what = sys.argv[1:]
00265   if target not in ('cxx','py',): usage()
00266   if what not in (schema_files.keys()): usage()
00267   call='{0}_{1}'.format(target,what)
00268   try:
00269     relpath = os.path.dirname(__file__)
00270     locals()[call](os.path.join(relpath,schema_files[what]))
00271   except Exception as e:
00272     usage(e)
00273 
00274 


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40