Skip to content

Commit 9a217e2

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 246671a commit 9a217e2

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
@@ -20,6 +20,7 @@
2020
import sys
2121
import tempfile
2222
import warnings
23+
from collections import OrderedDict, defaultdict
2324
from configparser import RawConfigParser
2425
from io import StringIO
2526
from typing import BinaryIO, Iterable, Literal
@@ -910,20 +911,20 @@ class MessageConcatenation(CommandMixin):
910911
}
911912

912913
def initialize_options(self):
913-
self.input_files = None
914+
self.input_files = None #
914915
self.files_from = None
915916
self.directory = None
916-
self.output_file = None
917-
self.less_than = None
918-
self.more_than = None
919-
self.unique = None
917+
self.output_file = None #
918+
self.less_than = None #
919+
self.more_than = 0 #
920+
self.unique = False #
920921
self.properties_input = None
921922
self.stringtable_input = None
922923
self.to_code = None
923-
self.use_first = None
924+
# временно всегда используется первый перевод
925+
self.use_first = True #~
924926
self.lang = None
925927
self.color = None
926-
self.color = None
927928
self.style = None
928929
self.no_escape = None
929930
self.escape = None
@@ -934,8 +935,8 @@ def initialize_options(self):
934935
self.strict = None
935936
self.properties_output = None
936937
self.stringtable_output = None
937-
self.width = None
938-
self.no_wrap = None
938+
self.width = None #
939+
self.no_wrap = None #
939940
self.sort_output = None
940941
self.sort_by_file = None
941942

@@ -945,32 +946,61 @@ def finalize_options(self):
945946
if not self.output_file:
946947
raise OptionError('you must specify the output file')
947948

948-
# временно всегда используется первый перевод
949+
if self.unique is None:
950+
self.unique = False
949951
if self.use_first is None:
950952
self.use_first = True
951953

954+
if self.no_wrap and self.width:
955+
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
956+
if not self.no_wrap and not self.width:
957+
self.width = 76
958+
elif self.width is not None:
959+
self.width = int(self.width)
960+
961+
if self.more_than is None:
962+
self.more_than = 0
963+
else:
964+
self.more_than = int(self.more_than)
965+
if self.less_than is not None:
966+
self.less_than = int(self.less_than)
967+
if self.unique:
968+
self.less_than = 2
969+
970+
def _prepare(self):
971+
self.message_count = defaultdict(int)
972+
973+
for filename in self.input_files:
974+
with open(filename, 'r') as pofile:
975+
template = read_po(pofile)
976+
for message in template:
977+
self.message_count[message.id] += 1
978+
952979
def run(self):
953980
catalog = Catalog(fuzzy=False)
981+
self._prepare()
954982

955-
for filenum, filename in enumerate(self.input_files):
983+
for filename in self.input_files:
956984
with open(filename, 'r') as pofile:
957985
template = read_po(pofile)
958986

959-
if filenum == 0:
960-
catalog.update(template)
961-
continue
962-
963987
for message in template:
964988
if not message.id:
965989
continue
966990

967991
if message.id in catalog and catalog[message.id].string != message.string and not self.use_first:
968992
raise NotImplementedError()
969993

970-
catalog[message.id] = message
994+
message_count = self.message_count[message.id]
995+
if message_count > self.more_than and (self.less_than is None or message_count < self.less_than):
996+
catalog[message.id] = message
971997

972998
with open(self.output_file, 'wb') as outfile:
973-
write_po(outfile, catalog)
999+
write_po(
1000+
outfile,
1001+
catalog,
1002+
width=self.width
1003+
)
9741004

9751005

9761006
class MessageMerge(CommandMixin):

0 commit comments

Comments
 (0)