<commit_before># -*- coding: utf-8 -*-
from ws4py.client.threadedclient import WebSocketClient
from types import FunctionType
import json
import socket
import subprocess
import re
import os
import sys
import wakeonlan

try:
    import urllib.parse
    _urllib_parse_unquote = urllib.parse.unquote
except ImportError:
    import urllib
    _urllib_parse_unquote = urllib.unquote


class HashableDict(dict):
    def __hash__(self):
        return hash(frozenset(self.items()))


hello_data = {
    "id": "register_0",
    "payload": {
        "forcePairing": False,
        "manifest": {
            "appVersion": "1.1",
            "manifestVersion": 1,
            "permissions": [
                "LAUNCH",
                "LAUNCH_WEBAPP",
                "APP_TO_APP",
                "CLOSE",
                "TEST_OPEN",
                "TEST_PROTECTED",
                "CONTROL_AUDIO",
                "CONTROL_DISPLAY",
                "CONTROL_INPUT_JOYSTICK",
                "CONTROL_INPUT_MEDIA_RECORDING",
                "CONTROL_INPUT_MEDIA_PLAYBACK",
                "CONTROL_INPUT_TV",
                "CONTROL_POWER",
                "READ_APP_STATUS",
                "READ_CURRENT_CHANNEL",
                "READ_INPUT_DEVICE_LIST",
                "READ_NETWORK_STATE",
                "READ_RUNNING_APPS",
                "READ_TV_CHANNEL_LIST",
                "WRITE_NOTIFICATION_TOAST",
                "READ_POWER_STATE",
                "READ_COUNTRY_INFO"
            ],
            "signatures": [
                {
                    "signature": "<KEY>
                    "signatureVersion": 1
                }
            ],
            "signed": {
                "appId": "com.lge.test",
                "created": "20140509",
                "localizedAppNames": {
                    "": "LG Remote App",
                    "ko-KR": u"리모컨 앱",
                    "zxx-XX": u"ЛГ Rэмotэ AПП"
                },
                "localizedVendorNames": {
                    "": "LG Electronics"
                },
                "permissions": [
                    "TEST_SECURE",
                    "CONTROL_INPUT_TEXT",
                    "CONTROL_MOUSE_AND_KEYBOARD",
                    "READ_INSTALLED_APPS",
                    "READ_LGE_SDX",
                    "READ_NOTIFICATIONS",
                    "SEARCH",
                    "WRITE_SETTINGS",
                    "WRITE_NOTIFICATION_ALERT",
                    "CONTROL_POWER",
                    "READ_CURRENT_CHANNEL",
                    "READ_RUNNING_APPS",
                    "READ_UPDATE_INFO",
                    "UPDATE_FROM_REMOTE_APP",
                    "READ_LGE_TV_INPUT_EVENTS",
                    "READ_TV_CURRENT_TIME"
                ],
                "serial": "2f930e2d2cfe083771f68e4fe7bb07",
                "vendorId": "com.lge"
            }
        },
        "pairingType": "PROMPT"
    },
    "type": "register"
}


def LGTVScan(first_only=False):
    request = b'M-SEARCH * HTTP/1.1\r\n' \
              b'HOST: 192.168.3.11:1900\r\n' \
              b'MAN: "ssdp:discover"\r\n' \
              b'MX: 2\r\n' \
              b'ST: urn:schemas-upnp-org:device:MediaRenderer:1\r\n\r\n'

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)

    addresses = []
    attempts = 4
    while attempts > 0:
        sock.sendto(request, ('192.168.3.11', 1900))
        uuid = None
        model = None
        address = None
        data = {}
        try:
            response, address = sock.recvfrom(512)
            # print response
            for line in response.split('\n'):
                if line.startswith("USN"):
                    uuid = re.findall(r'uuid:(.*?):', line)[0]
                if line.startswith("DLNADeviceName"):
                    (junk, data) = line.split(':')
                    data = data.strip()
                    data = _urllib_parse_unquote(data)
                    model = re.findall(r'\[LG\] webOS TV (.*)', data)[0]
                data = HashableDict({
                    'uuid': uuid,
                    'model': model,
                    'address': address[0]
                })
        except Exception as e:
            print(e.message)
            attempts -= 1
            continue

        if re.search('LG', response):
            if first_only:
                sock.close()
                return data
            else:
                addresses.append(data)

        attempts -= 1

    sock.close()
    if first_only:
        return []

    if len(addresses) == 0:
        return []

    return list(set(addresses))


def resolveHost(hostname):
    return socket.gethostbyname(hostname)


def getMacAddress(address):
    pid = subprocess.Popen(["arp", "-n", address], stdout=subprocess.PIPE)
    s = pid.communicate()[0]
    matches = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s)
    if not matches:
        return None
    mac = matches.groups()[0]
    m = mac.split(':')
    mac = ':'.join(['%02x' % int(x, 16) for x in m])
    return mac


def methods(cls):
    return [x for x, y in cls.__dict__.items() if type(y) == FunctionType]


def getCommands(cls):
    excludes = [
        'opened',
        'closed',
        'received_message',
        'exec_command'
    ]
    out = []
    m = methods(cls)
    for method in m:
        if method.startswith("_" + cls.__name__):
            continue
        if method in excludes:<commit_msg>Add string decoding that's required now<commit_after># -*- coding: utf-8 -*-
from ws4py.client.threadedclient import WebSocketClient
from types import FunctionType
import json
import socket
import subprocess
import re
import os
import sys
import wakeonlan

