<commit_before>from __future__ import absolute_import, print_function
__author__ = 'katharine'

from datetime import datetime
import logging
import os
import os.path
import re
import subprocess
import time
import uuid
import sourcemap
import sys

from collections import OrderedDict
from libpebble2.protocol.logs import AppLogMessage, AppLogShippingControl
from libpebble2.communication.transports.websocket import MessageTargetPhone
from libpebble2.communication.transports.websocket.protocol import WebSocketPhoneAppLog, WebSocketConnectionStatusUpdate

from pebble_tool.exceptions import PebbleProjectException
from pebble_tool.sdk import add_tools_to_path
from pebble_tool.sdk.project import PebbleProject
from colorama import Fore, Back, Style
from sourcemap.exceptions import SourceMapDecodeError


logger = logging.getLogger("pebble_tool.util.logs")


class PebbleLogPrinter(object):
    colour_scheme = OrderedDict([
        # LOG_LEVEL_DEBUG_VERBOSE
        (255, Fore.CYAN),
        # LOG_LEVEL_DEBUG
        (200, Fore.MAGENTA),
        # LOG_LEVEL_INFO
        (100, ""),
        # LOG_LEVEL_WARNING
        (50, Style.BRIGHT + Fore.RED),
        # LOG_LEVEL_ERROR
        (1, Back.RED + Style.BRIGHT + Fore.WHITE),
        # LOG_LEVEL_ALWAYS
        (0, None)])
    phone_colour = None

    def __init__(self, pebble, force_colour=None):
        """
        :param pebble: libpebble2.communication.PebbleConnection
        :param force_colour: Bool
        """
        self.pebble = pebble
        self.print_with_colour = force_colour if force_colour is not None else sys.stdout.isatty()
        pebble.send_packet(AppLogShippingControl(enable=True))
        self.handles = []
        self.handles.append(pebble.register_endpoint(AppLogMessage, self.handle_watch_log))
        self.handles.append(pebble.register_transport_endpoint(MessageTargetPhone, WebSocketPhoneAppLog,
                                                               self.handle_phone_log))
        self.handles.append(pebble.register_transport_endpoint(MessageTargetPhone, WebSocketConnectionStatusUpdate,
                                                               self.handle_connection))
        self.sourcemap = self._load_js_sourcemap()
        add_tools_to_path()

    def _load_js_sourcemap(self):
        sourcemap_path = "build/pebble-js-app.js.map"
        if not os.path.exists(sourcemap_path):
            return None
        with open(sourcemap_path) as sourcemap_file:
            try:
                return sourcemap.load(sourcemap_file)
            except SourceMapDecodeError as e:
                logger.warning('Found %s, but failed to parse it: %s', sourcemap_path, str(e))
                return None

    def _sourcemap_translate_js_log(self, logstr):
        if not self.sourcemap:
            return logstr

        def sourcemap_replacer(matchobj):
            d = matchobj.groupdict()
            line = int(d['line'])
            column = int(d['column']) if 'column' in d else 0
            try:
                token = self.sourcemap.lookup(line - 1, column)  # sourcemap wants zero-based lines numbers!
                return u"{}:{}:{}".format(token.src, token.src_line + 1, token.src_col)
            except IndexError:
                return u"???:?:?"

        try:
            return re.sub(r"(:?file://[\w\/\.-]+)?pebble-js-app\.js((:?\:)(?P<line>\d+))((:?\:)(?P<column>\d+))?",
                          sourcemap_replacer, logstr, flags=re.MULTILINE)
        except IndexError:
            return logstr


    def _print(self, packet, message):
        colour = self._get_colour(packet)
        message_string = message.encode('utf-8')<commit_msg>Handle missing column correctly<commit_after>from __future__ import absolute_import, print_function
__author__ = 'katharine'

from datetime import datetime
import logging
import os
import os.path
import re
import subprocess
import time
import uuid
import sourcemap
import sys

from collections import OrderedDict
from libpebble2.protocol.logs import AppLogMessage, AppLogShippingControl
from libpebble2.communication.transports.websocket import MessageTargetPhone
from libpebble2.communication.transports.websocket.protocol import WebSocketPhoneAppLog, WebSocketConnectionStatusUpdate

from pebble_tool.exceptions import PebbleProjectException
from pebble_tool.sdk import add_tools_to_path
from pebble_tool.sdk.project import PebbleProject
from colorama import Fore, Back, Style
from sourcemap.exceptions import SourceMapDecodeError


logger = logging.getLogger("pebble_tool.util.logs")


class PebbleLogPrinter(object):
    colour_scheme = OrderedDict([
        # LOG_LEVEL_DEBUG_VERBOSE
        (255, Fore.CYAN),
        # LOG_LEVEL_DEBUG
        (200, Fore.MAGENTA),
        # LOG_LEVEL_INFO
        (100, ""),
        # LOG_LEVEL_WARNING
        (50, Style.BRIGHT + Fore.RED),
        # LOG_LEVEL_ERROR
        (1, Back.RED + Style.BRIGHT + Fore.WHITE),
        # LOG_LEVEL_ALWAYS
        (0, None)])
    phone_colour = None

    def __init__(self, pebble, force_colour=None):
        """
        :param pebble: libpebble2.communication.PebbleConnection
        :param force_colour: Bool
        """
        self.pebble = pebble
        self.print_with_colour = force_colour if force_colour is not None else sys.stdout.isatty()
        pebble.send_packet(AppLogShippingControl(enable=True))
        self.handles = []
        self.handles.append(pebble.register_endpoint(AppLogMessage, self.handle_watch_log))
        self.handles.append(pebble.register_transport_endpoint(MessageTargetPhone, WebSocketPhoneAppLog,
                                                               self.handle_phone_log))
        self.handles.append(pebble.register_transport_endpoint(MessageTargetPhone, WebSocketConnectionStatusUpdate,
                                                               self.handle_connection))
        self.sourcemap = self._load_js_sourcemap()
        add_tools_to_path()

    def _load_js_sourcemap(self):
        sourcemap_path = "build/pebble-js-app.js.map"
        if not os.path.exists(sourcemap_path):
            return None
        with open(sourcemap_path) as sourcemap_file:
            try:
                return sourcemap.load(sourcemap_file)
            except SourceMapDecodeError as e:
                logger.warning('Found %s, but failed to parse it: %s', sourcemap_path, str(e))
                return None

    def _sourcemap_translate_js_log(self, logstr):
        if not self.sourcemap:
            return logstr

        def sourcemap_replacer(matchobj):
            d = matchobj.groupdict()
            line = int(d['line'])
            column = int(d['column']) if d['column'] is not None else 0
            try:
                token = self.sourcemap.lookup(line - 1, column)  # sourcemap wants zero-based lines numbers!
                return u"{}:{}:{}".format(token.src, token.src_line + 1, token.src_col)
            except IndexError:
                return u"???:?:?"

        try:
            return re.sub(r"(:?file://[\w\/\.-]+)?pebble-js-app\.js((:?\:)(?P<line>\d+))((:?\:)(?P<column>\d+))?",
                          sourcemap_replacer, logstr, flags=re.MULTILINE)
        except IndexError:
            return logstr


    def _print(self, packet, message):
        colour = self._get_colour(packet)
        message_string = message.encode('utf-8')
{'fraction_non_alphanumeric': 0.09300433839479393, 'fraction_numerical': 0.0056941431670282, 'mean_word_length': 2.572397094430993, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6853653', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2123, 'n_words': 447}