(fp, file_header, rest_of_file, preserve_text_store, comment_type)
| 165 | |
| 166 | |
| 167 | def modify_file_header(fp, file_header, rest_of_file, preserve_text_store, comment_type): |
| 168 | header_text = "\n".join(file_header) |
| 169 | if not (header_text.strip() == "" or header_text in preserve_text_store): |
| 170 | # Unique header, need to get user input |
| 171 | print("\n", DELIM, "\n") |
| 172 | for idx, line in enumerate(file_header): |
| 173 | print(f"{idx}: {line}") |
| 174 | print("\n", DELIM, "\n") |
| 175 | print("\nIndicate the FIRST line of the Header to KEEP") |
| 176 | print("(shebang #! lines will be automatically processed and should not be included).") |
| 177 | keep_idx = input("Enter number (or leave blank if no lines should be preserved): ") |
| 178 | preserve_text_store[header_text] = file_header[int(keep_idx):] if keep_idx != "" else "" |
| 179 | |
| 180 | # Identify any shebang lines in the file |
| 181 | shebang = "\n".join([l for l in file_header if l.startswith("#!")]) |
| 182 | if shebang != "": |
| 183 | shebang += "\n" |
| 184 | |
| 185 | # Get the text we should preserve in this file and process to remove comment characters |
| 186 | text_to_preserve = preserve_text_store.get(header_text, [""]) |
| 187 | text_to_preserve = [remove_comments(l, comment_type) for l in text_to_preserve] |
| 188 | |
| 189 | # Format the text we want to keep into a new multiline comment |
| 190 | if "".join(text_to_preserve) == "": |
| 191 | text_to_preserve = "" |
| 192 | else: |
| 193 | text_to_preserve = format_multiline_comment(text_to_preserve, comment_type) |
| 194 | |
| 195 | # Generate the copyright text we will be adding |
| 196 | copyright_text = "\n".join([f"{comment_type[0]} {l}" if l != "" else l for l in NEW_COPYRIGHT]) |
| 197 | |
| 198 | # Assemble the new header |
| 199 | new_header = shebang + copyright_text + text_to_preserve |
| 200 | |
| 201 | # Write out the new file |
| 202 | new_file_contents = new_header + "\n" + "".join(rest_of_file) |
| 203 | with open(fp, "w") as f: |
| 204 | f.write(new_file_contents) |
| 205 | |
| 206 | return preserve_text_store # Return so we can reuse for future files |
| 207 | |
| 208 | |
| 209 | def main(args): |
no test coverage detected