PyLintPro / app.py
Canstralian's picture
Update app.py
eb73a33 verified
raw
history blame
3.29 kB
import gradio as gr
import flake8.main.application
import autopep8
import pylint.lint
import isort
import black
import tempfile
import os
import difflib
import io
def lint_and_format(code, enable_flake8, enable_pylint, enable_isort, enable_black):
flake8_output = ""
pylint_output = ""
isort_output = ""
formatted_code = code
diff_output = ""
black_output = ""
# Temporary file for linters
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as temp_file:
temp_file.write(code)
temp_file_path = temp_file.name
try:
# Flake8
if enable_flake8:
app = flake8.main.application.Application()
app.initialize(['--format=default', temp_file_path])
app.run_checks()
app.generate_reports()
flake8_output = app.results_manager.stdout.getvalue().strip()
# Pylint
if enable_pylint:
pylint_output_stream = io.StringIO()
pylint_runner = pylint.lint.Run([temp_file_path], do_exit=False, output_stream=pylint_output_stream)
pylint_output = pylint_output_stream.getvalue().strip()
# isort
if enable_isort:
isort_output = isort.file(temp_file_path, show_diff=True).strip()
# Black
if enable_black:
try:
formatted_code = black.format_file_in_place(
src=temp_file_path,
fast=False,
mode=black.FileMode(),
write_back=black.WriteBack.YES,
)
with open(temp_file_path, "r") as f:
formatted_code = f.read()
except black.NothingChanged:
pass # black did not change anything
# AutoPEP8 (if Black is not enabled)
if not enable_black:
formatted_code = autopep8.fix_code(formatted_code)
# Diff
diff = difflib.unified_diff(code.splitlines(keepends=True), formatted_code.splitlines(keepends=True), fromfile="original.py", tofile="formatted.py")
diff_output = "".join(diff)
return flake8_output, pylint_output, isort_output, formatted_code, diff_output
except Exception as e:
return f"An error occurred: {str(e)}", "", "", code, ""
finally:
os.remove(temp_file_path)
def main():
with gr.Interface(
fn=lint_and_format,
inputs=[
gr.Code(label="Python Code", language="python", lines=10),
gr.Checkbox(label="Enable Flake8", value=True),
gr.Checkbox(label="Enable Pylint", value=False),
gr.Checkbox(label="Enable isort", value=True),
gr.Checkbox(label="Enable Black", value=True),
],
outputs=[
gr.Textbox(label="Flake8 Output"),
gr.Textbox(label="Pylint Output"),
gr.Textbox(label="isort Output"),
gr.Code(label="Formatted Code", language="python"),
gr.Textbox(label="Diff"),
],
title="Python Code Linter and Formatter",
description="Paste your Python code, and this tool will lint and format it using Flake8, Pylint, isort and Black/AutoPEP8.",
) as demo:
demo.launch()
if __name__ == "__main__":
main()