Mercurial > kallithea
view scripts/logformat.py @ 7752:3929ff3f21c6
tests: introduce doctest_mock_ugettext to allow doctests of localized code
Future doctests will require some extra mocking, as the code-under-test uses
translation (ugettext aka '_') and its provider TurboGears2 needs a context.
Avoid this complexity by mocking ugettext as the identity function. This is
done by providing a pytest fixture 'doctest_mock_ugettext' that will mock
uggettext in the module that uses the fixture.
author | Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> |
---|---|
date | Tue, 11 Jun 2019 20:53:33 +0200 |
parents | 4473f1094d3d |
children | 0a277465fddf |
line wrap: on
line source
#!/usr/bin/env python2 import re import sys logre = r''' (log\.(?:error|info|warning|debug) [(][ \n]* ) %s ( [ \n]*[)] ) ''' res = [ # handle % () - keeping spaces around the old % (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) \( ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) \) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), # handle % without () - keeping spaces around the old % (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), # remove extra space if it is on next line (re.compile(logre % r'''("[^"]*"|'[^']*') , (\n [ ]) ([ ][\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), # remove extra space if it is on same line (re.compile(logre % r'''("[^"]*"|'[^']*') , [ ]+ () ( [\n ]+) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), # remove trailing , and space (re.compile(logre % r'''("[^"]*"|'[^']*') , () ( [\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* [^(), \n] ) [ ,]*''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), ] def rewrite(f): s = open(f).read() for r, t in res: s = r.sub(t, s) open(f, 'w').write(s) if __name__ == '__main__': if len(sys.argv) < 2: print 'Cleanup of superfluous % formatting of log statements.' print 'Usage:' print ''' hg revert `hg loc '*.py'|grep -v logformat.py` && scripts/logformat.py `hg loc '*.py'` && hg diff''' raise SystemExit(1) for f in sys.argv[1:]: rewrite(f)