-
Notifications
You must be signed in to change notification settings - Fork 0
/
Profile.py
192 lines (160 loc) · 6.62 KB
/
Profile.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Profile.py
# Edited Version
#
# ICS 32 Winter 2022
# Final Project
#
# Author: Mark S. Baldwin/ Nathan Yang/ Cole Thompson
#
# v0.1.8
import json, time, os
from pathlib import Path
class DsuFileError(Exception):
"""
DsuFileError is a custom exception handler that you should catch in your own code. It
is raised when attempting to load or save Profile objects to file the system.
"""
pass
class DsuProfileError(Exception):
"""
DsuProfileError is a custom exception handler that you should catch in your own code. It
is raised when attempting to deserialize a dsu file to a Profile object.
"""
pass
class Post(dict):
"""
The Post class is responsible for working with individual user posts. It currently supports two features:
A timestamp property that is set upon instantiation and when the entry object is set and an
entry property that stores the post message.
"""
def __init__(self, entry:str = None, timestamp:float = 0):
self._timestamp = timestamp
self.set_entry(entry)
# Subclass dict to expose Post properties for serialization
# Don't worry about this!
dict.__init__(self, entry=self._entry, timestamp=self._timestamp)
def set_entry(self, entry):
self._entry = entry
dict.__setitem__(self, 'entry', entry)
# If timestamp has not been set, generate a new from time module
if self._timestamp == 0:
self._timestamp = time.time()
def get_entry(self):
return self._entry
def set_time(self, time:float):
self._timestamp = time
dict.__setitem__(self, 'timestamp', time)
def get_time(self):
return self._timestamp
"""
The property method is used to support get and set capability for entry and time values.
When the value for entry is changed, or set, the timestamp field is updated to the
current time.
"""
entry = property(get_entry, set_entry)
timestamp = property(get_time, set_time)
class Profile:
"""
The Profile class exposes the properties required to join an ICS 32 DSU server. You will need to
use this class to manage the information provided by each new user created within your program for a2.
Pay close attention to the properties and functions in this class as you will need to make use of
each of them in your program.
When creating your program you will need to collect user input for the properties exposed by this class.
A Profile class should ensure that a username and password are set, but contains no conventions to do so.
You should make sure that your code verifies that required properties are set.
"""
def __init__(self, dsuserver=None, username=None, password=None):
self.dsuserver = dsuserver # REQUIRED
self.username = username # REQUIRED
self.password = password # REQUIRED
self.bio = '' # OPTIONAL
self._posts = [] # OPTIONAL
self.messages = []
self.recipients = []
def adding_user(self, user:str) -> bool:
"""
Takes in a user parameter and adds it to the recipients list attribute as well as checks to see if the given
username has already been inputted.
"""
if len(self.recipients) > 1:
for users in self.recipients:
if user == users:
return 0
else:
self.recipients.append(user)
return 1
else:
self.recipients.append(user)
return
def adding_messages(self, obj:str) -> None:
"""
Takes in an obj parameter and appends it to the messages list attribute as well as sorts it to make sure there
are no duplicates.
"""
self.messages.append(obj)
set_list = set(self.messages)
self.messages = list(set_list)
def add_post(self, post: Post) -> None:
"""
Accepts a Post object as parameter and appends it to the posts list. Posts are stored in a
list object in the order they are added. So if multiple Posts objects are created, but added to the
Profile in a different order, it is possible for the list to not be sorted by the Post.timestamp property.
So take caution as to how you implement your add_post code.
"""
self._posts.append(post)
def del_post(self, index: int) -> bool:
"""
Removes a Post at a given index and returns True if successful and False if an invalid
index was supplied.
To determine which post to delete you must implement your own search operation on the posts
returned from the get_posts function to find the correct index.
"""
try:
del self._posts[index]
return True
except IndexError:
return False
def get_posts(self) -> list[Post]:
"""
Returns the list object containing all posts that have been added to the Profile object
"""
return self._posts
def save_profile(self, path: str) -> None:
"""
Accepts an existing dsu file to save the current instance of Profile to the file system.
"""
p = Path(path)
if os.path.exists(p) and p.suffix == '.dsu':
try:
f = open(p, 'w')
json.dump(self.__dict__, f)
f.close()
except Exception as ex:
raise DsuFileError("An error occurred while attempting to process the DSU file.", ex)
else:
raise DsuFileError("Invalid DSU file path or type")
def load_profile(self, path: str) -> None:
"""
Populates the current instance of Profile with data stored in a DSU file.
"""
p = Path(path)
if os.path.exists(p) and p.suffix == '.dsu':
try:
f = open(p, 'r')
obj = json.load(f)
self.username = obj['username']
self.password = obj['password']
self.dsuserver = obj['dsuserver']
self.bio = obj['bio']
self.messages = obj['messages']
# self.messages_sent = obj['messages_sent']
# self.messages_received = obj['messages_received']
self.recipients = obj['recipients']
for post_obj in obj['_posts']:
post = Post(post_obj['entry'], post_obj['timestamp'])
self._posts.append(post)
f.close()
except Exception as ex:
raise DsuProfileError(ex)
else:
raise DsuFileError()