Exemplo n.º 1
0
def test_configure_parser_with_logging():
    p = configure_parser('foo', log_params=True)
    assert p.description == 'foo'
    with pytest.raises(SystemExit):
        p.parse_args(['--version'])
    with pytest.raises(SystemExit):
        p.parse_args(['-h'])
    c = p.parse_args(['--log-file', 'foo.log'])
    assert c.log_file == 'foo.log'
Exemplo n.º 2
0
def test_configure_parser():
    p = configure_parser('foo', log_params=False)
    assert p.description == 'foo'
    with pytest.raises(SystemExit):
        p.parse_args(['--version'])
    with pytest.raises(SystemExit):
        p.parse_args(['-h'])
    with pytest.raises(configargparse.ArgumentError):
        p.parse_args(['--log-file', 'foo.log'])
    c = p.parse_args([])
    assert c.configuration is None
Exemplo n.º 3
0
    def __call__(self, args=None):
        parser = terminal.configure_parser(__doc__, log_params=False)
        parser.add_argument(
            '--status-queue',
            metavar='ADDR',
            default=const.STATUS_QUEUE,
            help="The address of the queue used to report status to monitors "
            "(default: %(default)s)")
        parser.add_argument(
            '--control-queue',
            metavar='ADDR',
            default=const.CONTROL_QUEUE,
            dest='master_queue',
            help="The address of the queue a monitor can use to control the "
            "master (default: %(default)s)")
        parser.add_argument(
            '-r',
            '--rotate',
            metavar='DEGREES',
            default=0,
            type=int,
            help="The rotation of the HAT in degrees; must be 0 (the default) "
            "90, 180, or 270")
        try:
            config = parser.parse_args(args)
            config.control_queue = 'inproc://quit'
        except:  # pylint: disable=bare-except
            return terminal.error_handler(*sys.exc_info())

        with SenseHAT() as hat:
            hat.rotation = config.rotate
            ctx = transport.Context()
            quit_queue = ctx.socket(transport.PULL,
                                    protocol=protocols.task_control)
            quit_queue.bind(config.control_queue)
            try:
                stick = StickTask(config, hat)
                stick.start()
                screen = ScreenTask(config, hat)
                screen.start()
                msg, data = quit_queue.recv_msg()
                assert msg == 'QUIT'
                #signal.sigwait({signal.SIGINT, signal.SIGTERM})
            except KeyboardInterrupt:
                pass
            finally:
                screen.quit()
                screen.join()
                stick.quit()
                stick.join()
                ctx.close()
                hat.screen.fade_to(array(Color('black')))
Exemplo n.º 4
0
    def __call__(self, args=None):
        parser = terminal.configure_parser(__doc__, log_params=False)
        parser.add_argument(
            '--status-queue',
            metavar='ADDR',
            default=const.STATUS_QUEUE,
            help="The address of the queue used to report status to monitors "
            "(default: %(default)s)")
        parser.add_argument(
            '--control-queue',
            metavar='ADDR',
            default=const.CONTROL_QUEUE,
            help="The address of the queue a monitor can use to control the "
            "master (default: %(default)s)")
        try:
            config = parser.parse_args(args)
        except:  # pylint: disable=bare-except
            return terminal.error_handler(*sys.exc_info())

        ctx = transport.Context()
        self.status_queue = ctx.socket(transport.SUB,
                                       protocol=reversed(
                                           protocols.monitor_stats))
        self.status_queue.connect(config.status_queue)
        self.status_queue.subscribe('')
        sleep(1)
        self.ctrl_queue = ctx.socket(transport.PUSH,
                                     protocol=reversed(
                                         protocols.master_control))
        self.ctrl_queue.connect(config.control_queue)
        self.ctrl_queue.send_msg('HELLO')
        try:
            self.loop = widgets.MainLoop(self.build_ui(),
                                         self.palette,
                                         event_loop=widgets.ZMQEventLoop(),
                                         unhandled_input=self.unhandled_input)
            self.loop.event_loop.watch_queue(self.status_queue,
                                             self.status_message)
            widgets.connect_signal(self.slave_list, 'modified',
                                   self.list_modified)
            self.loop.event_loop.alarm(1, self.tick)
            self.loop.run()
        finally:
            ctx.close()
            sys.stdout.flush()
Exemplo n.º 5
0
def test_configure_parser_with_logging(tmpdir):
    p = configure_parser('foo', log_params=True)
    assert p.description == 'foo'
    with pytest.raises(SystemExit):
        p.parse_args(['--version'])
    with pytest.raises(SystemExit):
        p.parse_args(['-h'])
    c = p.parse_args(['--log-file', str(tmpdir.join('/foo.log'))])
    assert c.log_level == logging.WARNING
    assert c.log_file == str(tmpdir.join('/foo.log'))
    mock_logger = logging.getLogger('mock')
    with mock.patch('logging.getLogger') as m:
        m.return_value = mock_logger
        configure_logging(c.log_level, c.log_file)
        assert len(m.return_value.handlers) == 2
        assert m.return_value.handlers[0] is _CONSOLE
        assert m.return_value.handlers[0].level == logging.WARNING
        assert isinstance(m.return_value.handlers[1], logging.FileHandler)
        assert m.return_value.handlers[1].baseFilename == str(
            tmpdir.join('/foo.log'))
        assert m.return_value.handlers[1].level == logging.INFO
        assert m.return_value.level == logging.INFO