filehandling woes
This commit is contained in:
parent
3933cdbadf
commit
8c9206effe
14
datafiles.py
14
datafiles.py
@ -5,13 +5,15 @@ import os
|
|||||||
class DataFile(dict):
|
class DataFile(dict):
|
||||||
def __init__(self, filename: str, create: bool):
|
def __init__(self, filename: str, create: bool):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.__filename = filename
|
self.filename = filename
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.stat(self.__filename)
|
os.stat(self.filename)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if not create:
|
if not create:
|
||||||
raise e
|
raise e
|
||||||
|
else:
|
||||||
|
open(self.filename, "w").close()
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
@ -27,12 +29,14 @@ class JSONFile(DataFile):
|
|||||||
super().__init__(filename, create)
|
super().__init__(filename, create)
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
with open(self.__filename, "U") as f:
|
with open(self.filename, "rt") as f:
|
||||||
json_dict = json.loads(f.read())
|
c = f.read()
|
||||||
|
|
||||||
|
if len(c) > 0:
|
||||||
|
json_dict = json.loads(c)
|
||||||
for k in json_dict:
|
for k in json_dict:
|
||||||
self[k] = json_dict[k]
|
self[k] = json_dict[k]
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(self.__filename, "w") as f:
|
with open(self.filename, "wt") as f:
|
||||||
f.write(json.dumps(self.copy()))
|
f.write(json.dumps(self.copy()))
|
||||||
|
|||||||
3
irc.py
3
irc.py
@ -166,13 +166,12 @@ class Client:
|
|||||||
|
|
||||||
def receive(self):
|
def receive(self):
|
||||||
while line := self.__server.recvline():
|
while line := self.__server.recvline():
|
||||||
print(line)
|
|
||||||
if line.startswith("PING"):
|
if line.startswith("PING"):
|
||||||
self.__server.sendline("PONG " + line.split()[1])
|
self.__server.sendline("PONG " + line.split()[1])
|
||||||
continue
|
continue
|
||||||
|
|
||||||
(msg_from, msg_type, msg_to, *msg) = line[1:].split()
|
(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:]
|
msg[0] = msg[0][1:]
|
||||||
message = " ".join(msg)
|
message = " ".join(msg)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user