From 00171ec3f919f4458a3e8b771ef67c924631198a Mon Sep 17 00:00:00 2001 From: bananabr3d Date: Mon, 4 Mar 2024 07:45:46 +0100 Subject: [PATCH] feat: profile_save.py and profiles.json added, saves profiles and returns saved profiles --- Client/profile_save.py | 120 ++++++++++++++++++++++++++++++++++++++ json_schema/profiles.json | 1 + 2 files changed, 121 insertions(+) create mode 100644 Client/profile_save.py create mode 100644 json_schema/profiles.json diff --git a/Client/profile_save.py b/Client/profile_save.py new file mode 100644 index 0000000..22435bd --- /dev/null +++ b/Client/profile_save.py @@ -0,0 +1,120 @@ +import json +from os.path import exists + +# Path to the profiles.json file +path = ('../json_schema/profiles.json') + +class Profile: + """ + This class is used to handle the profiles.json file. It is used to get, set, and add profiles to the file. + """ + def check_file(self): + """ + This method checks if the file exists + :return: True if the file exists, False if it does not + """ + if exists(path): + print("found") + return True + else: + print("not found") + return False + + def get_profiles(self): + """ + This method returns all the profiles from the file + :return: An array of all profiles + """ + if self.check_file(): + with open(path, 'r') as file: + data = json.load(file) + return data + else: + return None + def get_profile(self,profile_uuid): + """ + This method returns a profile by its uuid + :param profile_uuid: + :return: profile matching given uuid + """ + if self.check_file(): + try: + with open(path, 'r') as file: + data = json.load(file) + for profile in data: + if profile["profile_uuid"] == profile_uuid: + return profile + except: + print("json error: Make sure profiles.json is formatted correctly") + return None + def set_profile(self, profile_uuid , profile_name = None, profile_color = None): + """ + This method sets the profile name and/or color by the uuid + :param profile_uuid: + :param profile_name: *optional* + :param profile_color: *optional* + """ + if self.check_file(): + try: + with open(path, 'r+') as file: + data = json.load(file) + for profile in data: + if profile["profile_uuid"] == profile_uuid: + if profile_name != None: + profile["profile_name"] = profile_name + if profile_color != None: + profile["profile_color"] = profile_color + break + with open(path, 'w') as file: + json.dump(data, file) + except: + print("json error: Make sure profiles.json is formatted correctly") + return None + def get_profile_by_name(self, profile_name): + if self.check_file(): + """ + This method returns a profile by its name + :param profile_name: + :return: profile matching given name + """ + try: + with open(path, 'r') as file: + data = json.load(file) + for profile in data: + if profile["profile_name"] == profile_name: + return profile + except: + print("json error: Make sure profiles.json is formatted correctly") + return None + + def add_new_profile(self, profile_name, profile_uuid, profile_color): + """ + This method adds a new profile to the file + :param profile_name: + :param profile_uuid: + :param profile_color: + """ + if self.check_file(): + entry = {"profile_name": profile_name, "profile_uuid": profile_uuid, "profile_color": profile_color} + try: + with open(path, 'r+') as file: + data = json.load(file) + file.seek(0) + data.append(entry) + json.dump(data, file) + file.truncate() + except: + print("json error: Make sure profiles.json is formatted correctly") + + else: + with open(path, 'w') as file: + entry = [{"profile_name": profile_name, "profile_uuid": profile_uuid, "profile_color": profile_color}] + json.dump(entry, file) + +#Testing +#profile = Profile() +#profile.add_new_profile("test", "test", "test") +#print(profile.get_profiles()) +#print(profile.get_profile("test")) +#profile.set_profile("test", "test2", "test3") +#print(profile.get_profiles()) diff --git a/json_schema/profiles.json b/json_schema/profiles.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/json_schema/profiles.json @@ -0,0 +1 @@ +[] \ No newline at end of file