def createConfiguration(directory: str, environment: str,
                         configuration: ConfigurationEnum):
     create_logger("ConfigurationFactory").debug(
         f'{directory}/{environment}.ini')
     parser = Parser(f'{directory}/{environment}.ini')
     parser.parse()
     _return = None
     if configuration == ConfigurationEnum.TelegramPusher:
         _return = impl.TelegramPusherConfiguration(parser)
     return _return
Exemple #2
0
 def __init__(self, parser: Parser) -> None:
     self.logger: Logger = create_logger("Configuration")
     self.logger.debug("in __init__")
     self._parser = parser
     self._queue = QueueConfiguration(parser, 'queue')
     self._telegram = TelegramConfiguration(parser, 'telegram')
     self._database = DatabaseConfiguration(parser, 'database')
 def __init__(self, host: str, port: int, queue_name: str) -> None:
     super().__init__()
     self.logger: Logger = create_logger("QueueChannel")
     self.logger.debug(f"in __init__({host}, {port}, {queue_name})")
     self._queue_name = queue_name
     self._connection = BlockingConnection(
         ConnectionParameters(host, port, connection_attempts=3, retry_delay=5))
     self._channel = self._connection.channel()
     self._consumer_tag: str = None
     self._callback: Callable[[dict], None] = None
Exemple #4
0
 def __init__(self, host: str, db: str, user: str, password: str) -> None:
     self.logger: Logger = create_logger("MessageRegistrar")
     self.logger.debug("in init")
     self._connection = pymysql.connect(
         host=host,
         db=db,
         user=user,
         password=password,
         charset='utf8mb4',
         cursorclass=pymysql.cursors.DictCursor)
     self._sql = "INSERT INTO `messages` (`message`, `time`) VALUES (\"%s\", now())"
 def __init__(self, queue_channel: interfaces.QueueChannel,
              message_sender: interfaces.MessageSender,
              message_registrar: interfaces.MessageRegistrar,
              queue_pusher: QueuePusher) -> None:
     self.logger: Logger = create_logger("QueueProcessor")
     self.logger.debug("in __init__")
     self._message_sender: interfaces.MessageSender = message_sender
     self._message_registrar: interfaces.MessageRegistrar = message_registrar
     self._queue_pusher: QueuePusher = queue_pusher
     self._channel: interfaces.QueueChannel = queue_channel
     self._channel.set_inner_message_processor(self.process_event)
     self._thread = Thread(target=lambda x: x._channel.start_consuming(),
                           args=(self, ))
 def __init__(self, queue_server: str, queue_port: int,
              queue_name: str) -> None:
     super().__init__()
     self.logger: Logger = create_logger("QueuePusher")
     self.logger.debug(
         f"in __init__({queue_server}, {queue_port}, {queue_name})")
     self._queue_name = queue_name
     self._connection = pika.BlockingConnection(
         pika.ConnectionParameters(queue_server,
                                   queue_port,
                                   connection_attempts=3,
                                   retry_delay=5))
     self._channel = self._connection.channel()
Exemple #7
0
def get_arguments() -> RunningParameters:
    logger: Logger = create_logger("ArgumentsParser")
    argumentParser = ArgumentParser(
        prog="Telegram pusher",
        usage="Provide all required parameters.",
        description="This is an entry point for a telegram pusher component which is the part of Telegram Bot",
        epilog="Ensure that your database has already set up a table to store not sent messages. Visit: https://goo.gl/yx7ryY"
    )
    argumentParser.add_argument('-d', '--directory', type=str, required=True)
    argumentParser.add_argument(
        '-e', '--environment', type=str, choices=['dev', 'prod'], required=True)
    logger.debug("Parse arguments")
    argumets = argumentParser.parse_args()
    runningParameters: RunningParameters = RunningParameters(
        argumets.directory, argumets.environment)
    return runningParameters
