~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/translations/utilities/sanitize.py

  • Committer: Steve Kowalik
  • Date: 2011-08-07 04:05:52 UTC
  • mto: This revision was merged to the branch mainline in revision 13626.
  • Revision ID: stevenk@ubuntu.com-20110807040552-mwnxo0flmhvl35e8
Correct the notification based on review comments, and remove request{,ed}
from the function names, switching to create{,d}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    windows_style = u'\r\n'
25
25
    mac_style = u'\r'
26
26
    unix_style = u'\n'
27
 
    mixed_style = object()
28
27
 
29
28
    dot_char = u'\u2022'
30
29
 
45
44
            self.prefix = ''
46
45
            self.postfix = ''
47
46
        # Get the newline style that is used in the English Singular.
48
 
        self.newline_style = self._getNewlineStyle(english_singular)
 
47
        self.newline_style = self._getNewlineStyle(
 
48
            english_singular, 'Original')
49
49
 
50
 
    @classmethod
51
 
    def _getNewlineStyle(cls, text):
 
50
    def _getNewlineStyle(self, text, text_name):
52
51
        """Find out which newline style is used in text."""
 
52
        error_message = (
 
53
            "%s text (%r) mixes different newline markers." % (
 
54
                text_name, text))
53
55
        style = None
54
56
        # To avoid confusing the single-character newline styles for mac and
55
57
        # unix with the two-character windows one, remove the windows-style
56
58
        # newlines from the text and use that text to search for the other
57
59
        # two.
58
 
        stripped_text = text.replace(cls.windows_style, u'')
 
60
        stripped_text = text.replace(self.windows_style, u'')
59
61
        if text != stripped_text:
60
62
            # Text contains windows style new lines.
61
 
            style = cls.windows_style
 
63
            style = self.windows_style
62
64
 
63
 
        for one_char_style in (cls.mac_style, cls.unix_style):
 
65
        for one_char_style in (self.mac_style, self.unix_style):
64
66
            if one_char_style in stripped_text:
65
67
                if style is not None:
66
 
                    return cls.mixed_style
 
68
                    raise MixedNewlineMarkersError(error_message)
67
69
                style = one_char_style
68
70
 
69
71
        return style
133
135
 
134
136
    def normalizeNewlines(self, translation_text):
135
137
        """Return 'translation_text' with newlines sync with english_singular.
136
 
 
137
 
        Raises an exception if the text has mixed newline styles.
138
138
        """
139
139
        if self.newline_style is None:
140
140
            # No newlines in the English singular, so we have nothing to do.
141
141
            return translation_text
142
142
 
143
 
        # Get the style that is used in the given text.
144
 
        translation_newline_style = self._getNewlineStyle(translation_text)
145
 
 
146
 
        if translation_newline_style == self.mixed_style:
147
 
            # The translation has mixed newlines in it; that is not allowed.
148
 
            raise MixedNewlineMarkersError(
149
 
                "Translations text (%r) mixes different newline markers." %
150
 
                    translation_text)
 
143
        # Get the style that uses the given text.
 
144
        translation_newline_style = self._getNewlineStyle(
 
145
            translation_text, 'Translations')
151
146
 
152
147
        if translation_newline_style is None:
153
 
            # The translation text doesn't contain any newlines, so there is
154
 
            # nothing for us to do.
 
148
            # We don't need to do anything, the text is not changed.
155
149
            return translation_text
156
150
 
157
 
        if self.newline_style is self.mixed_style:
158
 
            # The original has mixed newlines (some very old data are like
159
 
            # this, new data with mixed newlines are rejected), so we're just
160
 
            # going to punt and normalize to unix style.
161
 
            return translation_text.replace(
162
 
                translation_newline_style, self.unix_style)
163
 
        else:
164
 
            # Otherwise the translation text should be normalized to use the
165
 
            # same newline style as the original.
166
 
            return translation_text.replace(
167
 
                translation_newline_style, self.newline_style)
168
 
 
169
 
 
170
 
def sanitize_translations(
 
151
        # Fix the newline chars.
 
152
        return translation_text.replace(
 
153
            translation_newline_style, self.newline_style)
 
154
 
 
155
 
 
156
def sanitize_translations_from_webui(
171
157
        english_singular, translations, pluralforms):
172
158
    """Sanitize `translations` using sanitize_translation.
173
159
 
203
189
 
204
190
    return sanitized_translations
205
191
 
206
 
 
207
 
def sanitize_translations_from_import(
208
 
        english_singular, translations, pluralforms):
209
 
    # At import time we want to ensure that the english_singular does not
210
 
    # contain mixed newline styles.
211
 
    if Sanitizer._getNewlineStyle(english_singular) is Sanitizer.mixed_style:
212
 
        raise MixedNewlineMarkersError(
213
 
            "Original text (%r) mixes different newline markers." %
214
 
                english_singular)
215
 
    return sanitize_translations(english_singular, translations, pluralforms)
216
 
 
217
 
 
218
 
def sanitize_translations_from_webui(
219
 
        english_singular, translations, pluralforms):
220
 
    return sanitize_translations(english_singular, translations, pluralforms)
 
192
# There will be a different function for translation coming from imports but
 
193
# for now it is identical to the one used in browser code.
 
194
sanitize_translations_from_import = sanitize_translations_from_webui