XZise has uploaded a new change for review. https://gerrit.wikimedia.org/r/222907
Change subject: [FEAT] color_format to ignore valid colors ...................................................................... [FEAT] color_format to ignore valid colors This allows to add colors to strings which will be filled using format. Instead of calling 'str'.format(…), color_format('str', …) is called. That adds all valid colors as keyword arguments so that they are replaced by themselves. Change-Id: Ib354fb80180b0e421a18474cc67d7ee10cda3745 --- M pywikibot/bot.py M tests/bot_tests.py 2 files changed, 43 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/07/222907/1 diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 57e8ec0..f09dd18 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -49,6 +49,7 @@ ChoiceException, QuitKeyboardInterrupt, ) from pywikibot.tools import deprecated, deprecated_args, PY2, PYTHON_VERSION +from pywikibot.userinterfaces.terminal_interface_base import colors if not PY2: unicode = str @@ -84,6 +85,19 @@ def __init__(self, stop=False): """Constructor.""" self.stop = stop + + +def color_format(text, *args, **kwargs): + """Do C{str.format} without having to worry about colors.""" + valid_colors = set(colors) + if valid_colors.intersection(kwargs): # kwargs use colors + raise ValueError('Keyword argument(s) should not use valid color(s): ' + + '", "'.join(valid_colors.intersection(kwargs))) + else: + # Add all colors to kwargs to replace nothing + for color in valid_colors: + kwargs[color] = '{{{0}}}'.format(color) + return text.format(*args, **kwargs) # Logging module configuration @@ -930,17 +944,16 @@ '\03{default}' + text[rng[1]: rng[1] + self.context]) question = 'Should the link ' else: - question = 'Should the link \03{{lightred}}{0}\03{{default}} ' + question = 'Should the link \03{lightred}{0}\03{default} ' if self._new is False: question += 'be unlinked?' else: - question += ('target to ' - '\03{{{{lightpurple}}}}{0}\03{{{{default}}}}?').format( - self._new.canonical_title()) + question += color_format('target to \03{lightpurple}{0}\03{default}?', + self._new.canonical_title()) choice = pywikibot.input_choice( - question.format(self._old.canonical_title()), + color_format(question, self._old.canonical_title()), choices, default=self._default, automatic_quit=self._quit) return self.handle_answer(choice) diff --git a/tests/bot_tests.py b/tests/bot_tests.py index 4764e79..5227de4 100644 --- a/tests/bot_tests.py +++ b/tests/bot_tests.py @@ -19,6 +19,31 @@ ) +class ColorFormatTestCase(TestCase): + + """Test color_format function in bot module.""" + + net = False + + def test_no_colors(self): + """Test without colors in template string.""" + self.assertEqual(pywikibot.bot.color_format('42'), '42') + self.assertEqual(pywikibot.bot.color_format('{0}', 42), '42') + self.assertEqual(pywikibot.bot.color_format('{ans}', ans=42), '42') + + def test_colors(self): + """Test with colors in template string.""" + self.assertEqual(pywikibot.bot.color_format('{0}{black}', 42), + '42{black}') + self.assertEqual(pywikibot.bot.color_format('{ans}{black}', ans=42), + '42{black}') + + def test_color_kwargs(self): + """Test with a color as keyword argument.""" + self.assertRaises(ValueError, + pywikibot.bot.color_format, '{aqua}{black}', aqua=42) + + class FakeSaveBotTestCase(TestCase): """ -- To view, visit https://gerrit.wikimedia.org/r/222907 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib354fb80180b0e421a18474cc67d7ee10cda3745 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: XZise <commodorefabia...@gmx.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits