Example #1
0
    def test_container_get_commands_empty(self):

        reg = Registry()
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertFalse(len(commands) > 0)
Example #2
0
    def test_container_get_commands_empty(self):

        reg = Registry()
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertFalse(len(commands) > 0)
Example #3
0
    def test_container_get_commands(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertTrue(len(commands) > 0)
Example #4
0
    def test_container_get_commands(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertTrue(len(commands) > 0)
Example #5
0
    def test_container_error(self):

        # test if application not implement container abstract class
        with self.assertRaises(ContainerError) as container_error:
            reg = Registry()
            reg.register(FakeApp3())

        exception = container_error.exception
        self.assertEqual('FakeApp3', exception.app_name)
        self.assertEqual('ContainerError: Cannot use FakeApp3 as container application', exception.message)
Example #6
0
    def test_container_error(self):

        # test if application not implement container abstract class
        with self.assertRaises(ContainerError) as container_error:
            reg = Registry()
            reg.register(FakeApp3())

        exception = container_error.exception
        self.assertEqual('FakeApp3', exception.app_name)
        self.assertEqual(
            'ContainerError: Cannot use FakeApp3 as container application',
            exception.message)
Example #7
0
    def process_matches(self, params):
        val = super().process_matches(params)

        if val is None:
            return None
        else:
            character_service = Registry.get_instance("character_service")
            access_service = Registry.get_instance("access_service")
            char_id = character_service.resolve_char_to_id(val)
            if char_id is None:
                return SenderObj(char_id, val.capitalize(), None)
            else:
                return SenderObj(char_id, val.capitalize(), access_service.get_access_level(char_id))
Example #8
0
    def test_build_registry(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())
        reg.register(FakeApp4())

        # check if list of apps is not empty list
        self.assertIsNot(reg.get_apps(), [])
        self.assertEqual(3, len(reg.get_apps()))
        self.assertEqual(3, len(reg.get_routes()))
Example #9
0
    def start(self):
        access_levels = {}

        # process decorators
        for _, inst in Registry.get_all_instances().items():
            for name, method in get_attrs(inst).items():
                if hasattr(method, "command"):
                    cmd_name, params, access_level, description, help_file, sub_command, extended_description = getattr(
                        method, "command")
                    handler = getattr(inst, name)
                    help_text = self.get_help_file(inst.module_name, help_file)

                    command_key = self.get_command_key(
                        cmd_name.lower(),
                        sub_command.lower() if sub_command else "")
                    al = access_levels.get(command_key, None)
                    if al is not None and al != access_level.lower():
                        raise Exception(
                            "Different access levels specified for forms of command '%s'"
                            % command_key)
                    access_levels[command_key] = access_level

                    self.register(handler, cmd_name, params, access_level,
                                  description, inst.module_name, help_text,
                                  sub_command, extended_description)
Example #10
0
    def start(self):
        access_levels = {}

        # process decorators
        for _, inst in Registry.get_all_instances().items():
            for name, method in get_attrs(inst).items():
                if hasattr(method, "command"):
                    cmd_name, params, access_level, description, help_file, sub_command, extended_description = getattr(
                        method, "command")
                    handler = getattr(inst, name)
                    module = self.util.get_module_name(handler)
                    help_text = self.get_help_file(module, help_file)

                    # TODO move this check to register()
                    if len(inspect.signature(
                            handler).parameters) != len(params) + 1:
                        raise Exception(
                            "Incorrect number of arguments for handler '%s.%s()'"
                            % (handler.__module__, handler.__name__))

                    command_key = self.get_command_key(
                        cmd_name.lower(),
                        sub_command.lower() if sub_command else "")
                    al = access_levels.get(command_key, None)
                    if al is not None and al != access_level.lower():
                        raise Exception(
                            "Different access levels specified for forms of command '%s'"
                            % command_key)
                    access_levels[command_key] = access_level

                    self.register(handler, cmd_name, params, access_level,
                                  description, module, help_text, sub_command,
                                  extended_description)
Example #11
0
 def set_value(self, value):
     util = Registry.get_instance("util")
     time = util.parse_time(value)
     if time > 0:
         self.value = time
     else:
         raise Exception("You must enter time in a valid Budatime format")
Example #12
0
 def start(self):
     # process decorators
     for _, inst in Registry.get_all_instances().items():
         for name, method in get_attrs(inst).items():
             if hasattr(method, "setting"):
                 setting_name, value, description, extended_description, obj = getattr(method, "setting")
                 self.register(inst.module_name, setting_name, value, obj, description, extended_description)
Example #13
0
    def test_boolean_setting_type(self):
        Registry.add_instance("setting_service", MockSettingService())
        setting = BooleanSettingType()

        setting.set_value("true")
        self.assertTrue(setting.get_value())

        setting.set_value(True)
        self.assertTrue(setting.get_value())

        setting.set_value("false")
        self.assertFalse(setting.get_value())

        setting.set_value(False)
        self.assertFalse(setting.get_value())

        self.assertRaises(Exception, lambda: setting.set_value("test"))
    def process_matches(self, params):
        val = super().process_matches(params)

        if val is None:
            return None
        else:
            character_service = Registry.get_instance("character_service")
            return SenderObj(character_service.resolve_char_to_id(val), val.capitalize())
Example #15
0
 def get_item_image(self):
     if isinstance(self.item, DictObject) and self.item.icon:
         text = Registry.get_instance("text")
         return text.make_item(self.item.low_id,
                               self.item.high_id, self.item.ql,
                               text.make_image(self.item.icon))
     else:
         return None
Example #16
0
 def start(self):
     # process decorators
     for _, inst in Registry.get_all_instances().items():
         for name, method in get_attrs(inst).items():
             if hasattr(method, "event"):
                 attrs = getattr(method, "event")
                 handler = getattr(inst, name)
                 self.register(handler, attrs.event_type, attrs.description, inst.module_name, attrs.is_hidden, attrs.is_enabled)
Example #17
0
 def start(self):
     # process decorators
     for _, inst in Registry.get_all_instances().items():
         for name, method in get_attrs(inst).items():
             if hasattr(method, "event"):
                 event_type, description = getattr(method, "event")
                 handler = getattr(inst, name)
                 module = self.util.get_module_name(handler)
                 self.register(handler, event_type, description, module)
Example #18
0
def timerevent(handler,
               budatime,
               description,
               is_hidden=False,
               is_enabled=True):
    util = Registry.get_instance("util")
    t = util.parse_time(budatime)
    handler.event = ["timer:" + str(t), description, is_hidden, is_enabled]
    return handler
Example #19
0
 def start(self):
     # process decorators
     for _, inst in Registry.get_all_instances().items():
         for name, method in get_attrs(inst).items():
             if hasattr(method, "setting"):
                 setting_name, value, description, obj = getattr(method, "setting")
                 handler = getattr(inst, name)
                 module = self.util.get_module_name(handler)
                 self.register(setting_name, value, description, obj, module)
Example #20
0
    def test_build_registry(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())
        reg.register(FakeApp4())

        # check if list of apps is not empty list
        self.assertIsNot(reg.get_apps(), [])
        self.assertEqual(3, len(reg.get_apps()))
        self.assertEqual(3, len(reg.get_routes()))
Example #21
0
 def start(self):
     # process decorators
     for _, inst in Registry.get_all_instances().items():
         for name, method in get_attrs(inst).items():
             if hasattr(method, "command"):
                 cmd_name, params, access_level, description, help_file, sub_command = getattr(method, "command")
                 handler = getattr(inst, name)
                 module = self.util.get_module_name(handler)
                 help_text = self.get_help_file(module, help_file)
                 self.register(handler, cmd_name, params, access_level, description, module, help_text, sub_command)
Example #22
0
    def get_display(self):
        text = Registry.get_instance("text")
        options_str = "\n".join(map(lambda opt: text.make_chatcmd(str(opt), "/tell <myname> config setting %s set %s" % (self.name, opt)), self.options))

        return """For this setting you must enter a time value. See <a href='chatcmd:///tell <myname> help budatime'>budatime</a> for info on the format of the 'time' parameter.

To change this setting:

<highlight>/tell <myname> config setting """ + self.name + """ set <i>_time_</i><end>

Or choose an option below:\n\n""" + options_str
Example #23
0
    def process_matches(self, params):
        budatime_str = params.pop(0)
        params.pop(0)
        params.pop(0)
        params.pop(0)

        if budatime_str is None:
            return None
        else:
            util = Registry.get_instance("util")
            return util.parse_time(budatime_str[1:])
Example #24
0
 def get_item_str(self):
     if isinstance(self.item, DictObject):
         item_name = "%s (%s)" % (self.item.name, self.comment
                                  ) if self.comment else self.item.name
         text = Registry.get_instance("text")
         return text.make_item(self.item.low_id, self.item.high_id,
                               self.item.ql, item_name)
     else:
         item_name = "%s (%s)" % (
             self.item, self.comment) if self.comment else self.item
         return item_name
Example #25
0
    def get_display(self):
        text = Registry.get_instance("text")

        clear_str = ""
        if self.allow_empty:
            clear_str = "\n\nTo clear this setting:\n\n" + text.make_tellcmd("Clear this setting", "config setting %s clear" % self.name)

        return """For this setting you can enter any text you want (max. 255 characters).

To change this setting:

<highlight>/tell <myname> config setting """ + self.name + """ set <i>_value_</i></highlight>""" + clear_str + """
Example #26
0
def timerevent(handler, budatime, description, is_system=False, is_enabled=True, run_at_startup=False, is_hidden=False):
    if is_hidden:
        log_deprecated_is_hidden(handler)

    util = Registry.get_instance("util")
    t = util.parse_time(budatime)
    handler.event = DictObject({"event_type": "timer:" + str(t),
                                "description": description,
                                "is_system": is_system or is_hidden,
                                "is_enabled": is_enabled,
                                "run_at_startup": run_at_startup})
    return handler
Example #27
0
 def get_item_str(self):
     if isinstance(self.item, DictObject):
         item_name = "%s (%s)" % (self.item.name, self.comment
                                  ) if self.comment else self.item.name
         text = Registry.get_instance("text")
         return text.make_item(self.item.low_id, self.item.high_id,
                               self.item.ql,
                               item_name) + f" (QL {self.item.ql})"
     else:
         item_name = "<highlight>%s</highlight>" % self.item
         if self.comment:
             item_name += " (%s)" % self.comment
         return item_name
Example #28
0
    def get_display(self):
        text = Registry.get_instance("text")

        if self.options:
            options_str = "\n\nOr choose an option below:\n\n" + "\n".join(map(lambda opt: text.make_chatcmd(str(opt), "/tell <myname> config setting %s set %s" % (self.name, opt)), self.options))
        else:
            options_str = ""

        return """For this setting you can enter any text you want (max. 255 characters).

To change this setting:

<highlight>/tell <myname> config setting """ + self.name + """ set <i>_value_</i><end>""" + options_str
Example #29
0
class CommandProcessorFixture(unittest.TestCase):

    """ Command Processor tests """

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_handle_command(self):
        """ given that we have a handler registered for a command, when we send a command, it should call the handler"""
        self._handler = MyCommandHandler()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message")

    def test_handle_event(self):
        """ Given that we have many handlers registered of an event, when we raise an event, it should call all the handlers"""

        self._handler = MyEventHandler()
        self._other_handler = MyEventHandler()
        self._request = MyEvent()
        self._subscriber_registry.register(MyEvent, lambda: self._handler)
        self._subscriber_registry.register(MyEvent, lambda: self._other_handler)
        self._commandProcessor.publish(self._request)

        self.assertTrue(self._handler.called, "The first handler should be called with the message")
        self.assertTrue((self._other_handler, "The second handler should also be called with the message"))

    def test_missing_command_handler_registration(self):
        """Given that we are missing a handler for a command, when we send a command, it should throw an exception"""

        self._handler = MyCommandHandler()
        self._request = MyCommand()

        exception_thrown = False
        try:
            self._commandProcessor.send(self._request)
        except:
            exception_thrown = True

        self.assertTrue(exception_thrown, "Expected an exception to be thrown when no handler is registered for a command")

    def test_missing_event_handler_registration(self):
        """Given that we have no handlers register for an event, when we raise an event, it should not error """

        self._handler = MyEventHandler()
        self._other_handler = MyEventHandler()
        self._request = MyEvent()

        exception_thrown = False
        try:
            self._commandProcessor.publish(self._request)
        except:
            exception_thrown = True

        self.assertFalse(exception_thrown, "Did not expect an exception to be thrown where there are no handlers for an event")
Example #30
0
    def get_display(self):
        text = Registry.get_instance("text")
        options_str = "\n".join(
            map(
                lambda opt: text.make_chatcmd(
                    str(opt), "/tell <myname> config setting %s set %s" %
                    (self.name, opt)), self.options))

        return """For this setting you can set any positive integer.
To change this setting:

<highlight>/tell <myname> config setting """ + self.name + """ set <i>_number_</i><end>

Or choose an option below:\n\n""" + options_str
Example #31
0
    def post_start(self):
        # process decorators
        for _, inst in Registry.get_all_instances().items():
            for name, method in inst.__class__.__dict__.items():
                if hasattr(method, "event"):
                    event_type, description = getattr(method, "event")
                    handler = getattr(inst, name)
                    module = self.util.get_module_name(handler)
                    self.register(handler, event_type, description, module)

        # remove events that are no longer registered
        self.db.exec("DELETE FROM event_config WHERE verified = 0")

        self.db.exec(
            "DELETE FROM timer_event WHERE handler NOT IN (SELECT handler FROM event_config WHERE event_type = ?)",
            ["timer"])
Example #32
0
    def get_display(self):
        text = Registry.get_instance("text")

        clear_str = ""
        if self.allow_empty:
            clear_str = "\n\nTo clear this setting:\n\n" + text.make_tellcmd("Clear this setting", "config setting %s clear" % self.name)

        options_str = ""
        if self.options:
            options_str = "\n\nOr choose an option below:\n\n" + "\n".join(map(lambda opt: text.make_tellcmd(str(opt), "config setting %s set %s" % (self.name, opt)), self.options))

        return """For this setting you can enter any text you want (max. 255 characters).

To change this setting:

<highlight>/tell <myname> config setting """ + self.name + """ set <i>_value_</i></highlight>""" + clear_str + options_str
Example #33
0
    def get_display(self):
        text = Registry.get_instance("text")
        if self.options:
            options_str = "\n\nOr choose an option below:\n\n" + "\n".join(map(lambda opt: text.make_chatcmd(str(opt), "/tell <myname> config setting %s set %s" % (self.name, opt)), self.options))
        else:
            options_str = ""

        if self.allow_empty:
            clear_str = "\n\nOr: " + text.make_chatcmd("Clear this setting", "/tell <myname> config setting %s clear" % self.name)
        else:
            clear_str = ""

        return """For this setting you can set any positive integer.

To change this setting:

<highlight>/tell <myname> config setting """ + self.name + """ set <i>_number_</i><end>""" + clear_str + options_str
Example #34
0
    def post_start(self):
        # process decorators
        for _, inst in Registry.get_all_instances().items():
            for name, method in self.get_attrs(inst).items():
                if hasattr(method, "command"):
                    cmd_name, params, access_level, description, help_file, sub_command = getattr(
                        method, "command")
                    handler = getattr(inst, name)
                    module = self.util.get_module_name(handler)
                    help_text = self.get_help_file(module, help_file)
                    self.register(handler, cmd_name, params, access_level,
                                  description, module, help_text, sub_command)

        # process deferred register calls
        for args in self.deferred_register:
            self.do_register(**args)

        self.db.exec("DELETE FROM command_config WHERE verified = 0")
 def setUp(self):
     self._subscriber_registry = Registry()
     self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)
class PipelineTests(unittest.TestCase):

    """Tests of decorators on handlers.
        It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python
        already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators
        used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over
        rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable
        to Pythoh via its decorator syntax
        This test suite is really about proving we can provide equivalent functionality
        We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter
    """

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_handle_retry_on_command(self):
        """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """
        self._handler = MyHandlerSupportingRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")

    def test_exceed_retry_on_command(self):
        """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries"""
        self._handler = MyHandlerBreakingAfterRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        self.assertTrue(exception_raised, "Exepcted the exception to bubble out, when we run out of retries")
        self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")

    def test_handle_circuit_breaker_on_command(self):
        """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries"""
        self._handler = MyHandlerBreakingCircuitAfterThreeFailures()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        # Now see if the circuit is broken following the failed call
        circuit_broken = False
        try:
            self._commandProcessor.send(self._request)
        except CircuitBrokenError:
            circuit_broken = True

        self.assertTrue(exception_raised, "Exepcted an exception to be raised, when we run out of retries")
        self.assertTrue(circuit_broken, "Expected the circuit to be broken to further calls")
        self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")