forked from apache/cassandra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_yaml_to_rst.py
145 lines (117 loc) · 4.83 KB
/
convert_yaml_to_rst.py
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A script to convert cassandra.yaml into ReStructuredText for
the online documentation.
Usage:
convert_yaml_to_rest.py conf/cassandra.yaml docs/source/conf.rst
"""
import sys
import re
# Detects options, whether commented or uncommented.
# Group 1 will be non-empty if the option is commented out.
# Group 2 will contain the option name.
# Group 3 will contain the default value, if one exists.
option_re = re.compile(r"^(# ?)?([a-z0-9_]+): ?([^/].*)")
# Detects normal comment lines.
commented_re = re.compile(r"^# ?(.*)")
# A set of option names that have complex values (i.e. lists or dicts).
# This list is hardcoded because there did not seem to be another
# good way to reliably detect this case, especially considering
# that these can be commented out (making it useless to use a yaml parser).
COMPLEX_OPTIONS = (
'seed_provider',
'data_file_directories',
'commitlog_compression',
'hints_compression',
'server_encryption_options',
'client_encryption_options',
'transparent_data_encryption_options',
'hinted_handoff_disabled_datacenters'
)
def convert(yaml_file, dest_file):
with open(yaml_file, 'r') as f:
# Trim off the boilerplate header
lines = f.readlines()[7:]
with open(dest_file, 'w') as outfile:
outfile.write(".. _cassandra-yaml:\n")
outfile.write("\n")
outfile.write("Cassandra Configuration File\n")
outfile.write("============================\n")
# since comments preceed an option, this holds all of the comment
# lines we've seen since the last option
comments_since_last_option = []
line_iter = iter(lines)
while True:
try:
line = next(line_iter)
except StopIteration:
break
match = option_re.match(line)
if match:
option_name = match.group(2)
is_commented = bool(match.group(1))
is_complex = option_name in COMPLEX_OPTIONS
complex_option = read_complex_option(line_iter) if is_complex else None
write_section_header(option_name, outfile)
write_comments(comments_since_last_option, is_commented, outfile)
if is_complex:
write_complex_option(complex_option, outfile)
else:
maybe_write_default_value(match, outfile)
comments_since_last_option = []
else:
comment_match = commented_re.match(line)
if comment_match:
comments_since_last_option.append(comment_match.group(1))
elif line == "\n":
comments_since_last_option.append('')
def write_section_header(option_name, outfile):
outfile.write("\n")
outfile.write("``%s``\n" % (option_name,))
outfile.write("-" * (len(option_name) + 4) + "\n")
def write_comments(comment_lines, is_commented, outfile):
if is_commented:
outfile.write("*This option is commented out by default.*\n")
for comment in comment_lines:
if "SAFETY THRESHOLDS" not in comment_lines:
outfile.write(comment + "\n")
def maybe_write_default_value(option_match, outfile):
default_value = option_match.group(3)
if default_value and default_value != "\n":
outfile.write("\n*Default Value:* %s\n" % (default_value,))
def read_complex_option(line_iter):
option_lines = []
try:
while True:
line = next(line_iter)
if line == '\n':
return option_lines
else:
option_lines.append(line)
except StopIteration:
return option_lines
def write_complex_option(lines, outfile):
outfile.write("\n*Default Value (complex option)*::\n\n")
for line in lines:
outfile.write((" " * 4) + line)
if __name__ == '__main__':
if len(sys.argv) != 3:
print >> sys.stderr, "Usage: %s <yaml source file> <rst dest file>" % (sys.argv[0],)
sys.exit(1)
yaml_file = sys.argv[1]
dest_file = sys.argv[2]
convert(yaml_file, dest_file)