File size: 11,034 Bytes
43b66f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
Code quality tools and configuration for the application.
"""
import streamlit as st
import subprocess
import os
from pathlib import Path
import tempfile
import json

def render_code_quality_tools():
    """
    Render the code quality tools interface.
    """
    st.markdown("<h2>Code Quality Tools</h2>", unsafe_allow_html=True)
    
    # Tabs for different tools
    tab1, tab2, tab3, tab4 = st.tabs(["Linting", "Formatting", "Type Checking", "Testing"])
    
    with tab1:
        render_linting_tools()
    
    with tab2:
        render_formatting_tools()
    
    with tab3:
        render_type_checking_tools()
    
    with tab4:
        render_testing_tools()

def render_linting_tools():
    """
    Render linting tools interface.
    """
    st.markdown("### Linting with Pylint/Flake8")
    st.markdown("""
    Linting tools help identify potential errors, enforce coding standards, and encourage best practices.
    
    **Available Tools:**
    - **Pylint**: Comprehensive linter that checks for errors and enforces a coding standard
    - **Flake8**: Wrapper around PyFlakes, pycodestyle, and McCabe complexity checker
    """)
    
    # File upload for linting
    uploaded_file = st.file_uploader("Upload Python file for linting", type=["py"])
    
    linter = st.radio("Select linter", ["Pylint", "Flake8"])
    
    if uploaded_file and st.button("Run Linter"):
        with st.spinner(f"Running {linter}..."):
            # Save uploaded file to a temporary file
            with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as tmp_file:
                tmp_file.write(uploaded_file.getvalue())
                tmp_path = tmp_file.name
            
            try:
                if linter == "Pylint":
                    # Run pylint
                    result = subprocess.run(
                        ["pylint", tmp_path],
                        capture_output=True,
                        text=True
                    )
                else:
                    # Run flake8
                    result = subprocess.run(
                        ["flake8", tmp_path],
                        capture_output=True,
                        text=True
                    )
                
                # Display results
                st.subheader("Linting Results")
                if result.returncode == 0:
                    st.success("No issues found!")
                else:
                    st.error("Issues found:")
                    st.code(result.stdout or result.stderr, language="text")
            
            except Exception as e:
                st.error(f"Error running {linter}: {str(e)}")
            
            finally:
                # Clean up temporary file
                os.unlink(tmp_path)

def render_formatting_tools():
    """
    Render code formatting tools interface.
    """
    st.markdown("### Code Formatting with Black & isort")
    st.markdown("""
    Code formatters automatically reformat your code to follow a consistent style.
    
    **Available Tools:**
    - **Black**: The uncompromising Python code formatter
    - **isort**: A utility to sort imports alphabetically and automatically separate them into sections
    """)
    
    # File upload for formatting
    uploaded_file = st.file_uploader("Upload Python file for formatting", type=["py"])
    
    formatter = st.radio("Select formatter", ["Black", "isort", "Both"])
    
    if uploaded_file and st.button("Format Code"):
        with st.spinner(f"Running {formatter}..."):
            # Get original code
            original_code = uploaded_file.getvalue().decode("utf-8")
            
            # Save uploaded file to a temporary file
            with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as tmp_file:
                tmp_file.write(uploaded_file.getvalue())
                tmp_path = tmp_file.name
            
            try:
                formatted_code = ""
                
                if formatter in ["Black", "Both"]:
                    # Run black
                    result = subprocess.run(
                        ["black", tmp_path],
                        capture_output=True,
                        text=True
                    )
                    
                    with open(tmp_path, "r") as f:
                        formatted_code = f.read()
                
                if formatter in ["isort", "Both"]:
                    # If both, use the code formatted by black
                    if formatter == "Both":
                        with open(tmp_path, "w") as f:
                            f.write(formatted_code)
                    
                    # Run isort
                    result = subprocess.run(
                        ["isort", tmp_path],
                        capture_output=True,
                        text=True
                    )
                    
                    with open(tmp_path, "r") as f:
                        formatted_code = f.read()
                
                # Display results side by side
                st.subheader("Formatting Results")
                col1, col2 = st.columns(2)
                
                with col1:
                    st.markdown("#### Original Code")
                    st.code(original_code, language="python")
                
                with col2:
                    st.markdown("#### Formatted Code")
                    st.code(formatted_code, language="python")
            
            except Exception as e:
                st.error(f"Error running {formatter}: {str(e)}")
            
            finally:
                # Clean up temporary file
                os.unlink(tmp_path)

def render_type_checking_tools():
    """
    Render type checking tools interface.
    """
    st.markdown("### Type Checking with mypy")
    st.markdown("""
    Static type checking helps catch type errors before runtime.
    
    **Available Tool:**
    - **mypy**: Optional static typing for Python
    """)
    
    # File upload for type checking
    uploaded_file = st.file_uploader("Upload Python file for type checking", type=["py"])
    
    if uploaded_file and st.button("Check Types"):
        with st.spinner("Running mypy..."):
            # Save uploaded file to a temporary file
            with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as tmp_file:
                tmp_file.write(uploaded_file.getvalue())
                tmp_path = tmp_file.name
            
            try:
                # Run mypy
                result = subprocess.run(
                    ["mypy", tmp_path],
                    capture_output=True,
                    text=True
                )
                
                # Display results
                st.subheader("Type Checking Results")
                if result.returncode == 0:
                    st.success("No type issues found!")
                else:
                    st.error("Type issues found:")
                    st.code(result.stdout or result.stderr, language="text")
            
            except Exception as e:
                st.error(f"Error running mypy: {str(e)}")
            
            finally:
                # Clean up temporary file
                os.unlink(tmp_path)

def render_testing_tools():
    """
    Render testing tools interface.
    """
    st.markdown("### Testing with pytest")
    st.markdown("""
    Testing frameworks help ensure your code works as expected.
    
    **Available Tool:**
    - **pytest**: Simple and powerful testing framework
    """)
    
    # Test file upload
    test_file = st.file_uploader("Upload test file", type=["py"])
    
    # Code file upload (optional)
    code_file = st.file_uploader("Upload code file to test (optional)", type=["py"])
    
    if test_file and st.button("Run Tests"):
        with st.spinner("Running tests..."):
            # Create temporary directory for test files
            with tempfile.TemporaryDirectory() as tmp_dir:
                # Save test file
                test_path = os.path.join(tmp_dir, "test_" + test_file.name)
                with open(test_path, "wb") as f:
                    f.write(test_file.getvalue())
                
                # Save code file if provided
                if code_file:
                    code_path = os.path.join(tmp_dir, code_file.name)
                    with open(code_path, "wb") as f:
                        f.write(code_file.getvalue())
                
                try:
                    # Run pytest
                    result = subprocess.run(
                        ["pytest", "-v", test_path],
                        capture_output=True,
                        text=True
                    )
                    
                    # Display results
                    st.subheader("Test Results")
                    st.code(result.stdout, language="text")
                    
                    if result.returncode == 0:
                        st.success("All tests passed!")
                    else:
                        st.error("Some tests failed.")
                
                except Exception as e:
                    st.error(f"Error running tests: {str(e)}")

def create_pylintrc():
    """
    Create a sample pylintrc configuration file.
    """
    pylintrc = """[MASTER]
