#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2012-2015 Frédéric Magniette, Miguel Rubio-Roy
# This file is part of Pyrame.
#
# Pyrame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrame. If not, see <http://www.gnu.org/licenses/>
def init():
submod_newns("default")
submod_setvar("default","next_data_port",str(submod_getport("DS_BASE_PORT")),"0")
submod_setvar("default","next_dataready_port",str(submod_getport("DS_READY_BASE_PORT")),"0")
def newns_varmod(varmod_id,ns_name):
retcode,res=submod_newns(ns_name)
if retcode==0:
return 0,"already existing ns"
submod_setvar(ns_name,"next_data_port",str(submod_getport("DS_BASE_PORT")),"0")
submod_setvar(ns_name,"next_dataready_port",str(submod_getport("DS_READY_BASE_PORT")),"0")
return 1,ns_name
[docs]def delns_varmod(varmod_id,ns="default"):
"Delete a variables workspace and all its content"
return submod_delns(ns)
[docs]def checkns_varmod(varmod_id,ns="default"):
"Check if a workspace exists"
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"unknown workspace"
return 1,"ok"
[docs]def rmvar_varmod(varmod_id,ns,param_name):
"Remove a variable"
return submod_delvar(ns,param_name)
[docs]def setvar_varmod(varmod_id,ns,param_name,param_value):
"Set value on variable *param_name*"
return submod_setvar(ns,param_name,param_value,"0")
[docs]def getvar_varmod(varmod_id,ns,param_name):
"Get value of variable *param_name*"
return submod_getvar(ns,param_name)
def allvars_varmod(varmod_id,ns):
return submod_dumpns(ns)
[docs]def intopvar_varmod(varmod_id,ns,param_name,op,operand):
"Do integer operation *op* with *operand* on variable *param_name*. *op* can be '+', '-', 'x' or '/'"
#check ns
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"invalid workspace"
#check operand
try:
operand=int(operand)
except:
return 0,"operand is not integer"
#check old value
retcode,res=submod_getvar(ns,param_name)
if retcode==0:
old_value=0
else:
try:
old_value=int(res)
except:
return 0,"variable is not integer : %s"%(res)
#execute operation
new_value=None
if op=="+":
new_value=old_value+operand
if op=="-":
new_value=old_value-operand
if op=="x":
new_value=old_value*operand
if op=="/":
if operand==0:
return 0,"division by zero is forbidden"
new_value=old_value/operand
#check unknown operation
if new_value==None:
return 0,"unknown operation"
#set the new value
return submod_setvar(ns,param_name,str(new_value),"0")
[docs]def flopvar_varmod(varmod_id,ns,param_name,op,operand):
"Do floating point operation *op* with *operand* on variable *param_name*. *op* can be '+', '-', 'x' or '/'"
#check ns
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"invalid workspace"
#check operand
try:
operand=float(operand)
except:
return 0,"operand is not float"
#check old value
retcode,res=submod_getvar(ns,param_name)
if retcode==0:
old_value=0
else:
try:
old_value=float(res)
except:
return 0,"variable is not float : %s"%(res)
#execute operation
new_value=None
if op=="+":
new_value=old_value+operand
if op=="-":
new_value=old_value-operand
if op=="x":
new_value=old_value*operand
if op=="/":
if operand==0:
return 0,"division by zero is forbidden"
new_value=old_value/operand
#check unknown operation
if new_value==None:
return 0,"unknown operation"
#set the new value
return submod_setvar(ns,param_name,str(new_value),"0")
[docs]def stropvar_varmod(varmod_id,ns,param_name,op,operand):
"Do string operation *op* with *operand* on variable *param_name*. *op* can only be 'c' for concatenation"
#check ns
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"invalid workspace"
#check old value
retcode,res=submod_getvar(ns,param_name)
if retcode==0:
old_value=""
else:
old_value=res
#execute operation
new_value=None
if op=="c":
new_value=old_value+operand
#check unknown operation
if new_value==None:
return 0,"unknown operation"
#set the new value
return submod_setvar(ns,param_name,new_value,"0")
[docs]def get_token_varmod(varmod_id,ns,token_name):
"gives a token to the client if the token is free"
#check ns
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"invalid workspace"
retcode,res=submod_getvar(ns,token_name)
if retcode==0:
submod_setvar(ns,token_name,"1","0")
return 1,"ok"
else:
if res=="1":
return 0,"token already taken"
else:
submod_setvar(ns,token_name,"1","0")
return 1,"ok"
[docs]def release_token_varmod(varmod_id,ns,token_name):
"gives a token to the client if the token is free"
#check ns
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"invalid workspace"
retcode,res=submod_getvar(ns,token_name)
if retcode==0:
submod_setvar(ns,token_name,"0","0")
return 1,"ok"
else:
if res=="0":
return 0,"token is free"
else:
submod_setvar(ns,token_name,"0","0")
return 1,"ok"
[docs]def del_token_varmod(varmod_id,ns,token_name):
"delete a token"
#check ns
retcode,res=submod_checkns(ns)
if retcode==0:
return 0,"invalid workspace"
return submod_delvar(ns,token_name)
[docs]def register_ds_varmod(varmod_id,ns,source_name,host,data_port,dataready_port,schema):
"""register a data source"""
submod_setvar(ns,"ds_name_%s"%(source_name),source_name,"0")
submod_setvar(ns,"ds_host_%s"%(source_name),host,"0")
submod_setvar(ns,"ds_schema_%s"%(source_name),schema,"0")
sfields=schema.replace("!","|").replace("||","|")
if sfields[0]=='|':
sfields=sfields[1:]
fields=",".join(sfields.split("|"))
if fields[0]==',':
submod_setvar(ns,"ds_fields_%s"%(source_name),fields[1:],"0")
else:
submod_setvar(ns,"ds_fields_%s"%(source_name),fields,"0")
if data_port=="undef":
retcode,ndp=submod_getvar(ns,"next_data_port")
print("next_data_port=%s"%ndp)
submod_setvar(ns,"next_data_port",str(int(ndp)+1),"0")
else:
ndp=data_port
submod_setvar(ns,"ds_dataport_%s"%(source_name),ndp,"0")
if dataready_port=="undef":
retcode,ndrp=submod_getvar(ns,"next_dataready_port")
submod_setvar(ns,"next_dataready_port",str(int(ndrp)+1),"0")
else:
ndrp=dataready_port
submod_setvar(ns,"ds_datareadyport_%s"%(source_name),ndrp,"0")
return 1,"%s,%s"%(ndp,ndrp)
[docs]def get_ds_varmod(varmod_id,ns,source_name):
"""get a data source"""
retcode,res=submod_getvar(ns,"ds_name_%s"%(source_name))
if retcode==0:
return 0,"this data source does not exists"
else:
retcode,host=submod_getvar(ns,"ds_host_%s"%(source_name))
retcode,dp=submod_getvar(ns,"ds_dataport_%s"%(source_name))
retcode,drp=submod_getvar(ns,"ds_datareadyport_%s"%(source_name))
return 1,"%s,%s,%s"%(host,dp,drp)
[docs]def get_ds_fields_varmod(varmod_id,ns,source_name):
"""get a data source"""
retcode,res=submod_getvar(ns,"ds_name_%s"%(source_name))
if retcode==0:
return 0,"this data source does not exists"
else:
retcode,schema=submod_getvar(ns,"ds_fields_%s"%(source_name))
return 1,schema
[docs]def find_ds_varmod(varmod_id,ns,pattern):
"""find data sources"""
retcode,dump=submod_dumpns(ns)
if retcode==0:
return 0,"unknown ns"
values=eval(dump)
res=[]
for key in values:
if pattern in key and "ds_name_" in key:
res.append(key[8:])
if len(res)==0:
return 0,"no source with this pattern"
return 1,",".join(res)
[docs]def rm_ds_varmod(varmod_id,ns,source_name):
"""remove a data source"""
submod_delvar(ns,"ds_name_%s"%(source_name))
submod_delvar(ns,"ds_host_%s"%(source_name))
submod_delvar(ns,"ds_dataport_%s"%(source_name))
submod_delvar(ns,"ds_datareadyport_%s"%(source_name))
submod_delvar(ns,"ds_fields_%s"%(source_name))
submod_delvar(ns,"ds_schema_%s"%(source_name))
return 1,"ok"