-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
63 lines (49 loc) · 1.79 KB
/
settings.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
# Settings file
# Author: Finn G.
from constants import *
from utils import *
setLogLevel(logLevel)
class Settings():
settings = {}
def __init__(self):
try:
# Open the file...
file = open(".settings.txt", "r")
# Read each line and put the settings in the dictionary...
for line in file:
fragments = line.strip().split(":")
value = fragments[1]
try:
# Try to convert the value in an integer...
value = int(value)
except:
# If converting in an integer failed, convert the value in a boolean...
if value == "True":
value = True
elif value == "False":
value = False
# Add the key and value to the dictionary...
self.settings[fragments[0]] = value
file.close()
except:
error("Cannot find the settings file")
def get(self, key, default=None):
"""Get a value in the settings"""
# If the key is in the settings, return the value...
if key in self.settings:
return self.settings[key]
# If the key is not in the settings, return the default value...
else:
if default != None:
self.settings[key] = default
return default
def set(self, key, value):
"""Set a value in the dictionary"""
self.settings[key] = value
def save(self):
""""Write the settings"""
# Open the file...
file = open(".settings.txt", "w")
# Write all settings in the file...
for key in self.settings:
file.write("%s:%s\n" % (key, self.settings[key]))