<commit_before>from bluetooth import *
from wifi import Cell, Scheme
import json
import socket


def send(d):
    client_sock.send(json.dumps(d).encode("utf-8"))


def scan():
    ret = {}
    cells = list(Cell.all("wlan0"))
    value = []
    print(cells)
    for c in cells:
        ret[c.ssid.strip()] = c
    return ret


server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("", PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

advertise_service(server_sock, "SampleServer",
                  service_id=uuid,
                  service_classes=[uuid, SERIAL_PORT_CLASS],
                  profiles=[SERIAL_PORT_PROFILE],
#                  protocols = [ OBEX_UUID ]
                 )


CELLS = scan()
while True:
    print("Waiting for connection on RFCOMM channel %d" % port)
    client_sock, client_info = server_sock.accept()
    print("Accepted connection from ", client_info)
    try:
        while True:
            data = client_sock.recv(1024)
            if len(data) == 0: break
            command = json.loads(data.decode("utf-8"))
            if command["action"] == "list":
                cells = list(Cell.all("wlan0"))
                value = []
                for c in CELLS:
                    value.append(c)
                send({"value": value})
            elif command["action"] == "connect":
                cell = CELLS[command["args"][0]]
                scheme = Scheme.for_cell('wlan0', 'home', cell, command["args"][1])
                try:
                    scheme.save()
                except AssertionError:
                    # Schema already saved
                    pass
                try:
                    scheme.activate()
                    send({"value": "Successfully connected to network."})
                except ConnectionError:
                    send({"value": "Something went wrong. Please try again."})
            elif command["action"] == "address":
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.connect(("8.8.8.8", 80))
                address = s.getsockname()[0]
                s.close()
                send({"value": address})
            elif command["action"] == "rescan":
                CELLS = scan()
                send({})
            elif command["action"] == "exit":
                break
            print("received [%s]" % data)
    except IOError:
        pass
    finally:
        print("disconnected")
        client_sock.close()

server_sock.close()
print("all done")<commit_msg>error handling<commit_after>from bluetooth import *
from wifi import Cell, Scheme
import json
import socket


def send(d):
    client_sock.send(json.dumps(d).encode("utf-8"))


def scan():
    ret = {}
    cells = list(Cell.all("wlan0"))
    value = []
    print(cells)
    for c in cells:
        ret[c.ssid.strip()] = c
    return ret


server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("", PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

advertise_service(server_sock, "SampleServer",
                  service_id=uuid,
                  service_classes=[uuid, SERIAL_PORT_CLASS],
                  profiles=[SERIAL_PORT_PROFILE],
#                  protocols = [ OBEX_UUID ]
                 )


CELLS = scan()
while True:
    print("Waiting for connection on RFCOMM channel %d" % port)
    client_sock, client_info = server_sock.accept()
    print("Accepted connection from ", client_info)
    try:
        while True:
            data = client_sock.recv(1024)
            if len(data) == 0: break
            command = json.loads(data.decode("utf-8"))
            if command["action"] == "list":
                cells = list(Cell.all("wlan0"))
                value = []
                for c in CELLS:
                    value.append(c)
                send({"value": value})
            elif command["action"] == "connect":
                cell = CELLS[command["args"][0]]
                scheme = Scheme.for_cell('wlan0', 'home', cell, command["args"][1])
                try:
                    scheme.save()
                except AssertionError:
                    # Schema already saved
                    pass
                try:
                    scheme.activate()
                    send({"value": "Successfully connected to network."})
                except:
                    send({"value": "Something went wrong. Please try again."})
            elif command["action"] == "address":
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.connect(("8.8.8.8", 80))
                address = s.getsockname()[0]
                s.close()
                send({"value": address})
            elif command["action"] == "rescan":
                CELLS = scan()
                send({})
            elif command["action"] == "exit":
                break
            print("received [%s]" % data)
    except IOError:
        pass
    finally:
        print("disconnected")
        client_sock.close()

server_sock.close()
print("all done")
{'fraction_non_alphanumeric': 0.12652823597904134, 'fraction_numerical': 0.015524936929943722, 'mean_word_length': 1.6801872074882995, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4067703', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1451, 'n_words': 317}