<commit_before>from __future__ import division

"""The main window."""

import os
import logging
from bisect import bisect

from PyQt4 import QtCore, Qt, QtGui
from PyQt4.QtGui import (QAction, QMainWindow, QMessageBox, QSystemTrayIcon,
    QIcon, QMenu, QInputDialog, QPushButton, QLineEdit, QDialog)

from wefree.passwords_manager import PM
from wefree.interfaces import WifiInterfaces

#import NetworkManager

logger = logging.getLogger('wefree.main')

CURRENT_PATH = os.path.abspath(os.path.dirname(__file__))

ABOUT_TEXT = u"""
<center>
WeFree<br/>
Let's free the WiFi.<br/>
</center>
"""

# signals and levels to use them
SIGNALS_IMGS = [0, 25, 50, 75, 100]
SIGNAL_BREAKPOINTS = [.20, .45, .70, .90]

def debug_trace():
    '''Set a tracepoint in the Python debugger that works with Qt'''
    from PyQt4.QtCore import pyqtRemoveInputHook
    from ipdb import set_trace
    pyqtRemoveInputHook()
    set_trace()


class AddPasswordDialog(QDialog):
    def __init__(self, parent, wifi, signal):
        super(AddPasswordDialog, self).__init__(parent)

        self.signal = signal
        self.wifi   = wifi

        self.connect_btn = QPushButton("connect")
        self.connect_and_share_btn = QPushButton("connect and share (Free the world)")
        self.connect_and_share_btn.setDefault(True)
        self.cancel_btn = QPushButton("cancel")
        self.input_password = QLineEdit()
        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.input_password)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.connect_btn)
        hbox.addWidget(self.connect_and_share_btn)
        hbox.addWidget(self.cancel_btn)
        vbox.addLayout(hbox)

        self.connect_btn.clicked.connect(self.on_connect)
        self.connect_and_share_btn.clicked.connect(self.on_connect_and_share)
        self.cancel_btn.clicked.connect(self.close)

    def on_connect(self, share=False):
        password = self.input_password.text()
        if password:
            self.signal.add_password(password, report = True)
            if share:
                PM.add_new_password(password, essid=self.signal.ssid, bssid=self.signal.bssid)
        self.close()
        self.signal.report_to_db = share
        self.wifi.connect(self.signal)

    def on_connect_and_share(self):
        self.on_connect(share=True)

class MainUI(QMainWindow):
    """Main UI."""

    def __init__(self, app_quit):
        super(MainUI, self).__init__()
        self.app_quit = app_quit
        self.wifi = WifiInterfaces()
        logger.debug("Main UI started ok")
        self.sti = None
        self.load_icons()
        self.iconize()
        self.wifi.connect_signals(self.refresh_menu_items, self.update_connected_state)
        

    def open_about_dialog(self):
        """Show the about dialog."""
        QMessageBox.about(self, "WeFree", ABOUT_TEXT)

    def build_menu(self):
        """Build the menu."""
        menu = QMenu(self)

        connected = False
        # the signals
        for signal in self.wifi.get_signals():
            i = bisect(SIGNAL_BREAKPOINTS, signal.level)
            if signal.has_db_passwords():
                icon = self.icon_wefree[SIGNALS_IMGS[i]]
            else:
                if signal.encrypted:
                    lock = 'lock-'
                else:
                    lock = ''
                if not signal.encrypted or signal.has_password():
                    fname = lock+"signals-{}.png".format(SIGNALS_IMGS[i])
                else:
                    fname = "signals-unk-{}.png".format(SIGNALS_IMGS[i])
                icon = QIcon(os.path.join(CURRENT_PATH, "imgs", fname))

            if signal.is_connected():
                connected = True
            
            when_triggered = (lambda sign: lambda:self.please_connect(sign))(signal)
            action = QAction(icon, signal.ssid, self, triggered = when_triggered)
            menu.addAction(action)

        self.update_connected_state(connected)

        # the bottom part
        menu.addSeparator()
        menu.addAction(QAction("Update Database", self, triggered=self.update_database))<commit_msg>sorting signals by lexicografical order<commit_after>from __future__ import division

"""The main window."""

import os
import logging
from bisect import bisect

from PyQt4 import QtCore, Qt, QtGui
from PyQt4.QtGui import (QAction, QMainWindow, QMessageBox, QSystemTrayIcon,
    QIcon, QMenu, QInputDialog, QPushButton, QLineEdit, QDialog)

