blob: 57cd5ee2e30f61c3f61c6c0219fd4e17dca5b1ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Reorder Message File
#
# Reads a message file into memory, then outputs it with the messages and
# associated descriptions in alphabetical order.
#
# Invocation:
# The code is invoked using the command line:
#
# python reorder.py message_file
#
# Output is written to stdout.
import sys
def remove_empty_leading_trailing(lines):
"""
Removes leading and trailing empty lines.
A list of strings is passed as argument, some of which may be empty.
This function removes from the start and end of the list a contiguous
sequence of empty lines and returns the result. Embedded sequences of
empty lines are not touched.
Parameters:
lines List of strings to be modified.
Return:
Input list of strings with leading/trailing blank line sequences
removed.
"""
retlines = []
# Dispose of degenerate case of empty array
if len(lines) == 0:
return retlines
# Search for first non-blank line
start = 0
while start < len(lines):
if len(lines[start]) > 0:
break
start = start + 1
# Handle case when entire list is empty
if start >= len(lines):
return retlines
# Search for last non-blank line
finish = len(lines) - 1
while finish >= 0:
if len(lines[finish]) > 0:
break
finish = finish - 1
retlines = lines[start:finish + 1]
return retlines
def canonicalise_message_line(line):
"""
Given a line known to start with the '%' character (i.e. a line
introducing a message), canonicalise it by ensuring that the result
is of the form '%<single-space>MESSAGE_IDENTIFIER<single-space>text'.
Parameters:
line - input line. Known to start with a '%' and to have leading
and trailing spaces removed.
Return:
Canonicalised line.
"""
# Cope with degenerate case of a single "%"
if len(line) == 1:
return line
# Get the rest of the line
line = line[1:].lstrip()
# Extract the first word (the message ID)
words = line.split()
message_line = "% " + words[0]
# ... and now the rest of the line
if len(line) > len(words[0]):
message_line = message_line + " " + line[len(words[0]):].lstrip()
return message_line
def make_dict(lines):
"""
Split the lines into segments starting with the message definition and
place into a dictionary.
Parameters:
lines - list of lines containing the text of the message file (less the
header).
Returns:
dictionary - map of the messages, keyed by the line that holds the message
ID.
"""
dictionary = {}
message_key = canonicalise_message_line(lines[0])
message_lines = [message_key]
index = 1;
while index < len(lines):
if lines[index].startswith("%"):
# Start of new message
dictionary[message_key] = remove_empty_leading_trailing(message_lines)
message_key = canonicalise_message_line(lines[index])
message_lines = [message_key]
else:
message_lines.append(lines[index])
index = index + 1
dictionary[message_key] = remove_empty_leading_trailing(message_lines)
return dictionary
def print_dict(dictionary):
"""
Prints the dictionary with a blank line between entries.
Parameters:
dictionary - Map holding the message dictionary
"""
count = 0
for msgid in sorted(dictionary):
# Blank line before all entries but the first
if count > 0:
print("")
count = count + 1
# ... and the entry itself.
for l in dictionary[msgid]:
print(l.strip())
def process_file(filename):
"""
Processes a file by reading it and searching for the first line starting
with the '%' sign. Everything before that line is treated as the file
header and is copied to the output with leading and trailing spaces removed.
After that, each message block is read and stored for later sorting.
Parameters:
filename Name of the message file to process
"""
lines = open(filename).read().splitlines()
# Search for the first line starting with the percent character. Everything
# before it is considered the file header and is copied to the output with
# leading and trailing spaces removed.
index = 0
while index < len(lines):
if lines[index].startswith("%"):
break
print(lines[index].strip())
index = index + 1
# Now put the remaining lines into the message dictionary
dictionary = make_dict(lines[index:])
# ...and print it
print_dict(dictionary)
# Main program
if __name__ == "__main__":
# Read the files and load the data
if len(sys.argv) != 2:
print("Usage: python reorder.py message_file")
else:
process_file(sys.argv[1])
|