def update_documentation(rules): """Generate documentation""" # Update the overview of all rules in the linter filename = 'docs/rules.md' # Sort rules by the Rule ID sorted_rules = sorted(rules, key=lambda obj: obj.id) data = [] # Read current file up to the Rules part, everything up to that point is # static documentation. with open(filename, 'r') as origial_file: line = origial_file.readline() while line: data.append(line) if line == '## Rules\n': break line = origial_file.readline() # Rebuild the file content with open(filename, 'w') as new_file: # Rewrite the static documentation for line in data: new_file.write(line) # Add the rules new_file.write('The following **{}** rules are applied by this linter:\n\n'.format(len(sorted_rules))) new_file.write('| Rule ID | Title | Description | Source | Tags |\n') new_file.write('| -------- | ----- | ----------- | ------ | ---- |\n') rule_output = '| {0} <a name="{0}"></a> | {1} | {2} | [Source]({3}) | {4} |\n' # Add system Errors (hardcoded) parseerror = cfnlint.ParseError() tags = ','.join('`{0}`'.format(tag) for tag in parseerror.tags) new_file.write(rule_output.format( parseerror.id, parseerror.shortdesc, parseerror.description, '', tags)) transformerror = cfnlint.TransformError() tags = ','.join('`{0}`'.format(tag) for tag in transformerror.tags) new_file.write(rule_output.format( transformerror.id, transformerror.shortdesc, transformerror.description, '', tags)) ruleerror = cfnlint.RuleError() tags = ','.join('`{0}`'.format(tag) for tag in ruleerror.tags) new_file.write( rule_output.format(ruleerror.id, ruleerror.shortdesc, ruleerror.description, '', tags)) for rule in sorted_rules: tags = ','.join('`{0}`'.format(tag) for tag in rule.tags) new_file.write(rule_output.format(rule.id, rule.shortdesc, rule.description, rule.source_url, tags))
def update_documentation(rules): """Generate documentation""" # Update the overview of all rules in the linter filename = 'docs/rules.md' # Sort rules by the Rule ID sorted_rules = sorted(rules, key=lambda obj: obj.id) data = [] # Read current file up to the Rules part, everything up to that point is # static documentation. with open(filename, 'r') as origial_file: line = origial_file.readline() while line: data.append(line) if line == '## Rules\n': break line = origial_file.readline() # Rebuild the file content with open(filename, 'w') as new_file: # Rewrite the static documentation for line in data: new_file.write(line) # Add the rules new_file.write( 'The following **{}** rules are applied by this linter:\n'.format( len(sorted_rules))) new_file.write( '(_This documentation is generated from the Rules, do not alter this manually_)\n\n' ) new_file.write( '| Rule ID | Title | Description | Config<br />(Name:Type:Default) | Source | Tags |\n' ) new_file.write( '| -------- | ----- | ----------- | ---------- | ------ | ---- |\n' ) rule_output = '| {0}<a name="{0}"></a> | {1} | {2} | {3} | [Source]({4}) | {5} |\n' # Add system Errors (hardcoded) parseerror = cfnlint.ParseError() tags = ','.join('`{0}`'.format(tag) for tag in parseerror.tags) new_file.write( rule_output.format(parseerror.id, parseerror.shortdesc, parseerror.description, '', '', tags)) transformerror = cfnlint.TransformError() tags = ','.join('`{0}`'.format(tag) for tag in transformerror.tags) new_file.write( rule_output.format(transformerror.id, transformerror.shortdesc, transformerror.description, '', '', tags)) ruleerror = cfnlint.RuleError() tags = ','.join('`{0}`'.format(tag) for tag in ruleerror.tags) new_file.write( rule_output.format(ruleerror.id, ruleerror.shortdesc, ruleerror.description, '', '', tags)) # Seprate the experimental rules experimental_rules = [] for rule in sorted_rules: if rule.experimental: experimental_rules.append(rule) continue tags = ','.join('`{0}`'.format(tag) for tag in rule.tags) config = '<br />'.join( '{0}:{1}:{2}'.format(key, values.get('type'), values.get('default')) for key, values in rule.config_definition.items()) new_file.write( rule_output.format(rule.id, rule.shortdesc, rule.description, config, rule.source_url, tags)) # Output the experimental rules (if any) if experimental_rules: new_file.write('### Experimental rules\n') new_file.write( '| Rule ID | Title | Description | Source | Tags |\n') new_file.write( '| -------- | ----- | ----------- | ------ | ---- |\n') for rule in experimental_rules: tags = ','.join('`{0}`'.format(tag) for tag in rule.tags) config = '<br />'.join( '{0}:{1}:{2}'.format(key, values.get('type'), values.get('default')) for key, values in rule.config_definition.items()) new_file.write( rule_output.format(rule.id, rule.shortdesc, rule.description, config, rule.source_url, tags))