from wefree.passwords_manager import PM
from wefree.interfaces import WifiInterfaces

#import NetworkManager

logger = logging.getLogger('wefree.main')

CURRENT_PATH = os.path.abspath(os.path.dirname(__file__))

ABOUT_TEXT = u"""
<center>
WeFree<br/>
Let's free the WiFi.<br/>
</center>
"""

# signals and levels to use them
SIGNALS_IMGS = [0, 25, 50, 75, 100]
SIGNAL_BREAKPOINTS = [.20, .45, .70, .90]

def debug_trace():
    '''Set a tracepoint in the Python debugger that works with Qt'''
    from PyQt4.QtCore import pyqtRemoveInputHook
    from ipdb import set_trace
    pyqtRemoveInputHook()
    set_trace()


class AddPasswordDialog(QDialog):
    def __init__(self, parent, wifi, signal):
        super(AddPasswordDialog, self).__init__(parent)

        self.signal = signal
        self.wifi   = wifi

        self.connect_btn = QPushButton("connect")
        self.connect_and_share_btn = QPushButton("connect and share (Free the world)")
        self.connect_and_share_btn.setDefault(True)
        self.cancel_btn = QPushButton("cancel")
        self.input_password = QLineEdit()
        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.input_password)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.connect_btn)
        hbox.addWidget(self.connect_and_share_btn)
        hbox.addWidget(self.cancel_btn)
        vbox.addLayout(hbox)

        self.connect_btn.clicked.connect(self.on_connect)
        self.connect_and_share_btn.clicked.connect(self.on_connect_and_share)
        self.cancel_btn.clicked.connect(self.close)

    def on_connect(self, share=False):
        password = self.input_password.text()
        if password:
            self.signal.add_password(password, report = True)
            if share:
                PM.add_new_password(password, essid=self.signal.ssid, bssid=self.signal.bssid)
        self.close()
        self.signal.report_to_db = share
        self.wifi.connect(self.signal)

    def on_connect_and_share(self):
        self.on_connect(share=True)

class MainUI(QMainWindow):
    """Main UI."""

    def __init__(self, app_quit):
        super(MainUI, self).__init__()
        self.app_quit = app_quit
        self.wifi = WifiInterfaces()
        logger.debug("Main UI started ok")
        self.sti = None
        self.load_icons()
        self.iconize()
        self.wifi.connect_signals(self.refresh_menu_items, self.update_connected_state)
        

    def open_about_dialog(self):
        """Show the about dialog."""
        QMessageBox.about(self, "WeFree", ABOUT_TEXT)

    def build_menu(self):
        """Build the menu."""
        def action_cmp(a,b):
            aText = a.text().upper()
            bText = b.text().upper()
            if aText == bText: return 0
            if aText < bText: return -1
            return 1
        
        menu = QMenu(self)

        connected = False
        # the signals
        signal_actions = []
        for signal in self.wifi.get_signals():
            i = bisect(SIGNAL_BREAKPOINTS, signal.level)
            if signal.has_db_passwords():
                icon = self.icon_wefree[SIGNALS_IMGS[i]]
            else:
                if signal.encrypted:
                    lock = 'lock-'
                else:
                    lock = ''
                if not signal.encrypted or signal.has_password():
                    fname = lock+"signals-{}.png".format(SIGNALS_IMGS[i])
                else:
                    fname = "signals-unk-{}.png".format(SIGNALS_IMGS[i])
                icon = QIcon(os.path.join(CURRENT_PATH, "imgs", fname))

            if signal.is_connected():
                connected = True
            
            when_triggered = (lambda sign: lambda:self.please_connect(sign))(signal)
            action = QAction(icon, signal.ssid, self, triggered = when_triggered)
            signal_actions.append(action)
            
        signal_actions.sort(cmp = action_cmp)
        for action in signal_actions:
            print action.iconText()
            menu.addAction(action)

        self.update_connected_state(connected)

        # the bottom part
        menu.addSeparator()
        menu.addAction(QAction("Update Database", self, triggered=self.update_database))
{'fraction_non_alphanumeric': 0.10333716915995397, 'fraction_numerical': 0.005178365937859608, 'mean_word_length': 2.462549800796813, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '304519', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2634, 'n_words': 545}