backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / docs / reformat-news.py
blob955ce2d45b730f70e166afae53436094924e86dc
1 #!/usr/bin/env python
3 # reformat-news.py: Reformat the NEWS file properly
5 # Copyright (C) 2017 Red Hat, Inc.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library. If not, see
19 # <http://www.gnu.org/licenses/>.
21 from __future__ import print_function
23 import sys
25 COLUMNS = 80
27 def reformat_with_indent(text, initial_indent, indent):
29 res = ""
30 line = initial_indent
32 for word in text.split():
34 # If adding one more word (plus a whitespace, plus a newline)
35 # to the current line would push us over the desired number
36 # of columns we start a new line instead
37 if len(line) + len(word) > (COLUMNS - 2):
38 res = res + line + "\n"
39 line = indent
41 # We need to take care when we've just started a new line,
42 # as we don't want to add any additional leading whitespace
43 # in that case
44 if line == indent or line == initial_indent:
45 line = line + word
46 else:
47 line = line + " " + word
49 # Append whatever's left
50 res = res + line
52 return res
55 def reformat(line):
57 # Empty lines don't need to be reformatted or even inspected
58 if len(line) == 0:
59 return line
61 # For all non-empty lines, we decide the indentation level based
62 # on the first character
63 marker = line[0]
65 # Release
66 if marker == '#':
67 initial_indent = 0
68 indent = 2
69 # Section
70 elif marker == '*':
71 initial_indent = 2
72 indent = 4
73 # Change summary
74 elif marker == '-':
75 initial_indent = 4
76 indent = 6
77 # Change description
78 elif marker == '|':
79 initial_indent = 8
80 indent = 8
81 # In this one case, the marker should not ultimately show
82 # up in the output file, so we strip it before moving on
83 line = line[1:]
84 # Anything else should be left as-is
85 else:
86 return line
88 return reformat_with_indent(line, " " * initial_indent, " " * indent)
91 def main(args):
93 if len(args) < 2:
94 sys.stdout.write("Usage: " + args[0] + " FILE\n")
95 sys.exit(1)
97 with open(args[1], 'r') as f:
98 for line in f:
99 print(reformat(line.strip()))
102 if __name__ == "__main__":
103 main(sys.argv)