Files
secondo/apis/python2/SecondoAPI/config_pkg/load_secondo_config.ipynb
2026-01-23 17:03:45 +08:00

148 lines
6.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"The module load_secondo_config implements the class to read the needed parametes for API from\n",
"a configuration file and return them.\n",
"\"\"\"\n",
"import configparser as cfg\n",
"import os\n",
"\n",
"from libs_pkg.exception_handler import *\n",
"\n",
"class Config():\n",
" \"\"\"\n",
" This class implements the needed attributes and methods to read the parametes related to Secondo from\n",
" a configuration file and return them.\n",
" \"\"\"\n",
" \n",
" def __init__(self,file=None,sec_srv=None,user=None,passw=None,sec_port=None,opt_srv=None,opt_port=None, binary_result=None):\n",
" if file and not (sec_srv and user and passw and sec_port and opt_srv and opt_port and binary_result):\n",
" self.file_name = file\n",
" self.sec_srv = \"\"\n",
" self.user = \"\"\n",
" self.passw = \"\"\n",
" self.sec_port = 0\n",
" self.opt_srv = \"\"\n",
" self.opt_port = 0\n",
" self.binary_result = False\n",
" self.cfg = cfg.ConfigParser(delimiters=('=', ':'), comment_prefixes=('#', ';'))\n",
" self.get_from_file()\n",
" \n",
" elif not (file and sec_srv and user and passw and sec_port and opt_srv and opt_port and binary_result):\n",
" print(\"A needed parameter is missing!\")\n",
" elif not file and sec_srv and user and passw and sec_port and opt_srv and opt_port and binary_result:\n",
" \n",
" if sec_srv == \"\":\n",
" raise SecondoAPI_Error('SecondoServer address is not set.')\n",
" self.sec_srv = sec_srv\n",
" if user == \"\":\n",
" raise SecondoAPI_Error('User name is not set.')\n",
" self.user = user\n",
" if passw == \"\":\n",
" raise SecondoAPI_Error('Password is not set.')\n",
" self.passw = passw\n",
" if sec_port == \"\":\n",
" raise SecondoAPI_Error('SecondoServer port is not set.')\n",
" self.sec_port = sec_port\n",
" if opt_srv == \"\":\n",
" raise SecondoAPI_Error('OptimizerServer address is not set.')\n",
" self.opt_srv = opt_srv\n",
" if opt_port == \"\":\n",
" raise SecondoAPI_Error('OptimizerServer port is not set.')\n",
" self.opt_port = opt_port\n",
" if binary_result == \"\":\n",
" raise SecondoAPI_Error('Result format of queries is not set.')\n",
" self.binary_result = binary_result\n",
" \n",
" def ConfigSectionMap(self, section):\n",
" dict1 = {}\n",
" options = self.cfg.options(section)\n",
" for option in options:\n",
" try:\n",
" dict1[option] = self.cfg.get(section, option)\n",
" if dict1[option] == -1:\n",
" DebugPrint(\"skip: %s\" % option)\n",
" except:\n",
" print(\"exception on %s!\" % option)\n",
" dict1[option] = None\n",
" return dict1\n",
"\n",
" def get_from_file(self):\n",
" \n",
" if os.path.isfile(os.getcwd() + '/' + self.file_name):\n",
" self.cfg.read(os.getcwd() + '/' + self.file_name)\n",
" self.sec_srv = self.ConfigSectionMap(\"General\")['servername']\n",
" if self.sec_srv == \"\":\n",
" raise SecondoAPI_Error('SecondoServer address is not set.')\n",
" self.sec_port = self.ConfigSectionMap(\"General\")['serverport']\n",
" if self.sec_port == \"\":\n",
" raise SecondoAPI_Error('SecondoServer port is not set.')\n",
" self.user = self.ConfigSectionMap(\"General\")['user']\n",
" if self.user == \"\":\n",
" raise SecondoAPI_Error('User name is not set.')\n",
" self.passw = self.ConfigSectionMap(\"General\")['passwd']\n",
" if self.passw == \"\":\n",
" raise SecondoAPI_Error('Password is not set.')\n",
" self.binary_result = True if self.ConfigSectionMap(\"General\")['usebinarylists'] == 'BinaryTransfer' else False\n",
" if self.binary_result == \"\":\n",
" raise SecondoAPI_Error('Result format of queries is not set.')\n",
" self.opt_srv = self.ConfigSectionMap(\"General\")['optimizerhost']\n",
" if self.opt_srv == \"\":\n",
" raise SecondoAPI_Error('OptimizerServer address is not set.')\n",
" self.opt_port = self.ConfigSectionMap(\"General\")['optimizerport']\n",
" if self.opt_port == \"\":\n",
" raise SecondoAPI_Error('OptimizerServer port is not set.')\n",
" \n",
" else:\n",
" \n",
" raise SecondoAPI_Error('The Configuration File not found.')\n",
" \n",
" \n",
" def initialize(self):\n",
" \n",
" \"\"\"\n",
" This mezhod returns the needed parametes for Secondo.\n",
" \n",
" :return: The IP-Address of Secondo sever.\n",
" :return: The port number of Secondo sever.\n",
" :return: The username for authentication in Secondo sever.\n",
" :return: The password for authentication in Secondo sever.\n",
" :return: The IP-Address of Optimizer sever.\n",
" :return: The port number of Optimizer sever.\n",
" :return: The format of the returned result list from Secondo.\n",
" \n",
" \"\"\"\n",
" \n",
" return self.sec_srv, self.sec_port, self.user, self.passw, self.opt_srv, self.opt_port, self.binary_result \n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}