Skip to content

Commit 8695769

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 6d3212b commit 8695769

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
@@ -972,8 +972,8 @@ def initialize_options(self):
972972
self.stringtable_output = None
973973
self.width = None #
974974
self.no_wrap = None #
975-
self.sort_output = None
976-
self.sort_by_file = None
975+
self.sort_output = False #
976+
self.sort_by_file = False #
977977

978978
def finalize_options(self):
979979
if not self.input_files:
@@ -1002,6 +1002,11 @@ def finalize_options(self):
10021002
if self.unique:
10031003
self.less_than = 2
10041004

1005+
if self.sort_output is None:
1006+
self.sort_output = False
1007+
if self.sort_by_file is None:
1008+
self.sort_by_file = True
1009+
10051010
def _prepare(self):
10061011
self.message_count = defaultdict(int)
10071012

@@ -1034,7 +1039,9 @@ def run(self):
10341039
write_po(
10351040
outfile,
10361041
catalog,
1037-
width=self.width
1042+
width=self.width,
1043+
sort_by_file=self.sort_by_file,
1044+
sort_output=self.sort_output,
10381045
)
10391046

10401047

@@ -1101,11 +1108,11 @@ class MessageMerge(CommandMixin):
11011108
}
11021109

11031110
def initialize_options(self):
1104-
self.input_files = None
1111+
self.input_files = None #
11051112
self.directory = None
1106-
self.compendium = None
1113+
self.compendium = None #~
11071114
self.update = None
1108-
self.output_file = None
1115+
self.output_file = None #
11091116
self.backup = None
11101117
self.suffix = None
11111118
self.multi_domain = None
@@ -1126,16 +1133,64 @@ def initialize_options(self):
11261133
self.strict = None
11271134
self.properties_output = None
11281135
self.stringtable_output = None
1129-
self.width = None
1130-
self.no_wrap = None
1131-
self.sort_output = None
1132-
self.sort_by_file = None
1136+
self.width = None #
1137+
self.no_wrap = None #
1138+
self.sort_output = False #
1139+
self.sort_by_file = False #
11331140

11341141
def finalize_options(self):
1135-
pass
1142+
if len(self.input_files) != 2:
1143+
raise OptionError('must be two po files')
1144+
if not self.output_file:
1145+
raise OptionError('you must specify the output file')
1146+
1147+
if self.no_wrap and self.width:
1148+
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
1149+
if not self.no_wrap and not self.width:
1150+
self.width = 76
1151+
elif self.width is not None:
1152+
self.width = int(self.width)
1153+
1154+
if self.sort_output is None:
1155+
self.sort_output = False
1156+
if self.sort_by_file is None:
1157+
self.sort_by_file = True
11361158

11371159
def run(self):
1138-
pass
1160+
def_file, ref_file = self.input_files
1161+
with open(def_file, 'r') as pofile:
1162+
def_catalog = read_po(pofile)
1163+
1164+
with open(ref_file, 'r') as pofile:
1165+
ref_catalog = read_po(pofile)
1166+
1167+
ref_catalog.mime_headers = def_catalog.mime_headers
1168+
ref_catalog.header_comment = def_catalog.header_comment
1169+
1170+
for message in def_catalog:
1171+
if not message.id:
1172+
continue
1173+
if message.id in ref_catalog:
1174+
ref_catalog[message.id].string = message.string
1175+
else:
1176+
ref_catalog.obsolete[message.id] = message
1177+
1178+
if self.compendium:
1179+
with open(self.compendium, 'r') as pofile:
1180+
compendium_catalog = read_po(pofile)
1181+
for message in compendium_catalog:
1182+
if message.id in ref_catalog and not ref_catalog[message.id].string:
1183+
ref_catalog[message.id].string = message.string
1184+
1185+
ref_catalog.fuzzy = False
1186+
with open(self.output_file, 'wb') as outfile:
1187+
write_po(
1188+
outfile,
1189+
ref_catalog,
1190+
width=self.width,
1191+
sort_by_file=self.sort_by_file,
1192+
sort_output=self.sort_output,
1193+
)
11391194

11401195

11411196
class CommandLineInterface:

0 commit comments

Comments
 (0)