jenkins-bot has submitted this change and it was merged.
Change subject: [color] use pywikibot.tools.formatter.color_format
......................................................................
[color] use pywikibot.tools.formatter.color_format
Introduce a format variant {color} to be able to assign
a color argument to the format string to be more flexible.
Some tests added.
Change-Id: I203e58e0d4fb5bd7fc8702485beae3f5c2e562e2
---
M pywikibot/diff.py
M pywikibot/tools/formatter.py
M scripts/welcome.py
M tests/tools_formatter_tests.py
4 files changed, 45 insertions(+), 19 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
Xqt: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/pywikibot/diff.py b/pywikibot/diff.py
index 1a91ef5..299c65a 100644
--- a/pywikibot/diff.py
+++ b/pywikibot/diff.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Diff module."""
#
-# (C) Pywikibot team, 2014
+# (C) Pywikibot team, 2014-2015
#
# Distributed under the terms of the MIT license.
#
@@ -30,6 +30,7 @@
from pywikibot.backports import format_range_unified # introduced in 2.7.2
from pywikibot.tools import deprecated_args
+from pywikibot.tools.formatter import color_format
class Hunk(object):
@@ -147,7 +148,8 @@
if line_ref is None:
if color in self.colors:
- colored_line = '\03{%s}%s\03{default}' % (self.colors[color],
line)
+ colored_line = color_format('{color}{0}{default}',
+ line, color=self.colors[color])
return colored_line
else:
return line
@@ -158,16 +160,17 @@
char_tagged = char
if color_closed:
if char_ref != ' ':
- char_tagged = '\03{%s}%s' % (self.colors[color], char)
+ char_tagged = color_format('{color}{0}',
+ char, self.colors[color])
color_closed = False
else:
if char_ref == ' ':
- char_tagged = '\03{default}%s' % char
+ char_tagged = color_format('{default}{0}', char)
color_closed = True
colored_line += char_tagged
if not color_closed:
- colored_line += '\03{default}'
+ colored_line += color_format('{default}')
return colored_line
@@ -351,9 +354,10 @@
context_range = self._get_context_range(hunks)
- output = ('\03{aqua}' +
- Hunk.get_header_text(*context_range) + '\03{default}\n' +
- extend_context(context_range[0][0], hunks[0].a_rng[0]))
+ output = color_format('{aqua}{0}{default}\n{1}',
+ Hunk.get_header_text(*context_range),
+ extend_context(context_range[0][0],
+ hunks[0].a_rng[0]))
previous_hunk = None
for hunk in hunks:
if previous_hunk:
@@ -498,10 +502,10 @@
super_hunks[position + 1:])
pywikibot.output('Split into {0}
hunks'.format(len(super_hunk._hunks)))
elif choice == '?':
- pywikibot.output(
- '\03{purple}%s\03{default}' % '\n'.join(
+ pywikibot.output(color_format(
+ '{purple}{0}{default}', '\n'.join(
'{0} -> {1}'.format(answer, help_msg[answer])
- for answer in answers))
+ for answer in answers)))
else:
assert False, '%s is not a valid option' % choice
@@ -539,22 +543,24 @@
by_letter: if text_a and text_b are single lines, comparison can be done
"""
+ FORMAT = '{2}{lightpurple}{0:{1}^50}{default}{2}'
+
patch = PatchManager(oldtext, newtext, n=n, by_letter=by_letter)
- pywikibot.output('\03{{lightpurple}}\n{0:*^50}\03{{default}}\n'.format('
ALL CHANGES '))
+ pywikibot.output(color_format(FORMAT, ' ALL CHANGES ', '*',
'\n'))
for hunk in patch.hunks:
pywikibot.output(hunk.diff_text)
- pywikibot.output('\03{{lightpurple}}\n{0:*^50}\03{{default}}\n'.format('
REVIEW CHANGES '))
+ pywikibot.output(color_format(FORMAT, ' REVIEW CHANGES ', '*',
'\n'))
text_list = patch.apply()
- pywikibot.output('\03{{lightpurple}}\n{0:*^50}\03{{default}}\n'.format('
APPROVED CHANGES '))
+ pywikibot.output(color_format(FORMAT, ' APPROVED CHANGES ', '*',
'\n'))
if any(hunk.reviewed == hunk.APPR for hunk in patch.hunks):
for hunk in patch.hunks:
if hunk.reviewed == hunk.APPR:
pywikibot.output(hunk.diff_text)
else:
-
pywikibot.output('\03{{lightpurple}}{0:^50}\03{{default}}'.format('None.'))
+ pywikibot.output(color_format(FORMAT, 'None.', '', ''))
text = ''.join(text_list)
diff --git a/pywikibot/tools/formatter.py b/pywikibot/tools/formatter.py
index ae9c530..7697180 100644
--- a/pywikibot/tools/formatter.py
+++ b/pywikibot/tools/formatter.py
@@ -15,7 +15,7 @@
from string import Formatter
from pywikibot.logging import output
-from pywikibot.tools import UnicodeType
+from pywikibot.tools import PY2, UnicodeType
from pywikibot.userinterfaces.terminal_interface_base import colors
@@ -74,6 +74,13 @@
super(_ColorFormatter, self).__init__()
self._depth = len(inspect.stack())
+ def get_value(self, key, args, kwargs):
+ """Get value, filling in 'color' when it is a valid
color."""
+ if key == 'color' and kwargs['color'] in self.colors:
+ return '\03{{{0}}}'.format(kwargs[key])
+ else:
+ return super(_ColorFormatter, self).get_value(key, args, kwargs)
+
def parse(self, format_string):
"""Yield results similar to parse but skip
colors."""
previous_literal = ''
@@ -116,9 +123,11 @@
@rtype: unicode
"""
result = super(_ColorFormatter, self)._vformat(*args, **kwargs)
- if not isinstance(result, UnicodeType):
+ if PY2 and isinstance(result, str):
assert result == b''
result = '' # This is changing it into a unicode
+ elif not isinstance(result, UnicodeType):
+ result = UnicodeType(result)
return result
def vformat(self, format_string, args, kwargs):
@@ -150,6 +159,9 @@
It is automatically adding \03 in front of color fields so it's
unnecessary to add them manually. Any other \03 in the text is disallowed.
+ You may use a variant {color} by assigning a valid color to a named
+ parameter color.
+
@param text: The format template string
@type text: unicode
@return: The formatted string
diff --git a/scripts/welcome.py b/scripts/welcome.py
index 11224e7..45ac857 100755
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -195,6 +195,7 @@
from pywikibot import config
from pywikibot import i18n
+from pywikibot.tools.formatter import color_format
if sys.version_info[0] > 2:
unicode = str
@@ -882,8 +883,8 @@
4: 'Warning',
5: 'Done ',
}
- pywikibot.output("\03{%s}[%s]\03{default} "
- % (staColor[n], staMsg[n]), newline=False)
+ pywikibot.output(color_format('{color}[{0}]{default} ',
+ staMsg[n], color=staColor[n]), newline=False)
def load_word_function(raw):
diff --git a/tests/tools_formatter_tests.py b/tests/tools_formatter_tests.py
index bd3a2df..d6803f3 100644
--- a/tests/tools_formatter_tests.py
+++ b/tests/tools_formatter_tests.py
@@ -97,6 +97,13 @@
self.assertRaises(TypeError, formatter.color_format, b'{0}',
'a')
self.assertRaises(TypeError, formatter.color_format, b'{black}{0}',
'a')
+ def test_variant_colors(self):
+ """Test variant colors with {color} parameter."""
+ self.assert_format('{0}{color}', '42\03{black}', 42,
color='black')
+ self.assert_format('{ans}{color}', '42\03{black}', ans=42,
+ color='black')
+ self.assert_format('{color}', '42', color=42)
+
if __name__ == '__main__':
try:
--
To view, visit
https://gerrit.wikimedia.org/r/239689
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I203e58e0d4fb5bd7fc8702485beae3f5c2e562e2
Gerrit-PatchSet: 9
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>