132 lines
4.9 KiB
Python
132 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Automated refactoring script for download_data.py
|
|
Converts module-level functions to class-based cmdlet pattern.
|
|
"""
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
def main():
|
|
backup_file = Path('cmdlets/download_data_backup.py')
|
|
output_file = Path('cmdlets/download_data.py')
|
|
|
|
print(f"Reading: {backup_file}")
|
|
content = backup_file.read_text(encoding='utf-8')
|
|
lines = content.split('\n')
|
|
|
|
output = []
|
|
i = 0
|
|
in_cmdlet_def = False
|
|
skip_old_run_wrapper = False
|
|
class_section_added = False
|
|
|
|
# Track where to insert class definition
|
|
last_import_line = 0
|
|
|
|
while i < len(lines):
|
|
line = lines[i]
|
|
|
|
# Track imports
|
|
if line.strip().startswith(('import ', 'from ')):
|
|
last_import_line = len(output)
|
|
|
|
# Skip old _run wrapper function
|
|
if 'def _run(result: Any' in line:
|
|
skip_old_run_wrapper = True
|
|
i += 1
|
|
continue
|
|
|
|
if skip_old_run_wrapper:
|
|
if line and not line[0].isspace():
|
|
skip_old_run_wrapper = False
|
|
else:
|
|
i += 1
|
|
continue
|
|
|
|
# Skip old CMDLET definition
|
|
if line.strip().startswith('CMDLET = Cmdlet('):
|
|
in_cmdlet_def = True
|
|
i += 1
|
|
continue
|
|
|
|
if in_cmdlet_def:
|
|
if line.strip() == ')':
|
|
in_cmdlet_def = False
|
|
# Add class instantiation instead
|
|
output.append('')
|
|
output.append('# Create and register the cmdlet')
|
|
output.append('CMDLET = Download_Data()')
|
|
output.append('')
|
|
i += 1
|
|
continue
|
|
|
|
# Insert class definition before first helper function
|
|
if not class_section_added and line.strip().startswith('def _download_torrent_worker('):
|
|
output.append('')
|
|
output.append('')
|
|
output.append('class Download_Data(Cmdlet):')
|
|
output.append(' """Class-based download-data cmdlet with self-registration."""')
|
|
output.append('')
|
|
output.append(' # Full __init__ implementation to be added')
|
|
output.append(' # Full run() method to be added')
|
|
output.append('')
|
|
output.append(' # ' + '='*70)
|
|
output.append(' # HELPER METHODS')
|
|
output.append(' # ' + '='*70)
|
|
output.append('')
|
|
class_section_added = True
|
|
|
|
# Convert top-level helper functions to static methods
|
|
if class_section_added and line.strip().startswith('def _') and not line.strip().startswith('def __'):
|
|
# Check if this is a top-level function (no indentation)
|
|
if not line.startswith((' ', '\t')):
|
|
output.append(' @staticmethod')
|
|
output.append(f' {line}')
|
|
i += 1
|
|
# Copy function body with indentation
|
|
while i < len(lines):
|
|
next_line = lines[i]
|
|
# Stop at next top-level definition
|
|
if next_line and not next_line[0].isspace() and (next_line.strip().startswith('def ') or next_line.strip().startswith('class ') or next_line.strip().startswith('CMDLET')):
|
|
break
|
|
# Add indentation
|
|
if next_line.strip():
|
|
output.append(f' {next_line}')
|
|
else:
|
|
output.append(next_line)
|
|
i += 1
|
|
continue
|
|
|
|
# Convert _run_impl to method (but keep as-is for now, will be updated later)
|
|
if class_section_added and line.strip().startswith('def _run_impl('):
|
|
output.append(' def _run_impl(self, result: Any, args: Sequence[str], config: Dict[str, Any], emit_results: bool = True) -> int:')
|
|
i += 1
|
|
# Copy function body with indentation
|
|
while i < len(lines):
|
|
next_line = lines[i]
|
|
if next_line and not next_line[0].isspace() and next_line.strip():
|
|
break
|
|
if next_line.strip():
|
|
output.append(f' {next_line}')
|
|
else:
|
|
output.append(next_line)
|
|
i += 1
|
|
continue
|
|
|
|
output.append(line)
|
|
i += 1
|
|
|
|
# Write output
|
|
result_text = '\n'.join(output)
|
|
output_file.write_text(result_text, encoding='utf-8')
|
|
print(f"✓ Written: {output_file}")
|
|
print(f"✓ Converted {content.count('def _')} helper functions to static methods")
|
|
print("\nNext steps:")
|
|
print("1. Add full __init__ method with cmdlet args")
|
|
print("2. Add run() method that calls _run_impl")
|
|
print("3. Update function calls in _run_impl from _func() to self._func()")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|