Skip to content
Snippets Groups Projects
Commit e642bcf1 authored by jkerdreu's avatar jkerdreu
Browse files

- Added support for Golang

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@3229 b32b6428-25c9-4566-ad07-03861ab6144f
parent 4491786c
Branches
No related tags found
No related merge requests found
...@@ -19,20 +19,20 @@ BLACK_LIST=['application-layer.cddl', ...@@ -19,20 +19,20 @@ BLACK_LIST=['application-layer.cddl',
] ]
def name_to_method(name): def name_snake_case(name):
if name.endswith('.basic'): if name.endswith('.basic'):
return name.split('.basic')[0] return name.split('.basic')[0]
else: else:
return name.replace('.','_') return name.replace('.','_')
def name_to_class(name):
def name_to_camel_case(name):
if name.endswith('.basic'): if name.endswith('.basic'):
tmp = name.split('.basic')[0] tmp = name.split('.basic')[0]
else: else:
tmp = name.replace('.','_') tmp = name
return tmp[0].upper() + tmp[1:] tmp = tmp.title()
return tmp.replace('.','')
def dump(jsonDict): def dump(jsonDict):
...@@ -48,9 +48,9 @@ def dump(jsonDict): ...@@ -48,9 +48,9 @@ def dump(jsonDict):
class Schemas: class Schemas:
def __init__(self): def __init__(self):
self.__cache = {} self.__cache = {}
def load(self,filename): def load(self,filename):
""" load schema from disk, and put it in cache """ load schema from disk, and put it in cache
...@@ -65,10 +65,10 @@ class Schemas: ...@@ -65,10 +65,10 @@ class Schemas:
self.__cache.update({filename:jsonDict}) self.__cache.update({filename:jsonDict})
return jsonDict return jsonDict
def get_extends(self,name): def get_extends(self,name):
"""return the chain list off extends in reverse order, any.any is the first item""" """return the chain list off extends in reverse order, any.any is the first item"""
current = name current = name
extends = [name,] extends = [name,]
while 1: while 1:
...@@ -87,8 +87,8 @@ class Schemas: ...@@ -87,8 +87,8 @@ class Schemas:
ext = self.get_extends(name) ext = self.get_extends(name)
res = self.load(name) res = self.load(name)
tmpMethods = {} tmpMethods = {}
tmpAttr = {} tmpAttr = {}
tmpNotifs = {} tmpNotifs = {}
tmpDataModel = {} tmpDataModel = {}
for e in ext: for e in ext:
...@@ -97,7 +97,7 @@ class Schemas: ...@@ -97,7 +97,7 @@ class Schemas:
if "methods" in _dict.keys(): if "methods" in _dict.keys():
tmp = _dict["methods"] tmp = _dict["methods"]
tmpMethods.update(tmp) tmpMethods.update(tmp)
if "attributes" in _dict.keys(): if "attributes" in _dict.keys():
tmp = _dict["attributes"] tmp = _dict["attributes"]
tmpAttr.update(tmp) tmpAttr.update(tmp)
...@@ -115,7 +115,7 @@ class Schemas: ...@@ -115,7 +115,7 @@ class Schemas:
res["notifications"] = tmpNotifs res["notifications"] = tmpNotifs
res["datamodel"] = tmpDataModel res["datamodel"] = tmpDataModel
return res return res
def get_devtypes(self): def get_devtypes(self):
l = os.listdir(SCHEMA_DIR) l = os.listdir(SCHEMA_DIR)
...@@ -125,17 +125,17 @@ class Schemas: ...@@ -125,17 +125,17 @@ class Schemas:
r.append(k) r.append(k)
r.sort() r.sort()
return r return r
class DeviceBuilder: class DeviceBuilder:
def __init__(self): def __init__(self):
self.schemas = Schemas() self.schemas = Schemas()
self.basic = self.schemas.get('basic.basic') self.basic = self.schemas.get('basic.basic')
def is_basic_method(self,value): def is_basic_method(self,value):
return value in self.basic['methods'] return value in self.basic['methods']
def is_basic_attribute(self,value): def is_basic_attribute(self,value):
return value in self.basic['attributes'] return value in self.basic['attributes']
...@@ -165,29 +165,29 @@ class DeviceBuilder: ...@@ -165,29 +165,29 @@ class DeviceBuilder:
#print("%s: %s %s" % (k,dict_['description'],list(dict_['parameters'].keys()))) #print("%s: %s %s" % (k,dict_['description'],list(dict_['parameters'].keys())))
methods.update({k:dict_}) methods.update({k:dict_})
datamodel = {} datamodel = {}
for k in data['datamodel']: for k in data['datamodel']:
if not self.is_basic_datamodel(k): if not self.is_basic_datamodel(k):
dict_ = data['datamodel'][k] dict_ = data['datamodel'][k]
datamodel.update({k:dict_}) datamodel.update({k:dict_})
args = {} args = {}
args['name'] = name_to_method(name) args['name'] = name_snake_case(name)
args['Name'] = name_to_class(name) args['Name'] = name_to_camel_case(name)
args['doc'] = data['description'] args['doc'] = data['description']
args['devtype'] = name args['devtype'] = name
args['attributes'] = attributes args['attributes'] = attributes
args['methods'] = methods args['methods'] = methods
args['datamodel'] = datamodel args['datamodel'] = datamodel
print(tmpl.render(**args)) print(tmpl.render(**args))
#return args #return args
def build_all(self,template): def build_all(self,template):
devs = self.schemas.get_devtypes() devs = self.schemas.get_devtypes()
for k in devs: for k in devs:
self.build(k,template) self.build(k,template)
def build_py(self): def build_py(self):
head = open('./head_py.txt','r').read() head = open('./head_py.txt','r').read()
print(head) print(head)
...@@ -198,7 +198,12 @@ class DeviceBuilder: ...@@ -198,7 +198,12 @@ class DeviceBuilder:
print(head) print(head)
self.build_all('devices_js.mako') self.build_all('devices_js.mako')
def build_go(self):
head = open('./head_go.txt','r').read()
print(head)
self.build_all('devices_go.mako')
db = DeviceBuilder() db = DeviceBuilder()
db.build_py() # db.build_py()
#db.build_js() # db.build_js()
db.build_go()
//=====================================================================
func New${Name}(addr uuid.UUID) *xaal.Device {
dev := xaal.NewDevice("${devtype}")
dev.Address = addr
% if (len(attributes) !=0):
// -------- Attributes --------
% for attr in attributes:
<%
dm = datamodel[attributes[attr]]
desc = dm.get('description','no-desc ?')
type_ = dm.get('type')
unit = dm.get('unit')
comment = desc
if type_:
comment = comment +' | type: ' + type_
if unit:
comment = comment +' | unit: ' + unit
%> // ${comment}
<%
type_ = dm.get('type')
value = 'nil'
if type_ == 'data = bool':
value = 'false'
elif type_ in ['data = uint','data = number']:
value = 0
elif unit in ['K']:
value = 0
elif unit in ['%','%EL','%RH','°']:
value = 0.0
%> dev.AddAttribute("${attr}",${value})
% endfor
% endif
% if (len(methods) !=0):
// -------- Methods --------
% for meth in methods:
<%
keys = list(methods[meth].get('in',{}).keys())
print_keys = ', '.join(keys)
camel_meth = "default"+ f"{meth}".replace('_','.').title().replace('.','')
%> // ${methods[meth]['description']}
% if len(keys) != 0 :
${camel_meth} := func(args xaal.MessageBody) *xaal.MessageBody {
// Arguments: ${print_keys}
fmt.Printf("default ${meth}: %v\n", args)
% else:
${camel_meth} := func(xaal.MessageBody) *xaal.MessageBody {
fmt.Printf("default ${meth}\n")
% endif
return nil
}
% endfor
% for meth in methods:
<%
camel_meth = "default"+f"{meth}".replace('_','.').title().replace('.','')
%> dev.AddMethod("${meth}", ${camel_meth})
% endfor
% endif
return dev
}
package xaalschemas
import (
"fmt"
xaal "xAAL/xaal"
"github.com/google/uuid"
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment