MCPcopy Create free account
hub / github.com/NanmiCoder/MediaCrawler / process_file

Function process_file

tools/file_header_manager.py:147–210  ·  view source on GitHub ↗

Process single Python file Args: file_path: File path project_root: Project root directory dry_run: Check only without modification Returns: (whether modification needed, status message)

(file_path: str, project_root: str, dry_run: bool = False)

Source from the content-addressed store, hash-verified

145
146
147def process_file(file_path: str, project_root: str, dry_run: bool = False) -> Tuple[bool, str]:
148 """
149 Process single Python file
150
151 Args:
152 file_path: File path
153 project_root: Project root directory
154 dry_run: Check only without modification
155
156 Returns:
157 (whether modification needed, status message)
158 """
159 try:
160 with open(file_path, 'r', encoding='utf-8') as f:
161 content = f.read()
162 lines = content.splitlines(keepends=True)
163
164 # Skip if already has copyright header
165 if has_copyright_header(content):
166 return False, f"✓ Already has copyright header: {file_path}"
167
168 # Get relative path
169 relative_path = get_file_relative_path(file_path, project_root)
170
171 # Generate copyright header
172 copyright_header = generate_copyright_header(relative_path)
173
174 # Find insert position
175 insert_pos, has_encoding = find_insert_position(lines)
176
177 # Build new file content
178 new_lines = []
179
180 # Add encoding declaration if not present
181 if not has_encoding:
182 new_lines.append("# -*- coding: utf-8 -*-\n")
183
184 # Add front part (shebang and encoding declaration)
185 new_lines.extend(lines[:insert_pos])
186
187 # Add copyright header
188 new_lines.append(copyright_header + "\n")
189
190 # Add disclaimer if file doesn't have one
191 if not has_disclaimer(content):
192 new_lines.append(DISCLAIMER + "\n")
193
194 # Add empty line (if next line is not empty)
195 if insert_pos < len(lines) and lines[insert_pos].strip():
196 new_lines.append("\n")
197
198 # Add remaining content
199 new_lines.extend(lines[insert_pos:])
200
201 # Write to file if not dry run
202 if not dry_run:
203 with open(file_path, 'w', encoding='utf-8') as f:
204 f.writelines(new_lines)

Callers 1

mainFunction · 0.85

Calls 5

has_copyright_headerFunction · 0.85
get_file_relative_pathFunction · 0.85
find_insert_positionFunction · 0.85
has_disclaimerFunction · 0.85

Tested by

no test coverage detected