Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,20 +3,27 @@ from flake8.api import legacy as flake8
|
|
3 |
import autopep8
|
4 |
|
5 |
def lint_code(code: str) -> tuple[str, str]:
|
6 |
-
"""
|
7 |
-
|
|
|
|
|
|
|
8 |
style_guide = flake8.get_style_guide(ignore=["E501"])
|
9 |
report = style_guide.check_files(None, lines=code.splitlines())
|
10 |
|
11 |
# Auto-fix the code with autopep8
|
12 |
fixed_code = autopep8.fix_code(code)
|
13 |
|
14 |
-
#
|
15 |
lint_report = "\n".join(msg for msg in report.get_statistics())
|
|
|
16 |
return fixed_code, lint_report
|
17 |
|
18 |
def process_file(file) -> tuple[str, str]:
|
19 |
-
"""
|
|
|
|
|
|
|
20 |
# Read the file content and decode it to a string
|
21 |
code = file.read().decode("utf-8")
|
22 |
|
@@ -24,8 +31,10 @@ def process_file(file) -> tuple[str, str]:
|
|
24 |
return lint_code(code)
|
25 |
|
26 |
def handle_input(code: str, file) -> tuple[str, str]:
|
27 |
-
"""
|
28 |
-
|
|
|
|
|
29 |
if file:
|
30 |
return process_file(file)
|
31 |
return lint_code(code)
|
@@ -46,4 +55,4 @@ interface = gr.Interface(
|
|
46 |
)
|
47 |
|
48 |
# Launch the interface
|
49 |
-
interface.launch()
|
|
|
3 |
import autopep8
|
4 |
|
5 |
def lint_code(code: str) -> tuple[str, str]:
|
6 |
+
"""
|
7 |
+
Lint the provided Python code using Flake8 and auto-fix it with autopep8.
|
8 |
+
Returns the fixed code and the linting report.
|
9 |
+
"""
|
10 |
+
# Run Flake8 for linting, ignoring line-length violations (E501)
|
11 |
style_guide = flake8.get_style_guide(ignore=["E501"])
|
12 |
report = style_guide.check_files(None, lines=code.splitlines())
|
13 |
|
14 |
# Auto-fix the code with autopep8
|
15 |
fixed_code = autopep8.fix_code(code)
|
16 |
|
17 |
+
# Generate the linting report
|
18 |
lint_report = "\n".join(msg for msg in report.get_statistics())
|
19 |
+
|
20 |
return fixed_code, lint_report
|
21 |
|
22 |
def process_file(file) -> tuple[str, str]:
|
23 |
+
"""
|
24 |
+
Process the uploaded file, decode its content, and lint it.
|
25 |
+
Returns the fixed code and linting report.
|
26 |
+
"""
|
27 |
# Read the file content and decode it to a string
|
28 |
code = file.read().decode("utf-8")
|
29 |
|
|
|
31 |
return lint_code(code)
|
32 |
|
33 |
def handle_input(code: str, file) -> tuple[str, str]:
|
34 |
+
"""
|
35 |
+
Handle both code inputs: either text or file.
|
36 |
+
If a file is provided, process the file. Otherwise, process the pasted code.
|
37 |
+
"""
|
38 |
if file:
|
39 |
return process_file(file)
|
40 |
return lint_code(code)
|
|
|
55 |
)
|
56 |
|
57 |
# Launch the interface
|
58 |
+
interface.launch()
|