Skip to content

Commit a1bf8d4

Browse files
committed
Implement basic msgmerge logic for working with a compendium
* Implement basic functionality of msgmerge * Use and validate the main options: input-files and output-file * Use and validate options: no-wrap and width * Use and validate options: sort-output and sort-by-file, both in msgmerge and msgcat * In the basic version of working with a compendium, a translation for a message is taken from the compendium only if the resulting catalog lacks a translation.
1 parent 9a217e2 commit a1bf8d4

1 file changed

Lines changed: 67 additions & 12 deletions

File tree

babel/messages/frontend.py

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,8 @@ def initialize_options(self):
937937
self.stringtable_output = None
938938
self.width = None #
939939
self.no_wrap = None #
940-
self.sort_output = None
941-
self.sort_by_file = None
940+
self.sort_output = False #
941+
self.sort_by_file = False #
942942

943943
def finalize_options(self):
944944
if not self.input_files:
@@ -967,6 +967,11 @@ def finalize_options(self):
967967
if self.unique:
968968
self.less_than = 2
969969

970+
if self.sort_output is None:
971+
self.sort_output = False
972+
if self.sort_by_file is None:
973+
self.sort_by_file = True
974+
970975
def _prepare(self):
971976
self.message_count = defaultdict(int)
972977

@@ -999,7 +1004,9 @@ def run(self):
9991004
write_po(
10001005
outfile,
10011006
catalog,
1002-
width=self.width
1007+
width=self.width,
1008+
sort_by_file=self.sort_by_file,
1009+
sort_output=self.sort_output,
10031010
)
10041011

10051012

@@ -1066,11 +1073,11 @@ class MessageMerge(CommandMixin):
10661073
}
10671074

10681075
def initialize_options(self):
1069-
self.input_files = None
1076+
self.input_files = None #
10701077
self.directory = None
1071-
self.compendium = None
1078+
self.compendium = None #~
10721079
self.update = None
1073-
self.output_file = None
1080+
self.output_file = None #
10741081
self.backup = None
10751082
self.suffix = None
10761083
self.multi_domain = None
@@ -1091,16 +1098,64 @@ def initialize_options(self):
10911098
self.strict = None
10921099
self.properties_output = None
10931100
self.stringtable_output = None
1094-
self.width = None
1095-
self.no_wrap = None
1096-
self.sort_output = None
1097-
self.sort_by_file = None
1101+
self.width = None #
1102+
self.no_wrap = None #
1103+
self.sort_output = False #
1104+
self.sort_by_file = False #
10981105

10991106
def finalize_options(self):
1100-
pass
1107+
if len(self.input_files) != 2:
1108+
raise OptionError('must be two po files')
1109+
if not self.output_file:
1110+
raise OptionError('you must specify the output file')
1111+
1112+
if self.no_wrap and self.width:
1113+
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
1114+
if not self.no_wrap and not self.width:
1115+
self.width = 76
1116+
elif self.width is not None:
1117+
self.width = int(self.width)
1118+
1119+
if self.sort_output is None:
1120+
self.sort_output = False
1121+
if self.sort_by_file is None:
1122+
self.sort_by_file = True
11011123

11021124
def run(self):
1103-
pass
1125+
def_file, ref_file = self.input_files
1126+
with open(def_file, 'r') as pofile:
1127+
def_catalog = read_po(pofile)
1128+
1129+
with open(ref_file, 'r') as pofile:
1130+
ref_catalog = read_po(pofile)
1131+
1132+
ref_catalog.mime_headers = def_catalog.mime_headers
1133+
ref_catalog.header_comment = def_catalog.header_comment
1134+
1135+
for message in def_catalog:
1136+
if not message.id:
1137+
continue
1138+
if message.id in ref_catalog:
1139+
ref_catalog[message.id].string = message.string
1140+
else:
1141+
ref_catalog.obsolete[message.id] = message
1142+
1143+
if self.compendium:
1144+
with open(self.compendium, 'r') as pofile:
1145+
compendium_catalog = read_po(pofile)
1146+
for message in compendium_catalog:
1147+
if message.id in ref_catalog and not ref_catalog[message.id].string:
1148+
ref_catalog[message.id].string = message.string
1149+
1150+
ref_catalog.fuzzy = False
1151+
with open(self.output_file, 'wb') as outfile:
1152+
write_po(
1153+
outfile,
1154+
ref_catalog,
1155+
width=self.width,
1156+
sort_by_file=self.sort_by_file,
1157+
sort_output=self.sort_output,
1158+
)
11041159

11051160

11061161
class CommandLineInterface:

0 commit comments

Comments
 (0)