<commit_before>
from glob import glob
import logging
import json
import sys
from uuid import UUID

from serial.tools import list_ports as _list_ports
from fluxclient.encryptor import KeyObject
# from fluxclient.upnp import UpnpDiscover
from fluxclient.upnp.task import UpnpTask
from fluxclient.upnp import UpnpError
from .base import WebSocketBase, WebsocketBinaryHelperMixin, BinaryUploadHelper, SIMULATE, OnTextMessageMixin

logger = logging.getLogger(__name__)


def check_task(func):
    '''
    check whether it's connected to a delta
    or send_error
    '''
    def f(self, *args, **kwargs):
        if self.upnp_task:
            return func(self, *args, **kwargs)
        else:
            self.send_error('Not connected')
            return
    return f


class WebsocketUpnp(OnTextMessageMixin, WebsocketBinaryHelperMixin, WebSocketBase):

    def __init__(self, *args, **kw):
        super(WebsocketUpnp, self).__init__(*args, **kw)

        self.client_key = None
        self.password = <PASSWORD>
        self.upnp_task = None
        self.cmd_mapping = {
            'connect': [self.connect],
            'scan_wifi': [self.scan_wifi],
            'upload_key': [self.upload_key],
            'upload_password': [self.upload_password],
            'add_key': [self.add_key],
            'config_network': [self.config_network],
            'set_name': [self.set_name],
            'set_password': [self.set_password],
        }

    def upload_key(self, params):
        logger.debug('upload_key:' + params)
        pem = params
        self.client_key = KeyObject.load_keyobj(pem)
        self.send_json(status="ok")

    def upload_password(self, params):
        logger.debug('upload_password: ' + params)
        self.password = params.strip()
        self.send_json(status="ok")

    def connect(self, params):

        self.close_task()
        params = params.split(None, 1)
        if len(params) == 1:
            uuid = params[0].strip()
            params = "{}"
        else:
            uuid, params = params
        logger.debug('connect: ' + uuid)

        uuid = UUID(hex=uuid)

        params = json.loads(params)
        # uuid, client_key, ipaddr=None, device_metadata=None,
        #          remote_profile=None, backend_options={}, lookup_callback=None,
        #          lookup_timeout=float("INF")
        valid_patams = {'client_key': self.client_key, 'uuid': uuid}
        for i in ['ipaddr', 'device_metadata', 'remote_profile', 'backend_options', 'lookup_callback', 'lookup_timeout']:
            if i in params:<commit_msg>minor<commit_after>
from glob import glob
import logging
import json
import sys
from uuid import UUID

from serial.tools import list_ports as _list_ports
from fluxclient.encryptor import KeyObject
# from fluxclient.upnp import UpnpDiscover
from fluxclient.upnp.task import UpnpTask
from fluxclient.upnp import UpnpError
from .base import WebSocketBase, WebsocketBinaryHelperMixin, BinaryUploadHelper, SIMULATE, OnTextMessageMixin

logger = logging.getLogger(__name__)


def check_task(func):
    '''
    check whether it's connected to a delta
    or send_error
    '''
    def f(self, *args, **kwargs):
        if self.upnp_task:
            return func(self, *args, **kwargs)
        else:
            self.send_error('Not connected')
            return
    return f


class WebsocketUpnp(OnTextMessageMixin, WebsocketBinaryHelperMixin, WebSocketBase):

    def __init__(self, *args, **kw):
        super(WebsocketUpnp, self).__init__(*args, **kw)

        self.client_key = None
        self.password = None
        self.upnp_task = None
        self.cmd_mapping = {
            'connect': [self.connect],
            'scan_wifi': [self.scan_wifi],
            'upload_key': [self.upload_key],
            'upload_password': [self.upload_password],
            'add_key': [self.add_key],
            'config_network': [self.config_network],
            'set_name': [self.set_name],
            'set_password': [self.<PASSWORD>],
        }

    def upload_key(self, params):
        logger.debug('upload_key:' + params)
        pem = params
        self.client_key = KeyObject.load_keyobj(pem)
        self.send_json(status="ok")

    def upload_password(self, params):
        logger.debug('upload_password: ' + params)
        self.password = params.strip()
        self.send_json(status="ok")

    def connect(self, params):

        self.close_task()
        params = params.split(None, 1)
        if len(params) == 1:
            self.uuid = params[0].strip()
            params = "{}"
        else:
            self.uuid, params = params
        logger.debug('connect: ' + self.uuid)

        self.uuid = UUID(hex=self.uuid)

        params = json.loads(params)
        # uuid, client_key, ipaddr=None, device_metadata=None,
        #          remote_profile=None, backend_options={}, lookup_callback=None,
        #          lookup_timeout=float("INF")
        valid_patams = {'client_key': self.client_key, 'uuid': self.uuid}
        for i in ['ipaddr', 'device_metadata', 'remote_profile', 'backend_options', 'lookup_callback', 'lookup_timeout']:
            if i in params:
{'fraction_non_alphanumeric': 0.11287820015515904, 'fraction_numerical': 0.0011636927851047323, 'mean_word_length': 2.6189473684210527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '642366', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1535, 'n_words': 349}