Esempio n. 1
0
    def test_reply():
        # Setup
        baseplate = Lego.start(None, threading.Lock())
        baseplate_proxy = baseplate.proxy()
        urn = baseplate.actor_urn
        meta = Metadata(source=urn).__dict__
        message = Message(text='0', metadata=meta, should_log=True).__dict__
        log_file_name = 'test_reply.log'

        children = baseplate_proxy.children.get()
        for child in children:
            print(child.actor_urn)
        # Test
        baseplate_proxy.add_child(ReplyTestingPingLego, log_file_name)
        baseplate.tell(message)
        time.sleep(1)

        # Cleanup
        children = baseplate_proxy.children.get()
        for child in children:
            child.stop()
        baseplate.stop()

        with open(log_file_name, mode='r') as f:
            log = json.loads(f.read())
        os.remove(log_file_name)
        assert log['text'] == '1'  # nosec
Esempio n. 2
0
    def test_reply():
        # Setup
        baseplate = Lego.start(None, threading.Lock())
        baseplate_proxy = baseplate.proxy()
        urn = baseplate.actor_urn
        meta = Metadata(source=urn).__dict__
        message = Message(text='0', metadata=meta, should_log=True).__dict__
        log_file_name = 'test_reply.log'

        children = baseplate_proxy.children.get()
        for child in children:
            print(child.actor_urn)
        # Test
        baseplate_proxy.add_child(ReplyTestingPingLego, log_file_name)
        baseplate.tell(message)
        time.sleep(1)

        # Cleanup
        children = baseplate_proxy.children.get()
        for child in children:
            child.stop()
        baseplate.stop()

        with open(log_file_name, mode='r') as f:
            log = json.loads(f.read())
        os.remove(log_file_name)
        assert log['text'] == '1'  # nosec
Esempio n. 3
0
 def test_on_receive_informs_children():
     log_file_name = 'test_child_informed.log'
     baseplate = Lego(None, threading.Lock())
     child = Lego.start(baseplate, threading.Lock(), log_file_name)
     baseplate.children.append(child)
     message = Message('Test Message 1',
                       Metadata(None).__dict__, True).__dict__
     baseplate.on_receive(message)
     child.stop()
     with open(log_file_name, mode='r') as f:
         log = json.loads(f.read())
     os.remove(log_file_name)
     assert log == message  # nosec
Esempio n. 4
0
 def test_on_receive_informs_children():
     log_file_name = 'test_child_informed.log'
     baseplate = Lego(None, threading.Lock())
     child = Lego.start(baseplate, threading.Lock(), log_file_name)
     baseplate.children.append(child)
     message = Message('Test Message 1',
                       Metadata(None).__dict__, True).__dict__
     baseplate.on_receive(message)
     child.stop()
     with open(log_file_name, mode='r') as f:
         log = json.loads(f.read())
     os.remove(log_file_name)
     assert log == message  # nosec
Esempio n. 5
0
 def test_initialization():
     baseplate = Lego.start(None, threading.Lock())
     baseplate_proxy = baseplate.proxy()
     baseplate_proxy.add_child(IRC,  # nosec
                               channels=['#foo'],
                               nickname='test_nick',
                               server='foo.testing',
                               port=6667,
                               use_ssl=False,
                               username='******',
                               password='******')
     # Cleanup
     children = baseplate_proxy.children.get()
     for child in children:
         child.stop()
     baseplate.stop()
Esempio n. 6
0
 def test_reply(self):
     log_file_name = 'test_reply.log'
     baseplate = Lego.start(None, threading.Lock())
     baseplate_proxy = baseplate.proxy()
     baseplate_proxy.add_child(ReplyTestingPingLego, log_file_name)
     baseplate_proxy.add_child(ReplyTestingPongLego)
     baseplate.tell(Message('0', Metadata(None).__dict__, True).__dict__)
     time.sleep(1)
     children = baseplate_proxy.children.get()
     for child in children:
         child.stop()
     baseplate.stop()
     with open(log_file_name, mode='r') as f:
         log = json.loads(f.read())
     os.remove(log_file_name)
     assert log['text'] == '4'  # nosec
