platypush/platypush/__init__.py

133 lines
4 KiB
Python
Raw Normal View History

import argparse
2017-10-29 05:17:42 +01:00
import logging
import sys
import traceback
2017-10-29 05:17:42 +01:00
from threading import Thread
2017-12-18 01:10:51 +01:00
from .bus import Bus
from .config import Config
from .event.processor import EventProcessor
from .utils import get_or_load_plugin, init_backends, get_module_and_name_from_action
from .message.event import Event, StopEvent
from .message.request import Request
from .message.response import Response
2017-10-29 05:17:42 +01:00
__author__ = 'Fabio Manganiello <blacklight86@gmail.com>'
__version__ = '0.5'
2017-11-03 01:56:27 +01:00
#-----------#
class Daemon(object):
""" Main class for the Platypush daemon """
""" Configuration file (default: either ~/.config/platypush/config.yaml or
/etc/platypush/config.yaml) """
config_file = None
""" Application bus. It's an internal queue where:
- backends will post the messages they receive
- plugins will post the responses they process """
bus = None
""" backend_name => backend_obj map """
backends = None
""" number of executions retries before a request fails """
n_tries = 2
2017-12-22 02:11:56 +01:00
def __init__(self, config_file=None, requests_to_process=None):
""" Constructor
Params:
config_file -- Configuration file override (default: None)
2017-12-22 02:11:56 +01:00
requests_to_process -- Exit after processing the specified number
of requests (default: None, loop forever)
"""
self.config_file = config_file
self.event_processor = EventProcessor()
2017-12-22 02:11:56 +01:00
self.requests_to_process = requests_to_process
self.processed_requests = 0
Config.init(self.config_file)
logging.basicConfig(level=Config.get('logging'), stream=sys.stdout)
@classmethod
def build_from_cmdline(cls, args):
""" Build the app from command line arguments.
Params:
args -- Your sys.argv[1:] [List of strings]
"""
parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c', dest='config', required=False,
default=None, help=cls.config_file.__doc__)
opts, args = parser.parse_known_args(args)
return cls(config_file=opts.config)
def on_message(self):
""" Default message handler """
def _f(msg):
""" on_message closure
Params:
msg -- platypush.message.Message instance """
if isinstance(msg, Request):
logging.info('Processing request: {}'.format(msg))
2017-12-22 18:11:42 +01:00
msg.execute(n_tries=self.n_tries)
self.processed_requests += 1
if self.requests_to_process \
and self.processed_requests >= self.requests_to_process:
self.stop_app()
elif isinstance(msg, Response):
logging.info('Received response: {}'.format(msg))
elif isinstance(msg, StopEvent) and msg.targets_me():
logging.info('Received STOP event: {}'.format(msg))
self.stop_app()
elif isinstance(msg, Event):
logging.info('Received event: {}'.format(msg))
self.event_processor.process_event(msg)
return _f
2017-12-22 02:11:56 +01:00
def stop_app(self):
""" Stops the backends and the bus """
2017-12-22 02:11:56 +01:00
for backend in self.backends.values():
backend.stop()
self.bus.stop()
def start(self):
""" Start the daemon """
self.bus = Bus(on_message=self.on_message())
# Initialize the backends and link them to the bus
self.backends = init_backends(bus=self.bus)
# Start the backend threads
for backend in self.backends.values():
backend.start()
# Poll for messages on the bus
try:
self.bus.poll()
except KeyboardInterrupt as e:
logging.info('SIGINT received, terminating application')
2017-12-22 02:11:56 +01:00
finally:
self.stop_app()
def main(args=sys.argv[1:]):
print('Starting platypush v.{}'.format(__version__))
app = Daemon.build_from_cmdline(args)
app.start()
2017-10-29 05:17:42 +01:00
if __name__ == '__main__':
main()
2017-10-29 20:45:19 +01:00
# vim:sw=4:ts=4:et: