| 500 | |
| 501 | |
| 502 | class Patcher: |
| 503 | def __init__(self, args=None): |
| 504 | self.exitcode = 0 |
| 505 | self.pythoncapi_compat_added = 0 |
| 506 | self.want_pythoncapi_compat = False |
| 507 | self.operations = None |
| 508 | self.applied_operations = set() |
| 509 | |
| 510 | # Set temporarily by patch() |
| 511 | self._has_pythoncapi_compat = None |
| 512 | self._applied_operations = None |
| 513 | |
| 514 | self._parse_options(args) |
| 515 | |
| 516 | def log(self, msg=''): |
| 517 | print(msg, file=sys.stderr, flush=True) |
| 518 | |
| 519 | def warning(self, msg): |
| 520 | self.log(f"WARNING: {msg}") |
| 521 | |
| 522 | def _get_operations(self, parser): |
| 523 | args_names = self.args.operations.split(',') |
| 524 | |
| 525 | wanted = set() |
| 526 | for name in args_names: |
| 527 | name = name.strip() |
| 528 | if not name: |
| 529 | continue |
| 530 | |
| 531 | if name == "all": |
| 532 | wanted |= all_operations() |
| 533 | elif name.startswith("-"): |
| 534 | name = name[1:] |
| 535 | wanted.discard(name) |
| 536 | else: |
| 537 | wanted.add(name) |
| 538 | |
| 539 | operations = [] |
| 540 | for operation_class in OPERATIONS: |
| 541 | name = operation_class.NAME |
| 542 | if name not in wanted: |
| 543 | continue |
| 544 | wanted.discard(name) |
| 545 | operation = operation_class(self) |
| 546 | operations.append(operation) |
| 547 | |
| 548 | if wanted: |
| 549 | print(f"invalid operations: {','.join(wanted)}") |
| 550 | print() |
| 551 | self.usage(parser) |
| 552 | sys.exit(1) |
| 553 | |
| 554 | return operations |
| 555 | |
| 556 | def add_line(self, content, line): |
| 557 | # Use the first matching newline |
| 558 | match = re.search(r'(?:\r\n|\n|\r)', content) |
| 559 | newline = match.group(0) if match else '\n' |