From e297854e45c219569f30cff475cf05b52d326a25 Mon Sep 17 00:00:00 2001 From: Daniel afx Date: Mon, 26 Oct 2020 16:06:25 +0200 Subject: [PATCH] link kivyapp to the logqueue and implement permanent consuming of it --- cncbot.py | 44 ++++++++++++++++++++++++++++---------------- forestmapview.py | 3 +-- main.py | 9 +++++---- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/cncbot.py b/cncbot.py index adeea9d..7594b5d 100644 --- a/cncbot.py +++ b/cncbot.py @@ -15,12 +15,25 @@ def get_random_string(length): result_str = ''.join(random.choice(letters) for i in range(length)) return result_str -class ForestBot(pydle.Client): +class ForestBot(pydle.Client): + verbose = True + async def on_connect(self): await super().on_connect() await self.join(CNC_CHANNEL) - await self.message(CNC_CHANNEL, MOTD) - + #await self.message(CNC_CHANNEL, MOTD) + await self.dumplog(CNC_CHANNEL) + + async def dumplog(self, target): + sleep_time = 1 + while True: + msg = self.queue.get() + if self.verbose: + await asyncio.sleep(sleep_time) + await self.message(target, msg) + else: + print(msg) + async def is_admin(self, nickname): """ Check whether or not a user has administrative rights for this bot. @@ -44,7 +57,8 @@ class ForestBot(pydle.Client): await self.message(target, '] cnc usage:') await self.message(target, '] help - displays help') await self.message(target, '] id - cnc user info') - await self.message(target, '] dump - dump cnc log') + await self.message(target, '] log on - dump cnc log') + await self.message(target, '] log off - stop cnc log dump') # Show user info if message.startswith('id {}'.format(self.nickname)): @@ -54,22 +68,20 @@ class ForestBot(pydle.Client): else: await self.message(target, '] You are NOT an administrator. :('.format(source)) - if message.startswith('dump {}'.format(self.nickname)): + if message.startswith('log on {}'.format(self.nickname)): admin = await self.is_admin(source) if admin: - await self.dumplog(target) + self.verbose = True else: await self.message(target, '] You are NOT an administrator. :('.format(source)) - - async def dumplog(self, target): - sleep_time = 1 - while True: - msg = self.queue.get() - await asyncio.sleep(sleep_time) - await self.message(target, msg) - if len(msg) == 0: - break - + + if message.startswith('log off {}'.format(self.nickname)): + admin = await self.is_admin(source) + if admin: + self.verbose = False + else: + await self.message(target, '] You are NOT an administrator. :('.format(source)) + def run(self, *args, **kwargs): self.queue = kwargs['extqueue'] diff --git a/forestmapview.py b/forestmapview.py index 53d84ea..ddedc6c 100644 --- a/forestmapview.py +++ b/forestmapview.py @@ -12,8 +12,7 @@ class ForestMapView(MapView): image_ext="png", attribution="@ afx") def print_current_zoom(self, *args): - print("] Zoom level: " + str(self.zoom)) - #pqueue.put("] Zoom level: " + str(self.zoom)) + App.get_running_app().logqueue.put("] Zoom level: " + str(self.zoom)) def start_get_fov_trees(self): # After one second get the trees in field of view diff --git a/main.py b/main.py index 87bb5eb..1124114 100644 --- a/main.py +++ b/main.py @@ -69,7 +69,7 @@ class MainApp(MDApp): def on_start(self): # Welcome print(MOTD) - queue.put('] Framework started.') + self.logqueue.put('] Framework started.') self.theme_cls.primary_palette = 'BlueGray' @@ -82,15 +82,16 @@ class MainApp(MDApp): # Instantiate SearchPopupMenu #self.search_menu = SearchPopupMenu() - + if __name__ == '__main__': # Start the Command and Control bot. Mainly for reporting. - queue = Queue() - cnc = Process(target=CnCApp, args=((queue),)) + logqueue = Queue() + cnc = Process(target=CnCApp, args=((logqueue),)) cnc.start() # Start the Kivy Framework main loop kivyapp = MainApp() + kivyapp.logqueue = logqueue kivyapp.run() # Cleanup subprocesses