<commit_before>import errno
import sys
import logging
from websocket import *
from struct import unpack
from struct import pack

# This file contains the libpebble websocket client.
# Based on websocket.py from:
# https://github.com/liris/websocket-client

WS_CMD_WATCH_TO_PHONE = 0x00
WS_CMD_PHONE_TO_WATCH = 0x01
WS_CMD_PHONE_APP_LOG = 0x02
WS_CMD_SERVER_LOG = 0x03
WS_CMD_BUNDLE_INSTALL = 0x04
WS_CMD_STATUS = 0x5
WS_CMD_PHONE_INFO = 0x06
WS_CMD_WATCH_CONNECTION_UPDATE = 0x07

class WebSocketPebble(WebSocket):

######## libPebble Bridge Methods #########

    def write(self, payload, opcode = ABNF.OPCODE_BINARY, ws_cmd = WS_CMD_PHONE_TO_WATCH):
        """
        BRIDGES THIS METHOD:
        def write(self, message):
            try:
                self.send_queue.put(message)
                self.bt_message_sent.wait()
            except:
                self.bt_teardown.set()
                if self.debug_protocol:
                    log.debug("LightBlue process has shutdown (queue write)")

        """
        # Append command byte to the payload:
        payload = pack("B", ws_cmd) + payload
        frame = ABNF.create_frame(payload, opcode)
        if self.get_mask_key:
            frame.get_mask_key = self.get_mask_key
        data = frame.format()

        sent = 0
        while sent < len(data):
            sent += self.io_sock.send(data[sent:])
            if traceEnabled:
                logging.debug('send>>> ' + data.encode('hex'))

    def read(self):

        """
        BRIDGES THIS METHOD:
        def read(self):
            try:
                return self.rec_queue.get()
            except Queue.Empty:
                return (None, None, '')
            except:
                self.bt_teardown.set()
                if self.debug_protocol:
                    log.debug("LightBlue process has shutdown (queue read)")
                return (None, None, '')
                
        NOTE: The return value of this method was modified from 3 tuples to
        4 tuples in order to support multiple possible WS_CMD id's besides
        just WS_CMD_WATCH_TO_PHONE and WS_CMD_STATUS. Now, the first item in
        the tuple (source) identifies which WS_CMD we received. The other
        transports (LightBlue, etc.), if/when they are re-instantiated into
        active use will have to be updated to return this new 4 item tuple. 
                
        retval:   (source, topic, response, data)<commit_msg>Removed space between function definition and docstring<commit_after>import errno
import sys
import logging
from websocket import *
from struct import unpack
from struct import pack

# This file contains the libpebble websocket client.
# Based on websocket.py from:
# https://github.com/liris/websocket-client

WS_CMD_WATCH_TO_PHONE = 0x00
WS_CMD_PHONE_TO_WATCH = 0x01
WS_CMD_PHONE_APP_LOG = 0x02
WS_CMD_SERVER_LOG = 0x03
WS_CMD_BUNDLE_INSTALL = 0x04
WS_CMD_STATUS = 0x5
WS_CMD_PHONE_INFO = 0x06
WS_CMD_WATCH_CONNECTION_UPDATE = 0x07

class WebSocketPebble(WebSocket):

######## libPebble Bridge Methods #########

    def write(self, payload, opcode = ABNF.OPCODE_BINARY, ws_cmd = WS_CMD_PHONE_TO_WATCH):
        """
        BRIDGES THIS METHOD:
        def write(self, message):
            try:
                self.send_queue.put(message)
                self.bt_message_sent.wait()
            except:
                self.bt_teardown.set()
                if self.debug_protocol:
                    log.debug("LightBlue process has shutdown (queue write)")

        """
        # Append command byte to the payload:
        payload = pack("B", ws_cmd) + payload
        frame = ABNF.create_frame(payload, opcode)
        if self.get_mask_key:
            frame.get_mask_key = self.get_mask_key
        data = frame.format()

        sent = 0
        while sent < len(data):
            sent += self.io_sock.send(data[sent:])
            if traceEnabled:
                logging.debug('send>>> ' + data.encode('hex'))

    def read(self):
        """
        BRIDGES THIS METHOD:
        def read(self):
            try:
                return self.rec_queue.get()
            except Queue.Empty:
                return (None, None, '')
            except:
                self.bt_teardown.set()
                if self.debug_protocol:
                    log.debug("LightBlue process has shutdown (queue read)")
                return (None, None, '')
                
        NOTE: The return value of this method was modified from 3 tuples to
        4 tuples in order to support multiple possible WS_CMD id's besides
        just WS_CMD_WATCH_TO_PHONE and WS_CMD_STATUS. Now, the first item in
        the tuple (source) identifies which WS_CMD we received. The other
        transports (LightBlue, etc.), if/when they are re-instantiated into
        active use will have to be updated to return this new 4 item tuple. 
                
        retval:   (source, topic, response, data)
{'fraction_non_alphanumeric': 0.08237431859479104, 'fraction_numerical': 0.010902483343428226, 'mean_word_length': 2.1157232704402515, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '3188029', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1431, 'n_words': 413}