2020-03-29 15:26:43 +02:00
|
|
|
#!/usr/bin/env python
|
2020-03-29 20:00:27 +02:00
|
|
|
import logging
|
2020-03-29 16:33:18 +02:00
|
|
|
import random
|
|
|
|
import string
|
2020-03-29 20:00:27 +02:00
|
|
|
from communicator import Communicator
|
2020-03-29 15:26:43 +02:00
|
|
|
|
|
|
|
"""
|
2020-03-29 20:00:27 +02:00
|
|
|
Message sender component
|
2020-03-29 15:26:43 +02:00
|
|
|
"""
|
|
|
|
|
2020-03-29 20:00:27 +02:00
|
|
|
__author__ = "@kovacsbence"
|
2020-03-29 15:26:43 +02:00
|
|
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
|
|
|
__module_name__ = "messagesender"
|
|
|
|
__version__text__ = "1"
|
|
|
|
|
2020-03-29 20:00:27 +02:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-03-29 15:26:43 +02:00
|
|
|
class MessageSender:
|
|
|
|
"""
|
2020-04-06 20:48:36 +02:00
|
|
|
Component responsible for sending the messages. Requires an instance of :class:'communicator.Communicator'
|
2020-03-29 15:26:43 +02:00
|
|
|
"""
|
2020-03-29 16:33:18 +02:00
|
|
|
|
2020-03-29 20:00:27 +02:00
|
|
|
def __init__(self, communicator: Communicator):
|
2020-04-06 20:48:36 +02:00
|
|
|
"""Initializes the object.
|
|
|
|
:param: communicator: an instance of :class:'communicator.Communicator'
|
2020-03-29 15:26:43 +02:00
|
|
|
"""
|
2020-03-29 20:00:27 +02:00
|
|
|
self.communicator = communicator
|
2020-03-29 16:33:18 +02:00
|
|
|
|
2020-03-30 19:33:09 +02:00
|
|
|
def randomstring(self, stringlength: int) -> str:
|
2020-04-06 20:48:36 +02:00
|
|
|
"""
|
|
|
|
Generate a random string of fixed length
|
|
|
|
:param stringlength: the length of the string
|
|
|
|
:return: the generated string
|
|
|
|
"""
|
2020-03-29 16:33:18 +02:00
|
|
|
letters = string.ascii_lowercase
|
2020-03-30 19:33:09 +02:00
|
|
|
return ''.join(random.choice(letters) for i in range(stringlength))
|
2020-03-29 16:33:18 +02:00
|
|
|
|
2020-03-29 20:00:27 +02:00
|
|
|
def sendmessage(self, message: str = "") -> None:
|
2020-04-06 20:48:36 +02:00
|
|
|
"""Sends the given message.
|
|
|
|
If the message is omitted (empty), then a random message will be generated with length 23 (with
|
|
|
|
:func:'~messagesender.MessageSender.randomstring'. Calls :func:'~communicator.Communicator.sendmessage'
|
|
|
|
to send the message.
|
|
|
|
:param message: the message of type string that will be sent
|
|
|
|
:return: None
|
2020-03-29 15:26:43 +02:00
|
|
|
"""
|
2020-03-29 20:00:27 +02:00
|
|
|
if not message:
|
|
|
|
data = self.randomstring(32)
|
|
|
|
else:
|
|
|
|
data = message
|
|
|
|
self.communicator.sendmessage(data)
|