Esempio n. 7
0
 def test_reply(self):
     log_file_name = 'test_reply.log'
     baseplate = Lego.start(None, threading.Lock())
     baseplate_proxy = baseplate.proxy()
     baseplate_proxy.add_child(ReplyTestingPingLego, log_file_name)
     baseplate_proxy.add_child(ReplyTestingPongLego)
     baseplate.tell(Message('0', Metadata(None).__dict__, True).__dict__)
     time.sleep(1)
     children = baseplate_proxy.children.get()
     for child in children:
         child.stop()
     baseplate.stop()
     with open(log_file_name, mode='r') as f:
         log = json.loads(f.read())
     os.remove(log_file_name)
     assert log['text'] == '4'  # nosec
Esempio n. 8
0
from Legobot.Legos.Help import Help

config = configparser.ConfigParser()
config.read('config.ini')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(ch)

# Initialize lock and baseplate
lock = threading.Lock()
baseplate = Lego.start(None, lock)
baseplate_proxy = baseplate.proxy()
# Add children
baseplate_proxy.add_child(IRC,
                          channels=[channel.strip() for channel in config.get(
                              "irc1", "channel").split(",")],
                          nickname=config['irc1']['username'],
                          server=config['irc1']['host'],
                          port=int(config['irc1']['port']),
                          use_ssl=config.getboolean('irc1', 'ssl'),
                          username=config['irc1']['username'],
                          password=config['irc1']['password'])
baseplate_proxy.add_child(Help)
Esempio n. 9
0
config = configparser.ConfigParser()
config.read('config.ini.example')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(ch)

# Initialize lock and baseplate
lock = threading.Lock()
baseplate = Lego.start(None, lock)
baseplate_proxy = baseplate.proxy()
# Add children
baseplate_proxy.add_child(
    IRC,
    channels=[
        channel.strip() for channel in config.get("irc1", "channel").split(",")
    ],
    nickname=config['irc1']['username'],
    server=config['irc1']['host'],
    port=int(config['irc1']['port']),
    use_ssl=config.getboolean('irc1', 'ssl'),
    username=config['irc1']['username'],
    password=config['irc1']['password'])
baseplate_proxy.add_child(Help)
Esempio n. 10
0
import sys
import threading

from Legobot.Lego import Lego
from mock import patch

LOCAL_PATH = os.path.join(
    os.path.abspath(os.path.dirname(__file__)),
    '..',
    'Local'
)
sys.path.append(LOCAL_PATH)
from shakespeare import Shakespeare  # noqa: E402

LOCK = threading.Lock()
BASEPLATE = Lego.start(None, LOCK)
LEGO = Shakespeare(BASEPLATE, LOCK)


def test_listening_for():
    assert LEGO.listening_for({'text': 'shake'}) is False
    assert LEGO.listening_for({'text': '!shake moin'}) is True
    assert LEGO.listening_for({'text': '!shake'}) is True
    assert LEGO.listening_for({'text': '!Shake'}) is False


def test_get_quote():
    default = 'Not so much brain as ear wax.'
    assert LEGO._get_quote('') == default
    assert LEGO._get_quote('bad search') == default
    assert LEGO._get_quote('scurvy') == '11. I scorn you, scurvy companion.'
Esempio n. 11
0
 def initialize_baseplate(self):
     lock = threading.Lock()
     self.baseplate = Lego.start(None, lock)
     self.baseplate_proxy = self.baseplate.proxy()
Esempio n. 12
0
def bot_init():
    global baseplate_proxy
    # Initialize lock and baseplate
    lock = threading.Lock()
    baseplate = Lego.start(None, lock)
    baseplate_proxy = baseplate.proxy()