-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConfigManager.py
129 lines (113 loc) · 5.16 KB
/
ConfigManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
# ConfigManager
# Copyright (C) 2019-2020 yamahubuki <[email protected]>
import os
import configparser
import logging
from logging import getLogger
class ConfigManager(configparser.ConfigParser):
def __init__(self):
super().__init__(interpolation=None)
self.identifier = "ConfigManager"
self.log = getLogger(self.identifier)
self.log.debug("Create config instance")
def read(self, fileName):
self.fileName = fileName
if os.path.exists(fileName):
self.log.info("read configFile:" + fileName)
try:
return super().read(fileName, encoding='UTF-8')
except configparser.ParsingError:
self.log.warning("configFile parse failed.")
return []
else:
self.log.warning("configFile not found.")
return []
def write(self):
self.log.info("write configFile:" + self.fileName)
with open(self.fileName, "w", encoding='UTF-8') as f:
return super().write(f)
def __getitem__(self, key):
try:
return ConfigSection(super().__getitem__(key))
except KeyError as e:
self.log.debug("created new section:" + key)
self.add_section(key)
return self.__getitem__(key)
def getboolean(self, section, key, default=True):
if type(default) != bool:
raise ValueError("default value must be boolean")
try:
return super().getboolean(section, key)
except ValueError:
self.log.debug("value is not boolean. return default " + str(default) + " at section " + section + ", key " + key)
self[section][key] = str(default)
return int(default)
except configparser.NoOptionError as e:
self.log.debug("add new boolval " + str(default) + " at section " + section + ", key " + key)
self[section][key] = default
return default
except configparser.NoSectionError as e:
self.log.debug("add new section and boolval " + str(default) + " at section " + section + ", key " + key)
self.add_section(section)
self.__getitem__(section).__setitem__(key, default)
return default
def getint(self, section, key, default=0, min=None, max=None):
if type(default) != int:
raise ValueError("default value must be int")
if (min is not None and type(min) != int) or (max is not None and type(max) != int):
raise ValueError("min/max value must be int")
try:
ret = super().getint(section, key)
if (min is not None and ret < min) or (max is not None and ret > max):
self.log.debug("intvalue " + str(ret) + " out of range. at section " + section + ", key " + key)
self[section][key] = str(default)
return int(default)
return ret
except (configparser.NoOptionError, ValueError) as e:
self.log.debug("add new intval " + str(default) + " at section " + section + ", key " + key)
self[section][key] = str(default)
return int(default)
except configparser.NoSectionError as e:
self.log.debug("add new section and intval " + str(default) + " at section " + section + ", key " + key)
self.add_section(section)
self.__getitem__(section).__setitem__(key, str(default))
return int(default)
def getstring(self, section, key, default="", selection=None, *, raw=False, vars=None, fallback=None):
if selection is not None and not hasattr(selection, "__iter__"):
raise TypeError("selection must be iterable. %s were given." % type(selection))
ret = self.__getitem__(section)[key]
if ret == "":
if default == "":
self[section][key] = ""
return ""
else:
self.log.debug("add default value. at section " + section + ", key " + key)
self[section][key] = default
ret = default
if selection is None:
return ret
if selection is not None and ret not in selection:
self.log.debug("value " + ret + " not in selection. at section " + section + ", key " + key)
self[section][key] = default
ret = default
return ret
def add_section(self, name):
if not self.has_section(name):
return super().add_section(name)
def items(self, section):
try:
return super().items(section)
except configparser.NoSectionError:
return []
class ConfigSection(configparser.SectionProxy):
def __init__(self, proxy):
super().__init__(proxy._parser, proxy._name)
def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError:
self._parser[self._name][key] = ""
return ""
def __setitem__(self, key, value):
return super().__setitem__(key, str(value))