예제 #1
0
    def setUp(self):

        self.positional = [flags.Color.Red | flags.Fill.Blue, flags.Style.Invert, flags.Style.Blink]

        self.codes = [flags.codify(i) for i in self.positional]

        self.always = {"always": flags.Color.White}

        self.always_code = flags.codify(self.always["always"])

        self.parser = parser.Parser(self.positional, self.always)
예제 #2
0
	def test_codifies_single_flag(self):

		self.assertEqual(flags.codify(flags.Style.Reset),
						 str(flags.Style.Reset))

		self.assertEqual(flags.codify(flags.Style.Blink),
						 str(flags.Style.Blink))

		self.assertEqual(flags.codify(flags.Color.Red),
						 str(flags.Color.Red))

		self.assertEqual(flags.codify(flags.Fill.White),
						 str(flags.Fill.White))
예제 #3
0
    def setUp(self):

        self.positional = [
            flags.Color.Red | flags.Fill.Blue, flags.Style.Invert,
            flags.Style.Blink
        ]

        self.codes = [flags.codify(i) for i in self.positional]

        self.always = {"always": flags.Color.White}

        self.always_code = flags.codify(self.always["always"])

        self.parser = parser.Parser(self.positional, self.always)
예제 #4
0
	def test_codifies_flag_combination(self):

		combination = flags.Style.Underline | flags.Style.Blink

		expected = "{0};{1}".format(flags.Style.Underline,
				 				  flags.Style.Blink)

		self.assertEqual(flags.codify(combination), expected)

		combination = flags.Color.Red | flags.Fill.Black

		expected = "{0};{1}".format(flags.Color.Red,
								  flags.Fill.Black)

		self.assertEqual(flags.codify(combination), expected)
예제 #5
0
    def test_stringify_combines_always_with_argument_phrase_correctly(self):

        result = self.parser.beautify("<(0)always>")

        combined = flags.codify(self.always["always"] | self.positional[0])

        expected = "\033[{0}malways\033[0;m".format(combined)

        self.assertEqual(result, expected)
예제 #6
0
    def test_stringify_combines_always_with_argument_phrase_correctly(self):

        result = self.parser.beautify("<(0)always>")

        combined = flags.codify(self.always["always"] | self.positional[0])

        expected = "\033[{0}malways\033[0;m".format(combined)

        self.assertEqual(result, expected)
예제 #7
0
    def test_stringify_increments_simple_phrase_correctly(self):

        result = self.parser.beautify("<(+)always> <next>")

        combined = flags.codify(self.always["always"] | self.positional[0])

        expected = "\033[{0}malways\033[0;m ".format(combined)
        expected += "\033[{0}mnext\033[0;m".format(self.codes[1])

        self.assertEqual(result, expected)
예제 #8
0
    def test_stringify_increments_simple_phrase_correctly(self):

        result = self.parser.beautify("<(+)always> <next>")

        combined = flags.codify(self.always["always"] | self.positional[0])

        expected = "\033[{0}malways\033[0;m ".format(combined)
        expected += "\033[{0}mnext\033[0;m".format(self.codes[1])

        self.assertEqual(result, expected)
예제 #9
0
	def stringify(self, string, phrases, parent=None):

		"""
		Stringifies phrases.

		After parsing of the string via self.parse(), this method takes the
		escaped string and the list of phrases returned by self.parse() and
		replaces the original phrases (with tags) with the Phrase-objects in
		the list and adds the appropriate flag-combinations as determined by
		the string or the position of the phrase (the string if it's in
		self.always, i.e. an 'always' argument). This method also works
		recursively to handle nested phrases (and resetting of parent-phrase
		styles).

		Arguments:
			string (str): The escaped string returned by self.parse().
			phrases (list): The list of Phrase-objects returned by self.parse().
			parent (Phrase): For recursive calls, the current parent Phrase.

		Returns:
			The finished, beautifully beautified string.

		Raises:
			errors.ArgumentError: If more positional arguments are requested
								  than were supplied.
		"""

		last_tag = 0

		beauty = ""

		for phrase in phrases:

			beauty += string[last_tag : phrase.opening]

			if phrase.string in self.always and not phrase.override:
				phrase.style = self.always[phrase.string]

			if phrase.arguments:
				combination = 0
				for i in phrase.arguments:
					try:
						combination |= self.positional[i]
					except IndexError:
						raise errors.ArgumentError("Positional argument '{0}' "
							 					   "is out of range"
							 					   "!".format(i))

				phrase.style |= combination

			elif (phrase.string not in self.always or
				  phrase.increment or phrase.override):
				try:
					combination = self.positional[self.counter]

					if phrase.increment or not phrase.override:
						self.counter += 1
				except IndexError:
					self.raise_not_enough_arguments(phrase.string)

				phrase.style |= combination

			phrase.style = flags.codify(phrase.style)

			if phrase.nested:
				phrase.string = self.stringify(phrase.string,
											   phrase.nested,
											   phrase)

			# After a nested phrase is over, we reset the style to the
			# parent style, this gives the notion of nested styles.
			reset = parent.style if parent else ""

			# \033[ signifies the start of a command-line escape-sequence
			beauty += "\033[{0}m{1}\033[0;{2}m".format(phrase.style,
													   phrase,
													   reset)
			last_tag = phrase.closing + 1

		beauty += string[last_tag:]

		return beauty
예제 #10
0
    def stringify(self, string, phrases, parent=None):
        """
		Stringifies phrases.

		After parsing of the string via self.parse(), this method takes the
		escaped string and the list of phrases returned by self.parse() and
		replaces the original phrases (with tags) with the Phrase-objects in
		the list and adds the appropriate flag-combinations as determined by
		the string or the position of the phrase (the string if it's in
		self.always, i.e. an 'always' argument). This method also works
		recursively to handle nested phrases (and resetting of parent-phrase
		styles).

		Arguments:
			string (str): The escaped string returned by self.parse().
			phrases (list): The list of Phrase-objects returned by self.parse().
			parent (Phrase): For recursive calls, the current parent Phrase.

		Returns:
			The finished, beautifully beautified string.

		Raises:
			errors.ArgumentError: If more positional arguments are requested
								  than were supplied.
		"""

        last_tag = 0

        beauty = ""

        for phrase in phrases:

            beauty += string[last_tag:phrase.opening]

            if phrase.string in self.always and not phrase.override:
                phrase.style = self.always[phrase.string]

            if phrase.arguments:
                combination = 0
                for i in phrase.arguments:
                    try:
                        combination |= self.positional[i]
                    except IndexError:
                        raise errors.ArgumentError("Positional argument '{0}' "
                                                   "is out of range"
                                                   "!".format(i))

                phrase.style |= combination

            elif (phrase.string not in self.always or phrase.increment
                  or phrase.override):
                try:
                    combination = self.positional[self.counter]

                    if phrase.increment or not phrase.override:
                        self.counter += 1
                except IndexError:
                    self.raise_not_enough_arguments(phrase.string)

                phrase.style |= combination

            phrase.style = flags.codify(phrase.style)

            if phrase.nested:
                phrase.string = self.stringify(phrase.string, phrase.nested,
                                               phrase)

            # After a nested phrase is over, we reset the style to the
            # parent style, this gives the notion of nested styles.
            reset = parent.style if parent else ""

            # \033[ signifies the start of a command-line escape-sequence
            beauty += "\033[{0}m{1}\033[0;{2}m".format(phrase.style, phrase,
                                                       reset)
            last_tag = phrase.closing + 1

        beauty += string[last_tag:]

        return beauty