{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import configparser as cfg\n", "\n", "class Config():\n", " \n", " def __init__(self,file=None,sec_srv=None,user=None,passw=None,sec_port=None,opt_srv=None,opt_port=None,opt_enable=None):\n", " if file and not (sec_srv and user and passw and sec_port and opt_srv and opt_port and opt_enable):\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.opt_enable = \"\"\n", " self.startconnection = False\n", " self.cfg = cfg.ConfigParser()\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 opt_enable):\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 opt_enable):\n", " self.sec_srv = sec_srv\n", " self.user = user\n", " self.passw = passw\n", " self.sec_port = sec_port\n", " self.opt_srv = opt_srv\n", " self.opt_port = opt_port\n", " self.opt_enable = opt_enable\n", " self.startconnection = True\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", " #dict1= ConfigSectionMap\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", " self.sec_port = self.ConfigSectionMap(\"General\")['serverport']\n", " self.startconnection = self.ConfigSectionMap(\"General\")['start_connection']\n", " self.user = self.ConfigSectionMap(\"General\")['user']\n", " self.passw = self.ConfigSectionMap(\"General\")['passwd']\n", " self.opt_srv = self.ConfigSectionMap(\"General\")['optimizer_host']\n", " self.opt_port = self.ConfigSectionMap(\"General\")['optimizer_port']\n", " self.opt_enable = self.ConfigSectionMap(\"General\")['enable_optimizer']\n", " \n", " else:\n", " print(\"The ConfigFile not found!\") \n", "\n", " if self.opt_srv == \"\":\n", " self.opt_srv = \"localhost\"\n", " if int(self.opt_port) <= 0:\n", " self.opt_port = 1235\n", " \n", " def initialize(self):\n", " print(self.sec_srv, self.sec_port, self.startconnection, self.user, self.passw, self.opt_srv, self.opt_port, self.opt_enable)\n", " return self.sec_srv, self.sec_port, self.startconnection, self.user, self.passw, self.opt_srv, self.opt_port, self.opt_enable \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 }