<commit_before>#!/usr/bin/env python
"""
Appindicator that subscribe to mqtt broker and notify user for new messages
Please edit config file 'mqtt-appindicator.ini' to provide settings.
Example:
    [mqtt_broker]
    mqtt_server = localhost
    mqtt_port = 1883
    mqtt_topics = [("my/topic", 0), ("another/topic", 2)]
"""
__author__ = '<NAME> <<EMAIL>>'
__license__ = """Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)"""

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import appindicator
import os
import pynotify
import ConfigParser
import paho.mqtt.client as paho  # pip install paho-mqtt
import threading
import sys
gtk.gdk.threads_init()

# Config
APP_NAME = 'mqtt-appindicator'
WORK_DIR = os.path.dirname(os.path.realpath(__file__)) + '/' 
CONFIGFILE = WORK_DIR + APP_NAME + '.ini'
config_section = 'mqtt_broker'
icon_ok = WORK_DIR + 'icons/mqtticon.png'
icon_fail = WORK_DIR + 'icons/mqtticon-fail.png'
# Default settings
config = {'mqtt_server': 'localhost',
          'mqtt_port': 1883,
          'mqtt_topics': [("my/topic", 0), ("another/topic", 2)]}
# End Config


class MQTTIndicator:
    def __init__(self):
        # create an indicator applet
        self.ind = appindicator.Indicator('MQTT Tray', 'mqtt-messages', appindicator.CATEGORY_APPLICATION_STATUS)
        self.ind.set_status(appindicator.STATUS_ACTIVE)
        self.set_icon(False)
        self.item = ''
        self.items_max = 10  # maximum number of items

        # create a menu
        self.menu = gtk.Menu()
        # A separator
        separator = gtk.SeparatorMenuItem()
        separator.show()
        self.menu.append(separator)
        # create items for the menu
        self.status = gtk.MenuItem('')
        self.status.show()
        self.menu.append(self.status)
        # quit menu
        btnquit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        btnquit.connect("activate", self.quit)
        btnquit.show()
        self.menu.append(btnquit)
        self.menu.show()
        self.ind.set_menu(self.menu)
        # MQTT thread
        self.t_mqtt = MQTTBroker()

    def update_status(self, msg):
        self.status.get_child().set_text(msg)

    def set_icon(self, status):
        if status is True:
            self.ind.set_icon(icon_ok)
        else:
            self.ind.set_icon(icon_fail)

    def update(self, msg=''):
        if len(self.menu) >= self.items_max + 2:
            # Remove old items
            # TODO Remove not all items in more beautiful way
            count = 1
            for i in self.menu.get_children():
                if hasattr(i, 'mqtt_msq') and count == self.items_max:
                    self.menu.remove(i)
                count += 1
        # Add new item
        self.item = gtk.MenuItem(msg)
        self.item.mqtt_msg = True
        self.item.connect("activate", self.remove_item)<commit_msg>Fix auto removing old messages<commit_after>#!/usr/bin/env python
"""
Appindicator that subscribe to mqtt broker and notify user for new messages
Please edit config file 'mqtt-appindicator.ini' to provide settings.
Example:
    [mqtt_broker]
    mqtt_server = localhost
    mqtt_port = 1883
    mqtt_topics = [("my/topic", 0), ("another/topic", 2)]
"""
__author__ = '<NAME> <<EMAIL>>'
__license__ = """Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)"""

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import appindicator
import os
import pynotify
import ConfigParser
import paho.mqtt.client as paho  # pip install paho-mqtt
import threading
import sys
gtk.gdk.threads_init()

# Config
APP_NAME = 'mqtt-appindicator'
WORK_DIR = os.path.dirname(os.path.realpath(__file__)) + '/' 
CONFIGFILE = WORK_DIR + APP_NAME + '.ini'
config_section = 'mqtt_broker'
icon_ok = WORK_DIR + 'icons/mqtticon.png'
icon_fail = WORK_DIR + 'icons/mqtticon-fail.png'
# Default settings
config = {'mqtt_server': 'localhost',
          'mqtt_port': 1883,
          'mqtt_topics': [("my/topic", 0), ("another/topic", 2)]}
# End Config


class MQTTIndicator:
    def __init__(self):
        # create an indicator applet
        self.ind = appindicator.Indicator('MQTT Tray', 'mqtt-messages', appindicator.CATEGORY_APPLICATION_STATUS)
        self.ind.set_status(appindicator.STATUS_ACTIVE)
        self.set_icon(False)
        self.item = ''
        self.items_max = 10  # maximum number of items

        # create a menu
        self.menu = gtk.Menu()
        # A separator
        separator = gtk.SeparatorMenuItem()
        separator.show()
        self.menu.append(separator)
        # create items for the menu
        self.status = gtk.MenuItem('')
        self.status.show()
        self.menu.append(self.status)
        # quit menu
        btnquit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        btnquit.connect("activate", self.quit)
        btnquit.show()
        self.menu.append(btnquit)
        self.menu.show()
        self.ind.set_menu(self.menu)
        # MQTT thread
        self.t_mqtt = MQTTBroker()

    def update_status(self, msg):
        self.status.get_child().set_text(msg)

    def set_icon(self, status):
        if status is True:
            self.ind.set_icon(icon_ok)
        else:
            self.ind.set_icon(icon_fail)

    def update(self, msg=''):
        if len(self.menu) >= self.items_max + 2:
            # Remove old items
            # TODO Remove not all items in more beautiful way
            count = 1
            for i in self.menu.get_children():
                if count == self.items_max:
                    self.menu.remove(i)
                count += 1
        # Add new item
        self.item = gtk.MenuItem(msg)
        self.item.mqtt_msg = True
        self.item.connect("activate", self.remove_item)
{'fraction_non_alphanumeric': 0.11693970024398745, 'fraction_numerical': 0.00801673056814221, 'mean_word_length': 2.5338669950738915, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6773972', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1844, 'n_words': 428}