Skip to content

Commit 6d3212b

Browse files
committed
Add options: unique, less-than, more-than, no-wrap, and width
* Implement options unique, less-than, and more-than, and validate their dependencies with each other. * These options specify which messages to include in the output file. * Implement and validate options no-wrap and width. * Create a helper function _prepare that collects data on message occurrences across different catalogs. * Mark options that are already implemented #
1 parent a5f6295 commit 6d3212b

1 file changed

Lines changed: 47 additions & 17 deletions

File tree

babel/messages/frontend.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import sys
2222
import tempfile
2323
import warnings
24+
from collections import OrderedDict, defaultdict
2425
from configparser import RawConfigParser
2526
from io import StringIO
2627
from typing import Any, BinaryIO, Iterable, Literal
@@ -945,20 +946,20 @@ class MessageConcatenation(CommandMixin):
945946
}
946947

947948
def initialize_options(self):
948-
self.input_files = None
949+
self.input_files = None #
949950
self.files_from = None
950951
self.directory = None
951-
self.output_file = None
952-
self.less_than = None
953-
self.more_than = None
954-
self.unique = None
952+
self.output_file = None #
953+
self.less_than = None #
954+
self.more_than = 0 #
955+
self.unique = False #
955956
self.properties_input = None
956957
self.stringtable_input = None
957958
self.to_code = None
958-
self.use_first = None
959+
# временно всегда используется первый перевод
960+
self.use_first = True #~
959961
self.lang = None
960962
self.color = None
961-
self.color = None
962963
self.style = None
963964
self.no_escape = None
964965
self.escape = None
@@ -969,8 +970,8 @@ def initialize_options(self):
969970
self.strict = None
970971
self.properties_output = None
971972
self.stringtable_output = None
972-
self.width = None
973-
self.no_wrap = None
973+
self.width = None #
974+
self.no_wrap = None #
974975
self.sort_output = None
975976
self.sort_by_file = None
976977

@@ -980,32 +981,61 @@ def finalize_options(self):
980981
if not self.output_file:
981982
raise OptionError('you must specify the output file')
982983

983-
# временно всегда используется первый перевод
984+
if self.unique is None:
985+
self.unique = False
984986
if self.use_first is None:
985987
self.use_first = True
986988

989+
if self.no_wrap and self.width:
990+
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
991+
if not self.no_wrap and not self.width:
992+
self.width = 76
993+
elif self.width is not None:
994+
self.width = int(self.width)
995+
996+
if self.more_than is None:
997+
self.more_than = 0
998+
else:
999+
self.more_than = int(self.more_than)
1000+
if self.less_than is not None:
1001+
self.less_than = int(self.less_than)
1002+
if self.unique:
1003+
self.less_than = 2
1004+
1005+
def _prepare(self):
1006+
self.message_count = defaultdict(int)
1007+
1008+
for filename in self.input_files:
1009+
with open(filename, 'r') as pofile:
1010+
template = read_po(pofile)
1011+
for message in template:
1012+
self.message_count[message.id] += 1
1013+
9871014
def run(self):
9881015
catalog = Catalog(fuzzy=False)
1016+
self._prepare()
9891017

990-
for filenum, filename in enumerate(self.input_files):
1018+
for filename in self.input_files:
9911019
with open(filename, 'r') as pofile:
9921020
template = read_po(pofile)
9931021

994-
if filenum == 0:
995-
catalog.update(template)
996-
continue
997-
9981022
for message in template:
9991023
if not message.id:
10001024
continue
10011025

10021026
if message.id in catalog and catalog[message.id].string != message.string and not self.use_first:
10031027
raise NotImplementedError()
10041028

1005-
catalog[message.id] = message
1029+
message_count = self.message_count[message.id]
1030+
if message_count > self.more_than and (self.less_than is None or message_count < self.less_than):
1031+
catalog[message.id] = message
10061032

10071033
with open(self.output_file, 'wb') as outfile:
1008-
write_po(outfile, catalog)
1034+
write_po(
1035+
outfile,
1036+
catalog,
1037+
width=self.width
1038+
)
10091039

10101040

10111041
class MessageMerge(CommandMixin):

0 commit comments

Comments
 (0)