Source code for apipools
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2012-2017 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/>
import pools
try:
from __main__ import submod
except:
pass
[docs]class api_pool:
"API pool"
def __init__(self):
self.pool=pools.pool()
[docs] def add_api_from_string(self,module_name,str_api):
"""Add API to pool from string. semicolons between functions; colon between function name and parameters; comma between parameters. Example:
get_voltage_ps:ps_id,channel;get_current_ps:ps_id,channel;..."""
for f in str_api.strip("\n;").split(";"):
f = f.split(":")
if f[1]=="":
args=[]
else:
args=f[1]
self.pool.new("%s_%s"%(module_name,f[0]),{"model":module_name,"function":f[0],"args":args})
[docs] def add_api_from_module(self,module_name):
"""Add API to pool from another module"""
if self.is_present(module_name):
return 1,"ok"
retcode,res=submod.execcmd("getapi@"+module_name)
if retcode==0:
return 0,"cant get API for %s <- %s"%(module_name,res)
return 1,self.add_api_from_string(module_name,res)
[docs] def add_api_from_file(self,module_name,file_name):
"""Add API to pool from file. newlines or semicolons between functions; colon between function name and parameters; comma between parameters. Example:
get_voltage_ps:ps_id,channel
get_current_ps:ps_id,channel
..."""
with open(file_name,"r") as api_file:
api = api_file.read().replace("\n",";")
return self.add_api_from_string(module_name,api)
[docs] def get_api(self,module_name,func_name):
"Find *func_name* of *module_name* in pool and return a dictionary with keys *model*, *function* and *args*, where the two first are strings and the latter is a list of strings. If not found, return -1"
func_name=func_name.replace("@","_")
try:
api=self.pool.get("%s_%s"%(module_name,func_name))
except:
return None
return api["args"].split(",")
[docs] def is_present(self,module_name):
"Check if the API of *module_name* is present in the pool"
retcode,res=self.pool.find_by_field("model",module_name)
if retcode==-1:
return False
return True