Commit 08996bc8 authored by Michal Čihař's avatar Michal Čihař

More consistent objet model of fixes with checks

- each class is instantionated on load
- they get all data as parameters
- better naming of fixup methods
- see issue #257
parent 1b01f914
......@@ -8,14 +8,14 @@ a sortable data object so fixes are applied in desired order.
'''
autofixes = []
for path in appsettings.AUTOFIX_LIST:
autofixes.append(load_class(path))
autofixes.append(load_class(path)())
def fix_target(target, unit):
'''
Apply each autofix to the target translation.
'''
for Fix in autofixes:
fix = Fix(target, unit)
target = fix.new_target()
for fix in autofixes:
target = fix.fix_target(target, unit)
return target
......@@ -5,22 +5,14 @@ class AutoFix(object):
'''
basic class for AutoFixes
'''
def __init__(self, target, unit):
'''
may need to extend this to support multiple targets and sources
'''
self.unit = unit
self.target = target
self.source = unit.source
def fix_target(self, single_target):
def fix_single_target(self, target, unit):
'''
fix a single target, implement this method with subclasses
'''
raise NotImplementedError()
def new_target(self):
def fix_target(self, target, unit):
'''
returns a target translation array with a single fix applied
'''
return [self.fix_target(t) for t in self.target]
return [self.fix_single_target(t, unit) for t in target]
......@@ -9,8 +9,8 @@ class ReplaceTrailingDotsWithEllipsis(AutoFix):
Replace Trailing Dots with an Ellipsis.
Ignore and maintain exisiting trailing whitespace
'''
def fix_target(self, target):
source_match = re.compile(u'…(\s*)$').search(self.source)
def fix_single_target(self, target, unit):
source_match = re.compile(u'…(\s*)$').search(unit.get_source_plurals()[0])
re_dots = re.compile(u'\.\.\.(\s*)$')
target_match = re_dots.search(target)
if source_match and target_match:
......
......@@ -5,14 +5,13 @@ from base import AutoFix
class SameBookendingWhitespace(AutoFix):
'''
Help non-techy translators with their whitespace
'''
def bookend_whitespace(self, target):
'''
Help non-techy translators with their whitespace
'''
def fix_single_target(self, target, unit):
# normalize newlines of source
source = re.compile(r'\r\n|\r|\n').sub('\n', self.source)
source = re.compile(r'\r\n|\r|\n').sub('\n', unit.get_source_plurals()[0])
#capture preceding and tailing whitespace
start = re.compile(r'^(\s+)').search(source)
......@@ -25,6 +24,3 @@ class SameBookendingWhitespace(AutoFix):
if stripped:
target = '%s%s%s' % (head, stripped, tail)
return target
def fix_target(self, single_target):
return self.bookend_whitespace(single_target)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment