Exemplo n.º 1
0
class Main(object):

    def __init__(self):
        self._logging = logging.getLogger(self.__class__.__name__)
        self._stop = False

        self._setup_logger()
        self._setup_console()

    def start(self):
        self._logging.info('* Started.')

        self._console.start()

        while not self._stop:
            time.sleep(0.5)

        self._logging.info('* Ended.')

    def stop(self):
        self._logging.info('* Stopping...')
        self._console.stop()
        self._stop = True

    def _process_command(self, command):
        command = command.strip().lower()
        if command == 'exit':
            self._logging.info('* Exit request received.')
            self.stop()

    def _on_console_message(self, message):
        self._logging.info('* Received from console: {}'.format(message))
        self._process_command(message)

    @staticmethod
    def _setup_logger():
        log_file = '{}.log'.format(os.path.splitext(os.path.basename(__file__))[0])
        Logger.setup(log_path=log_file)

    def _setup_console(self):
        self._console = Console()
        self._console.register(self._on_console_message)
Exemplo n.º 2
0
 def _setup_console(self):
     self._console = Console()
     self._console.register(self._on_console_message)