<commit_before>import os
import logging
import pkg_resources
from datetime import datetime

import gtk
import deluge.component as component
import deluge.common
from deluge.ui.client import client
from deluge.ui.gtkui import notification
from deluge.plugins.pluginbase import GtkPluginBase
from twisted.internet.defer import Deferred
from twisted.internet.task import LoopingCall


log = logging.getLogger(__name__)


class UpdatorNotification(notification.Notification):

    _title = 'Deluge Updatorr'
    _message = ''

    def get_torrent_status(self, torrent_id=None):
        if torrent_id is not None:
            component.get('SessionProxy').get_torrent_status(torrent_id,
                ['name', 'num_files', 'total_payload_download']).addCallback(self._on_get_torrent_status)
        else:
            self._on_get_torrent_status({'total_payload_download': 'fake'})

    def _on_get_torrent_status(self, status):
        if status is None:
            return None
        if self.config['ntf_popup']:
            self.popup(status)

    def set_title(self, title):
        self._title = title

    def set_message(self, message):
        self._message = message

    def get_title(self):
        return self._enc(self._title)

    def get_message(self):
        return self._enc(self._message)

    def _enc(self, value):
        return deluge.common.xml_encode(value)

    def popup(self, status):
        if not deluge.common.windows_check():
            try:
                import pynotify
            except Exception:
                pass
            else:
                if not pynotify.init('Deluge'):
                    return None
                message = self.get_message()
                if 'name' in status:
                    message = '%s %s' % (message, self._enc('%s' % status['name']))
                self.note = pynotify.Notification(self.get_title(), message)
                self.note.show()


class GtkUI(GtkPluginBase):

    plugin_id = 'Updatorr'
    trackers_data_model = None
    status_bar_item = None

    def get_resource(self, name):
        """Returns resource from data dir."""
        return pkg_resources.resource_filename('updatorr', os.path.join('data', name))

    def enable(self):
        """Triggers when plugin is enabled."""
        self.glade = gtk.glade.XML(self.get_resource('config.glade'))

        self.tv_trackers = self.glade.get_widget('tv_tracker')
        self.ed_login = self.glade.get_widget('ed_login')
        self.ed_password = self.glade.get_widget('ed_password')

        component.get('Preferences').add_page(self.plugin_id, self.glade.get_widget('prefs_box'))
        pmanager = component.get('PluginManager')
        pmanager.register_hook('on_apply_prefs', self.on_apply_prefs)
        # TODO Cope with tv_tarckers repopulation on plugin reenable.
        pmanager.register_hook('on_show_prefs', self.on_show_prefs)
        self.add_context_menu_items()

        self.status_updator = LoopingCall(self.update_status_bar)
        self.status_updator.start(60)

        client.register_event_handler('UpdatorrUpdateDoneEvent', self.on_update_done_event)
        client.register_event_handler('UpdatorrErrorEvent', self.on_update_error_event)
        client.register_event_handler('UpdatorrUpdatesCheckStartedEvent', self.on_updates_started_event)
        client.register_event_handler('UpdatorrUpdatesCheckFinishedEvent', self.on_updates_finished_event)

    def update_status_bar(self):
        """Updates status bar item."""
        if self.status_bar_item is None:
            self.status_bar_item = component.get('StatusBar').add_item(stock=gtk.STOCK_REFRESH,
                text='', tooltip=_('Updatorr autoupdate status information'))
        client.updatorr.get_status().addCallback(self.get_status)

    DATE_FORMAT = '%b %d %H:%M'

    def get_status(self, status_data):
        """Returns autoupdate status and puts it our status bar item."""
        last_updated = datetime.fromtimestamp(status_data[0]).strftime(self.DATE_FORMAT)
        next_update = datetime.fromtimestamp(status_data[0] + (status_data[1] * 3600)).strftime(self.DATE_FORMAT)
        self.status_bar_item.set_text(_('last: %s, next: %s') % (last_updated, next_update))

    def disable(self):
        """Triggers when plugin is disabled."""
        self.status_updator.stop()
        component.get('StatusBar').remove_item(self.status_bar_item)
        self.status_bar_item = None
        self.remove_context_menu_items()
        self.trackers_data_model = None
        component.get('Preferences').remove_page(self.plugin_id)
        component.get('PluginManager').deregister_hook('on_apply_prefs', self.on_apply_prefs)
        component.get('PluginManager').deregister_hook('on_show_prefs', self.on_show_prefs)

    def init_tv_trackers(self):
        """Initializes trackers TreeView in preferences window."""

        # Initialize only once.
        if self.trackers_data_model is None:
            self.trackers_data_model = gtk.ListStore(str, bool, str, str)

            text_cell = gtk.CellRendererText()
            hidden_cell = gtk.CellRendererText()
            hidden_cell.set_visible(False)
            bool_cell = gtk.CellRendererToggle()

            tvc_domain = gtk.TreeViewColumn('Tracker domain', text_cell, text=0)
            tvc_login_req = gtk.TreeViewColumn('Login required', bool_cell)
            tvc_login_req.add_attribute(bool_cell, 'active', 1)
            tvc_login = gtk.TreeViewColumn('Login', text_cell, text=2)
            tvc_password = gtk.TreeViewColumn('Password', hidden_cell, text=3)
            tvc_password.set_visible(False)

            self.tv_trackers.append_column(tvc_domain)
            self.tv_trackers.append_column(tvc_login_req)
            self.tv_trackers.append_column(tvc_login)
            self.tv_trackers.append_column(tvc_password)

            self.tv_trackers.set_model(self.trackers_data_model)

            self.glade.signal_autoconnect({
                'on_btn_test_clicked': self.on_btn_test_clicked,
                'on_btn_apply_clicked': self.on_btn_apply_clicked,
                'on_tv_tracker_cursor_changed': self.on_tv_tracker_cursor_changed
            })

    def on_tv_tracker_cursor_changed(self, widget):
        """Triggered when trackers TreeView row is selected.
        Puts data from trackers TreeView into credentials Entry controls."""
        list_store, iter = self.tv_trackers.get_selection().get_selected()
        self.tv_current_domain = list_store.get_value(iter, 0)
        self.tv_current_login = list_store.get_value(iter, 2)
        self.tv_current_password = list_store.get_value(iter, 3)

        self.ed_login.set_text(self.tv_current_login)
        self.ed_password.set_text(self.tv_current_password)<commit_msg>Hidden text cell renderer is removed in favour of hidden TreeView column.<commit_after>import os
import logging
import pkg_resources
from datetime import datetime

import gtk
import deluge.component as component
import deluge.common
from deluge.ui.client import client
from deluge.ui.gtkui import notification
from deluge.plugins.pluginbase import GtkPluginBase
from twisted.internet.defer import Deferred
from twisted.internet.task import LoopingCall


log = logging.getLogger(__name__)


class UpdatorNotification(notification.Notification):

    _title = 'Deluge Updatorr'
    _message = ''

    def get_torrent_status(self, torrent_id=None):
        if torrent_id is not None:
            component.get('SessionProxy').get_torrent_status(torrent_id,
                ['name', 'num_files', 'total_payload_download']).addCallback(self._on_get_torrent_status)
        else:
            self._on_get_torrent_status({'total_payload_download': 'fake'})

    def _on_get_torrent_status(self, status):
        if status is None:
            return None
        if self.config['ntf_popup']:
            self.popup(status)

    def set_title(self, title):
        self._title = title

    def set_message(self, message):
        self._message = message

    def get_title(self):
        return self._enc(self._title)

    def get_message(self):
        return self._enc(self._message)

    def _enc(self, value):
        return deluge.common.xml_encode(value)

    def popup(self, status):
        if not deluge.common.windows_check():
            try:
                import pynotify
            except Exception:
                pass
            else:
                if not pynotify.init('Deluge'):
                    return None
                message = self.get_message()
                if 'name' in status:
                    message = '%s %s' % (message, self._enc('%s' % status['name']))
                self.note = pynotify.Notification(self.get_title(), message)
                self.note.show()


