|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import re, collections, textwrap, sys, argparse, platform, os |
| 4 | + |
| 5 | +Field = collections.namedtuple('Field', ['name', 'contents']) |
| 6 | + |
| 7 | +Header = collections.namedtuple('Header', ['module']) |
| 8 | + |
| 9 | +Function = collections.namedtuple('Function', |
| 10 | + ['name', 'purpose', 'remarks', 'template_params', 'inputs', 'returns']) |
| 11 | + |
| 12 | +Class = collections.namedtuple('Class', ['name', 'purpose', 'remarks', 'template_params']) |
| 13 | + |
| 14 | + |
| 15 | +def warn(message): |
| 16 | + """ Print a labelled message to stderr. """ |
| 17 | + sys.stderr.write('Warning: %s\n' % message) |
| 18 | + |
| 19 | + |
| 20 | +def header_from_block(file, block): |
| 21 | + """ Create a Header structure from a parsed Block. """ |
| 22 | + paragraphs = [] |
| 23 | + module = block.fields['Module'].strip() |
| 24 | + if not os.path.basename(file).startswith(module): |
| 25 | + paragraphs.append(module) |
| 26 | + purpose = block.fields.get('Purpose', None) |
| 27 | + if purpose: |
| 28 | + end_of_purpose = purpose.find('@ Copyright') |
| 29 | + if end_of_purpose == -1: |
| 30 | + end_of_purpose = len(purpose) |
| 31 | + paragraphs.append(purpose[ : end_of_purpose ].strip()) |
| 32 | + date = block.fields.get('Date', '') |
| 33 | + end_of_first_date_line = date.find('\n') |
| 34 | + if end_of_first_date_line != -1: |
| 35 | + paragraphs.append(date[ end_of_first_date_line+1 : ].strip()) |
| 36 | + return Header('\n\n'.join(paragraphs)) |
| 37 | + |
| 38 | +def function_from_block(block): |
| 39 | + """ Create a Function structure from a parsed Block. """ |
| 40 | + return Function( |
| 41 | + block.fields.get('Function', None), |
| 42 | + block.fields.get('Purpose', None), |
| 43 | + block.fields.get('Remarks', None), |
| 44 | + block.fields.get('Template parameters', None), |
| 45 | + block.fields.get('Inputs', None), |
| 46 | + block.fields.get('Outputs', block.fields.get('Output', None))) |
| 47 | + |
| 48 | +def class_from_block(block): |
| 49 | + """ Create a Class structure from a parsed Block. """ |
| 50 | + return Class( |
| 51 | + block.fields.get('Class', None), |
| 52 | + block.fields.get('Purpose', None), |
| 53 | + block.fields.get('Remarks', None), |
| 54 | + block.fields.get('Template parameters', None)) |
| 55 | + |
| 56 | +FIELD_PARSE_EXPR = r'\n *Purpose:(.*?)(?=\Z|\n\s*\n\s*(?!NOTE:)(?:[a-zA-Z0-9]+|Template parameters)\s*:)|\n *Date:(.*?)(?=@ Copyright)|\n *([a-zA-Z0-9]+|Template parameters)\s*:\n?(.*?)^$' |
| 57 | + |
| 58 | +def remove_extra_space(text): |
| 59 | + return '\n'.join([line.strip() for line in re.sub(r' +', ' ', text).splitlines()]) |
| 60 | + |
| 61 | +def fill(wrapper, text): |
| 62 | + return '\n'.join([ |
| 63 | + wrapper.fill(match.group(1)) |
| 64 | + for match |
| 65 | + in re.finditer(r'(.*?)(?:^\s*$|\Z)', text, re.MULTILINE | re.DOTALL) |
| 66 | + ]).strip() |
| 67 | + |
| 68 | +def parse_fields(block_contents): |
| 69 | + """ Extract the named fields of an old-style comment block. """ |
| 70 | + |
| 71 | + field_re = re.compile(FIELD_PARSE_EXPR, re.MULTILINE | re.DOTALL) |
| 72 | + for match in field_re.finditer(block_contents): |
| 73 | + if match.lastindex == 1: |
| 74 | + # The field is a Purpose field |
| 75 | + yield Field('Purpose', remove_extra_space(match.group(1))) |
| 76 | + elif match.lastindex == 2: |
| 77 | + # The field is a Date field |
| 78 | + yield Field('Date', remove_extra_space(match.group(2))) |
| 79 | + elif match.group(3): |
| 80 | + # The field is any other field |
| 81 | + yield Field(match.group(3), remove_extra_space(match.group(4))) |
| 82 | + |
| 83 | + |
| 84 | +Block = collections.namedtuple('Block', ['fields']) |
| 85 | + |
| 86 | + |
| 87 | +def has_field(block, field_name): |
| 88 | + """ Return whether the block has a field with the given name. """ |
| 89 | + return field_name in block.fields |
| 90 | + |
| 91 | + |
| 92 | +def make_doxy_comment(text): |
| 93 | + text = re.sub(r'^(?!$)', r'/// ', text, flags=re.MULTILINE) |
| 94 | + text = re.sub(r'^(?=$)', r'///', text, flags=re.MULTILINE) |
| 95 | + return text |
| 96 | + |
| 97 | + |
| 98 | +class GenericFormatter(object): |
| 99 | + def __init__(self, doc_width): |
| 100 | + self.text_wrapper = textwrap.TextWrapper(width=doc_width) |
| 101 | + self.indented_wrapper = textwrap.TextWrapper(width=doc_width, |
| 102 | + subsequent_indent=r' ') |
| 103 | + |
| 104 | + def convert(self, block): |
| 105 | + sections = filter(None, self.convert_sections(block)) |
| 106 | + if sections: |
| 107 | + return make_doxy_comment('\n'.join(sections)) + '\n' |
| 108 | + return '' |
| 109 | + |
| 110 | +HEADER_TEMPLATE = r'// Copyright 2016-2017 DiffBlue Limited. All Rights Reserved.' |
| 111 | + |
| 112 | +class HeaderFormatter(GenericFormatter): |
| 113 | + def format_module(self, header): |
| 114 | + if not header.module: |
| 115 | + return None |
| 116 | + |
| 117 | + # The file directive must be followed by a newline in order to refer to |
| 118 | + # the current file |
| 119 | + return '\\file\n' + fill(self.indented_wrapper, header.module) |
| 120 | + |
| 121 | + def is_block_valid(self, block): |
| 122 | + return has_field(block, 'Module') |
| 123 | + |
| 124 | + def convert_sections(self, block): |
| 125 | + return [self.format_module(block)] |
| 126 | + |
| 127 | + |
| 128 | +class FunctionFormatter(GenericFormatter): |
| 129 | + def __init__(self, doc_width): |
| 130 | + super(FunctionFormatter, self).__init__(doc_width) |
| 131 | + |
| 132 | + def format_purpose(self, block): |
| 133 | + return fill(self.text_wrapper, block.purpose) if block.purpose else None |
| 134 | + |
| 135 | + def format_remarks(self, block): |
| 136 | + return fill( |
| 137 | + self.indented_wrapper, |
| 138 | + r'\remark %s' % block.remarks) if block.remarks else None |
| 139 | + |
| 140 | + def format_template_params(self, function): |
| 141 | + if not function.template_params: |
| 142 | + return None |
| 143 | + |
| 144 | + def param_replacement(match): |
| 145 | + return r'\tparam %s: ' % match.group(1) |
| 146 | + |
| 147 | + text = re.sub(r'\n(?![a-zA-Z0-9_`]+\s*[:-])', ' ', function.template_params, flags=re.MULTILINE) |
| 148 | + text, num_replacements = re.subn( |
| 149 | + r'^([a-zA-Z0-9_`]+)\s*[:-]\s*', |
| 150 | + param_replacement, |
| 151 | + text, |
| 152 | + flags=re.MULTILINE) |
| 153 | + |
| 154 | + if num_replacements == 0: |
| 155 | + return None |
| 156 | + |
| 157 | + return '\n'.join(fill(self.indented_wrapper, param) for param in text.split('\n')) |
| 158 | + |
| 159 | + def format_inputs(self, function): |
| 160 | + if not function.inputs: |
| 161 | + return None |
| 162 | + |
| 163 | + if re.match(r'^\s*\S+\s*$', function.inputs) or function.inputs == 'See purpose': |
| 164 | + return None |
| 165 | + |
| 166 | + def param_replacement(match): |
| 167 | + return r'\param %s: ' % match.group(1) |
| 168 | + |
| 169 | + text = re.sub(r'\n(?![a-zA-Z0-9_`]+\s*[:-])', ' ', function.inputs, flags=re.MULTILINE) |
| 170 | + text, num_replacements = re.subn( |
| 171 | + r'^([a-zA-Z0-9_`]+)\s*[:-]\s*', |
| 172 | + param_replacement, |
| 173 | + text, |
| 174 | + flags=re.MULTILINE) |
| 175 | + |
| 176 | + if num_replacements == 0: |
| 177 | + text = r'\par parameters: %s' % text |
| 178 | + |
| 179 | + return '\n'.join(fill(self.indented_wrapper, param) for param in text.split('\n')) |
| 180 | + |
| 181 | + def format_returns(self, function): |
| 182 | + if not function.returns: |
| 183 | + return None |
| 184 | + if function.returns == 'See purpose': |
| 185 | + return None |
| 186 | + return fill(self.indented_wrapper, r'\return %s' % function.returns) |
| 187 | + |
| 188 | + def is_block_valid(self, block): |
| 189 | + return has_field(block, 'Function') |
| 190 | + |
| 191 | + def convert_sections(self, block): |
| 192 | + return [ |
| 193 | + self.format_purpose(block), |
| 194 | + self.format_remarks(block), |
| 195 | + self.format_template_params(block), |
| 196 | + self.format_inputs(block), |
| 197 | + self.format_returns(block) |
| 198 | + ] |
| 199 | + |
| 200 | + |
| 201 | +class ClassFormatter(GenericFormatter): |
| 202 | + def __init__(self, doc_width): |
| 203 | + super(ClassFormatter, self).__init__(doc_width) |
| 204 | + |
| 205 | + def format_purpose(self, block): |
| 206 | + return fill(self.text_wrapper, block.purpose) if block.purpose else None |
| 207 | + |
| 208 | + def format_remarks(self, block): |
| 209 | + return fill( |
| 210 | + self.indented_wrapper, |
| 211 | + r'\remark %s' % block.remarks) if block.remarks else None |
| 212 | + |
| 213 | + def format_template_params(self, function): |
| 214 | + if not function.template_params: |
| 215 | + return None |
| 216 | + |
| 217 | + def param_replacement(match): |
| 218 | + return r'\tparam %s: ' % match.group(1) |
| 219 | + |
| 220 | + text = re.sub(r'\n(?![a-zA-Z0-9_`]+\s*[:-])', ' ', function.template_params, flags=re.MULTILINE) |
| 221 | + text, num_replacements = re.subn( |
| 222 | + r'^([a-zA-Z0-9_`]+)\s*[:-]\s*', |
| 223 | + param_replacement, |
| 224 | + text, |
| 225 | + flags=re.MULTILINE) |
| 226 | + |
| 227 | + if num_replacements == 0: |
| 228 | + return None |
| 229 | + |
| 230 | + return '\n'.join(fill(self.indented_wrapper, param) for param in text.split('\n')) |
| 231 | + |
| 232 | + def is_block_valid(self, block): |
| 233 | + return has_field(block, 'Class') |
| 234 | + |
| 235 | + def convert_sections(self, block): |
| 236 | + return [ |
| 237 | + self.format_purpose(block), |
| 238 | + self.format_remarks(block), |
| 239 | + self.format_template_params(block) |
| 240 | + ] |
| 241 | + |
| 242 | + |
| 243 | +def replace_block( |
| 244 | + block_contents, |
| 245 | + file_contents, |
| 246 | + file, |
| 247 | + header_formatter, |
| 248 | + class_formatter, |
| 249 | + function_formatter): |
| 250 | + """ |
| 251 | + Replace an old-style documentation block with the doxygen equivalent |
| 252 | + """ |
| 253 | + block = Block( |
| 254 | + {f.name: f.contents for f in parse_fields(block_contents.group(1))}) |
| 255 | + |
| 256 | + if header_formatter.is_block_valid(block): |
| 257 | + converted = header_formatter.convert(header_from_block(file, block)) |
| 258 | + return HEADER_TEMPLATE + '\n\n' + converted + '\n' |
| 259 | + |
| 260 | + if class_formatter.is_block_valid(block): |
| 261 | + return class_formatter.convert(class_from_block(block)) |
| 262 | + |
| 263 | + if function_formatter.is_block_valid(block): |
| 264 | + return function_formatter.convert(function_from_block(block)) |
| 265 | + |
| 266 | + warn('block in "%s" has unrecognised format:\n%s' % |
| 267 | + (file, block_contents.group(1))) |
| 268 | + |
| 269 | + return '' |
| 270 | + |
| 271 | + |
| 272 | +def convert_file(file, inplace): |
| 273 | + """ Replace documentation in file with doxygen-styled comments. """ |
| 274 | + with open(file) as f: |
| 275 | + contents = f.read() |
| 276 | + |
| 277 | + doc_width = 76 |
| 278 | + |
| 279 | + header_formatter = HeaderFormatter(doc_width) |
| 280 | + class_formatter = ClassFormatter(doc_width) |
| 281 | + function_formatter = FunctionFormatter(doc_width) |
| 282 | + |
| 283 | + block_re = re.compile( |
| 284 | + r'^/\*+\\$(.*?)^\\\*+/$\s*', re.MULTILINE | re.DOTALL) |
| 285 | + new_contents = block_re.sub( |
| 286 | + lambda match: replace_block( |
| 287 | + match, |
| 288 | + contents, |
| 289 | + file, |
| 290 | + header_formatter, |
| 291 | + class_formatter, |
| 292 | + function_formatter), contents) |
| 293 | + |
| 294 | + if inplace: |
| 295 | + with open(file, 'w') as f: |
| 296 | + f.write(new_contents) |
| 297 | + else: |
| 298 | + sys.stdout.write(new_contents) |
| 299 | + |
| 300 | + |
| 301 | +def main(): |
| 302 | + """ Run convert_file from the command-line. """ |
| 303 | + parser = argparse.ArgumentParser() |
| 304 | + parser.add_argument('file', type=str, help='The file to process') |
| 305 | + parser.add_argument('-i', '--inplace', action='store_true', |
| 306 | + help='Process in place') |
| 307 | + args = parser.parse_args() |
| 308 | + |
| 309 | + convert_file(args.file, args.inplace) |
| 310 | + |
| 311 | + return 0 |
| 312 | + |
| 313 | + |
| 314 | +if __name__ == '__main__': |
| 315 | + sys.exit(main()) |
0 commit comments