From cd169028ed7b6773a8002254aca2d70741f15218 Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Mon, 30 May 2016 19:50:21 -0700 Subject: [PATCH] Add regex filter support --- README.md | 14 ++++++++++-- screeps_console/command.py | 39 ++++++++++++++++++++++++++++++++-- screeps_console/interactive.py | 35 +++++++++++++++++++++++++++--- 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 77b4372..af33719 100644 --- a/README.md +++ b/README.md @@ -92,10 +92,20 @@ Scrolling can be done one line at a time using `alt up` and `alt down`. Using through the console buffer a bit quicker. +## Filters + +Console output can be filtered using regular expressions and the `filter` +command. Only commands that match at least one filter will be displayed. + +`filter list` - this lists each current regex filter and it's index. +`filter add` - add a regular expression to the filter list. +`filter remove` - remove a regular expression using it's index. + + ## Colors and Severity -Console output can have colors, in both the website version and the shell. To get -the best of both worlds use font tags that also have a severity attribute. +Console output can have colors, in both the website version and the shell. To +get the best of both worlds use font tags that also have a severity attribute. ``` Message goes here! diff --git a/screeps_console/command.py b/screeps_console/command.py index 2a11a35..1dc1867 100644 --- a/screeps_console/command.py +++ b/screeps_console/command.py @@ -21,15 +21,17 @@ def __init__(self): self.lastkeytime = 0 self.listbox = False self.listwalker = False + self.consolemonitor = False self.edit = False self.getApiClient() self.autocomplete = Autocomplete(self) - def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit): - self.listbox = listbox + def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit, consolemonitor): + self.listbox = listbox # console self.listwalker = listwalker self.edit = edit self.loop = loop + self.consolemonitor = consolemonitor def getApiClient(self): if not self.apiclient: @@ -159,6 +161,39 @@ def clear(self, comp): def exit(self, comp): raise urwid.ExitMainLoop() + def filter(self, comp): + + user_text = comp.edit.get_edit_text() + user_command_split = user_text.split(' ') + + if len(user_command_split) <= 1: + subcommand = 'list' + else: + subcommand = user_command_split[1] + + if subcommand == 'list': + filters = comp.consolemonitor.filters[:] + + if len(filters) <= 0: + comp.listwalker.append(urwid.Text(('logged_response', 'No filters'))) + comp.listbox.autoscroll() + return + else: + for index, pattern in enumerate(filters): + comp.listwalker.append(urwid.Text(('logged_response', str(index) + '. ' + pattern))) + comp.listbox.autoscroll() + return + + elif subcommand == 'add': + regex = user_command_split[2:] + comp.consolemonitor.filters.append(' '.join(regex)) + + elif subcommand == 'remove': + toRemove = user_command_split[2] + comp.consolemonitor.filters.pop(int(toRemove)) + pass + + def list(self, comp): command_list = '' aliases = list(comp.aliases) diff --git a/screeps_console/interactive.py b/screeps_console/interactive.py index e9cfee6..c1c1f86 100755 --- a/screeps_console/interactive.py +++ b/screeps_console/interactive.py @@ -3,9 +3,11 @@ import atexit import command import json +import logging import os from os.path import expanduser import outputparser +import re from settings import getSettings import signal import subprocess @@ -19,14 +21,17 @@ class ScreepsInteractiveConsole: consoleWidget = False listWalker = False userInput = False + consoleMonitor = False def __init__(self): frame = self.getFrame() comp = self.getCommandProcessor() self.loop = urwid.MainLoop(urwid.AttrMap(frame, 'bg'), unhandled_input=comp.onInput, palette=themes['dark']) - comp.setDisplayWidgets(self.loop, frame, self.getConsole(), self.getConsoleListWalker(), self.getEdit()) - console_monitor = ScreepsConsoleMonitor(self.consoleWidget, self.listWalker, self.loop) + self.consoleMonitor = ScreepsConsoleMonitor(self.consoleWidget, self.listWalker, self.loop) + + comp.setDisplayWidgets(self.loop, frame, self.getConsole(), self.getConsoleListWalker(), self.getEdit(), self.consoleMonitor) + self.loop.run() @@ -214,6 +219,7 @@ def keypress(self, size, key): class ScreepsConsoleMonitor: proc = False + filters = [] def __init__(self, widget, walker, loop): self.widget = widget @@ -249,6 +255,10 @@ def onUpdate(self, data): data_lines = data.rstrip().split('\n') for line_json in data_lines: try: + + if len(line_json) <= 0: + continue + line = json.loads(line_json.strip()) log_type = outputparser.getType(line) @@ -266,10 +276,29 @@ def onUpdate(self, data): line = line.replace(' ', " ") line = outputparser.clearTags(line) + + if log_type == 'log' and len(self.filters) > 0: + has_match = False + + for pattern in self.filters: + try: + match = re.search(pattern, line) + except: + e = sys.exc_info()[0] + logging.exception('dammit') + + if match is not None: + has_match = True + break + + if not has_match: + continue + self.walker.append(urwid.Text((formatting, line))) self.widget.autoscroll() + except: - '' + logging.exception('error processing data') def __del__(self): if self.proc: