Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import yaml
- def main():
- dir_orig_ossn = "security-notes"
- dir_yaml_ossn = "yaml-notes"
- test_note = "security-notes/OSSN-0001"
- out_file = "yaml-notes/OSSN-0001.yaml"
- for root, dirs, files in os.walk(dir_orig_ossn):
- for f in files:
- path = root + "/" + f
- print "Converting: { " + path + " }"
- note_file = open(path, 'r')
- note_text = note_file.read()
- note_file.close()
- title = parse_title(note_text)
- summary = find_text_between_markers(note_text,
- '### Summary###',
- '### Affected Services / Software ###')
- affected_services = find_text_between_markers(note_text,
- '### Affected Services / Software ###',
- '### Discussion ###')
- discussion = find_text_between_markers(note_text,
- '### Discussion ###',
- '### Recommended Actions ###')
- recommended_actions = find_text_between_markers(note_text,
- '### Recommended Actions ###',
- '### Contacts / References ###')
- contacts_and_refs = find_text_between_markers(note_text,
- '### Contacts / References ###',
- None)
- output_file = dir_yaml_ossn + "/" + f + ".yaml"
- yaml_doc = open(output_file, 'w')
- yaml_doc.write(create_yaml_section('title: ', title))
- yaml_doc.write(create_yaml_section('summary: ', summary))
- yaml_doc.write(create_yaml_section('affected_services: ', affected_services))
- yaml_doc.write(create_yaml_section('discussion: ', discussion))
- yaml_doc.write(create_yaml_section('recommended_actions: ', recommended_actions))
- yaml_doc.write(create_yaml_section('contacts_and_refs:', contacts_and_refs, strip_newlines=False))
- yaml_doc.close()
- print "... done"
- def parse_title(note_text):
- end_loc = note_text.find('---')
- return note_text[:end_loc].replace('\n','').replace('\r','')
- def find_text_between_markers(text, start_marker, end_marker):
- # gets all of the text in between two marker strings
- starting_loc = text.find(start_marker) + len(start_marker)
- if end_marker:
- end_loc = text.find(end_marker) - 1
- else:
- end_loc = len(text)
- return text[starting_loc:end_loc].strip('\n\r').rstrip('\n\r')
- def create_yaml_section(section_name, text, trailing_newlines=2, strip_newlines=True):
- return_string = ""
- return_string += section_name
- return_string += "\'"
- return_string += _prepare_for_yaml(text, strip_newlines)
- return_string += "\'"
- return_string += '\n' * trailing_newlines
- return return_string
- def _prepare_for_yaml(orig_text, strip_newlines):
- transf_text = orig_text
- if strip_newlines:
- transf_text = _strip_newlines(transf_text)
- transf_text = _escape_quotes(transf_text)
- return transf_text
- def _strip_newlines(text_to_strip):
- return text_to_strip.replace('\n', ' ')
- def _escape_quotes(orig_str):
- return orig_str.replace("'", '"')
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement