Skip to content

Commit

Permalink
Merge branch 'network_client' of github.com:TINF21CS1/TicTacToeKojote…
Browse files Browse the repository at this point in the history
… into network_client
  • Loading branch information
HOOK-Hawkins committed Mar 4, 2024
2 parents e8218bb + 84bab05 commit b62e9a6
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
120 changes: 120 additions & 0 deletions Client/profile_save.py
Original file line number Diff line number Diff line change
@@ -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())
1 change: 1 addition & 0 deletions json_schema/profiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit b62e9a6

Please sign in to comment.