<commit_before>#!/usr/bin/env python3

import socket
import asyncio
import functools
from phasortoolbox.message import Command
from phasortoolbox import Parser
"""A synchrphaor protocol connection clinet.

Connects to any devices that follow IEEE Std C37.118.2-2011, send
commands, and receiving data.
According to IEEE Std C37.118.2-2011 'The device providing data is the
server and the device receiving data is the client.'

Examples:

    # To quickly test a remote host:
    loop = asyncio.get_event_loop()
    remote_pmu = Client(SERVER_IP='10.0.0.1',
              SERVER_TCP_PORT=4712, IDCODE=1, loop=loop)
    remote_pmu.connection_test()

    # An easy way to process a received message would be use function
    # 'Client.transmit_callback()' to callback your function when a packet
    # is received:

    from datetime import datetime
    from phasortoolbox import Parser


    def your_print_time_tag_fun(raw_pkt, my_parser):
        message = my_parser.parse_message(raw_pkt)
        time_tag = float(message.soc) + \
                    float(message.fracsec.fraction_of_second)
        time_tag = datetime.utcfromtimestamp(
            time_tag).strftime("UTC: %m-%d-%Y %H:%M:%S.%f")
        print(time_tag)

    def main():
        my_parser = Parser()
        try:
            loop.run_until_complete(
            remote_pmu.transmit_callback(your_print_time_tag_fun, my_parser))
        except KeyboardInterrupt:
            loop.run_until_complete(remote_pmu.cleanup())

    if __name__ == '__main__':
        main()


    # Overwrite the transmit_callback() function:

"""


class Client(object):
    def __init__(self,
                 SERVER_IP='10.0.0.1',
                 SERVER_TCP_PORT=4712,
                 CLIENT_TCP_PORT='AUTO',
                 SERVER_UDP_PORT=4713,
                 CLIENT_UDP_PORT='AUTO',
                 MODE='TCP',
                 IDCODE=1,
                 loop=None,
                 executor=None,
                 parser=None
                 ):
        self.IDCODE = IDCODE
        self.SERVER_IP = SERVER_IP
        self.SERVER_TCP_PORT = SERVER_TCP_PORT
        self.CLIENT_TCP_PORT = CLIENT_TCP_PORT
        self.SERVER_UDP_PORT = SERVER_UDP_PORT
        self.CLIENT_UDP_PORT = CLIENT_UDP_PORT
        self.MODE = MODE
        self.executor = executor
        if loop:
            self.loop = loop
        else:
            self.loop = asyncio.get_event_loop()
        if parser:
            self.parser = parser
        else:
            self.parser = Parser()
        if self.MODE == 'TCP':
            async def connect():
                self.tsock = socket.socket(
                    socket.AF_INET, socket.SOCK_STREAM)
                self.tsock.setblocking(False)
                self.server_address = (self.SERVER_IP, self.SERVER_TCP_PORT)
                if self.CLIENT_TCP_PORT != 'AUTO':
                    self.tsock.bind('', self.CLIENT_TCP_PORT)
                try:
                    await self.loop.sock_connect(
                        self.tsock, self.server_address)
                except Exception as e:
                    print('Connection', str(self.server_address),
                          'failed. Please check IP and PORT settings')
                    print('Exit the program and try again.')
                    print(e)
                    raise

            async def send_command(CMD):
                await self.loop.sock_sendall(
                    self.tsock, Command(self.IDCODE, CMD))

            async def receive_data_message():
                #raw_pkt = await self.loop.sock_recv(self.tsock, 4)
                #raw_pkt += await self.loop.sock_recv(
                #    self.tsock, int.from_bytes(
                #        raw_pkt[2:4], byteorder='big'))
                #return raw_pkt
                return await self.loop.sock_recv(self.tsock, 4096)
            receive_conf = receive_data_message
            # No different under TCP mode

            async def close_connection():
                self.tsock.close()
        self.connect = connect
        self.send_command = send_command
        self.receive_conf = receive_conf
        self.receive_data_message = receive_data_message
        self.close_connection = close_connection

    async def transmit_callback(self, target, *args):
        if not callable(target):
            raise TypeError("target must be a callable, "
                            "not {!r}".format(type(target)))
        await self.connect()
        await self.send_command('off')
        await self.send_command('cfg2')
        raw_pkt = await self.receive_conf()
        self.loop.run_in_executor(
            self.executor, functools.partial(target, raw_pkt, *args))
        await self.send_command('on')
        try:<commit_msg>Before merge to pdc<commit_after>#!/usr/bin/env python3

