filehandling woes

This commit is contained in:
Stefan Harmuth 2021-12-05 16:06:25 +01:00
parent 3933cdbadf
commit 8c9206effe
2 changed files with 12 additions and 9 deletions

View File

@ -5,13 +5,15 @@ import os
class DataFile(dict):
def __init__(self, filename: str, create: bool):
super().__init__()
self.__filename = filename
self.filename = filename
try:
os.stat(self.__filename)
os.stat(self.filename)
except OSError as e:
if not create:
raise e
else:
open(self.filename, "w").close()
self.load()
@ -27,12 +29,14 @@ class JSONFile(DataFile):
super().__init__(filename, create)
def load(self):
with open(self.__filename, "U") as f:
json_dict = json.loads(f.read())
with open(self.filename, "rt") as f:
c = f.read()
for k in json_dict:
self[k] = json_dict[k]
if len(c) > 0:
json_dict = json.loads(c)
for k in json_dict:
self[k] = json_dict[k]
def save(self):
with open(self.__filename, "w") as f:
with open(self.filename, "wt") as f:
f.write(json.dumps(self.copy()))

3
irc.py
View File

@ -166,13 +166,12 @@ class Client:
def receive(self):
while line := self.__server.recvline():
print(line)
if line.startswith("PING"):
self.__server.sendline("PONG " + line.split()[1])
continue
(msg_from, msg_type, msg_to, *msg) = line[1:].split()
if msg[0].startswith(":"):
if len(msg) > 0 and msg[0].startswith(":"):
msg[0] = msg[0][1:]
message = " ".join(msg)