Skip to content

Commit f7ddd85

Browse files
committed
Add options update, backup, and c_overwrite for a different compendium handling logic
* Implement `update` to update the source file instead of writing to the current output file * Implement `backup` to save a backup of the source file before making any updates * Implement `c_overwrite` to use a new mode of handling the compendium, where translations from the compendium overwrite messages in the output file
1 parent 5a445c4 commit f7ddd85

1 file changed

Lines changed: 31 additions & 25 deletions

File tree

babel/messages/frontend.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,10 @@ class MessageMerge(CommandMixin):
10401040
('input-files', None, ''),
10411041
('directory=', 'D', ''),
10421042
('compendium=', 'C', ''),
1043+
('c-overwrite', '', ''),
10431044
('update', 'U', ''),
10441045
('output-file=', 'o', ''),
1045-
('backup=', None, ''),
1046+
('backup', None, ''),
10461047
('suffix=', None, ''),
10471048
('multi-domain', 'm', ''),
10481049
('for-msgfmt', None, ''),
@@ -1090,6 +1091,8 @@ class MessageMerge(CommandMixin):
10901091
'no-wrap',
10911092
'sort-output',
10921093
'sort-by-file',
1094+
'c-overwrite',
1095+
'backup',
10931096
]
10941097

10951098
option_choices = {
@@ -1100,13 +1103,14 @@ def initialize_options(self):
11001103
self.input_files = None #
11011104
self.directory = None
11021105
self.compendium = None #~
1103-
self.update = None
1106+
self.c_overwrite = False #
1107+
self.update = None #
11041108
self.output_file = None #
1105-
self.backup = None
1106-
self.suffix = None
1109+
self.backup = False #
1110+
self.suffix = '~' #
11071111
self.multi_domain = None
11081112
self.for_msgfmt = None
1109-
self.no_fuzzy_matching = None
1113+
self.no_fuzzy_matching = None #
11101114
self.previous = None
11111115
self.properties_input = None
11121116
self.stringtable_input = None
@@ -1130,8 +1134,8 @@ def initialize_options(self):
11301134
def finalize_options(self):
11311135
if not self.input_files or len(self.input_files) != 2:
11321136
raise OptionError('must be two po files')
1133-
if not self.output_file:
1134-
raise OptionError('you must specify the output file')
1137+
if not self.output_file and not self.update:
1138+
raise OptionError('you must specify the output file or update existing')
11351139

11361140
if self.no_wrap and self.width:
11371141
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
@@ -1142,36 +1146,38 @@ def finalize_options(self):
11421146

11431147
def run(self):
11441148
def_file, ref_file = self.input_files
1145-
with open(def_file, 'r') as pofile:
1146-
def_catalog = read_po(pofile)
11471149

1150+
if self.update and self.backup:
1151+
shutil.copy(def_file, def_file + self.suffix)
1152+
1153+
with open(def_file, 'r') as pofile:
1154+
catalog = read_po(pofile)
11481155
with open(ref_file, 'r') as pofile:
11491156
ref_catalog = read_po(pofile)
1150-
1151-
ref_catalog.mime_headers = def_catalog.mime_headers
1152-
ref_catalog.header_comment = def_catalog.header_comment
1153-
1154-
for message in def_catalog:
1155-
if not message.id:
1156-
continue
1157-
if message.id in ref_catalog:
1158-
ref_catalog[message.id].string = message.string
1159-
else:
1160-
ref_catalog.obsolete[message.id] = message
1157+
catalog.update(
1158+
ref_catalog,
1159+
no_fuzzy_matching=self.no_fuzzy_matching
1160+
)
11611161

11621162
if self.compendium:
11631163
with open(self.compendium, 'r') as pofile:
11641164
compendium_catalog = read_po(pofile)
11651165

11661166
for message in compendium_catalog:
1167-
if message.id in ref_catalog and not ref_catalog[message.id].string:
1168-
ref_catalog[message.id].string = message.string
1167+
current = catalog[message.id]
1168+
if message.id in catalog and (not current.string or current.fuzzy or self.c_overwrite):
1169+
if self.c_overwrite and not current.fuzzy and current.string:
1170+
catalog.obsolete[message.id] = current.clone()
11691171

1170-
ref_catalog.fuzzy = False
1171-
with open(self.output_file, 'wb') as outfile:
1172+
current.string = message.string
1173+
current.flags = [flag for flag in current.flags if flag != 'fuzzy']
1174+
current.auto_comments.append(self.compendium)
1175+
1176+
output_path = def_file if self.update else self.output_file
1177+
with open(output_path, 'wb') as outfile:
11721178
write_po(
11731179
outfile,
1174-
ref_catalog,
1180+
catalog,
11751181
width=self.width,
11761182
sort_by_file=self.sort_by_file,
11771183
sort_output=self.sort_output,

0 commit comments

Comments
 (0)