Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- debug = False
- class DiffData:
- def __init__(self,start_line ,origin_count, patched_count):
- self.start_line = start_line
- self.origin_count = origin_count
- self.patched_count = patched_count
- self.patched_contents = []
- def __repr__(self):
- return f"Diff: start:{self.start_line} prev: {self.origin_count} line(s) --> patched: {self.patched_count} line(s)"
- def apply_to_file_lines(self, file_lines, offset=0):
- # first part of original file
- new_file_lines = file_lines[0 : self.start_line-1+offset]
- # diff patched part
- new_file_lines.extend(self.patched_contents)
- # second part of the original file
- new_file_lines += file_lines[self.start_line-1+offset + self.origin_count : ]
- return new_file_lines
- def input_diff_block(start_line):
- meta_info_str_list = start_line.split(' ')
- diff_start_info_str = meta_info_str_list[1].split(',')
- diff_end_info_str = meta_info_str_list[2].split(',')
- diff_start_line = int(diff_start_info_str[0][1:])
- original_line_count = int(diff_start_info_str[1])
- patched_line_count = int(diff_end_info_str[1])
- # create diff data
- diff_data = DiffData(start_line = diff_start_line, origin_count=original_line_count, patched_count=patched_line_count)
- if debug:
- print(diff_data)
- origin_received = 0
- patched_received = 0
- while origin_received < diff_data.origin_count or patched_received < diff_data.patched_count:
- if debug:
- print(f'Expecting more diff info: {origin_received} < {diff_data.origin_count} or {patched_received} < {diff_data.patched_count}')
- new_line = input()
- new_line_type = new_line[0]
- new_line_content = new_line[1:]
- if new_line_type!='+' and new_line_type !=' ' and new_line_type!='#':
- origin_received += 1
- elif new_line_type == ' ':
- origin_received += 1
- patched_received += 1
- diff_data.patched_contents.append(new_line_content)
- elif new_line_type == '+':
- patched_received += 1
- diff_data.patched_contents.append(new_line_content)
- else:
- pass
- return diff_data
- def input_file_and_diff():
- """
- Currently only support input one diff block
- """
- file_lines = []
- diff_datas = []
- file_total_lines = int(input())
- input_file_lines = 0
- while True:
- try:
- current_line = input()
- # maybe some comment of diff
- if current_line.startswith("#"):
- continue
- except:
- # expect raise eof when input end
- break
- # meets a new line of diff block
- if current_line.startswith('@@'):
- diff_datas.append(input_diff_block(current_line))
- continue
- # just a new file lines
- input_file_lines += 1
- if input_file_lines > file_total_lines:
- continue
- file_lines.append(current_line)
- return file_lines, diff_datas
- def main():
- file_lines, diff_datas = input_file_and_diff()
- if debug:
- print('Input Statistics')
- print(f'file_lines:')
- for i in file_lines:
- print(i)
- print('diff_data')
- print(diff_datas)
- # sort diff data by start point
- diff_datas.sort(key=lambda diff_data: diff_data.start_line)
- # apply diff to file lines
- offset = 0
- for diff_data in diff_datas:
- file_lines = diff_data.apply_to_file_lines(file_lines, offset=offset)
- offset += (diff_data.patched_count - diff_data.origin_count)
- for i in file_lines:
- print(i)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement