-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
65 lines (56 loc) · 2.12 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
64
65
# Copyright Beatrice Tohni 2021
""" This file is part of wiggleVerse.
wiggleVerse is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
wiggleVerse is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wiggleVerse. If not, see <https://www.gnu.org/licenses/>."""
import logging
import random
import json
import os.path
class Settings:
def __init__(self):
# Check if settings file exists
if os.path.isfile('settings.json'):
with open('settings.json', 'r') as f:
self.dictionary = json.load(f)
else:
self.dictionary = {
'nick': 'Wiggler' + str(random.randint(111,999)),
'user': 'wiggler',
'real': 'A Very Cool Wiggler!'
}
self.write_settings()
def put(self, setting, value):
try:
self.dictionary[setting] # This will throw an exception if it fails
if setting == 'nick' or setting == 'user':
value = value[0]
elif setting == 'real':
value = ' '.join(value)
else:
return False
self.dictionary[setting] = value
self.write_settings()
return value
except KeyError:
return False
def get(self, setting):
try:
return self.dictionary[setting]
except KeyError:
return False
def get_all(self):
strings = []
for key in self.dictionary:
strings.append(f'{key}: {self.dictionary[key]}')
return strings
def write_settings(self):
with open('settings.json', 'w') as f:
json.dump(self.dictionary, f)