MCPcopy Create free account
hub / github.com/catid/supercharger / clean_code

Function clean_code

codegen/clean_code.py:60–109  ·  view source on GitHub ↗
(code, strip_md=True, strip_globals=True, strip_leading_comments=False, strip_import_mods=[], strip_import_funcs=[], try_autoimport=True)

Source from the content-addressed store, hash-verified

58 return clean_script
59
60def clean_code(code, strip_md=True, strip_globals=True, strip_leading_comments=False, strip_import_mods=[], strip_import_funcs=[], try_autoimport=True):
61
62 #print(f"CODE:\n\n----\n{code}\n----\n\n")
63
64 if strip_md:
65 try:
66 code = extract_code_from_md(code)
67 except Exception as e:
68 logging.info(f"clean_code::extract_code_from_md failed due to exception: {e}")
69
70 #print(f"extract_code_from_md:\n\n----\n{code}\n----\n\n")
71
72 try:
73 code = fix_ast_errors(code)
74 except Exception as e:
75 logging.info(f"clean_code::fix_ast_errors failed due to exception: {e}")
76
77 #print(f"fix_ast_errors:\n\n----\n{code}\n----\n\n")
78
79 if strip_globals:
80 try:
81 code = only_defs_and_imports(code, strip_import_mods=strip_import_mods, strip_import_funcs=strip_import_funcs)
82 except Exception as e:
83 logging.info(f"clean_code::only_defs_and_imports failed due to exception: {e}")
84
85 #print(f"only_defs_and_imports:\n\n----\n{code}\n----\n\n")
86
87 if strip_leading_comments:
88 try:
89 code = remove_comments_before_first_function(code)
90 except Exception as e:
91 logging.info(f"clean_code::remove_comments_before_first_function failed due to exception: {e}")
92
93 #print(f"remove_comments_before_first_function:\n\n----\n{code}\n----\n\n")
94
95 if try_autoimport:
96 try:
97 code = fix_code(code)
98 except Exception as e:
99 logging.info(f"clean_code::try_autoimport failed due to exception: {e}")
100
101 try:
102 code, _ = FormatCode(code)
103 except Exception as e:
104 logging.info(f"clean_code::yapf failed due to exception: {e}")
105 return code, False
106
107 #print(f"FormatCode:\n\n----\n{code}\n----\n\n")
108
109 return code, True

Callers 5

autopy_funcFunction · 0.90
autopy_func_improveFunction · 0.90
autopy_testFunction · 0.90
autopy_test_improveFunction · 0.90
mainFunction · 0.90

Calls 4

extract_code_from_mdFunction · 0.90
fix_ast_errorsFunction · 0.90
only_defs_and_importsFunction · 0.85

Tested by 1

mainFunction · 0.72