@@ 291-367 (lines=77) @@ | ||
288 | ||
289 | ||
290 | # master merge target |
|
291 | def _merge(master, target): |
|
292 | if type(master) != type(target): |
|
293 | print("yaml format not match:\n") |
|
294 | yaml.dump(master, sys.stdout) |
|
295 | print("\n&&\n") |
|
296 | yaml.dump(target, sys.stdout) |
|
297 | ||
298 | sys.exit(1) |
|
299 | ||
300 | ## item is a sequence |
|
301 | if isinstance(target, CommentedSeq): |
|
302 | for index in range(len(target)): |
|
303 | # get target comment |
|
304 | target_comment = _seq_comment(target, index) |
|
305 | ||
306 | master_index = len(master) |
|
307 | ||
308 | target_item = target[index] |
|
309 | ||
310 | if isinstance(target_item, CommentedMap): |
|
311 | merge_flag = False |
|
312 | for idx in range(len(master)): |
|
313 | if isinstance(master[idx], CommentedMap): |
|
314 | if master[idx].keys() == target_item.keys(): |
|
315 | _merge(master[idx], target_item) |
|
316 | # nonlocal merge_flag |
|
317 | master_index = idx |
|
318 | merge_flag = True |
|
319 | break |
|
320 | ||
321 | if merge_flag is False: |
|
322 | master.append(target_item) |
|
323 | elif target_item not in master: |
|
324 | master.append(target[index]) |
|
325 | else: |
|
326 | # merge(master[index], target[index]) |
|
327 | pass |
|
328 | ||
329 | # # remove enter signal in previous item |
|
330 | previous_comment = _seq_comment(master, master_index - 1) |
|
331 | _add_eol_comment(master, _extract_comment(previous_comment), master_index - 1) |
|
332 | ||
333 | origin_comment = _seq_comment(master, master_index) |
|
334 | comment = _obtain_comment(origin_comment, target_comment) |
|
335 | if len(comment) > 0: |
|
336 | _add_eol_comment(master, _extract_comment(comment) + "\n\n", len(master) - 1) |
|
337 | ||
338 | ## item is a map |
|
339 | elif isinstance(target, CommentedMap): |
|
340 | for item in target: |
|
341 | if item == "flag": |
|
342 | print("") |
|
343 | origin_comment = _map_comment(master, item) |
|
344 | target_comment = _map_comment(target, item) |
|
345 | ||
346 | # get origin start comment |
|
347 | origin_start_comment = _start_comment(master) |
|
348 | ||
349 | # get target start comment |
|
350 | target_start_comment = _start_comment(target) |
|
351 | ||
352 | m = master.get(item, default=None) |
|
353 | if m is None or \ |
|
354 | (not (isinstance(m, CommentedMap) or |
|
355 | isinstance(m, CommentedSeq))): |
|
356 | master.update({item: target[item]}) |
|
357 | ||
358 | else: |
|
359 | _merge(master[item], target[item]) |
|
360 | ||
361 | comment = _obtain_comment(origin_comment, target_comment) |
|
362 | if len(comment) > 0: |
|
363 | _add_eol_comment(master, _extract_comment(comment), item) |
|
364 | ||
365 | start_comment = _obtain_comment(origin_start_comment, target_start_comment) |
|
366 | if len(start_comment) > 0: |
|
367 | master.yaml_set_start_comment(_extract_comment(start_comment)) |
|
368 | ||
369 | ||
370 | def _save(_code, _file): |
@@ 291-367 (lines=77) @@ | ||
288 | ||
289 | ||
290 | # master merge target |
|
291 | def _merge(master, target): |
|
292 | if type(master) != type(target): |
|
293 | print("yaml format not match:\n") |
|
294 | yaml.dump(master, sys.stdout) |
|
295 | print("\n&&\n") |
|
296 | yaml.dump(target, sys.stdout) |
|
297 | ||
298 | sys.exit(1) |
|
299 | ||
300 | ## item is a sequence |
|
301 | if isinstance(target, CommentedSeq): |
|
302 | for index in range(len(target)): |
|
303 | # get target comment |
|
304 | target_comment = _seq_comment(target, index) |
|
305 | ||
306 | master_index = len(master) |
|
307 | ||
308 | target_item = target[index] |
|
309 | ||
310 | if isinstance(target_item, CommentedMap): |
|
311 | merge_flag = False |
|
312 | for idx in range(len(master)): |
|
313 | if isinstance(master[idx], CommentedMap): |
|
314 | if master[idx].keys() == target_item.keys(): |
|
315 | _merge(master[idx], target_item) |
|
316 | # nonlocal merge_flag |
|
317 | master_index = idx |
|
318 | merge_flag = True |
|
319 | break |
|
320 | ||
321 | if merge_flag is False: |
|
322 | master.append(target_item) |
|
323 | elif target_item not in master: |
|
324 | master.append(target[index]) |
|
325 | else: |
|
326 | # merge(master[index], target[index]) |
|
327 | pass |
|
328 | ||
329 | # # remove enter signal in previous item |
|
330 | previous_comment = _seq_comment(master, master_index - 1) |
|
331 | _add_eol_comment(master, _extract_comment(previous_comment), master_index - 1) |
|
332 | ||
333 | origin_comment = _seq_comment(master, master_index) |
|
334 | comment = _obtain_comment(origin_comment, target_comment) |
|
335 | if len(comment) > 0: |
|
336 | _add_eol_comment(master, _extract_comment(comment) + "\n\n", len(master) - 1) |
|
337 | ||
338 | ## item is a map |
|
339 | elif isinstance(target, CommentedMap): |
|
340 | for item in target: |
|
341 | if item == "flag": |
|
342 | print("") |
|
343 | origin_comment = _map_comment(master, item) |
|
344 | target_comment = _map_comment(target, item) |
|
345 | ||
346 | # get origin start comment |
|
347 | origin_start_comment = _start_comment(master) |
|
348 | ||
349 | # get target start comment |
|
350 | target_start_comment = _start_comment(target) |
|
351 | ||
352 | m = master.get(item, default=None) |
|
353 | if m is None or \ |
|
354 | (not (isinstance(m, CommentedMap) or |
|
355 | isinstance(m, CommentedSeq))): |
|
356 | master.update({item: target[item]}) |
|
357 | ||
358 | else: |
|
359 | _merge(master[item], target[item]) |
|
360 | ||
361 | comment = _obtain_comment(origin_comment, target_comment) |
|
362 | if len(comment) > 0: |
|
363 | _add_eol_comment(master, _extract_comment(comment), item) |
|
364 | ||
365 | start_comment = _obtain_comment(origin_start_comment, target_start_comment) |
|
366 | if len(start_comment) > 0: |
|
367 | master.yaml_set_start_comment(_extract_comment(start_comment)) |
|
368 | ||
369 | ||
370 | def _save(_code, _file): |