<commit_before># -*- coding: UTF-8 -*-

# Imports
from twisted.python import log
import signal
gobjectnotimported = False
try:
    from dbus.mainloop.glib import DBusGMainLoop
    DBusGMainLoop(set_as_default=True)
    import gobject
    import pygst
    pygst.require('0.10')
    gobject.threads_init()
    from lisa.client import lib
    from lib import Listener
except:
    gobjectnotimported = True
from twisted.internet import ssl, utils
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet.defer import inlineCallbacks, DeferredQueue
from twisted.application.internet import TCPClient
from twisted.protocols.basic import LineReceiver
from twisted.application import internet, service
from twisted.internet import reactor
from lisa.client.ConfigManager import ConfigManagerSingleton
import json, os
from OpenSSL import SSL
import platform
from lib import Speaker

# Globals
PWD = os.path.dirname(os.path.abspath(__file__))
sound_queue = DeferredQueue()
configuration = None
LisaFactory = None


class LisaClient(LineReceiver):
    """
    Lisa TCP client
    """
    def __init__(self):
        self.factory = None
        self.configuration = ConfigManagerSingleton.get().getConfiguration()
        self.listener = None
        self.debug_input = False
        self.debug_output = False
        if self.configuration.has_key("debug"):
            if self.configuration["debug"].has_key("debug_input"):
                self.debug_input = self.configuration["debug"]["debug_input"]
            if self.configuration["debug"].has_key("debug_output"):
                self.debug_output = self.configuration["debug"]["debug_output"]
        self.zone = ""
        if self.configuration.has_key("zone"):
            self.zone = self.configuration['zone']

    def sendMessage(self, message, type='chat', dict=None):
        if dict:
            line = json.dumps(
                {
                    "from": unicode(platform.node()),
                    "type": type,
                    "body": unicode(message),
                    "zone": self.zone,
                    "outcome": dict
                }
            )
        else:
            line = json.dumps(
                {
                    "from": unicode(platform.node()),
                    "type": type,
                    "body": unicode(message),
                    "zone": self.zone
                }
            )

        if self.debug_output:
            log.msg('OUTPUT: %s' % line)

        # send line to the server
        self.sendLine(line)

    def lineReceived(self, data):
        """
        Data received callback
        """

        datajson = json.loads(data)
        if self.debug_input == True:
            log.msg("INPUT: " + unicode(datajson))

        if datajson.has_key("type"):
            if datajson['type'] == 'chat':
                Speaker.speak(datajson['body'])

            elif datajson['type'] == 'command':
                if datajson['command'] == 'LOGIN':
                    # Get Bot name
                    botname = unicode(datajson['bot_name'])
                    log.msg("setting botname to %s" % botname)
                    self.botname = botname

                    # Send TTS
                    Speaker.speak(datajson['body'])
                    
                    # Create listener
                    if datajson.has_key('nolistener') == False and not self.listener:
                        self.listener = Listener(lisa_client = self, botname = botname)<commit_msg>Fixing speaker (bis...)<commit_after># -*- coding: UTF-8 -*-

# Imports
from twisted.python import log
import signal
gobjectnotimported = False
try:
    from dbus.mainloop.glib import DBusGMainLoop
    DBusGMainLoop(set_as_default=True)
    import gobject
    import pygst
    pygst.require('0.10')
    gobject.threads_init()
    from lisa.client import lib
    from lib import Listener
    from lib import Speaker
except:
    gobjectnotimported = True
from twisted.internet import ssl, utils
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet.defer import inlineCallbacks, DeferredQueue
from twisted.application.internet import TCPClient
from twisted.protocols.basic import LineReceiver
from twisted.application import internet, service
from twisted.internet import reactor
from lisa.client.ConfigManager import ConfigManagerSingleton
import json, os
from OpenSSL import SSL
import platform

# Globals
PWD = os.path.dirname(os.path.abspath(__file__))
sound_queue = DeferredQueue()
configuration = None
LisaFactory = None


class LisaClient(LineReceiver):
    """
    Lisa TCP client
    """
    def __init__(self):
        self.factory = None
        self.configuration = ConfigManagerSingleton.get().getConfiguration()
        self.listener = None
        self.debug_input = False
        self.debug_output = False
        if self.configuration.has_key("debug"):
            if self.configuration["debug"].has_key("debug_input"):
                self.debug_input = self.configuration["debug"]["debug_input"]
            if self.configuration["debug"].has_key("debug_output"):
                self.debug_output = self.configuration["debug"]["debug_output"]
        self.zone = ""
        if self.configuration.has_key("zone"):
            self.zone = self.configuration['zone']

    def sendMessage(self, message, type='chat', dict=None):
        if dict:
            line = json.dumps(
                {
                    "from": unicode(platform.node()),
                    "type": type,
                    "body": unicode(message),
                    "zone": self.zone,
                    "outcome": dict
                }
            )
        else:
            line = json.dumps(
                {
                    "from": unicode(platform.node()),
                    "type": type,
                    "body": unicode(message),
                    "zone": self.zone
                }
            )

        if self.debug_output:
            log.msg('OUTPUT: %s' % line)

        # send line to the server
        self.sendLine(line)

    def lineReceived(self, data):
        """
        Data received callback
        The nolistener in configuration is here to let the travis build pass without loading gst
        """

        datajson = json.loads(data)
        if self.debug_input == True:
            log.msg("INPUT: " + unicode(datajson))

        if datajson.has_key("type"):
            if datajson['type'] == 'chat':
                Speaker.speak(datajson['body'])

            elif datajson['type'] == 'command':
                if datajson['command'] == 'LOGIN':
                    # Get Bot name
                    botname = unicode(datajson['bot_name'])
                    log.msg("setting botname to %s" % botname)
                    self.botname = botname

                    if datajson.has_key('nolistener') == False:
                        # Send TTS
                        Speaker.speak(datajson['body'])
                    
                    # Create listener
                    if datajson.has_key('nolistener') == False and not self.listener:
                        self.listener = Listener(lisa_client = self, botname = botname)
{'fraction_non_alphanumeric': 0.10089717046238786, 'fraction_numerical': 0.0011042097998619738, 'mean_word_length': 1.91941982272361, 'pattern_counts': {'":': 18, '<': 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': '733358', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1872, 'n_words': 455}