1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=E0611,W0212
"""`SQLObject` implementation of `IPOTemplate` interface."""
__metaclass__ = type
__all__ = [
'get_pofiles_for',
'POTemplate',
'POTemplateSet',
'POTemplateSubset',
'POTemplateToTranslationFileDataAdapter',
'TranslationTemplatesCollection',
]
import datetime
import logging
import operator
import os
from psycopg2.extensions import TransactionRollbackError
from sqlobject import (
BoolCol,
ForeignKey,
IntCol,
SQLMultipleJoin,
SQLObjectNotFound,
StringCol,
)
from storm.expr import (
And,
Desc,
Func,
In,
Join,
LeftJoin,
Or,
Select,
)
from storm.info import ClassAlias
from storm.store import (
EmptyResultSet,
Store,
)
from zope.component import (
getAdapter,
getUtility,
)
from zope.interface import implements
from zope.security.proxy import removeSecurityProxy
from canonical.database.constants import DEFAULT
from canonical.database.datetimecol import UtcDateTimeCol
from canonical.database.enumcol import EnumCol
from canonical.database.sqlbase import (
flush_database_updates,
quote,
quote_like,
SQLBase,
sqlvalues,
)
from lp.services.helpers import shortlist
from lp.services.mail.helpers import get_email_template
from lp.services.database.lpstorm import (
IMasterStore,
IStore,
)
from lp.app.enums import ServiceUsage
from lp.app.errors import NotFoundError
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.registry.interfaces.person import validate_public_person
from lp.registry.model.packaging import Packaging
from lp.registry.model.sourcepackagename import SourcePackageName
from lp.services.database.collection import Collection
from lp.services.database.decoratedresultset import DecoratedResultSet
from lp.services.propertycache import cachedproperty
from lp.services.worlddata.model.language import Language
from lp.translations.enums import RosettaImportStatus
from lp.translations.interfaces.pofile import IPOFileSet
from lp.translations.interfaces.potemplate import (
IPOTemplate,
IPOTemplateSet,
IPOTemplateSharingSubset,
IPOTemplateSubset,
LanguageNotFound,
)
from lp.translations.interfaces.side import TranslationSide
from lp.translations.interfaces.translationcommonformat import (
ITranslationFileData,
)
from lp.translations.interfaces.translationexporter import (
ITranslationExporter,
)
from lp.translations.interfaces.translationfileformat import (
TranslationFileFormat,
)
from lp.translations.interfaces.translationimporter import (
ITranslationImporter,
TranslationFormatInvalidInputError,
TranslationFormatSyntaxError,
)
from lp.translations.model.pofile import POFile
from lp.translations.model.pomsgid import POMsgID
from lp.translations.model.potmsgset import POTMsgSet
from lp.translations.model.translationimportqueue import collect_import_info
from lp.translations.model.translationtemplateitem import (
TranslationTemplateItem,
)
from lp.translations.model.vpotexport import VPOTExport
from lp.translations.utilities.rosettastats import RosettaStats
from lp.translations.utilities.sanitize import MixedNewlineMarkersError
from lp.translations.utilities.translation_common_format import (
TranslationMessageData,
)
log = logging.getLogger(__name__)
standardPOFileTopComment = ''' %(languagename)s translation for %(origin)s
Copyright %(copyright)s %(year)s
This file is distributed under the same license as the %(origin)s package.
FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s.
'''
standardTemplateHeader = (
"Project-Id-Version: %(origin)s\n"
"Report-Msgid-Bugs-To: FULL NAME <EMAIL@ADDRESS>\n"
"POT-Creation-Date: %(templatedate)s\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: %(languagename)s <%(languagecode)s@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n")
standardPOFileHeader = (standardTemplateHeader +
"Plural-Forms: nplurals=%(nplurals)d; plural=%(pluralexpr)s\n")
def get_pofiles_for(potemplates, language):
"""Return list of `IPOFile`s for given templates in given language.
:param potemplates: a list or sequence of `POTemplate`s.
:param language: the language that the `IPOFile`s should be for.
:return: a list of exactly one `IPOFile` for each `POTemplate`
in `potemplates`. They will be `POFile`s where available,
and `DummyPOFile`s where not.
"""
potemplates = list(potemplates)
if len(potemplates) == 0:
return []
template_ids = [template.id for template in potemplates]
pofiles = Store.of(potemplates[0]).find(POFile, And(
POFile.potemplateID.is_in(template_ids),
POFile.language == language))
mapping = dict((pofile.potemplate.id, pofile) for pofile in pofiles)
result = [mapping.get(id) for id in template_ids]
for entry, pofile in enumerate(result):
assert pofile == result[entry], "This enumerate confuses me."
if pofile is None:
result[entry] = potemplates[entry].getDummyPOFile(
language, check_for_existing=False)
return result
class POTemplate(SQLBase, RosettaStats):
implements(IPOTemplate)
_table = 'POTemplate'
productseries = ForeignKey(foreignKey='ProductSeries',
dbName='productseries', notNull=False, default=None)
priority = IntCol(dbName='priority', notNull=True, default=DEFAULT)
name = StringCol(dbName='name', notNull=True)
translation_domain = StringCol(dbName='translation_domain', notNull=True)
description = StringCol(dbName='description', notNull=False, default=None)
copyright = StringCol(dbName='copyright', notNull=False, default=None)
datecreated = UtcDateTimeCol(dbName='datecreated', default=DEFAULT)
path = StringCol(dbName='path', notNull=True)
source_file = ForeignKey(foreignKey='LibraryFileAlias',
dbName='source_file', notNull=False, default=None)
source_file_format = EnumCol(dbName='source_file_format',
schema=TranslationFileFormat, default=TranslationFileFormat.PO,
notNull=True)
iscurrent = BoolCol(dbName='iscurrent', notNull=True, default=True)
messagecount = IntCol(dbName='messagecount', notNull=True, default=0)
owner = ForeignKey(
dbName='owner', foreignKey='Person',
storm_validator=validate_public_person, notNull=True)
sourcepackagename = ForeignKey(foreignKey='SourcePackageName',
dbName='sourcepackagename', notNull=False, default=None)
from_sourcepackagename = ForeignKey(foreignKey='SourcePackageName',
dbName='from_sourcepackagename', notNull=False, default=None)
sourcepackageversion = StringCol(dbName='sourcepackageversion',
notNull=False, default=None)
distroseries = ForeignKey(foreignKey='DistroSeries',
dbName='distroseries', notNull=False, default=None)
header = StringCol(dbName='header', notNull=True)
binarypackagename = ForeignKey(foreignKey='BinaryPackageName',
dbName='binarypackagename', notNull=False, default=None)
languagepack = BoolCol(dbName='languagepack', notNull=True, default=False)
date_last_updated = UtcDateTimeCol(dbName='date_last_updated',
default=DEFAULT)
# joins
pofiles = SQLMultipleJoin('POFile', joinColumn='potemplate')
# In-memory cache: maps language_code to list of POFiles
# translating this template to that language.
_cached_pofiles_by_language = None
_uses_english_msgids = None
@cachedproperty
def _sharing_ids(self):
"""Return the IDs of all sharing templates including this one."""
subset = getUtility(IPOTemplateSet).getSharingSubset(
product=self.product,
distribution=self.distribution,
sourcepackagename=self.sourcepackagename)
# Convert to a list for caching.
result = list(subset.getSharingPOTemplateIDs(self.name))
if len(result) == 0:
# Always return at least the template itself.
return [self.id]
else:
return result
def __storm_invalidated__(self):
super(POTemplate, self).__storm_invalidated__()
self.clearPOFileCache()
self._uses_english_msgids = None
def clearPOFileCache(self):
"""See `IPOTemplate`."""
self._cached_pofiles_by_language = None
def _removeFromSuggestivePOTemplatesCache(self):
"""One level of indirection to make testing easier."""
getUtility(
IPOTemplateSet).removeFromSuggestivePOTemplatesCache(self)
def setActive(self, active):
"""See `IPOTemplate`."""
if not active and active != self.iscurrent:
self._removeFromSuggestivePOTemplatesCache()
self.iscurrent = active
@property
def uses_english_msgids(self):
"""See `IPOTemplate`."""
if self._uses_english_msgids is not None:
return self._uses_english_msgids
translation_importer = getUtility(ITranslationImporter)
format = translation_importer.getTranslationFormatImporter(
self.source_file_format)
self._uses_english_msgids = not format.uses_source_string_msgids
return self._uses_english_msgids
def __iter__(self):
"""See `IPOTemplate`."""
for potmsgset in self.getPOTMsgSets():
yield potmsgset
def __getitem__(self, key):
"""See `IPOTemplate`."""
potmsgset = self.getPOTMsgSetByMsgIDText(key, only_current=True)
if potmsgset is None:
raise NotFoundError(key)
else:
return potmsgset
def sharingKey(self):
"""See `IPOTemplate.`"""
if self.productseries is not None:
product_focus = (
self.productseries ==
self.productseries.product.primary_translatable)
else:
product_focus = False
if self.distroseries is not None:
distro_focus = (
self.distroseries ==
self.distroseries.distribution.translation_focus)
else:
distro_focus = False
return self.iscurrent, product_focus, distro_focus, self.id
@property
def displayname(self):
"""See `IPOTemplate`."""
if self.productseries:
dn = '%s in %s %s' % (
self.name,
self.productseries.product.displayname,
self.productseries.displayname)
if self.distroseries:
dn = '%s in %s %s package "%s"' % (
self.name,
self.distroseries.distribution.displayname,
self.distroseries.displayname,
self.sourcepackagename.name)
return dn
@property
def title(self):
"""See `IPOTemplate`."""
if self.productseries:
title = 'Template "%s" in %s %s' % (
self.name,
self.productseries.product.displayname,
self.productseries.displayname)
if self.distroseries:
title = 'Template "%s" in %s %s package "%s"' % (
self.name,
self.distroseries.distribution.displayname,
self.distroseries.displayname,
self.sourcepackagename.name)
return title
@property
def distribution(self):
"""See `IPOTemplate`."""
if self.distroseries is not None:
return self.distroseries.distribution
else:
return None
@property
def product(self):
"""See `IPOTemplate`."""
if self.productseries is not None:
return self.productseries.product
else:
return None
def getTranslationPolicy(self):
"""See `IPOTemplate`."""
if self.productseries is not None:
return self.productseries.product
else:
return self.distroseries.distribution
@property
def translationgroups(self):
"""See `IPOTemplate`."""
return self.getTranslationPolicy().getTranslationGroups()
@property
def translationpermission(self):
"""See `IPOTemplate`."""
return self.getTranslationPolicy().getEffectiveTranslationPermission()
@property
def relatives_by_source(self):
"""See `IPOTemplate`."""
if self.productseries is not None:
return POTemplate.select(
'id <> %s AND productseries = %s AND iscurrent' % sqlvalues(
self, self.productseries), orderBy=['name'])
elif (self.distroseries is not None and
self.sourcepackagename is not None):
return POTemplate.select('''
id <> %s AND
distroseries = %s AND
sourcepackagename = %s AND
iscurrent
''' % sqlvalues(
self, self.distroseries, self.sourcepackagename),
orderBy=['name'])
else:
raise AssertionError('Unknown POTemplate source.')
@property
def language_count(self):
return Language.select('''
POFile.language = Language.id AND
POFile.currentcount + POFile.rosettacount > 0 AND
POFile.potemplate = %s
''' % sqlvalues(self.id),
clauseTables=['POFile'],
distinct=True).count()
@cachedproperty
def sourcepackage(self):
"""See `IPOTemplate`."""
# Avoid circular imports
from lp.registry.model.sourcepackage import SourcePackage
if self.distroseries is None:
return None
return SourcePackage(
distroseries=self.distroseries,
sourcepackagename=self.sourcepackagename)
@property
def translationtarget(self):
"""See `IPOTemplate`."""
if self.productseries is not None:
return self.productseries
else:
return self.sourcepackage
def getHeader(self):
"""See `IPOTemplate`."""
translation_importer = getUtility(ITranslationImporter)
format_importer = translation_importer.getTranslationFormatImporter(
self.source_file_format)
header = format_importer.getHeaderFromString(self.header)
header.has_plural_forms = self.hasPluralMessage()
return header
def _getPOTMsgSetSelectionClauses(self):
"""Return SQL clauses for finding POTMsgSets which belong
to this POTemplate."""
clauses = [
'TranslationTemplateItem.potemplate = %s' % sqlvalues(self),
'TranslationTemplateItem.potmsgset = POTMsgSet.id',
]
return clauses
def getPOTMsgSetByMsgIDText(self, singular_text, plural_text=None,
only_current=False, context=None):
"""See `IPOTemplate`."""
clauses = self._getPOTMsgSetSelectionClauses()
if only_current:
clauses.append('TranslationTemplateItem.sequence > 0')
if context is not None:
clauses.append('context = %s' % sqlvalues(context))
else:
clauses.append('context IS NULL')
# Find a message ID with the given text.
try:
singular_msgid = POMsgID.byMsgid(singular_text)
except SQLObjectNotFound:
return None
clauses.append('msgid_singular = %s' % sqlvalues(singular_msgid))
# Find a message ID for the plural string.
if plural_text is not None:
try:
plural_msgid = POMsgID.byMsgid(plural_text)
clauses.append('msgid_plural = %s' % sqlvalues(plural_msgid))
except SQLObjectNotFound:
return None
else:
# You have to be explicit now.
clauses.append('msgid_plural IS NULL')
# Find a message set with the given message ID.
return POTMsgSet.selectOne(' AND '.join(clauses),
clauseTables=['TranslationTemplateItem'])
def getPOTMsgSetBySequence(self, sequence):
"""See `IPOTemplate`."""
assert sequence > 0, ('%r is out of range')
clauses = self._getPOTMsgSetSelectionClauses()
clauses.append(
'TranslationTemplateItem.sequence = %s' % sqlvalues(sequence))
return POTMsgSet.selectOne(' AND '.join(clauses),
clauseTables=['TranslationTemplateItem'])
def getPOTMsgSets(self, current=True, prefetch=True):
"""See `IPOTemplate`."""
clauses = self._getPOTMsgSetSelectionClauses()
if current:
# Only count the number of POTMsgSet that are current.
clauses.append('TranslationTemplateItem.sequence > 0')
query = POTMsgSet.select(" AND ".join(clauses),
clauseTables=['TranslationTemplateItem'],
orderBy=['TranslationTemplateItem.sequence'])
if prefetch:
query = query.prejoin(['msgid_singular', 'msgid_plural'])
return query
def getTranslationCredits(self):
"""See `IPOTemplate`."""
# Find potential credits messages by the message ids.
store = IStore(POTemplate)
origin1 = Join(TranslationTemplateItem,
TranslationTemplateItem.potmsgset == POTMsgSet.id)
origin2 = Join(POMsgID, POTMsgSet.msgid_singular == POMsgID.id)
result = store.using(POTMsgSet, origin1, origin2).find(
POTMsgSet,
TranslationTemplateItem.potemplate == self,
POMsgID.msgid.is_in(POTMsgSet.credits_message_ids))
# Filter these candidates because is_translation_credit checks for
# more conditions than the special msgids.
for potmsgset in result:
if potmsgset.is_translation_credit:
yield potmsgset
def getPOTMsgSetsCount(self, current=True):
"""See `IPOTemplate`."""
results = self.getPOTMsgSets(current, prefetch=False)
return results.count()
def getPOTMsgSetByID(self, id):
"""See `IPOTemplate`."""
clauses = self._getPOTMsgSetSelectionClauses()
clauses.append('POTMsgSet.id = %s' % sqlvalues(id))
return POTMsgSet.selectOne(' AND '.join(clauses),
clauseTables=['TranslationTemplateItem'])
def languages(self):
"""See `IPOTemplate`."""
return Language.select("POFile.language = Language.id AND "
"Language.code <> 'en' AND "
"POFile.potemplate = %d" % self.id,
clauseTables=['POFile', 'Language'],
distinct=True)
def getPOFileByPath(self, path):
"""See `IPOTemplate`."""
return POFile.selectOneBy(potemplate=self, path=path)
def getPOFileByLang(self, language_code):
"""See `IPOTemplate`."""
# Consult cache first.
if self._cached_pofiles_by_language is None:
self._cached_pofiles_by_language = {}
elif language_code in self._cached_pofiles_by_language:
# Cache contains a remembered POFile for this language. Don't do
# the usual get() followed by "is None"; the dict may contain None
# values to indicate we looked for a POFile and found none.
return self._cached_pofiles_by_language[language_code]
self._cached_pofiles_by_language[language_code] = POFile.selectOne("""
POFile.potemplate = %d AND
POFile.language = Language.id AND
Language.code = %s
""" % (self.id,
quote(language_code)),
clauseTables=['Language'])
return self._cached_pofiles_by_language[language_code]
def getOtherSidePOTemplate(self):
"""See `IPOTemplate`."""
if self.translation_side == TranslationSide.UBUNTU:
other_side_object = self.sourcepackage.productseries
else:
other_side_object = (
self.productseries.getUbuntuTranslationFocusPackage())
if other_side_object is None:
return None
return other_side_object.getTranslationTemplateByName(self.name)
def messageCount(self):
"""See `IRosettaStats`."""
return self.messagecount
def updateMessageCount(self):
"""Update `self.messagecount`."""
self.messagecount = self.getPOTMsgSetsCount()
def currentCount(self, language=None):
"""See `IRosettaStats`."""
if language is None:
return 0
pofile = self.getPOFileByLang(language)
if pofile is None:
return 0
else:
return pofile.currentCount()
def updatesCount(self, language=None):
"""See `IRosettaStats`."""
if language is None:
return 0
pofile = self.getPOFileByLang(language)
if pofile is None:
return 0
else:
pofile.updatesCount()
def rosettaCount(self, language=None):
"""See `IRosettaStats`."""
if language is None:
return 0
pofile = self.getPOFileByLang(language)
if pofile is None:
return 0
else:
pofile.rosettaCount()
def unreviewedCount(self, language=None):
"""See `IRosettaStats`."""
if language is None:
return 0
pofile = self.getPOFileByLang(language)
if pofile is None:
return 0
else:
pofile.unreviewedCount()
def _null_quote(self, value):
if value is None:
return " IS NULL "
else:
return " = %s" % sqlvalues(value)
def _getPOTMsgSetBy(self, msgid_singular, msgid_plural=None, context=None,
sharing_templates=False):
"""Look for a POTMsgSet by msgid_singular, msgid_plural, context.
If `sharing_templates` is True, and the current template has no such
POTMsgSet, look through sharing templates as well.
"""
clauses = [
'TranslationTemplateItem.potmsgset = POTMsgSet.id',
]
clause_tables = ['TranslationTemplateItem']
if sharing_templates:
clauses.append(
'TranslationTemplateItem.potemplate in %s' % sqlvalues(
self._sharing_ids))
else:
clauses.append(
'TranslationTemplateItem.potemplate = %s' % sqlvalues(self))
clauses.append(
'POTMsgSet.msgid_singular %s' % self._null_quote(msgid_singular))
clauses.append(
'POTMsgSet.msgid_plural %s' % self._null_quote(msgid_plural))
clauses.append(
'POTMsgSet.context %s' % self._null_quote(context))
result = POTMsgSet.select(
' AND '.join(clauses),
clauseTables=clause_tables,
# If there are multiple messages, make the one from the
# current POTemplate be returned first.
orderBy=['(TranslationTemplateItem.POTemplate<>%s)' % (
sqlvalues(self))])[:2]
if result.count() > 0:
return result[0]
else:
return None
def hasMessageID(self, msgid_singular, msgid_plural, context=None):
"""See `IPOTemplate`."""
return bool(
self._getPOTMsgSetBy(msgid_singular, msgid_plural, context))
def hasPluralMessage(self):
"""See `IPOTemplate`."""
clauses = self._getPOTMsgSetSelectionClauses()
clauses.append(
'POTMsgSet.msgid_plural IS NOT NULL')
return bool(POTMsgSet.select(
' AND '.join(clauses),
clauseTables=['TranslationTemplateItem']))
def export(self):
"""See `IPOTemplate`."""
translation_exporter = getUtility(ITranslationExporter)
template_file = getAdapter(
self, ITranslationFileData, 'all_messages')
exported_file = translation_exporter.exportTranslationFiles(
[template_file])
try:
file_content = exported_file.read()
finally:
exported_file.close()
return file_content
def _generateTranslationFileDatas(self):
"""Yield `ITranslationFileData` objects for translations and self.
This lets us construct the in-memory representations of the template
and its translations one by one before exporting them, rather than
building them all beforehand and keeping them in memory at the same
time.
"""
for pofile in self.pofiles:
yield getAdapter(pofile, ITranslationFileData, 'all_messages')
yield getAdapter(self, ITranslationFileData, 'all_messages')
def exportWithTranslations(self):
"""See `IPOTemplate`."""
translation_exporter = getUtility(ITranslationExporter)
return translation_exporter.exportTranslationFiles(
self._generateTranslationFileDatas())
def expireAllMessages(self):
"""See `IPOTemplate`."""
for potmsgset in self.getPOTMsgSets(prefetch=False):
potmsgset.setSequence(self, 0)
def _lookupLanguage(self, language_code):
"""Look up named `Language` object, or raise `LanguageNotFound`."""
try:
return Language.byCode(language_code)
except SQLObjectNotFound:
raise LanguageNotFound(language_code)
def isPOFilePathAvailable(self, path):
"""Can we assign given path to a new `POFile` without clashes?
Tests for uniqueness within the context of all templates for either
self's product release series, or the combination of self's distro
release series and source package (whichever applies).
"""
pofileset = getUtility(IPOFileSet)
existing_pofiles = pofileset.getPOFilesByPathAndOrigin(
path, self.productseries, self.distroseries,
self.sourcepackagename)
return existing_pofiles.is_empty()
def _composePOFilePath(self, language):
"""Make up a good name for a new `POFile` for given language.
The name should be unique in this `ProductSeries` or this combination
of `DistroSeries` and source package. It is not guaranteed that the
returned name will be unique, however, to avoid hiding obvious
naming mistakes.
"""
potemplate_dir = os.path.dirname(self.path)
path = '%s-%s.po' % (
self.translation_domain, language.code)
return os.path.join(potemplate_dir, path)
def _createPOFilesInSharingPOTemplates(self, pofile):
"""Create copies of the given pofile in all sharing potemplates."""
subset = getUtility(IPOTemplateSet).getSharingSubset(
distribution=self.distribution,
sourcepackagename=self.sourcepackagename,
product=self.product)
for template in subset.getSharingPOTemplates(self.name):
template = removeSecurityProxy(template)
if template is self:
continue
language_code = pofile.language.code
existingpo = template.getPOFileByLang(language_code)
if existingpo is not None:
continue
newpopath = template._composePOFilePath(pofile.language)
pofile = POFile(
potemplate=template,
language=pofile.language,
topcomment=pofile.topcomment,
header=pofile.header,
fuzzyheader=pofile.fuzzyheader,
owner=pofile.owner,
path=newpopath)
# Update cache to reflect the change.
template._cached_pofiles_by_language[language_code] = pofile
def newPOFile(self, language_code, create_sharing=True, owner=None):
"""See `IPOTemplate`."""
# Make sure we don't already have a PO file for this language.
existingpo = self.getPOFileByLang(language_code)
assert existingpo is None, (
'There is already a valid IPOFile (%s)' % existingpo.title)
# Since we have no PO file for this language yet, create one.
language = self._lookupLanguage(language_code)
now = datetime.datetime.now()
data = {
'year': now.year,
'languagename': language.englishname,
'languagecode': language_code,
'date': now.isoformat(' '),
'templatedate': self.datecreated,
'copyright': '(c) %d Rosetta Contributors and Canonical Ltd'
% now.year,
'nplurals': language.pluralforms or 1,
'pluralexpr': language.pluralexpression or '0',
}
if self.productseries is not None:
data['origin'] = self.productseries.product.name
else:
data['origin'] = self.sourcepackagename.name
# The default POFile owner is the Rosetta Experts team.
if owner is None:
owner = getUtility(ILaunchpadCelebrities).rosetta_experts
path = self._composePOFilePath(language)
pofile = POFile(
potemplate=self,
language=language,
topcomment=standardPOFileTopComment % data,
header=standardPOFileHeader % data,
fuzzyheader=True,
owner=owner,
path=path)
# Update cache to reflect the change.
self._cached_pofiles_by_language[language_code] = pofile
# Set dummy translations for translation credits in this POFile.
for credits in self.getTranslationCredits():
credits.setTranslationCreditsToTranslated(pofile)
if create_sharing:
self._createPOFilesInSharingPOTemplates(pofile)
pofile.updateStatistics()
# Store the changes.
flush_database_updates()
return pofile
def getDummyPOFile(self, language, requester=None,
check_for_existing=True):
"""See `IPOTemplate`."""
# Avoid circular import.
from lp.translations.model.pofile import DummyPOFile
if check_for_existing:
# see if a valid one exists.
existingpo = self.getPOFileByLang(language.code)
assert existingpo is None, (
'There is already a valid IPOFile (%s)' % existingpo.title)
return DummyPOFile(self, language, owner=requester)
def createPOTMsgSetFromMsgIDs(self, msgid_singular, msgid_plural=None,
context=None, sequence=0):
"""See `IPOTemplate`."""
potmsgset = POTMsgSet(
context=context,
msgid_singular=msgid_singular,
msgid_plural=msgid_plural,
sequence=0,
potemplate=None,
commenttext=None,
filereferences=None,
sourcecomment=None,
flagscomment=None)
potmsgset.setSequence(self, sequence)
if potmsgset.is_translation_credit:
for language in self.languages():
pofile = self.getPOFileByLang(language.code)
if pofile is not None:
potmsgset.setTranslationCreditsToTranslated(pofile)
return potmsgset
@staticmethod
def getOrCreatePOMsgID(text):
"""Creates or returns existing POMsgID for given `text`."""
try:
msgid = POMsgID.byMsgid(text)
except SQLObjectNotFound:
# If there are no existing message ids, create a new one.
# We do not need to check whether there is already a message set
# with the given text in this template.
msgid = POMsgID(msgid=text)
return msgid
def createMessageSetFromText(self, singular_text, plural_text,
context=None, sequence=0):
"""See `IPOTemplate`."""
msgid_singular = self.getOrCreatePOMsgID(singular_text)
if plural_text is None:
msgid_plural = None
else:
msgid_plural = self.getOrCreatePOMsgID(plural_text)
assert not self.hasMessageID(msgid_singular, msgid_plural, context), (
"There is already a message set for this template, file and"
" primary msgid and context '%r'" % context)
return self.createPOTMsgSetFromMsgIDs(msgid_singular, msgid_plural,
context, sequence)
def getOrCreateSharedPOTMsgSet(self, singular_text, plural_text,
context=None, initial_file_references=None,
initial_source_comment=None):
"""See `IPOTemplate`."""
msgid_singular = self.getOrCreatePOMsgID(singular_text)
if plural_text is None:
msgid_plural = None
else:
msgid_plural = self.getOrCreatePOMsgID(plural_text)
potmsgset = self._getPOTMsgSetBy(msgid_singular, msgid_plural,
context, sharing_templates=True)
if potmsgset is None:
potmsgset = self.createPOTMsgSetFromMsgIDs(
msgid_singular, msgid_plural, context, sequence=0)
potmsgset.filereferences = initial_file_references
potmsgset.sourcecomment = initial_source_comment
return potmsgset
def importFromQueue(self, entry_to_import, logger=None, txn=None):
"""See `IPOTemplate`."""
assert entry_to_import is not None, "Attempt to import None entry."
assert entry_to_import.import_into.id == self.id, (
"Attempt to import entry to POTemplate it doesn't belong to.")
assert entry_to_import.status == RosettaImportStatus.APPROVED, (
"Attempt to import non-approved entry.")
# XXX: JeroenVermeulen 2007-11-29: This method is called from the
# import script, which can provide the right object but can only
# obtain it in security-proxied form. We need full, unguarded access
# to complete the import.
entry_to_import = removeSecurityProxy(entry_to_import)
translation_importer = getUtility(ITranslationImporter)
rosetta_experts = getUtility(ILaunchpadCelebrities).rosetta_experts
subject = 'Translation template import - %s' % self.displayname
# Can use template_mail = 'poimport-template-confirmation.txt' to send
# mail when everything is imported, but those mails aren't very useful
# to or much welcomed by the recipients. See bug 855150.
template_mail = None
errors, warnings = None, None
try:
errors, warnings = translation_importer.importFile(
entry_to_import, logger)
except (MixedNewlineMarkersError, TranslationFormatSyntaxError,
TranslationFormatInvalidInputError, UnicodeDecodeError), (
exception):
if logger:
logger.info(
'We got an error importing %s', self.title, exc_info=1)
subject = 'Import problem - %s' % self.displayname
if isinstance(exception, UnicodeDecodeError):
template_mail = 'poimport-bad-encoding.txt'
else:
template_mail = 'poimport-syntax-error.txt'
entry_to_import.setStatus(RosettaImportStatus.FAILED,
rosetta_experts)
error_text = str(exception)
entry_to_import.setErrorOutput(error_text)
else:
error_text = None
entry_to_import.setErrorOutput(None)
replacements = collect_import_info(entry_to_import, self, warnings)
replacements.update({
'import_title': 'translation templates for %s' % self.displayname,
})
if error_text is not None:
replacements['error'] = error_text
entry_to_import.addWarningOutput(replacements['warnings'])
if entry_to_import.status != RosettaImportStatus.FAILED:
entry_to_import.setStatus(RosettaImportStatus.IMPORTED,
rosetta_experts)
# Assign karma to the importer if this is not an automatic import
# (all automatic imports come from the rosetta expert team).
celebs = getUtility(ILaunchpadCelebrities)
rosetta_experts = celebs.rosetta_experts
if entry_to_import.importer.id != rosetta_experts.id:
entry_to_import.importer.assignKarma(
'translationtemplateimport',
product=self.product,
distribution=self.distribution,
sourcepackagename=self.sourcepackagename)
# Synchronize changes to database so we can calculate fresh
# statistics on the server side.
flush_database_updates()
# Update cached number of msgsets.
self.updateMessageCount()
# The upload affects the statistics for all translations of this
# template. Recalculate those as well. This takes time and
# covers a lot of data, so if appropriate, break this up
# into smaller transactions.
if txn is not None:
txn.commit()
txn.begin()
for pofile in self.pofiles:
try:
pofile.updateStatistics()
if txn is not None:
txn.commit()
txn.begin()
except TransactionRollbackError, error:
if txn is not None:
txn.abort()
txn.begin()
if logger:
logger.warn(
"Statistics update failed: %s" % unicode(error))
if template_mail is not None:
template = get_email_template(
template_mail, 'translations')
message = template % replacements
return (subject, message)
else:
return None, None
def getTranslationRows(self):
"""See `IPOTemplate`."""
Singular = ClassAlias(POMsgID)
Plural = ClassAlias(POMsgID)
SingularJoin = LeftJoin(
Singular, Singular.id == POTMsgSet.msgid_singularID)
PluralJoin = LeftJoin(Plural, Plural.id == POTMsgSet.msgid_pluralID)
source = Store.of(self).using(
TranslationTemplateItem, POTMsgSet, SingularJoin, PluralJoin)
rows = source.find(
(TranslationTemplateItem, POTMsgSet, Singular, Plural),
TranslationTemplateItem.potemplate == self,
POTMsgSet.id == TranslationTemplateItem.potmsgsetID)
rows = rows.order_by(TranslationTemplateItem.sequence)
for row in rows:
yield VPOTExport(self, *row)
@property
def translation_side(self):
"""See `IPOTemplate`."""
if self.productseriesID is not None:
return TranslationSide.UPSTREAM
else:
return TranslationSide.UBUNTU
def awardKarma(self, person, action_name):
"""See `IPOTemplate`."""
person.assignKarma(
action_name, product=self.product, distribution=self.distribution,
sourcepackagename=self.sourcepackagename)
class POTemplateSubset:
implements(IPOTemplateSubset)
def __init__(self, sourcepackagename=None, from_sourcepackagename=None,
distroseries=None, productseries=None, iscurrent=None,
ordered_by_names=False):
"""Create a new `POTemplateSubset` object.
The set of POTemplate depends on the arguments you pass to this
constructor. The sourcepackagename, from_sourcepackagename,
distroseries and productseries are just filters for that set.
In addition, iscurrent sets the filter for the iscurrent flag.
"""
self.sourcepackagename = sourcepackagename
self.distroseries = distroseries
self.productseries = productseries
self.iscurrent = iscurrent
self.orderby = [POTemplate.id]
self.clauses = []
assert productseries is None or distroseries is None, (
'A product series must not be used with a distro series.')
assert productseries is not None or distroseries is not None, (
'Either productseries or distroseries must be not None.')
# Construct the base clauses.
if productseries is not None:
self.clauses.append(
POTemplate.productseriesID == productseries.id)
if ordered_by_names:
self.orderby = [POTemplate.name]
else:
self.clauses.append(
POTemplate.distroseriesID == distroseries.id)
if ordered_by_names:
self.orderby = [SourcePackageName.name, POTemplate.name]
if from_sourcepackagename is not None:
self.clauses.append(
POTemplate.from_sourcepackagenameID ==
from_sourcepackagename.id)
self.sourcepackagename = from_sourcepackagename
elif sourcepackagename is not None:
self.clauses.append(
POTemplate.sourcepackagename == sourcepackagename.id)
else:
# Select all POTemplates in a Distroseries.
pass
# Add the filter for the iscurrent flag if requested.
if iscurrent is not None:
self.clauses.append(
POTemplate.iscurrent == iscurrent)
def _build_query(self, additional_clause=None,
ordered=True, do_prejoin=True):
"""Construct the storm query."""
if additional_clause is None:
condition = And(self.clauses)
else:
condition = And(self.clauses + [additional_clause])
if self.productseries is not None:
store = Store.of(self.productseries)
query = store.find(POTemplate, condition)
else:
store = Store.of(self.distroseries)
if do_prejoin:
query = DecoratedResultSet(store.find(
(POTemplate, SourcePackageName),
(POTemplate.sourcepackagenameID ==
SourcePackageName.id), condition),
operator.itemgetter(0))
else:
query = store.find(POTemplate, condition)
if ordered:
return query.order_by(self.orderby)
return query
def __iter__(self):
"""See `IPOTemplateSubset`."""
for potemplate in self._build_query():
yield potemplate
def __len__(self):
"""See `IPOTemplateSubset`."""
result = self._build_query(do_prejoin=False, ordered=False)
return result.count()
def __getitem__(self, name):
"""See `IPOTemplateSubset`."""
potemplate = self.getPOTemplateByName(name)
if potemplate is None:
raise NotFoundError(name)
else:
return potemplate
@property
def title(self):
"""See `IPOTemplateSubset`."""
titlestr = ''
if self.distroseries:
titlestr += ' ' + self.distroseries.displayname
if self.sourcepackagename:
titlestr += ' ' + self.sourcepackagename.name
if self.productseries:
titlestr += ' '
titlestr += self.productseries.displayname
return titlestr
def _copyPOFilesFromSharingTemplates(self, template):
subset = getUtility(IPOTemplateSet).getSharingSubset(
distribution=template.distribution,
sourcepackagename=template.sourcepackagename,
product=template.product)
for shared_template in subset.getSharingPOTemplates(template.name):
shared_template = removeSecurityProxy(shared_template)
if shared_template is template:
continue
for pofile in shared_template.pofiles:
template.newPOFile(pofile.language.code, create_sharing=False)
# Do not continue, else it would trigger an existingpo assertion.
return
def new(self, name, translation_domain, path, owner, copy_pofiles=True):
"""See `IPOTemplateSubset`."""
header_params = {
'origin': 'PACKAGE VERSION',
'templatedate': datetime.datetime.now(),
'languagename': 'LANGUAGE',
'languagecode': 'LL',
}
template = POTemplate(name=name,
translation_domain=translation_domain,
sourcepackagename=self.sourcepackagename,
distroseries=self.distroseries,
productseries=self.productseries,
path=path,
owner=owner,
header=standardTemplateHeader % header_params)
if copy_pofiles:
self._copyPOFilesFromSharingTemplates(template)
return template
def getPOTemplateByName(self, name):
"""See `IPOTemplateSubset`."""
result = self._build_query(POTemplate.name == name, ordered=False)
return result.one()
def getPOTemplatesByTranslationDomain(self, translation_domain):
"""See `IPOTemplateSubset`."""
return self._build_query(
POTemplate.translation_domain == translation_domain)
def getPOTemplateByPath(self, path):
"""See `IPOTemplateSubset`."""
result = self._build_query(
POTemplate.path == path, ordered=False)
return result.one()
def getAllOrderByDateLastUpdated(self):
"""See `IPOTemplateSet`."""
result = self._build_query(ordered=False)
return result.order_by(Desc(POTemplate.date_last_updated))
def getClosestPOTemplate(self, path):
"""See `IPOTemplateSubset`."""
if path is None:
return None
closest_template = None
closest_template_path_length = 0
repeated = False
for template in self:
template_path_length = len(
os.path.commonprefix([template.path, path]))
if template_path_length > closest_template_path_length:
# This template is more near than the one we got previously
closest_template = template
closest_template_path_length = template_path_length
repeated = False
elif template_path_length == closest_template_path_length:
# We found two templates with the same length, we note that
# fact, if we don't get a better template, we ignore them and
# leave it to the admins.
repeated = True
if repeated:
return None
else:
return closest_template
def findUniquePathlessMatch(self, filename):
"""See `IPOTemplateSubset`."""
result = self._build_query(
("(POTemplate.path = %s OR POTemplate.path LIKE '%%%%/' || %s)"
% (quote(filename), quote_like(filename))),
ordered=False)
candidates = list(result.config(limit=2))
if len(candidates) == 1:
# Found exactly one match.
return candidates[0]
else:
return None
class POTemplateSet:
implements(IPOTemplateSet)
def __iter__(self):
"""See `IPOTemplateSet`."""
res = POTemplate.select()
for potemplate in res:
yield potemplate
def getByIDs(self, ids):
"""See `IPOTemplateSet`."""
values = ",".join(sqlvalues(*ids))
return POTemplate.select("POTemplate.id in (%s)" % values,
prejoins=["productseries", "distroseries", "sourcepackagename"],
orderBy=["POTemplate.id"])
def getAllByName(self, name):
"""See `IPOTemplateSet`."""
return POTemplate.selectBy(name=name, orderBy=['name', 'id'])
def getAllOrderByDateLastUpdated(self):
"""See `IPOTemplateSet`."""
return POTemplate.select(orderBy=['-date_last_updated'])
def getSubset(self, distroseries=None, sourcepackagename=None,
productseries=None, iscurrent=None,
ordered_by_names=False):
"""See `IPOTemplateSet`."""
return POTemplateSubset(
distroseries=distroseries,
sourcepackagename=sourcepackagename,
productseries=productseries,
iscurrent=iscurrent,
ordered_by_names=ordered_by_names)
def getSubsetFromImporterSourcePackageName(self, distroseries,
sourcepackagename, iscurrent=None):
"""See `IPOTemplateSet`."""
if distroseries is None or sourcepackagename is None:
raise AssertionError(
'distroseries and sourcepackage must be not None.')
return POTemplateSubset(
distroseries=distroseries,
sourcepackagename=sourcepackagename,
iscurrent=iscurrent)
def getSharingSubset(self, distribution=None, sourcepackagename=None,
product=None):
"""See `IPOTemplateSet`."""
return POTemplateSharingSubset(self, distribution=distribution,
sourcepackagename=sourcepackagename,
product=product)
def getPOTemplateByPathAndOrigin(self, path, productseries=None,
distroseries=None, sourcepackagename=None):
"""See `IPOTemplateSet`."""
assert (productseries is None) != (sourcepackagename is None), (
"Must specify either productseries or sourcepackagename.")
conditions = And(
POTemplate.iscurrent == True,
POTemplate.path == path,
POTemplate.productseries == productseries,
Or(
POTemplate.from_sourcepackagename == sourcepackagename,
POTemplate.sourcepackagename == sourcepackagename))
if distroseries:
conditions = And(
conditions, POTemplate.distroseries == distroseries)
store = IStore(POTemplate)
matches = shortlist(store.find(POTemplate, conditions))
if len(matches) == 0:
# Nope. Sorry.
return None
elif len(matches) == 1:
# Yup. Great.
return matches[0]
elif sourcepackagename is None:
# Multiple matches, and for a product not a package.
logging.warn(
"Found %d templates with path '%s' for productseries %s",
len(matches), path, productseries.title)
return None
else:
# Multiple matches, for a distribution package. Prefer a
# match on from_sourcepackagename: the file may have been
# uploaded for another package than the one it is meant to
# be imported into.
preferred_matches = [
match
for match in matches
if match.from_sourcepackagename == sourcepackagename]
if len(preferred_matches) == 1:
return preferred_matches[0]
else:
logging.warn(
"Found %d templates with path '%s' for package %s "
"(%d matched on from_sourcepackagename).",
len(matches), path, sourcepackagename.name,
len(preferred_matches))
return None
def wipeSuggestivePOTemplatesCache(self):
"""See `IPOTemplateSet`."""
return IMasterStore(POTemplate).execute(
"DELETE FROM SuggestivePOTemplate").rowcount
def removeFromSuggestivePOTemplatesCache(self, potemplate):
"""See `IPOTemplateSet`."""
rowcount = IMasterStore(POTemplate).execute(
"DELETE FROM SuggestivePOTemplate "
"WHERE potemplate = %s" % sqlvalues(potemplate)).rowcount
return rowcount == 1
def populateSuggestivePOTemplatesCache(self):
"""See `IPOTemplateSet`."""
return IMasterStore(POTemplate).execute("""
INSERT INTO SuggestivePOTemplate (
SELECT POTemplate.id
FROM POTemplate
LEFT JOIN DistroSeries ON
DistroSeries.id = POTemplate.distroseries
LEFT JOIN Distribution ON
Distribution.id = DistroSeries.distribution
LEFT JOIN ProductSeries ON
ProductSeries.id = POTemplate.productseries
LEFT JOIN Product ON
Product.id = ProductSeries.product
WHERE
POTemplate.iscurrent AND (
Distribution.translations_usage IN %(usage)s OR
Product.translations_usage IN %(usage)s)
ORDER BY POTemplate.id
)
""" % {
'usage': sqlvalues(
ServiceUsage.LAUNCHPAD, ServiceUsage.EXTERNAL)}
).rowcount
class POTemplateSharingSubset(object):
implements(IPOTemplateSharingSubset)
distribution = None
sourcepackagename = None
product = None
def __init__(self, potemplateset,
distribution=None, sourcepackagename=None,
product=None):
assert product or distribution, "Pick a product or distribution!"
assert not (product and distribution), (
"Pick a product or distribution, not both!")
assert distribution or not sourcepackagename, (
"Picking a source package only makes sense with a distribution.")
self.potemplateset = potemplateset
self.distribution = distribution
self.sourcepackagename = sourcepackagename
self.product = product
def _get_potemplate_equivalence_class(self, template):
"""Return whatever we group `POTemplate`s by for sharing purposes."""
if template.sourcepackagename is None:
package = None
else:
package = template.sourcepackagename.name
return (template.name, package)
def _queryByProduct(self, templatename_clause):
"""Build the query that finds POTemplates by their linked product.
Queries the Packaging table to find templates in linked source
packages, too.
:param templatename_clause: A string or a storm expression to
add to the where clause of the query that will select the template
name.
:return: A ResultSet for the query.
"""
# Avoid circular imports.
from lp.registry.model.distroseries import DistroSeries
from lp.registry.model.productseries import ProductSeries
subquery = Select(
(DistroSeries.distributionID, Packaging.sourcepackagenameID),
tables=(Packaging, ProductSeries, DistroSeries),
where=And(
Packaging.productseriesID == ProductSeries.id,
Packaging.distroseriesID == DistroSeries.id,
ProductSeries.product == self.product),
distinct=True)
origin = LeftJoin(
LeftJoin(
POTemplate, ProductSeries,
POTemplate.productseriesID == ProductSeries.id),
DistroSeries,
POTemplate.distroseriesID == DistroSeries.id)
return Store.of(self.product).using(origin).find(
POTemplate,
And(
templatename_clause,
Or(
ProductSeries.product == self.product,
In(
Func(
'ROW',
DistroSeries.distributionID,
POTemplate.sourcepackagenameID),
subquery))))
def _queryBySourcepackagename(self, templatename_clause):
"""Build the query that finds POTemplates by their names.
:param templatename_clause: A string or a storm expression to
add to the where clause of the query that will select the template
name.
:return: A ResultSet for the query.
"""
# Avoid circular imports.
from lp.registry.model.distroseries import DistroSeries
from lp.registry.model.productseries import ProductSeries
subquery = Select(
ProductSeries.productID,
tables=(Packaging, ProductSeries, DistroSeries),
where=And(
Packaging.productseriesID == ProductSeries.id,
Packaging.distroseriesID == DistroSeries.id,
DistroSeries.distribution == self.distribution,
Packaging.sourcepackagename == self.sourcepackagename),
distinct=True)
origin = LeftJoin(
LeftJoin(
POTemplate, ProductSeries,
POTemplate.productseriesID == ProductSeries.id),
DistroSeries,
POTemplate.distroseriesID == DistroSeries.id)
return Store.of(self.distribution).using(origin).find(
POTemplate,
And(
templatename_clause,
Or(
And(
DistroSeries.distribution == self.distribution,
POTemplate.sourcepackagename == self.sourcepackagename),
In(ProductSeries.productID, subquery))))
def _queryByDistribution(self, templatename_clause):
"""Special case when templates are searched across a distribution."""
return Store.of(self.distribution).find(
POTemplate, templatename_clause)
def _queryPOTemplates(self, templatename_clause):
"""Select the right query to be used."""
if self.product is not None:
return self._queryByProduct(templatename_clause)
elif self.sourcepackagename is not None:
return self._queryBySourcepackagename(templatename_clause)
elif self.distribution is not None and self.sourcepackagename is None:
return self._queryByDistribution(templatename_clause)
else:
return EmptyResultSet()
def getSharingPOTemplates(self, potemplate_name):
"""See `IPOTemplateSharingSubset`."""
if self.distribution is not None:
assert self.sourcepackagename is not None, (
"Need sourcepackagename to select from distribution.")
return self._queryPOTemplates(
POTemplate.name == potemplate_name)
def getSharingPOTemplatesByRegex(self, name_pattern=None):
"""See `IPOTemplateSharingSubset`."""
if name_pattern is None:
templatename_clause = True
else:
templatename_clause = (
"potemplate.name ~ %s" % sqlvalues(name_pattern))
return self._queryPOTemplates(templatename_clause)
def getSharingPOTemplateIDs(self, potemplate_name):
"""See `IPOTemplateSharingSubset`."""
return self.getSharingPOTemplates(potemplate_name).values(
POTemplate.id)
def groupEquivalentPOTemplates(self, name_pattern=None):
"""See `IPOTemplateSharingSubset`."""
equivalents = {}
for template in self.getSharingPOTemplatesByRegex(name_pattern):
key = self._get_potemplate_equivalence_class(template)
if key not in equivalents:
equivalents[key] = []
equivalents[key].append(template)
for equivalence_list in equivalents.itervalues():
# Sort potemplates from "most representative" to "least
# representative."
equivalence_list.sort(key=POTemplate.sharingKey, reverse=True)
return equivalents
class POTemplateToTranslationFileDataAdapter:
"""Adapter from `IPOTemplate` to `ITranslationFileData`."""
implements(ITranslationFileData)
def __init__(self, potemplate):
self._potemplate = potemplate
self.messages = self._getMessages()
self.format = potemplate.source_file_format
@cachedproperty
def path(self):
"""See `ITranslationFileData`."""
return self._potemplate.path
@cachedproperty
def translation_domain(self):
"""See `ITranslationFileData`."""
return self._potemplate.translation_domain
@property
def is_template(self):
"""See `ITranslationFileData`."""
return True
@property
def language_code(self):
"""See `ITraslationFile`."""
return None
@cachedproperty
def header(self):
"""See `ITranslationFileData`."""
return self._potemplate.getHeader()
def _getMessages(self):
"""Return a list of `ITranslationMessageData`."""
potemplate = self._potemplate
# Get all rows related to this file. We do this to speed the export
# process so we have a single DB query to fetch all needed
# information.
rows = potemplate.getTranslationRows()
messages = []
for row in rows:
assert row.potemplate.id == potemplate.id, (
'Got a row for a different IPOTemplate.')
# Skip messages which aren't anymore in the PO template.
if row.sequence == 0:
continue
# Create new message set
msgset = TranslationMessageData()
msgset.sequence = row.sequence
msgset.obsolete = False
msgset.msgid_singular = row.msgid_singular
msgset.singular_text = row.potmsgset.singular_text
msgset.msgid_plural = row.msgid_plural
msgset.plural_text = row.potmsgset.plural_text
msgset.context = row.context
msgset.comment = row.comment
msgset.source_comment = row.source_comment
msgset.file_references = row.file_references
if row.flags_comment:
msgset.flags = set([
flag.strip()
for flag in row.flags_comment.split(',')
if flag])
# Store the message.
messages.append(msgset)
return messages
class TranslationTemplatesCollection(Collection):
"""A `Collection` of `POTemplate`."""
starting_table = POTemplate
def restrictProductSeries(self, productseries):
return self.refine(POTemplate.productseriesID == productseries.id)
def restrictDistroSeries(self, distroseries):
return self.refine(POTemplate.distroseriesID == distroseries.id)
def restrictSourcePackageName(self, sourcepackagename):
return self.refine(
POTemplate.sourcepackagenameID == sourcepackagename.id)
def restrictCurrent(self, current_value=True):
"""Select based on `POTemplate.iscurrent`.
:param current_value: The value for `iscurrent` that you are
looking for. Defaults to True, meaning this will restrict
to current templates. If False, will select obsolete
templates instead.
:return: A `TranslationTemplatesCollection` based on this one,
but restricted to ones with the desired `iscurrent` value.
"""
return self.refine(POTemplate.iscurrent == current_value)
def restrictName(self, template_name):
"""Select based on `POTemplate.name`.
:param template: The value for `name` that you are looking for.
:return: A `TranslationTemplatesCollection based on this one but
restricted to ones with the desired `name` value.
"""
return self.refine(POTemplate.name == template_name)
def joinPOFile(self):
"""Join `POFile` into the collection.
:return: A `TranslationTemplatesCollection` with an added inner
join to `POFile`.
"""
return self.joinInner(POFile, POTemplate.id == POFile.potemplateID)
def joinOuterPOFile(self, language=None):
"""Outer-join `POFile` into the collection.
:return: A `TranslationTemplatesCollection` with an added outer
join to `POFile`.
"""
if language is not None:
return self.joinOuter(
POFile, And(POTemplate.id == POFile.potemplateID,
POFile.languageID == language.id))
else:
return self.joinOuter(
POFile, POTemplate.id == POFile.potemplateID)
|