Beispiel #1
0
    def parse(self, msg):
        """
        Parse a message, return relevant output.

        @param msg: unparsed message
        @type msg: str
        @return: message to print or None (if there's no output to print)
        @rtype: str|None
        """

        # split message into first word + rest of line
        try:
            command, args  = msg.split(None, 1)
        except ValueError:
            command, args = msg, ""

        if command in self.COMMANDS:
            stat = self.COMMANDS[command](args)
            if state.is_done(stat):
                return stat.value
            elif state.is_next(stat):
                print(stat.value)
                return None
            else:
                return None

        for func in self.FILTERS:
            stat = func(msg)

            if state.is_done(stat):
                return stat.value
            elif state.is_replace(stat):
                msg = stat.value

        return msg
Beispiel #2
0
	def parse(self, msg):
		"""
		Parses given input.

		@param msg: input message to parse
		@type msg: str
		@return: return message processed with associated commands and filters
		@rtype: str
		"""
		try:
			cmd, args = msg.split(" ", 1)
		except ValueError:
			cmd, args = msg.rstrip(), ""

		if cmd in self.COMMANDS:
			ret = self.COMMANDS[cmd](args)

			if state.is_done(ret):
				return ret.value
			elif state.is_next(ret):
				pass
			else:
				raise Exception("Illegal state of command.")

		for filter in self.FILTERS:
			ret = filter(msg)

			if state.is_done(ret):
				return ret.value

			if state.is_replace(ret):
				msg = ret.value

		return msg
Beispiel #3
0
 def test_none(self):
     self.assertTrue(state.is_next(plugins.calculator.cmd_calc(None)))
Beispiel #4
0
 def test_incorrect_parenthesis(self):
     self.assertTrue(state.is_next(plugins.calculator.cmd_calc("2 * ((1 + 2)")))
     self.assertEqual(plugins.calculator.cmd_calc("2 * ((1 + 2)").value, "2 * ((1 + 2)")
Beispiel #5
0
 def test_division_by_zero(self):
     self.assertTrue(state.is_next(plugins.calculator.cmd_calc("1 / 0")))
     self.assertEqual(plugins.calculator.cmd_calc("1 / 0").value, "1 / 0")
     self.assertTrue(state.is_next(plugins.calculator.cmd_calc("1.0 / 0")))
     self.assertEqual(plugins.calculator.cmd_calc("1.0 / 0").value, "1.0 / 0")
Beispiel #6
0
 def test_not_state(self):
     self.assertFalse(state.is_next(object()))
Beispiel #7
0
 def test_not_next(self):
     self.assertFalse(state.is_next(state.done(None)))
     self.assertFalse(state.is_next(state.replace(None)))
Beispiel #8
0
 def test_next(self):
     self.assertTrue(state.is_next(state.next(None)))
Beispiel #9
0
 def test_not_math(self):
     self.assertTrue(state.is_next(plugins.calculator.cmd_calc("foo +1")))
Beispiel #10
0
 def test_minus_minus(self):
     self.assertTrue(state.is_next(plugins.karma.f_karma("bar--")))
     self.assertEqual(plugins.karma.KARMA["bar"], -1)
Beispiel #11
0
 def test_plus_plus(self):
     self.assertTrue(state.is_next(plugins.karma.f_karma("foo++")))
     self.assertEqual(plugins.karma.KARMA["foo"], 1)
Beispiel #12
0
 def test_triple(self):
     old_count = plugins.wordcount.WORDCOUNT
     self.assertTrue(state.is_next(plugins.wordcount.f_word_count("foo bar baz")))
     self.assertEqual(plugins.wordcount.WORDCOUNT, old_count + 3)
Beispiel #13
0
 def test_single(self):
     old_count = plugins.wordcount.WORDCOUNT
     self.assertTrue(state.is_next(plugins.wordcount.f_word_count("foo")))
     self.assertEqual(plugins.wordcount.WORDCOUNT, old_count + 1)
Beispiel #14
0
	def test_plus_plus(self):
		self.assertTrue(state.is_next(plugins.karma.f_karma('foo++')))
		self.assertEqual(plugins.karma.KARMA['foo'], 1)
Beispiel #15
0
	def test_double(self):
		old_count = plugins.wordcount.WORDCOUNT
		self.assertTrue(state.is_next(plugins.wordcount.f_word_count('foo bar')))
		self.assertEqual(plugins.wordcount.WORDCOUNT, old_count + 2)