Skip to content

Commit cb71c93

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 6020107 commit cb71c93

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
@@ -1005,9 +1005,10 @@ class MessageMerge(CommandMixin):
10051005
('input-files', None, ''),
10061006
('directory=', 'D', ''),
10071007
('compendium=', 'C', ''),
1008+
('c-overwrite', '', ''),
10081009
('update', 'U', ''),
10091010
('output-file=', 'o', ''),
1010-
('backup=', None, ''),
1011+
('backup', None, ''),
10111012
('suffix=', None, ''),
10121013
('multi-domain', 'm', ''),
10131014
('for-msgfmt', None, ''),
@@ -1055,6 +1056,8 @@ class MessageMerge(CommandMixin):
10551056
'no-wrap',
10561057
'sort-output',
10571058
'sort-by-file',
1059+
'c-overwrite',
1060+
'backup',
10581061
]
10591062

10601063
option_choices = {
@@ -1065,13 +1068,14 @@ def initialize_options(self):
10651068
self.input_files = None #
10661069
self.directory = None
10671070
self.compendium = None #~
1068-
self.update = None
1071+
self.c_overwrite = False #
1072+
self.update = None #
10691073
self.output_file = None #
1070-
self.backup = None
1071-
self.suffix = None
1074+
self.backup = False #
1075+
self.suffix = '~' #
10721076
self.multi_domain = None
10731077
self.for_msgfmt = None
1074-
self.no_fuzzy_matching = None
1078+
self.no_fuzzy_matching = None #
10751079
self.previous = None
10761080
self.properties_input = None
10771081
self.stringtable_input = None
@@ -1095,8 +1099,8 @@ def initialize_options(self):
10951099
def finalize_options(self):
10961100
if not self.input_files or len(self.input_files) != 2:
10971101
raise OptionError('must be two po files')
1098-
if not self.output_file:
1099-
raise OptionError('you must specify the output file')
1102+
if not self.output_file and not self.update:
1103+
raise OptionError('you must specify the output file or update existing')
11001104

11011105
if self.no_wrap and self.width:
11021106
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
@@ -1107,36 +1111,38 @@ def finalize_options(self):
11071111

11081112
def run(self):
11091113
def_file, ref_file = self.input_files
1110-
with open(def_file, 'r') as pofile:
1111-
def_catalog = read_po(pofile)
11121114

1115+
if self.update and self.backup:
1116+
shutil.copy(def_file, def_file + self.suffix)
1117+
1118+
with open(def_file, 'r') as pofile:
1119+
catalog = read_po(pofile)
11131120
with open(ref_file, 'r') as pofile:
11141121
ref_catalog = read_po(pofile)
1115-
1116-
ref_catalog.mime_headers = def_catalog.mime_headers
1117-
ref_catalog.header_comment = def_catalog.header_comment
1118-
1119-
for message in def_catalog:
1120-
if not message.id:
1121-
continue
1122-
if message.id in ref_catalog:
1123-
ref_catalog[message.id].string = message.string
1124-
else:
1125-
ref_catalog.obsolete[message.id] = message
1122+
catalog.update(
1123+
ref_catalog,
1124+
no_fuzzy_matching=self.no_fuzzy_matching
1125+
)
11261126

11271127
if self.compendium:
11281128
with open(self.compendium, 'r') as pofile:
11291129
compendium_catalog = read_po(pofile)
11301130

11311131
for message in compendium_catalog:
1132-
if message.id in ref_catalog and not ref_catalog[message.id].string:
1133-
ref_catalog[message.id].string = message.string
1132+
current = catalog[message.id]
1133+
if message.id in catalog and (not current.string or current.fuzzy or self.c_overwrite):
1134+
if self.c_overwrite and not current.fuzzy and current.string:
1135+
catalog.obsolete[message.id] = current.clone()
11341136

1135-
ref_catalog.fuzzy = False
1136-
with open(self.output_file, 'wb') as outfile:
1137+
current.string = message.string
1138+
current.flags = [flag for flag in current.flags if flag != 'fuzzy']
1139+
current.auto_comments.append(self.compendium)
1140+
1141+
output_path = def_file if self.update else self.output_file
1142+
with open(output_path, 'wb') as outfile:
11371143
write_po(
11381144
outfile,
1139-
ref_catalog,
1145+
catalog,
11401146
width=self.width,
11411147
sort_by_file=self.sort_by_file,
11421148
sort_output=self.sort_output,

0 commit comments

Comments
 (0)