Merge lp:~jtv/launchpad/recife-retire-traits-helpers into lp:~launchpad/launchpad/recife

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: no longer in the source branch.
Merged at revision: 9162
Proposed branch: lp:~jtv/launchpad/recife-retire-traits-helpers
Merge into: lp:~launchpad/launchpad/recife
Diff against target: 242 lines (+18/-135)
2 files modified
lib/lp/translations/model/potmsgset.py (+18/-77)
lib/lp/translations/tests/test_potmsgset.py (+0/-58)
To merge this branch: bzr merge lp:~jtv/launchpad/recife-retire-traits-helpers
Reviewer Review Type Date Requested Status
Henning Eggers (community) code Approve
Review via email: mp+33491@code.launchpad.net

Commit message

Retire MessageSideHelper

Description of the change

= Retire MessageSideHelper =

For the Recife feature branch. Retire the last remnants of MessageSideHelper; its main function has already been extracted into MessageSideTraits.

To test:
{{{
./bin/test -vvc -m lp.translations.tests.test_potmsgset
}}}

Jeroen

To post a comment you must log in.
Revision history for this message
Henning Eggers (henninge) wrote :

Yeah for removing code! :-D

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/translations/model/potmsgset.py'
--- lib/lp/translations/model/potmsgset.py 2010-08-20 04:15:11 +0000
+++ lib/lp/translations/model/potmsgset.py 2010-08-24 07:42:48 +0000
@@ -5,7 +5,6 @@
55
6__metaclass__ = type6__metaclass__ = type
7__all__ = [7__all__ = [
8 'make_message_side_helpers',
9 'POTMsgSet',8 'POTMsgSet',
10 ]9 ]
1110
@@ -90,67 +89,6 @@
90incumbent_unknown = object()89incumbent_unknown = object()
9190
9291
93class MessageSideHelper:
94 """Helper for manipulating messages on one `TranslationSide`.
95
96 Does some caching so that the caller doesn't need to worry about
97 unnecessary queries e.g. when disabling a previously current
98 message.
99 """
100
101 # The TranslationSideTraits that this helper is for.
102 traits = None
103
104 # MessageSideHelper for this message on the "other side."
105 other_side = None
106
107 _incumbent = incumbent_unknown
108
109 def __init__(self, side, potmsgset, potemplate=None, language=None):
110 self.traits = getUtility(ITranslationSideTraitsSet).getTraits(side)
111 self.potmsgset = potmsgset
112 self.potemplate = potemplate
113 self.language = language
114
115 @property
116 def incumbent_message(self):
117 """Message that currently has the flag."""
118 if self._incumbent == incumbent_unknown:
119 self._incumbent = self.traits.getCurrentMessage(
120 self.potmsgset, self.potemplate, self.language)
121 return self._incumbent
122
123 def setFlag(self, translationmessage, value):
124 """Set or clear a message's "current" flag for this side."""
125 if value == self.traits.getFlag(translationmessage):
126 return
127
128 if value:
129 if self.incumbent_message is not None:
130 Store.of(self.incumbent_message).add_flush_order(
131 self.incumbent_message, translationmessage)
132 self.setFlag(self.incumbent_message, False)
133 self._incumbent = translationmessage
134 else:
135 self._incumbent = incumbent_unknown
136
137 self.traits.setFlag(translationmessage, value)
138
139
140def make_message_side_helpers(side, potmsgset, potemplate, language):
141 """Create `MessageSideHelper` object of the appropriate subtype."""
142 upstream = MessageSideHelper(
143 TranslationSide.UPSTREAM, potmsgset, potemplate, language)
144 ubuntu = MessageSideHelper(
145 TranslationSide.UBUNTU, potmsgset, potemplate, language)
146 upstream.other_side = ubuntu
147 ubuntu.other_side = upstream
148 mapping = dict(
149 (helper.traits.side, helper)
150 for helper in (ubuntu, upstream))
151 return mapping[side]
152
153
154class POTMsgSet(SQLBase):92class POTMsgSet(SQLBase):
155 implements(IPOTMsgSet)93 implements(IPOTMsgSet)
15694
@@ -1129,12 +1067,12 @@
11291067
1130 twin = identical_message1068 twin = identical_message
11311069
1132 translation_side = pofile.potemplate.translation_side1070 traits = getUtility(ITranslationSideTraitsSet).getTraits(
1133 helper = make_message_side_helpers(1071 pofile.potemplate.translation_side)
1134 translation_side, self, pofile.potemplate, pofile.language)
11351072
1136 # The current message on this translation side, if any.1073 # The current message on this translation side, if any.
1137 incumbent_message = helper.incumbent_message1074 incumbent_message = traits.getCurrentMessage(
1075 self, pofile.potemplate, pofile.language)
11381076
1139 # Summary of the matrix:1077 # Summary of the matrix:
1140 # * If the incumbent message is diverged and we're setting a1078 # * If the incumbent message is diverged and we're setting a
@@ -1178,8 +1116,8 @@
1178 }1116 }
11791117
1180 incumbent_state = "incumbent_%s" % self._nameMessageStatus(1118 incumbent_state = "incumbent_%s" % self._nameMessageStatus(
1181 incumbent_message, helper.traits)1119 incumbent_message, traits)
1182 twin_state = "twin_%s" % self._nameMessageStatus(twin, helper.traits)1120 twin_state = "twin_%s" % self._nameMessageStatus(twin, traits)
11831121
1184 decisions = decision_matrix[incumbent_state][twin_state]1122 decisions = decision_matrix[incumbent_state][twin_state]
1185 assert re.match('[ABZ]?[124567]?[+*]?$', decisions), (1123 assert re.match('[ABZ]?[124567]?[+*]?$', decisions), (
@@ -1189,11 +1127,11 @@
1189 if character == 'A':1127 if character == 'A':
1190 # Deactivate & converge.1128 # Deactivate & converge.
1191 # There may be an identical shared message.1129 # There may be an identical shared message.
1192 helper.traits.setFlag(incumbent_message, False)1130 traits.setFlag(incumbent_message, False)
1193 incumbent_message.shareIfPossible()1131 incumbent_message.shareIfPossible()
1194 elif character == 'B':1132 elif character == 'B':
1195 # Deactivate.1133 # Deactivate.
1196 helper.setFlag(incumbent_message, False)1134 traits.setFlag(incumbent_message, False)
1197 elif character == 'Z':1135 elif character == 'Z':
1198 # There is no incumbent message, so do nothing to it.1136 # There is no incumbent message, so do nothing to it.
1199 assert incumbent_message is None, (1137 assert incumbent_message is None, (
@@ -1214,14 +1152,14 @@
1214 # (If not, it's already active and has been unmasked by1152 # (If not, it's already active and has been unmasked by
1215 # our deactivating the incumbent).1153 # our deactivating the incumbent).
1216 message = twin1154 message = twin
1217 if not helper.traits.getFlag(twin):1155 if not traits.getFlag(twin):
1218 assert not helper.other_side.traits.getFlag(twin), (1156 assert not traits.other_side_traits.getFlag(twin), (
1219 "Trying to diverge a message that is current on the "1157 "Trying to diverge a message that is current on the "
1220 "other side.")1158 "other side.")
1221 message.potemplate = pofile.potemplate1159 message.potemplate = pofile.potemplate
1222 elif character == '6':1160 elif character == '6':
1223 # If other is not active, fork a diverged message.1161 # If other is not active, fork a diverged message.
1224 if helper.traits.getFlag(twin):1162 if traits.getFlag(twin):
1225 message = twin1163 message = twin
1226 else:1164 else:
1227 # The twin is used on the other side, so we can't1165 # The twin is used on the other side, so we can't
@@ -1236,11 +1174,14 @@
1236 message.shareIfPossible()1174 message.shareIfPossible()
1237 elif character == '*':1175 elif character == '*':
1238 if share_with_other_side:1176 if share_with_other_side:
1239 if helper.other_side.incumbent_message is None:1177 other_incumbent = (
1240 helper.other_side.setFlag(message, True)1178 traits.other_side_traits.getCurrentMessage(
1179 self, pofile.potemplate, pofile.language))
1180 if other_incumbent is None:
1181 traits.other_side_traits.setFlag(message, True)
1241 elif character == '+':1182 elif character == '+':
1242 if share_with_other_side:1183 if share_with_other_side:
1243 helper.other_side.setFlag(message, True)1184 traits.other_side_traits.setFlag(message, True)
1244 else:1185 else:
1245 raise AssertionError(1186 raise AssertionError(
1246 "Bad character in decision string: %s" % character)1187 "Bad character in decision string: %s" % character)
@@ -1248,7 +1189,7 @@
1248 if decisions == '':1189 if decisions == '':
1249 message = twin1190 message = twin
12501191
1251 helper.setFlag(message, True)1192 traits.setFlag(message, True)
12521193
1253 return message1194 return message
12541195
12551196
=== modified file 'lib/lp/translations/tests/test_potmsgset.py'
--- lib/lp/translations/tests/test_potmsgset.py 2010-08-17 09:55:30 +0000
+++ lib/lp/translations/tests/test_potmsgset.py 2010-08-24 07:42:48 +0000
@@ -25,7 +25,6 @@
25from lp.translations.interfaces.translationmessage import (25from lp.translations.interfaces.translationmessage import (
26 RosettaTranslationOrigin, TranslationConflict)26 RosettaTranslationOrigin, TranslationConflict)
27from lp.translations.interfaces.side import TranslationSide27from lp.translations.interfaces.side import TranslationSide
28from lp.translations.model.potmsgset import make_message_side_helpers
29from lp.translations.model.translationmessage import (28from lp.translations.model.translationmessage import (
30 DummyTranslationMessage)29 DummyTranslationMessage)
3130
@@ -1597,63 +1596,6 @@
1597 pofile.potemplate, pofile.language))1596 pofile.potemplate, pofile.language))
1598 self.assertEqual(origin, message.origin)1597 self.assertEqual(origin, message.origin)
15991598
1600 def test_make_message_side_helpers(self):
1601 # make_message_side_helpers is a factory for helpers that help
1602 # setCurrentTranslations deal with the dichotomy between
1603 # upstream and Ubuntu translations.
1604 pofile, potmsgset = self._makePOFileAndPOTMsgSet()
1605 sides = (TranslationSide.UPSTREAM, TranslationSide.UBUNTU)
1606 for side in sides:
1607 helper = make_message_side_helpers(
1608 side, potmsgset, pofile.potemplate, pofile.language)
1609 self.assertEqual(side, helper.traits.side)
1610 self.assertNotEqual(side, helper.other_side.traits.side)
1611 self.assertIn(helper.other_side.traits.side, sides)
1612 self.assertIs(helper, helper.other_side.other_side)
1613
1614 def test_UpstreamSideTraits_upstream(self):
1615 pofile, potmsgset = self._makePOFileAndPOTMsgSet()
1616 message = self.factory.makeTranslationMessage(
1617 pofile=pofile, potmsgset=potmsgset)
1618
1619 helper = make_message_side_helpers(
1620 TranslationSide.UPSTREAM, potmsgset, pofile.potemplate,
1621 pofile.language)
1622
1623 self.assertEqual('is_current_upstream', helper.traits.flag_name)
1624
1625 self.assertFalse(helper.traits.getFlag(message))
1626 self.assertFalse(message.is_current_upstream)
1627 self.assertEquals(None, helper.incumbent_message)
1628
1629 helper.setFlag(message, True)
1630
1631 self.assertTrue(helper.traits.getFlag(message))
1632 self.assertTrue(message.is_current_upstream)
1633 self.assertEquals(message, helper.incumbent_message)
1634
1635 def test_UpstreamSideTraits_ubuntu(self):
1636 pofile, potmsgset = self._makePOFileAndPOTMsgSet()
1637 message = self.factory.makeTranslationMessage(
1638 pofile=pofile, potmsgset=potmsgset)
1639 message.makeCurrentUbuntu(False)
1640
1641 helper = make_message_side_helpers(
1642 TranslationSide.UBUNTU, potmsgset, pofile.potemplate,
1643 pofile.language)
1644
1645 self.assertEqual('is_current_ubuntu', helper.traits.flag_name)
1646
1647 self.assertFalse(helper.traits.getFlag(message))
1648 self.assertFalse(message.is_current_ubuntu)
1649 self.assertEquals(None, helper.incumbent_message)
1650
1651 helper.setFlag(message, True)
1652
1653 self.assertTrue(helper.traits.getFlag(message))
1654 self.assertTrue(message.is_current_ubuntu)
1655 self.assertEquals(message, helper.incumbent_message)
1656
1657 def test_identical(self):1599 def test_identical(self):
1658 # Setting the same message twice leaves the original as-is.1600 # Setting the same message twice leaves the original as-is.
1659 pofile, potmsgset = self._makePOFileAndPOTMsgSet()1601 pofile, potmsgset = self._makePOFileAndPOTMsgSet()

Subscribers

People subscribed via source and target branches