-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
271 lines (215 loc) · 7.94 KB
/
main.py
File metadata and controls
271 lines (215 loc) · 7.94 KB
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
# -*- coding: utf-8 -*-
"""
RIdiom - Python Code Refactoring Tool
Main entry point for the refactoring/explaining tool.
Uses the new ridiom_core architecture with unified rule registry.
"""
import argparse
import os
import fnmatch
from typing import List, Dict
# Import the new core infrastructure
from ridiom_core import (
RuleRegistry,
Config,
load_config,
load_file_path,
save_json_file_path,
RuleResult,
CodeInfo,
)
from ridiom_core.config import set_config
import ridiom_core.rules # Trigger rule registration
# ==========================================
# Helper Functions
# ==========================================
def check_noqa(file_lines: List[str], lineno_list: List[List[int]]) -> bool:
"""Check if any line in the range contains '# noqa'."""
if not file_lines:
return False
for start, end in lineno_list:
start_idx = max(0, start - 1)
end_idx = min(len(file_lines), end)
for i in range(start_idx, end_idx):
if "# noqa" in file_lines[i]:
return True
return False
def get_target_files(base_paths: List[str], exclude_patterns: List[str]) -> List[str]:
"""Scan for Python files based on include/exclude patterns."""
target_files = []
for base_path in base_paths:
if base_path == ".":
base_path = os.getcwd()
if os.path.isfile(base_path):
target_files.append(base_path)
continue
for root, dirs, files in os.walk(base_path):
# Filter directories
dirs[:] = [
d
for d in dirs
if not any(fnmatch.fnmatch(d, pat) for pat in exclude_patterns)
]
for file in files:
if not file.endswith(".py"):
continue
filepath = os.path.join(root, file)
relpath = os.path.relpath(filepath, os.getcwd())
# Filter files
if any(
fnmatch.fnmatch(relpath, pat) or fnmatch.fnmatch(file, pat)
for pat in exclude_patterns
):
continue
target_files.append(filepath)
return target_files
# ==========================================
# Core Processing Logic
# ==========================================
def process_file(
filepath: str,
config: Config,
mode: str,
) -> List[Dict]:
"""
Process a single file with all active rules.
Returns:
List of result dictionaries for this file.
"""
print(
f"************ [{mode.title()}] Processing {os.path.basename(filepath)} ************"
)
file_results_output: List[Dict] = []
# Load file content
try:
content = load_file_path(filepath)
except Exception as e:
print(f"Failed to load {filepath}: {e}")
return file_results_output
# Read source lines for noqa checking
file_lines = []
if config.enable_noqa:
try:
with open(filepath, "r", encoding="utf-8") as f:
file_lines = f.readlines()
except Exception as e:
print(f"⚠️ Warning: Cannot read {filepath} for noqa check: {e}")
file_results: List[RuleResult] = []
# Get active rules for this mode
active_rules = RuleRegistry.all_instances(mode)
for rule in active_rules:
# Check if rule is enabled in config
if not config.is_rule_enabled(rule.name):
continue
# Get rule-specific config and add mode indicator
rule_config = config.get_rule_config(rule.name).copy()
rule_config["_mode"] = mode
try:
results = rule.match(content, config=rule_config)
if results:
# Filter out noqa-marked results
if config.enable_noqa and file_lines:
valid_results = []
for res in results:
if not check_noqa(file_lines, res.lineno):
valid_results.append(res)
file_results.extend(valid_results)
else:
file_results.extend(results)
except Exception as e:
import traceback
print(f"Error executing rule '{rule.name}' on {filepath}: {e}")
traceback.print_exc()
# Output results
if file_results:
print(
f"************ Result Summary of {os.path.basename(filepath)} ************"
)
for ind, res in enumerate(file_results):
code_info = CodeInfo(
file_path=filepath,
idiom=res.idiom_name,
class_name=res.class_name,
method_name=res.method_name,
old_code=res.old_code,
new_code=res.new_code,
lineno=res.lineno,
)
print(f">>> Result {ind + 1} \n{code_info.full_info()}")
file_results_output.append(code_info.__dict__)
print(f"************ End of {os.path.basename(filepath)} ************\n")
return file_results_output
# ==========================================
# Main Entry Point
# ==========================================
def main():
parser = argparse.ArgumentParser(
description="RIdiom: Detect and refactor/explain Python code."
)
parser.add_argument(
"--config",
type=str,
default="ridiom.toml",
help="Path to ridiom.toml config file",
)
parser.add_argument(
"--filepath",
type=str,
help="Override target path (file or directory)",
)
parser.add_argument(
"--outputpath",
type=str,
help="Override output path (if specified, all results go to this single file)",
)
parser.add_argument(
"--mode",
type=str,
choices=["refactor", "explain"],
help="Operation mode: 'refactor' (default) or 'explain'",
)
args = parser.parse_args()
# Load configuration
config = load_config(args.config)
set_config(config)
# Determine mode: CLI args > Config > Default
mode = args.mode if args.mode else config.mode
print(f"Operation Mode: {mode}")
# Print registered rules for debugging
print(f"Registered rules: {RuleRegistry.names()}")
# Scan for files
include_paths = [args.filepath] if args.filepath else config.include
target_files = get_target_files(include_paths, config.exclude)
print(f"Found {len(target_files)} python files to analyze.")
# Check active rules
active_rules = [r for r in RuleRegistry.all(mode) if config.is_rule_enabled(r.name)]
if not active_rules:
print("No active rules selected. Exiting.")
return
print(f"Active rules: {[r.name for r in active_rules]}")
# Process files and save results
if args.outputpath:
# User specified output path: collect all results into single file
all_results = []
for filepath in target_files:
file_results = process_file(filepath, config, mode)
all_results.extend(file_results)
if all_results:
save_json_file_path(args.outputpath, all_results)
print(f"Results saved to {args.outputpath}")
else:
# Default: save results per file in same directory
for filepath in target_files:
file_results = process_file(filepath, config, mode)
if file_results:
# Generate output path: same directory as source file
# Format: filename_mode_output_file
file_dir = os.path.dirname(os.path.abspath(filepath))
file_basename = os.path.splitext(os.path.basename(filepath))[0]
output_filename = f"{file_basename}_{mode}_{config.output_file}"
output_path = os.path.join(file_dir, output_filename)
save_json_file_path(output_path, file_results)
print(f"Results saved to {output_path}")
print("Analysis finished!")
if __name__ == "__main__":
main()