try:
    import urllib.parse
    _urllib_parse_unquote = urllib.parse.unquote
except ImportError:
    import urllib
    _urllib_parse_unquote = urllib.unquote


class HashableDict(dict):
    def __hash__(self):
        return hash(frozenset(self.items()))


hello_data = {
    "id": "register_0",
    "payload": {
        "forcePairing": False,
        "manifest": {
            "appVersion": "1.1",
            "manifestVersion": 1,
            "permissions": [
                "LAUNCH",
                "LAUNCH_WEBAPP",
                "APP_TO_APP",
                "CLOSE",
                "TEST_OPEN",
                "TEST_PROTECTED",
                "CONTROL_AUDIO",
                "CONTROL_DISPLAY",
                "CONTROL_INPUT_JOYSTICK",
                "CONTROL_INPUT_MEDIA_RECORDING",
                "CONTROL_INPUT_MEDIA_PLAYBACK",
                "CONTROL_INPUT_TV",
                "CONTROL_POWER",
                "READ_APP_STATUS",
                "READ_CURRENT_CHANNEL",
                "READ_INPUT_DEVICE_LIST",
                "READ_NETWORK_STATE",
                "READ_RUNNING_APPS",
                "READ_TV_CHANNEL_LIST",
                "WRITE_NOTIFICATION_TOAST",
                "READ_POWER_STATE",
                "READ_COUNTRY_INFO"
            ],
            "signatures": [
                {
                    "signature": "<KEY>
                    "signatureVersion": 1
                }
            ],
            "signed": {
                "appId": "com.lge.test",
                "created": "20140509",
                "localizedAppNames": {
                    "": "LG Remote App",
                    "ko-KR": u"리모컨 앱",
                    "zxx-XX": u"ЛГ Rэмotэ AПП"
                },
                "localizedVendorNames": {
                    "": "LG Electronics"
                },
                "permissions": [
                    "TEST_SECURE",
                    "CONTROL_INPUT_TEXT",
                    "CONTROL_MOUSE_AND_KEYBOARD",
                    "READ_INSTALLED_APPS",
                    "READ_LGE_SDX",
                    "READ_NOTIFICATIONS",
                    "SEARCH",
                    "WRITE_SETTINGS",
                    "WRITE_NOTIFICATION_ALERT",
                    "CONTROL_POWER",
                    "READ_CURRENT_CHANNEL",
                    "READ_RUNNING_APPS",
                    "READ_UPDATE_INFO",
                    "UPDATE_FROM_REMOTE_APP",
                    "READ_LGE_TV_INPUT_EVENTS",
                    "READ_TV_CURRENT_TIME"
                ],
                "serial": "2f930e2d2cfe083771f68e4fe7bb07",
                "vendorId": "com.lge"
            }
        },
        "pairingType": "PROMPT"
    },
    "type": "register"
}


def LGTVScan(first_only=False):
    request = b'M-SEARCH * HTTP/1.1\r\n' \
              b'HOST: 192.168.3.11:1900\r\n' \
              b'MAN: "ssdp:discover"\r\n' \
              b'MX: 2\r\n' \
              b'ST: urn:schemas-upnp-org:device:MediaRenderer:1\r\n\r\n'

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(1)

    addresses = []
    attempts = 4
    while attempts > 0:
        sock.sendto(request, ('192.168.3.11', 1900))
        uuid = None
        model = None
        address = None
        data = {}
        try:
            response, address = sock.recvfrom(512)
            # print response
            for line in response.decode().split('\n'):
                if line.startswith("USN"):
                    uuid = re.findall(r'uuid:(.*?):', line)[0]
                if line.startswith("DLNADeviceName"):
                    (junk, data) = line.split(':')
                    data = data.strip()
                    data = _urllib_parse_unquote(data)
                    model = re.findall(r'\[LG\] webOS TV (.*)', data)[0]
                data = HashableDict({
                    'uuid': uuid,
                    'model': model,
                    'address': address[0]
                })
        except Exception as e:
            print(e.message)
            attempts -= 1
            continue

        if re.search('LG', response.decode()):
            if first_only:
                sock.close()
                return data
            else:
                addresses.append(data)

        attempts -= 1

    sock.close()
    if first_only:
        return []

    if len(addresses) == 0:
        return []

    return list(set(addresses))


def resolveHost(hostname):
    return socket.gethostbyname(hostname)


def getMacAddress(address):
    pid = subprocess.Popen(["arp", "-n", address], stdout=subprocess.PIPE)
    s = pid.communicate()[0].decode()
    matches = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s)
    if not matches:
        return None
    mac = matches.groups()[0]
    m = mac.split(':')
    mac = ':'.join(['%02x' % int(x, 16) for x in m])
    return mac


def methods(cls):
    return [x for x, y in cls.__dict__.items() if type(y) == FunctionType]


def getCommands(cls):
    excludes = [
        'opened',
        'closed',
        'received_message',
        'exec_command'
    ]
    out = []
    m = methods(cls)
    for method in m:
        if method.startswith("_" + cls.__name__):
            continue
        if method in excludes:
{'fraction_non_alphanumeric': 0.11861164252528013, 'fraction_numerical': 0.01566912635510613, 'mean_word_length': 1.3695229872652708, 'pattern_counts': {'":': 48, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '4177941', 'n_tokens_mistral': 3512, 'n_tokens_neox': 3251, 'n_words': 592}