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

"""
Console Communications
"""

import sys
import asyncore

from .debugging import Logging, ModuleLogger

from .core import deferred
from .comm import PDU, Client, Server

# some debugging
_debug = 0
_log = ModuleLogger(globals())

try:
    asyncore.file_dispatcher
except:
    class _barf: pass
    asyncore.file_dispatcher = _barf

#
#   ConsoleClient
#

class ConsoleClient(asyncore.file_dispatcher, Client, Logging):

    def __init__(self, cid=None):
        ConsoleClient._debug("__init__ cid=%r", cid)
        asyncore.file_dispatcher.__init__(self, sys.stdin)
        Client.__init__(self, cid)

    def readable(self):
        return True     # We are always happy to read

    def writable(self):
        return False    # we don't have anything to write

    def handle_read(self):
        deferred(ConsoleClient._debug, "handle_read")
        data = sys.stdin.read()
        deferred(ConsoleClient._debug, "    - data: %r", data)
        deferred(self.request, PDU(data))

    def confirmation(self, pdu):
        deferred(ConsoleClient._debug, "confirmation %r", pdu)
        try:
            sys.stdout.write(pdu.pduData)
        except Exception as err:
            ConsoleClient._exception("Confirmation sys.stdout.write exception: %r", err)

#
#   ConsoleServer
#

class ConsoleServer(asyncore.file_dispatcher, Server, Logging):

    def __init__(self, sid=None):
        ConsoleServer._debug("__init__ sid=%r", sid)
        asyncore.file_dispatcher.__init__(self, sys.stdin)
        Server.__init__(self, sid)

    def readable(self):
        return True     # We are always happy to read

    def writable(self):
        return False    # we don't have anything to write

    def handle_read(self):
        deferred(ConsoleServer._debug, "handle_read")
        data = sys.stdin.read()
        deferred(ConsoleServer._debug, "    - data: %r", data)
        deferred(self.response, PDU(data))

    def indication(self, pdu):
        deferred(ConsoleServer._debug, "Indication %r", pdu)
        try:
            sys.stdout.write(pdu.pduData)
        except Exception as err:
            ConsoleServer._exception("Indication sys.stdout.write exception: %r", err)<commit_msg>console had to convert strings to bytes to go in the data portion of a PDU<commit_after>#!/usr/bin/env python

"""
Console Communications
"""

import sys
import asyncore

from .debugging import bacpypes_debugging, ModuleLogger

from .core import deferred
from .comm import PDU, Client, Server

# some debugging
_debug = 0
_log = ModuleLogger(globals())

try:
    asyncore.file_dispatcher
except:
    class _barf: pass
    asyncore.file_dispatcher = _barf

#
#   ConsoleClient
#

@bacpypes_debugging
class ConsoleClient(asyncore.file_dispatcher, Client):

    def __init__(self, cid=None):
        if _debug: ConsoleClient._debug("__init__ cid=%r", cid)
        asyncore.file_dispatcher.__init__(self, sys.stdin)
        Client.__init__(self, cid)

    def readable(self):
        return True     # We are always happy to read

    def writable(self):
        return False    # we don't have anything to write

    def handle_read(self):
        if _debug: deferred(ConsoleClient._debug, "handle_read")
        data = sys.stdin.read()
        if _debug: deferred(ConsoleClient._debug, "    - data: %r", data)
        deferred(self.request, PDU(data.encode('utf_8')))

    def confirmation(self, pdu):
        if _debug: deferred(ConsoleClient._debug, "confirmation %r", pdu)
        try:
            sys.stdout.write(pdu.pduData.decode('utf_8'))
        except Exception as err:
            ConsoleClient._exception("Confirmation sys.stdout.write exception: %r", err)

#
#   ConsoleServer
#

@bacpypes_debugging
class ConsoleServer(asyncore.file_dispatcher, Server):

    def __init__(self, sid=None):
        if _debug: ConsoleServer._debug("__init__ sid=%r", sid)
        asyncore.file_dispatcher.__init__(self, sys.stdin)
        Server.__init__(self, sid)

    def readable(self):
        return True     # We are always happy to read

    def writable(self):
        return False    # we don't have anything to write

    def handle_read(self):
        if _debug: deferred(ConsoleServer._debug, "handle_read")
        data = sys.stdin.read()
        if _debug: deferred(ConsoleServer._debug, "    - data: %r", data)
        deferred(self.response, PDU(data.encode('utf_8')))

    def indication(self, pdu):
        if _debug: deferred(ConsoleServer._debug, "Indication %r", pdu)
        try:
            sys.stdout.write(pdu.pduData.decode('utf_8'))
        except Exception as err:
            ConsoleServer._exception("Indication sys.stdout.write exception: %r", err)
{'fraction_non_alphanumeric': 0.1051063829787234, 'fraction_numerical': 0.001276595744680851, 'mean_word_length': 2.997448979591837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5415309', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1430, 'n_words': 364}