Debian Codegraph @

po/fix_newlines.py

raw symbols SCIP
1 import os
2 import sys
3
4 def process_file(input_file, output_file):
5 with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
6 lines = infile.readlines()
7 i = 0
8
9 while i < len(lines):
10 msgid = lines[i]
11 buf_msgid = []
12 buf_msgstr = []
13 if msgid.startswith("msgid"):
14 while lines[i+1].startswith("\""):
15 buf_msgid.append(lines[i])
16 i += 1
17 msgid = lines[i]
18 msgid_has_nl = lines[i].endswith('\\n\"\n')
19
20 # Read the corresponding msgstr line
21 i += 1
22 while i+1 < len(lines) and lines[i+1].startswith("\""):
23 buf_msgstr.append(lines[i])
24 i += 1
25 msgstr = lines[i]
26 msgstr_has_nl = lines[i].endswith('\\n\"\n')
27
28 msgstr = lines[i]
29 msgstr_has_nl = lines[i].endswith('\\n\"\n')
30 # Check if msgid ends with a newline
31 if msgstr_has_nl and not msgid_has_nl:
32 msgstr = msgstr[:-4] + '\"\n' # Remove the newline character from msgstr
33 if not msgstr_has_nl and msgid_has_nl:
34 msgstr = msgstr[:-2] + '\\n\"\n' # Add the newline character to msgstr
35
36 # Write the modified lines to the output file
37 outfile.write("".join(buf_msgid))
38 outfile.write(msgid)
39 outfile.write("".join(buf_msgstr))
40 outfile.write(msgstr)
41 else:
42 outfile.write(msgid)
43 outfile.flush()
44
45 i += 1
46
47 # Example usage
48 input_file = sys.argv[1] # Replace with your input file name
49 output_file = input_file+".1" # Replace with your desired output file name
50
51 process_file(input_file, output_file)
52 os.rename(output_file, input_file)