moved Communication to a more appropriate place

master
BodgeMaster 2022-03-19 08:37:43 +01:00
parent a33dbea7c8
commit 02e095c321
2 changed files with 45 additions and 45 deletions

View File

@ -1,51 +1,6 @@
import tkinter as tk import tkinter as tk
import util import util
# easy way to communicate across events
class Communication:
def __init__(self):
self.__messages = {}
# send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default
def send(self, name, content, reverse_order=True):
if name in self.__messages:
if reverse_order:
self.__messages[name] = [content] + self.__messages[name]
else:
self.__messages[name] = self.__messages[name] + [content]
else:
self.__messages[name] = [content]
# get the content of the first message tagged with name, removes the returned message by default
def get(self, name, remove=True):
if name in self.__messages and len(self.__messages[name])>0:
if remove:
content = self.__messages[name].pop(0)
if len(self.__messages[name])==0:
del self.__messages[name]
return content
else:
return self.__messages[name][0]
else:
return None
# get the contents for all messages tagged with name
def get_all(self, name, clear=False):
if name in self.__messages and len(self.__messages[name])>0:
contents = self.__messages[name]
if clear:
del self.__messages[name]
return contents
# deletes all messages tagged with name
def clear(self, name):
if name in self.__messages:
del self.__messages[name]
def __del__(self):
if len(self.__messages)>0:
util.warn("__messages not empty upon destruction of Communitation object:\n"+str(self.__messages))
def not_implemented(): def not_implemented():
util.warn("Not implemented!") util.warn("Not implemented!")

45
util.py
View File

@ -3,6 +3,51 @@ import sys, traceback
EXIT_SUCCESS=0 EXIT_SUCCESS=0
EXIT_ERROR=1 EXIT_ERROR=1
# easy way to communicate across events
class Communication:
def __init__(self):
self.__messages = {}
# send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default
def send(self, name, content, reverse_order=True):
if name in self.__messages:
if reverse_order:
self.__messages[name] = [content] + self.__messages[name]
else:
self.__messages[name] = self.__messages[name] + [content]
else:
self.__messages[name] = [content]
# get the content of the first message tagged with name, removes the returned message by default
def get(self, name, remove=True):
if name in self.__messages and len(self.__messages[name])>0:
if remove:
content = self.__messages[name].pop(0)
if len(self.__messages[name])==0:
del self.__messages[name]
return content
else:
return self.__messages[name][0]
else:
return None
# get the contents for all messages tagged with name
def get_all(self, name, clear=False):
if name in self.__messages and len(self.__messages[name])>0:
contents = self.__messages[name]
if clear:
del self.__messages[name]
return contents
# deletes all messages tagged with name
def clear(self, name):
if name in self.__messages:
del self.__messages[name]
def __del__(self):
if len(self.__messages)>0:
util.warn("__messages not empty upon destruction of Communitation object:\n"+str(self.__messages))
def info(message): def info(message):
# print info to sys.stderr because it isnt really output, just debug information # print info to sys.stderr because it isnt really output, just debug information
print("INFO: "+str(message), file=sys.stderr) print("INFO: "+str(message), file=sys.stderr)