class GtkUI(GtkPluginBase):

    plugin_id = 'Updatorr'
    trackers_data_model = None
    status_bar_item = None

    def get_resource(self, name):
        """Returns resource from data dir."""
        return pkg_resources.resource_filename('updatorr', os.path.join('data', name))

    def enable(self):
        """Triggers when plugin is enabled."""
        self.glade = gtk.glade.XML(self.get_resource('config.glade'))

        self.tv_trackers = self.glade.get_widget('tv_tracker')
        self.ed_login = self.glade.get_widget('ed_login')
        self.ed_password = self.glade.get_widget('ed_password')

        component.get('Preferences').add_page(self.plugin_id, self.glade.get_widget('prefs_box'))
        pmanager = component.get('PluginManager')
        pmanager.register_hook('on_apply_prefs', self.on_apply_prefs)
        # TODO Cope with tv_tarckers repopulation on plugin reenable.
        pmanager.register_hook('on_show_prefs', self.on_show_prefs)
        self.add_context_menu_items()

        self.status_updator = LoopingCall(self.update_status_bar)
        self.status_updator.start(60)

        client.register_event_handler('UpdatorrUpdateDoneEvent', self.on_update_done_event)
        client.register_event_handler('UpdatorrErrorEvent', self.on_update_error_event)
        client.register_event_handler('UpdatorrUpdatesCheckStartedEvent', self.on_updates_started_event)
        client.register_event_handler('UpdatorrUpdatesCheckFinishedEvent', self.on_updates_finished_event)

    def update_status_bar(self):
        """Updates status bar item."""
        if self.status_bar_item is None:
            self.status_bar_item = component.get('StatusBar').add_item(stock=gtk.STOCK_REFRESH,
                text='', tooltip=_('Updatorr autoupdate status information'))
        client.updatorr.get_status().addCallback(self.get_status)

    DATE_FORMAT = '%b %d %H:%M'

    def get_status(self, status_data):
        """Returns autoupdate status and puts it our status bar item."""
        last_updated = datetime.fromtimestamp(status_data[0]).strftime(self.DATE_FORMAT)
        next_update = datetime.fromtimestamp(status_data[0] + (status_data[1] * 3600)).strftime(self.DATE_FORMAT)
        self.status_bar_item.set_text(_('last: %s, next: %s') % (last_updated, next_update))

    def disable(self):
        """Triggers when plugin is disabled."""
        self.status_updator.stop()
        component.get('StatusBar').remove_item(self.status_bar_item)
        self.status_bar_item = None
        self.remove_context_menu_items()
        self.trackers_data_model = None
        component.get('Preferences').remove_page(self.plugin_id)
        component.get('PluginManager').deregister_hook('on_apply_prefs', self.on_apply_prefs)
        component.get('PluginManager').deregister_hook('on_show_prefs', self.on_show_prefs)

    def init_tv_trackers(self):
        """Initializes trackers TreeView in preferences window."""

        # Initialize only once.
        if self.trackers_data_model is None:
            self.trackers_data_model = gtk.ListStore(str, bool, str, str)

            text_cell = gtk.CellRendererText()
            bool_cell = gtk.CellRendererToggle()

            tvc_domain = gtk.TreeViewColumn('Tracker domain', text_cell, text=0)
            tvc_login_req = gtk.TreeViewColumn('Login required', bool_cell)
            tvc_login_req.add_attribute(bool_cell, 'active', 1)
            tvc_login = gtk.TreeViewColumn('Login', text_cell, text=2)
            tvc_password = gtk.TreeViewColumn('Password', text_cell, text=3)
            tvc_password.set_visible(False)

            self.tv_trackers.append_column(tvc_domain)
            self.tv_trackers.append_column(tvc_login_req)
            self.tv_trackers.append_column(tvc_login)
            self.tv_trackers.append_column(tvc_password)

            self.tv_trackers.set_model(self.trackers_data_model)

            self.glade.signal_autoconnect({
                'on_btn_test_clicked': self.on_btn_test_clicked,
                'on_btn_apply_clicked': self.on_btn_apply_clicked,
                'on_tv_tracker_cursor_changed': self.on_tv_tracker_cursor_changed
            })

    def on_tv_tracker_cursor_changed(self, widget):
        """Triggered when trackers TreeView row is selected.
        Puts data from trackers TreeView into credentials Entry controls."""
        list_store, iter = self.tv_trackers.get_selection().get_selected()
        self.tv_current_domain = list_store.get_value(iter, 0)
        self.tv_current_login = list_store.get_value(iter, 2)
        self.tv_current_password = list_store.get_value(iter, 3)

        self.ed_login.set_text(self.tv_current_login)
        self.ed_password.set_text(self.tv_current_password)
{'fraction_non_alphanumeric': 0.09850613814524478, 'fraction_numerical': 0.00236651382931519, 'mean_word_length': 3.0597418192734915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441528', 'n_tokens_mistral': 4203, 'n_tokens_neox': 4075, 'n_words': 711}