Exemple #8
0
    def __init__(self, directory_path, environment='dev'):
        super().__init__()
        self.logger: Logger = create_logger("RunningParameters")
        if not directory_path:
            self.logger.debug('Directory path can\'t be empty')
            raise Exception('Directory path can\'t be empty')

        if not environment or not environment in ['dev', 'prod']:
            self.logger.debug(
                'Environment has to be set to either \'dev\' or \'prod\'')
            raise Exception(
                'Environment has to be set to either \'dev\' or \'prod\'')

        self.logger.debug(
            f"Directory path: {directory_path}, environment: {environment}")
        self._directory_path = directory_path
        self._environment = environment
Exemple #9
0
def prepare_processor(parameters: RunningParameters) -> interfaces.QueueProcessor:
    logger: Logger = create_logger("PrepareProcessor")
    logger.debug("create configuration")
    configuration: Configuration = ConfigurationFactory.createConfiguration(
        parameters.configuration_directory,
        parameters.environment)
    logger.debug("create queue channel")
    queueChannel: QueueChannel = QueueChannel(
        configuration.queueServer,
        configuration.queuePort,
        configuration.messagesQueueName
    )
    queueChannel.declare_queue()  # TODO: maybe move it out of this method
    logger.debug("create message sender")
    messageSender: MessageSender = MessageSender(
        configuration.telegramProtocol,
        configuration.telegramHost,
        configuration.telegramPort,
        configuration.telegramBotKey
    )
    logger.debug("create message registrar")
    messageRegistrar: MessageRegistrar = MessageRegistrar(
        configuration.databaseHost,
        configuration.databaseName,
        configuration.databaseUser,
        configuration.databasePassword
    )
    logger.debug("create queue pusher")
    queuePusher: QueuePusher = QueuePusher(
        configuration.queueServer,
        configuration.queuePort,
        configuration.messagesQueueName
    )
    logger.debug("create queue processor")
    queueProcessor: QueueProcessor = QueueProcessor(
        queueChannel,
        messageSender,
        messageRegistrar,
        queuePusher
    )
    return queueProcessor
 def __init__(self, file_name: str) -> None:
     self.logger: Logger = create_logger("Parser")
     self.logger.debug(f"in __init__({file_name})")
     self._file_name: str = file_name
     self._parser: ConfigParser = ConfigParser()
Exemple #11
0
 def __init__(self):
     self.logger: Logger = create_logger('GracefulKiller')
     signal.signal(signal.SIGINT, self.exit_gracefully)
     signal.signal(signal.SIGTERM, self.exit_gracefully)
Exemple #12
0
        configuration.queuePort,
        configuration.messagesQueueName
    )
    logger.debug("create queue processor")
    queueProcessor: QueueProcessor = QueueProcessor(
        queueChannel,
        messageSender,
        messageRegistrar,
        queuePusher
    )
    return queueProcessor


if __name__ == '__main__':
    configure_logger("telegram-pusher")
    logger: Logger = create_logger("main")
    logger.info("Running in %s" % getcwd())
    try:
        parameters = get_arguments()
        queueProcessor: interfaces.QueueProcessor = prepare_processor(
            parameters)
        killer = GracefulKiller()
        logger.debug("start queue processor")
        queueProcessor.start()
    except Exception as ex:
        log_exception(ex)
        exit(-1)

    while True:
        time.sleep(1)
        logger.debug("Working...")
Exemple #13
0
 def __init__(self, parser: Parser, section: str) -> None:
     self.logger: Logger = create_logger("Configuration")
     self.logger.debug("in __init__")
     self._parser = parser
     self._section = section
 def __init__(self, protocol: str, host: str, port: int,
              bot_key: str) -> None:
     super().__init__()
     self.logger: Logger = create_logger("MessageSender")
     self._url: str = f"{protocol}://{host}:{port}/bot{bot_key}/"
     self.logger.debug("in __init__, url = %s" % self._url)