(old_f, actions)
| 31 | |
| 32 | |
| 33 | def patch(old_f, actions): |
| 34 | new_f = io.BytesIO() |
| 35 | for action, param in actions: |
| 36 | if type(action) is bytes: |
| 37 | action = action.decode() |
| 38 | if action == "=": # Same lines |
| 39 | new_f.write(old_f.read(param)) |
| 40 | elif action == "-": # Delete lines |
| 41 | old_f.seek(param, 1) # Seek from current position |
| 42 | continue |
| 43 | elif action == "+": # Add lines |
| 44 | for add_line in param: |
| 45 | new_f.write(add_line) |
| 46 | else: |
| 47 | raise "Unknown action: %s" % action |
| 48 | return new_f |