| 33 | return ast.unparse(filtered_nodes) |
| 34 | |
| 35 | def remove_comments_before_first_function(script): |
| 36 | # Match function definition pattern |
| 37 | function_pattern = re.compile(r'^def .*\(', re.MULTILINE) |
| 38 | |
| 39 | # Find the index of the first function definition |
| 40 | match = function_pattern.search(script) |
| 41 | if match: |
| 42 | start_index = match.start() |
| 43 | else: |
| 44 | # If there's no function definition, return the original script |
| 45 | return script |
| 46 | |
| 47 | # Remove line comments |
| 48 | line_comment_pattern = re.compile(r'(?<=\n)#.*\n', re.MULTILINE) |
| 49 | clean_script = line_comment_pattern.sub('\n', script[:start_index]) |
| 50 | |
| 51 | # Remove multi-line comments |
| 52 | multiline_comment_pattern = re.compile(r'("""[\s\S]*?"""|\'\'\'[\s\S]*?\'\'\')', re.MULTILINE) |
| 53 | clean_script = multiline_comment_pattern.sub('', clean_script) |
| 54 | |
| 55 | # Add the remaining script after the first function definition |
| 56 | clean_script += script[start_index:] |
| 57 | |
| 58 | return clean_script |
| 59 | |
| 60 | def clean_code(code, strip_md=True, strip_globals=True, strip_leading_comments=False, strip_import_mods=[], strip_import_funcs=[], try_autoimport=True): |
| 61 | |