import socket
import asyncio
import functools
from phasortoolbox.message import Command
from phasortoolbox import Parser
"""A synchrphaor protocol connection clinet.

Connects to any devices that follow IEEE Std C37.118.2-2011, send
commands, and receiving data.
According to IEEE Std C37.118.2-2011 'The device providing data is the
server and the device receiving data is the client.'

Examples:

    # To quickly test a remote host:
    loop = asyncio.get_event_loop()
    remote_pmu = Client(SERVER_IP='10.0.0.1',
              SERVER_TCP_PORT=4712, IDCODE=1, loop=loop)
    remote_pmu.connection_test()

    # An easy way to process a received message would be use function
    # 'Client.transmit_callback()' to callback your function when a packet
    # is received:

    from datetime import datetime
    from phasortoolbox import Parser


    def your_print_time_tag_fun(raw_pkt, my_parser):
        message = my_parser.parse_message(raw_pkt)
        time_tag = float(message.soc) + \
                    float(message.fracsec.fraction_of_second)
        time_tag = datetime.utcfromtimestamp(
            time_tag).strftime("UTC: %m-%d-%Y %H:%M:%S.%f")
        print(time_tag)

    def main():
        my_parser = Parser()
        try:
            loop.run_until_complete(
            remote_pmu.transmit_callback(your_print_time_tag_fun, my_parser))
        except KeyboardInterrupt:
            loop.run_until_complete(remote_pmu.cleanup())

    if __name__ == '__main__':
        main()


    # Overwrite the transmit_callback() function:

"""


class Client(object):
    def __init__(self,
                 SERVER_IP='10.0.0.1',
                 SERVER_TCP_PORT=4712,
                 CLIENT_TCP_PORT='AUTO',
                 SERVER_UDP_PORT=4713,
                 CLIENT_UDP_PORT='AUTO',
                 MODE='TCP',
                 IDCODE=1,
                 loop=None,
                 executor=None,
                 parser=None
                 ):
        self.IDCODE = IDCODE
        self.SERVER_IP = SERVER_IP
        self.SERVER_TCP_PORT = SERVER_TCP_PORT
        self.CLIENT_TCP_PORT = CLIENT_TCP_PORT
        self.SERVER_UDP_PORT = SERVER_UDP_PORT
        self.CLIENT_UDP_PORT = CLIENT_UDP_PORT
        self.MODE = MODE
        self.executor = executor
        if loop:
            self.loop = loop
        else:
            self.loop = asyncio.get_event_loop()
        if parser:
            self.parser = parser
        else:
            self.parser = Parser()
        if self.MODE == 'TCP':
            async def connect():
                self.tsock = socket.socket(
                    socket.AF_INET, socket.SOCK_STREAM)
                self.tsock.setblocking(False)
                self.server_address = (self.SERVER_IP, self.SERVER_TCP_PORT)
                if self.CLIENT_TCP_PORT != 'AUTO':
                    self.tsock.bind('', self.CLIENT_TCP_PORT)
                try:
                    await self.loop.sock_connect(
                        self.tsock, self.server_address)
                except Exception as e:
                    print('Connection', str(self.server_address),
                          'failed. Please check IP and PORT settings')
                    print('Exit the program and try again.')
                    print(e)
                    raise

            async def send_command(CMD):
                await self.loop.sock_sendall(
                    self.tsock, Command(self.IDCODE, CMD))

            async def receive_data_message():
                return await self.loop.sock_recv(self.tsock, 4096)
            receive_conf = receive_data_message
            # No different under TCP mode

            async def close_connection():
                self.tsock.close()
        self.connect = connect
        self.send_command = send_command
        self.receive_conf = receive_conf
        self.receive_data_message = receive_data_message
        self.close_connection = close_connection

    async def transmit_callback(self, target, *args):
        if not callable(target):
            raise TypeError("target must be a callable, "
                            "not {!r}".format(type(target)))
        await self.connect()
        await self.send_command('off')
        await self.send_command('cfg2')
        raw_pkt = await self.receive_conf()
        self.loop.run_in_executor(
            self.executor, functools.partial(target, raw_pkt, *args))
        await self.send_command('on')
        try:
{'fraction_non_alphanumeric': 0.08404083825900054, 'fraction_numerical': 0.01106931757119828, 'mean_word_length': 1.854601226993865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5360232', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2622, 'n_words': 597}