From ef863983c1499787ea70c8f796ed087a57d0f76a Mon Sep 17 00:00:00 2001 From: Gobinath Date: Tue, 18 Apr 2017 19:34:22 -0400 Subject: [PATCH] Limit the plugins text length --- debian/changelog | 2 +- safeeyes/Plugins.py | 9 +++++-- safeeyes/SafeEyesCore.py | 18 ++++++------- safeeyes/Utility.py | 55 ++++++++++++++++++++++++++++++---------- safeeyes/__main__.py | 2 +- setup.py | 2 +- 6 files changed, 60 insertions(+), 28 deletions(-) diff --git a/debian/changelog b/debian/changelog index f416bc2..7476484 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -safeeyes (1.2.0a7-3) xenial; urgency=low +safeeyes (1.2.0-1) xenial; urgency=low * Move to Python 3 diff --git a/safeeyes/Plugins.py b/safeeyes/Plugins.py index fc6cf81..559125c 100644 --- a/safeeyes/Plugins.py +++ b/safeeyes/Plugins.py @@ -42,7 +42,7 @@ class Plugins: if os.path.isfile(os.path.join(plugins_directory, plugin['name'] + '.py')): module = importlib.import_module(plugin['name']) if self.__has_method(module, 'start') and self.__has_method(module, 'pre_notification') and self.__has_method(module, 'pre_break') and self.__has_method(module, 'post_break') and self.__has_method(module, 'exit'): - self.__plugins.append({'module': module, 'location': plugin['location'].lower()}) + self.__plugins.append({'name': plugin['name'], 'module': module, 'location': plugin['location'].lower()}) else: logging.warning('Ignoring the plugin ' + str(plugin['name']) + ' due to invalid method signature') else: @@ -88,10 +88,15 @@ class Plugins: try: result = multiple_results[i].get(timeout=1) if result: + # Limit the line length to 50 characters + large_lines = list(filter(lambda x: len(x) > 50, Utility.html_to_text(result).splitlines())) + if large_lines: + logging.warning('Ignoring lengthy result from the plugin ' + self.__plugins[i]['name']) + continue output[self.__plugins[i]['location']] += (result + '\n\n') except Exception: # Something went wrong in the plugin - pass + raise return output diff --git a/safeeyes/SafeEyesCore.py b/safeeyes/SafeEyesCore.py index 671989d..7bbf768 100644 --- a/safeeyes/SafeEyesCore.py +++ b/safeeyes/SafeEyesCore.py @@ -34,8 +34,6 @@ class SafeEyesCore: self.break_count = -1 self.long_break_message_index = -1 self.short_break_message_index = -1 - self.skipped = False - self.postponed = False self.active = False self.running = False self.show_notification = show_notification @@ -47,6 +45,8 @@ class SafeEyesCore: self.idle_condition = threading.Condition() self.lock = threading.Lock() self.context = context + self.context['skipped'] = False + self.context['postponed'] = False """ @@ -174,13 +174,13 @@ class SafeEyesCore: User skipped the break using Skip button """ def skip_break(self): - self.skipped = True + self.context['skipped'] = True """ User postponed the break using Postpone button """ def postpone_break(self): - self.postponed = True + self.context['postponed'] = True """ @@ -192,7 +192,7 @@ class SafeEyesCore: time_to_wait = self.break_interval # In minutes - if self.postponed: + if self.context['postponed']: # Reduce the break count by 1 to show the same break again if self.break_count == 0: self.break_count = -1 @@ -205,7 +205,7 @@ class SafeEyesCore: # Wait until the postpone time time_to_wait = self.postpone_duration - self.postponed = False + self.context['postponed'] = False next_break_time = datetime.datetime.now() + datetime.timedelta(minutes=time_to_wait) self.update_next_break_info(next_break_time) @@ -303,7 +303,7 @@ class SafeEyesCore: self.start_break(message, image) # Use self.active instead of self.__is_running to avoid idle pause interrupting the break - while seconds and self.active and not self.skipped and not self.postponed: + while seconds and self.active and not self.context['skipped'] and not self.context['postponed']: self.context['count_down'] = total_break_time - seconds mins, secs = divmod(seconds, 60) timeformat = '{:02d}:{:02d}'.format(mins, secs) @@ -312,12 +312,12 @@ class SafeEyesCore: seconds -= 1 # Loop terminated because of timeout (not skipped) -> Close the break alert - if not self.skipped and not self.postponed: + if not self.context['skipped'] and not self.context['postponed']: logging.info("Break is terminated automatically") self.end_break(audible_alert) # Reset the skipped flag - self.skipped = False + self.context['skipped'] = False # Resume if self.__is_running(): diff --git a/safeeyes/Utility.py b/safeeyes/Utility.py index 3b7ab02..6ee739e 100644 --- a/safeeyes/Utility.py +++ b/safeeyes/Utility.py @@ -19,6 +19,7 @@ import gi gi.require_version('Gdk', '3.0') from gi.repository import Gdk, GLib +from html.parser import HTMLParser import babel.dates, os, errno, re, subprocess, threading, logging, locale, json import pyaudio, wave @@ -45,9 +46,9 @@ def play_notification(): # Create a sound stream wrapper = pyaudio.PyAudio() stream = wrapper.open(format=wrapper.get_format_from_width(sound.getsampwidth()), - channels=sound.getnchannels(), - rate=sound.getframerate(), - output=True) + channels=sound.getnchannels(), + rate=sound.getframerate(), + output=True) # Write file data into the sound stream data = sound.readframes(CHUNK) @@ -136,17 +137,17 @@ def is_active_window_skipped(skip_break_window_classes, take_break_window_classe # Extract the process name process_names = re.findall('"(.+?)"', stdout) if process_names: - process = process_names[1].lower() - if process in skip_break_window_classes: - return True - elif process in take_break_window_classes: - if is_fullscreen and unfullscreen_allowed: - try: - active_window.unfullscreen() - except: - logging.error('Error in unfullscreen the window ' + process) - pass - return False + process = process_names[1].lower() + if process in skip_break_window_classes: + return True + elif process in take_break_window_classes: + if is_fullscreen and unfullscreen_allowed: + try: + active_window.unfullscreen() + except: + logging.error('Error in unfullscreen the window ' + process) + pass + return False return is_fullscreen @@ -289,3 +290,29 @@ def lock_desktop(user_defined_command): subprocess.Popen(command) except Exception as e: logging.error('Error in executing the commad' + str(command) + ' to lock screen', e) + + +def html_to_text(html): + """ + Convert HTML to plain text + """ + extractor = __HTMLTextExtractor() + extractor.feed(html) + return extractor.get_data() + + +class __HTMLTextExtractor(HTMLParser): + """ + Helper class to convert HTML to text + """ + def __init__(self): + self.reset() + self.strict = False + self.convert_charrefs= True + self.fed = [] + + def handle_data(self, d): + self.fed.append(d) + + def get_data(self): + return ''.join(self.fed) \ No newline at end of file diff --git a/safeeyes/__main__.py b/safeeyes/__main__.py index 9cb4195..acbd317 100755 --- a/safeeyes/__main__.py +++ b/safeeyes/__main__.py @@ -44,7 +44,7 @@ system_style_sheet_path = os.path.join(Utility.bin_directory, "config/style/safe is_active = True CONFIGURATION_VERSION = 4 -SAFE_EYES_VERSION = "1.2.0a7" +SAFE_EYES_VERSION = "1.2.0" """ Listen to tray icon Settings action and send the signal to Settings dialog. diff --git a/setup.py b/setup.py index 16b48bb..4c3b3aa 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ def _data_files(path): setuptools.setup( name="safeeyes", - version="1.2.0a7", + version="1.2.0", description="Protect your eyes from eye strain using this continuous breaks reminder.", long_description=long_description, author="Gobinath Loganathan",