Canstralian commited on
Commit
189bfb8
·
verified ·
1 Parent(s): 9752219

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -2,42 +2,43 @@ import gradio as gr
2
  from flake8.api import legacy as flake8
3
  import autopep8
4
 
5
- # Lint code function that uses Flake8 and autopep8
6
  def lint_code(code: str) -> tuple[str, str]:
7
  """
8
  Lint the provided Python code using Flake8 and auto-fix it with autopep8.
9
  Returns the fixed code and the linting report.
10
  """
11
- # Run Flake8 for linting, ignoring line-length violations (E501)
12
- style_guide = flake8.get_style_guide(ignore=["E501"])
13
-
14
- # Using a string IO to simulate a file for flake8 (since flake8 expects file paths)
15
- from io import StringIO
16
- code_file = StringIO(code)
17
-
18
- report = style_guide.check_files([code_file])
19
-
20
- # Auto-fix the code with autopep8
21
- fixed_code = autopep8.fix_code(code)
22
-
23
- # Generate the linting report
24
- lint_report = "\n".join(msg for msg in report.get_statistics())
25
-
26
- return fixed_code, lint_report
27
 
28
- # Process the uploaded file (read, decode, and lint it)
29
  def process_file(file) -> tuple[str, str]:
30
  """
31
  Process the uploaded file, decode its content, and lint it.
32
  Returns the fixed code and linting report.
33
  """
34
- # Read the file content and decode it to a string
35
- code = file.read().decode("utf-8")
36
-
37
- # Lint the code and get the report
38
- return lint_code(code)
 
 
 
 
39
 
40
- # Handle input, whether code is pasted or a file is uploaded
41
  def handle_input(code: str, file) -> tuple[str, str]:
42
  """
43
  Handle both code inputs: either text or file.
 
2
  from flake8.api import legacy as flake8
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
+ try:
11
+ # Run Flake8 for linting, ignoring line-length violations (E501)
12
+ style_guide = flake8.get_style_guide(ignore=["E501"])
13
+
14
+ # Directly check code content
15
+ report = style_guide.check_code(code)
16
+
17
+ # Auto-fix the code with autopep8
18
+ fixed_code = autopep8.fix_code(code)
19
+
20
+ # Generate the linting report
21
+ lint_report = "\n".join(msg for msg in report.get_statistics())
22
+
23
+ return fixed_code, lint_report
24
+ except Exception as e:
25
+ return code, f"An error occurred during linting: {str(e)}"
26
 
 
27
  def process_file(file) -> tuple[str, str]:
28
  """
29
  Process the uploaded file, decode its content, and lint it.
30
  Returns the fixed code and linting report.
31
  """
32
+ try:
33
+ # Read the file content and decode it to a string
34
+ with file as f:
35
+ code = f.read().decode("utf-8")
36
+
37
+ # Lint the code and get the report
38
+ return lint_code(code)
39
+ except Exception as e:
40
+ return "", f"An error occurred while processing the file: {str(e)}"
41
 
 
42
  def handle_input(code: str, file) -> tuple[str, str]:
43
  """
44
  Handle both code inputs: either text or file.