This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.py
72 lines (59 loc) · 2.24 KB
/
profiles.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
import json
import os
class profilesManager():
def __init__(self):
self.profiles = self.loadProfiles()
self.changeProfile("default")
def save(self):
for profile in self.profiles.values():
profile.saveProfile()
def saveProfile(self, profileName):
"""Save profile"""
self.profiles[profileName].saveProfile()
def createProfile(self, profileName):
"""Create profile"""
self.profiles[profileName] = profile(profileName)
def renameProfile(self, oldName, newName):
"""Rename profile"""
profile = self.profiles[oldName].profile
self.deleteProfile(oldName)
self.createProfile(newName)
self.profiles[newName].profile = profile
self.profiles[newName].saveProfile()
def loadProfiles(self):
"""Load profiles, if nonexistent return empty dict"""
profiles = {}
if os.path.exists("profiles"):
for profileName in os.listdir("profiles"):
name = profileName[:-5]
profiles[name] = profile(name)
return profiles
def changeProfile(self, profileName):
"""Change profile"""
self.profile = self.profiles[profileName]
def deleteProfile(self, profileName):
"""Delete profile"""
self.profiles[profileName].deleteProfile()
del self.profiles[profileName]
class profile():
def __init__(self, profileName):
self.profileName = profileName
self.profile = self.loadProfile()
def __str__(self):
return self.profileName
def loadProfile(self):
"""Load profile, if nonexistent return empty dict"""
if os.path.exists(f"profiles/{self.profileName}.json"):
with open(f"profiles/{self.profileName}.json") as file:
return json.load(file)
else:
return profile("default").profile
def saveProfile(self):
"""Save profile"""
with open(f"profiles/{self.profileName}.json", 'w') as file:
json.dump(self.profile, file, indent=4, separators=(',', ': '))
def deleteProfile(self):
"""Delete profile"""
file = f"profiles/{self.profileName}.json"
if os.path.exists(file):
os.remove(file)