#NEWLINE# def restore_yaml_comments( data , default_data ) : #NEWLINE# #INDENT# 'Scan default_data for comments (we include empty lines in our\n    definition of comments) and place them before the same keys in data.\n    Only works with comments that are on one or more own lines, i.e.\n    not next to a yaml mapping.\n    ' #NEWLINE# comment_map = dict ( ) #NEWLINE# default_lines = iter ( default_data . splitlines ( ) ) #NEWLINE# for line in default_lines : #NEWLINE# #INDENT# if ( not line ) : #NEWLINE# #INDENT# comment = '\n' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . startswith ( '#' ) : #NEWLINE# #INDENT# comment = '{0}\n' . format ( line ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# while True : #NEWLINE# #INDENT# line = next ( default_lines ) #NEWLINE# if ( line and ( not line . startswith ( '#' ) ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# comment += '{0}\n' . format ( line ) #NEWLINE# #UNINDENT# key = line . split ( ':' ) [ 0 ] . strip ( ) #NEWLINE# comment_map [ key ] = comment #NEWLINE# #UNINDENT# out_lines = iter ( data . splitlines ( ) ) #NEWLINE# out_data = '' #NEWLINE# for line in out_lines : #NEWLINE# #INDENT# key = line . split ( ':' ) [ 0 ] . strip ( ) #NEWLINE# if ( key in comment_map ) : #NEWLINE# #INDENT# out_data += comment_map [ key ] #NEWLINE# #UNINDENT# out_data += '{0}\n' . format ( line ) #NEWLINE# #UNINDENT# return out_data