Coverage for pyilper/pilconfig.py: 65%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3# pilconfig for pyILPER
4#
5# (c) 2015 Joachim Siebold
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20#
21# pilconfig class -------------------------------------------
22#
23# Changelog
24# 06.10.2015 jsi:
25# - class statement syntax update
26# 17.09.2016 jsi:
27# - open method introduced
28# 14.10.2016 jsi:
29# - added filename parameter to cls_userconfig
30# 17.08.2017 jsi:
31# - assign "" to self.add_msg if parameter is none in PilConfigError
32# 20.01.2018 jsi
33# - added get_dual method
34# 12.02.2018 jsi
35# - added the clean parameter to the open method
36# 12.12.2021 jsi
37# - add configversion parameter to open method
38#
39from .userconfig import cls_userconfig, ConfigError
42class PilConfigError(Exception):
43 def __init__(self,msg,add_msg= None):
44 self.msg= msg
45 if add_msg is None:
46 self.add_msg=""
47 else:
48 self.add_msg = add_msg
51class cls_pilconfig:
52#
53# initialize: create instance
54#
55 def __init__(self):
56 self.__config__= { }
57 self.__userconfig__ = None
58 return
60#
61# open: read in the configuration file into the dictionary
62# if the configuration file does not exist, an empty file is created
63# If clean is true do not read the config file
64#
65 def open(self,name,configversion,instance,production,clean):
66 self.__userconfig__= cls_userconfig(name,name,configversion,instance,production)
67 if clean:
68 return
69 try:
70 self.__config__= self.__userconfig__.read(self.__config__)
71 except ConfigError as e:
72 raise PilConfigError(e.msg,e.add_msg)
73#
74# Get a key from the configuration dictionary. To initialize a key a default
75# value can be specified
76#
77 def get(self,name,param,default=None):
78 pname= name+"_"+param
79 try:
80 p= self.__config__[pname]
81 except KeyError:
82 if default is None:
83 raise PilConfigError("configuration parameter not found: "+pname)
84 else:
85 self.__config__[pname]= default
86 p=default
87 return(p)
88#
89# Get a key, first a local key, if the value is -1 then get the global key
90#
91 def get_dual(self,name,param):
92 p = self.get(name,param)
93 if p == -1:
94 p = self.get("pyilper",param)
95 return(p)
96#
97# Put a key into the configuration dictrionary
98#
99 def put(self,name,param,value):
100 pname= name+"_"+param
101 self.__config__[pname]= value
103#
104# Save the dictionary to the configuration file
105#
106 def save(self):
107 try:
108 self.__userconfig__.write(self.__config__)
109 except ConfigError as e:
110 raise PilConfigError(e.msg,e.add_msg)
111#
112# Get the keys of the configuration file
113#
114 def getkeys(self):
115 return(self.__config__.keys())
116#
117# remove an entry
118#
119 def remove(self,key):
120 try:
121 del(self.__config__[key])
122 except KeyError:
123 pass
124#
125# create config instance
126#
127PILCONFIG= cls_pilconfig()