示例#1
0
 def test_initial_contexts(self):
     """Tests whether initial contexts are getting created"""
     cm = ContextManager()
     cm.init_io(Mock(), Mock()) #Implicitly creates initial contexts
     for context_alias, context in cm.contexts.items():
         assert(context_alias in cm.initial_contexts)
         assert(context)
示例#2
0
 def test_targetless_context_switching(self):
     """Tests that switching to a target-less context fails"""
     cm = ContextManager()
     cm.init_io(Mock(), Mock())
     cm.switch_to_context(cm.fallback_context)
     cm.create_context("test1")
     assert(cm.current_context == cm.fallback_context)
     cm.switch_to_context("test1")
     assert(cm.current_context == cm.fallback_context)
示例#3
0
文件: main.py 项目: hramrach/ZPUI
def init():
    """Initialize input and output objects"""

    global input_processor, screen, cm, config, config_path
    config = None

    # Load config
    for config_path in config_paths:
        #Only try to load the config file if it's present
        #(unclutters the logs)
        if os.path.exists(config_path):
            try:
                logging.debug('Loading config from {}'.format(config_path))
                config = read_config(config_path)
            except:
                logging.exception(
                    'Failed to load config from {}'.format(config_path))
            else:
                logging.info(
                    'Successfully loaded config from {}'.format(config_path))
                break
    # After this loop, the config_path global should contain
    # path for config that successfully loaded

    if config is None:
        sys.exit('Failed to load any config files!')

    # Initialize output
    try:
        screen = output.init(config['output'])
    except:
        logging.exception('Failed to initialize the output object')
        logging.exception(traceback.format_exc())
        sys.exit(2)

    # Initialize the context manager
    cm = ContextManager()

    # Initialize input
    try:
        # Now we can show errors on the display
        input_processor = input.init(config["input"], cm)
    except:
        logging.exception('Failed to initialize the input object')
        logging.exception(traceback.format_exc())
        Printer(['Oops. :(', 'y u make mistake'], None, screen, 0)
        sys.exit(3)

    # Tying objects together
    if hasattr(screen, "set_backlight_callback"):
        screen.set_backlight_callback(input_processor)
    cm.init_io(input_processor, screen)
    cm.switch_to_context("main")
    i, o = cm.get_io_for_context("main")

    return i, o
示例#4
0
 def test_context_switching_on_context_finish(self):
     """Tests whether basic context switching works"""
     cm = ContextManager()
     cm.init_io(Mock(), Mock())
     cm.switch_to_context(cm.fallback_context)
     e1 = Event()
     c = cm.create_context("test1")
     cm.register_context_target("test1", e1.wait)
     cm.switch_to_context("test1")
     assert(cm.current_context == "test1")
     finished = Event()
     def new_signal_finished():
         c.event_cb(c.name, "finished")
         finished.set()
     with patch.object(c, 'signal_finished', side_effect=new_signal_finished) as p:
         e1.set()
         #Waiting for the thread to exit
         finished.wait()
     assert(cm.current_context == cm.fallback_context)
示例#5
0
def init():
    """Initialize input and output objects"""

    global input_processor, input_device_manager, screen, cm, config, config_path
    config, config_path = load_config()

    if config is None:
        sys.exit('Failed to load any config files!')

    # Initialize output
    try:
        screen = output.init(config['output'])
    except:
        logging.exception('Failed to initialize the output object')
        logging.exception(traceback.format_exc())
        sys.exit(2)

    # Initialize the context manager
    cm = ContextManager()
    # Initialize input
    try:
        # Now we can show errors on the display
        input_processor, input_device_manager = input.init(config["input"], cm)
    except:
        logging.exception('Failed to initialize the input object')
        logging.exception(traceback.format_exc())
        Printer(['Oops. :(', 'y u make mistake'], None, screen, 0)
        sys.exit(3)

    # Tying objects together
    if hasattr(screen, "set_backlight_callback"):
        screen.set_backlight_callback(input_processor)
    cm.init_io(input_processor, screen)
    c = cm.contexts["main"]
    c.register_action(ContextSwitchAction("switch_main_menu", None, menu_name="Main menu"))
    cm.switch_to_context("main")
    i, o = cm.get_io_for_context("main")

    return i, o
示例#6
0
 def test_basic_context_switching(self):
     """Tests whether basic context switching works"""
     cm = ContextManager()
     cm.initial_contexts = [cm.fallback_context, "test1", "test2"]
     cm.init_io(Mock(), Mock())
     assert(cm.current_context is None)
     cm.switch_to_context(cm.fallback_context)
     assert(cm.current_context == cm.fallback_context)
     e1 = Event()
     e2 = Event()
     cm.register_context_target("test1", e1.wait)
     cm.register_context_target("test2", e2.wait)
     cm.switch_to_context("test1")
     assert(cm.current_context == "test1")
     assert(cm.get_previous_context("test1") == cm.fallback_context)
     cm.switch_to_context("test2")
     assert(cm.current_context == "test2")
     assert(cm.get_previous_context("test2") == "test1")
     cm.switch_to_context("test1")
     assert(cm.current_context == "test1")
     assert(cm.get_previous_context("test1") == "test2")
     #Setting events so that threads exit
     e1.set()
     e2.set()
示例#7
0
 def test_failsafe_fallback_on_thread_fail(self):
     cm = ContextManager()
     cm.fallback_context = "m"
     cm.initial_contexts = ["m"]
     cm.init_io(Mock(), Mock())
     cm.switch_to_context(cm.fallback_context)
     c1 = cm.create_context("t1")
     c2 = cm.create_context("t2")
     e1 = Event()
     e2 = Event()
     cm.register_context_target("t1", e1.wait)
     cm.register_context_target("t2", e2.wait)
     cm.switch_to_context("t1")
     # Removing 
     c1.set_target(None)
     del c1.thread
     c1.signal_finished = lambda: True
     c2.set_target(None)
     # Again, switcing to the f****d up context
     cm.switch_to_context("t2")
     # Setting events so that threads exit
     e1.set()
     e2.set()
     assert(cm.current_context == cm.fallback_context)
示例#8
0
 def test_failsafe_fallback_on_io_fail(self):
     cm = ContextManager()
     cm.fallback_context = "m"
     cm.initial_contexts = ["m"]
     cm.init_io(Mock(), Mock())
     cm.switch_to_context(cm.fallback_context)
     c1 = cm.create_context("t1")
     c2 = cm.create_context("t2")
     e1 = Event()
     e2 = Event()
     cm.register_context_target("t1", e1.wait)
     cm.register_context_target("t2", e2.wait)
     cm.switch_to_context("t1")
     # F*****g things up - since context objects are app-accessible,
     # we can't really rely on them staying the same
     del c1.i
     c1.signal_finished = lambda: True
     del c2.i
     # Both current and new contexts are f****d up
     cm.switch_to_context("t2")
     # Setting events so that threads exit
     e1.set()
     e2.set()
     assert(cm.current_context == cm.fallback_context)
示例#9
0
 def test_constructor(self):
     """Tests constructor"""
     cm = ContextManager()
     self.assertIsNotNone(cm)
示例#10
0
 def __init__(self, size=(256, 256), components=4, samples=4):
     self.ctx = ContextManager.get_default_context()
     self.texture = self.ctx.texture(size, components=4)
     self.fbo1 = self.ctx.simple_framebuffer(size, samples=samples)
     self.fbo2 = self.ctx.framebuffer(self.texture)
     self.scope = self.ctx.scope(self.fbo1)