# Python version
py-version = 3.8

# Parallel processing
jobs = 1

[MESSAGES CONTROL]
# Disable specific messages
disable=
    C0111, # missing-docstring
    C0103, # invalid-name
    R0903, # too-few-public-methods
    R0913, # too-many-arguments
    W0511, # fixme

[FORMAT]
# Maximum line length
max-line-length = 100

# Expected indentation
indent-string = '    '

[DESIGN]
# Maximum number of locals for function / method body
max-locals = 15

# Maximum number of arguments for function / method
max-args = 5

# Maximum number of attributes for a class
max-attributes = 7
"""
    return pylintrc

def create_flake8_config():
    """
    Create a sample flake8 configuration file.
    """
    flake8_config = """[flake8]
max-line-length = 100
exclude = .git,__pycache__,build,dist
ignore =
    E203, # whitespace before ':'
    E501, # line too long
    W503  # line break before binary operator
"""
    return flake8_config

def create_mypy_config():
    """
    Create a sample mypy configuration file.
    """
    mypy_config = """[mypy]
python_version = 3.8
warn_return_any = True
warn_unused_configs = True
disallow_untyped_defs = False
disallow_incomplete_defs = False

[mypy.plugins.numpy.*]
follow_imports = skip

[mypy.plugins.pandas.*]
follow_imports = skip
"""
    return mypy_config

def create_pytest_config():
    """
    Create a sample pytest configuration file.
    """
    pytest_config = """[pytest]
testpaths = tests
python_files = test_*.py
python_functions = test_*
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    integration: marks tests as integration tests
"""
    return pytest_config