-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
225 lines (172 loc) · 7.72 KB
/
utils.py
File metadata and controls
225 lines (172 loc) · 7.72 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
"""
CodeSense - Utilities
File handling, GitHub integration, exports, and helper functions.
"""
import hashlib
import json
import os
import re
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from urllib.request import urlopen, Request
from urllib.error import URLError, HTTPError
from urllib.parse import urlparse
from constants import LANGUAGE_EXTENSIONS, MAX_FILE_SIZE_MB, MAX_CODE_LINES
from logger import get_logger
logger = get_logger(__name__)
# ─── File Handling ────────────────────────────────────────────────────────────
def read_file(path: str) -> Tuple[str, str]:
"""
Read a source file and detect its language.
Returns:
(code, language)
Raises:
ValueError on unsupported file types.
OSError on read errors.
"""
p = Path(path)
suffix = p.suffix.lower()
if suffix not in LANGUAGE_EXTENSIONS:
raise ValueError(f"Unsupported file extension '{suffix}'.")
size_mb = p.stat().st_size / (1024 * 1024)
if size_mb > MAX_FILE_SIZE_MB:
raise ValueError(f"File too large ({size_mb:.1f} MB). Max: {MAX_FILE_SIZE_MB} MB.")
code = p.read_text(encoding="utf-8", errors="replace")
if len(code.splitlines()) > MAX_CODE_LINES:
raise ValueError(f"File exceeds {MAX_CODE_LINES} lines.")
return code, LANGUAGE_EXTENSIONS[suffix]
def detect_language_from_code(code: str) -> str:
"""Heuristic language detection from code content."""
if re.search(r"#include\s*<|::\w+|std::", code):
return "cpp"
if re.search(r"\bpublic\s+class\b|\bimport\s+java\.|@Override", code):
return "java"
if re.search(r"\bdef\s+\w+\(|import\s+\w+|from\s+\w+\s+import", code):
return "python"
# Count keywords
python_score = len(re.findall(r"\b(def|import|print|elif|lambda|None|True|False)\b", code))
java_score = len(re.findall(r"\b(public|private|protected|void|int|String|class)\b", code))
cpp_score = len(re.findall(r"\b(#include|cout|cin|namespace|template|nullptr)\b", code))
scores = {"python": python_score, "java": java_score, "cpp": cpp_score}
return max(scores, key=scores.get)
def code_hash(code: str) -> str:
"""Return SHA-256 hash of source code."""
return hashlib.sha256(code.encode("utf-8")).hexdigest()
# ─── GitHub Integration ───────────────────────────────────────────────────────
def fetch_github_file(url: str, timeout: int = 10) -> Tuple[str, str]:
"""
Fetch a file from GitHub (raw URL or repository URL).
Args:
url: GitHub URL (repo page or raw).
timeout: Request timeout in seconds.
Returns:
(code, language)
"""
raw_url = _github_to_raw_url(url)
logger.info("Fetching GitHub file: %s", raw_url)
try:
req = Request(raw_url, headers={"User-Agent": "CodeSense/2.0"})
with urlopen(req, timeout=timeout) as resp:
if resp.status != 200:
raise ValueError(f"HTTP {resp.status} from GitHub.")
content = resp.read().decode("utf-8", errors="replace")
except HTTPError as exc:
raise ValueError(f"GitHub returned HTTP {exc.code}: {exc.reason}")
except URLError as exc:
raise ValueError(f"Could not reach GitHub: {exc.reason}")
# Detect language from URL extension
parsed = urlparse(raw_url)
suffix = Path(parsed.path).suffix.lower()
language = LANGUAGE_EXTENSIONS.get(suffix, detect_language_from_code(content))
return content, language
def _github_to_raw_url(url: str) -> str:
"""Convert a github.com URL to raw.githubusercontent.com."""
if "raw.githubusercontent.com" in url:
return url
# github.com/user/repo/blob/branch/path → raw.githubusercontent.com/user/repo/branch/path
m = re.match(
r"https://github\.com/([^/]+)/([^/]+)/blob/([^/]+)/(.+)", url
)
if m:
return f"https://raw.githubusercontent.com/{m.group(1)}/{m.group(2)}/{m.group(3)}/{m.group(4)}"
raise ValueError(f"Cannot convert URL to raw: {url}")
# ─── Timing ──────────────────────────────────────────────────────────────────
class Timer:
"""Simple context manager for timing code blocks."""
def __init__(self) -> None:
self._start = 0.0
self.elapsed_ms = 0
def __enter__(self) -> "Timer":
self._start = time.perf_counter()
return self
def __exit__(self, *_) -> None:
self.elapsed_ms = int((time.perf_counter() - self._start) * 1000)
# ─── Export Helpers ───────────────────────────────────────────────────────────
def results_to_markdown(results: Dict[str, Any]) -> str:
"""Convert analysis results to a readable Markdown report."""
score = results.get("score", 0)
grade = results.get("grade", "?")
lang = results.get("language", "unknown")
fb = results.get("feedback", {})
lines = [
f"# CodeSense Analysis Report",
f"",
f"**Score:** {score}/100 | **Grade:** {grade} | **Language:** {lang.upper()}",
f"",
f"## Summary",
f"{fb.get('opening', '')}",
f"",
]
strengths = fb.get("strengths", [])
if strengths:
lines += ["## ✅ Strengths", ""]
for s in strengths:
lines.append(f"- {s}")
lines.append("")
items = fb.get("items", [])
errors = [i for i in items if i["severity"] == "error"]
warnings = [i for i in items if i["severity"] == "warning"]
if errors:
lines += ["## ❌ Errors", ""]
for i in errors:
lines.append(f"### {i['title']}")
lines.append(i["message"])
if i.get("code_before"):
lines += ["```", i["code_before"], "```"]
if i.get("code_after"):
lines += ["**Fix:**", "```", i["code_after"], "```"]
lines.append("")
if warnings:
lines += ["## ⚠️ Warnings", ""]
for i in warnings:
lines.append(f"### {i['title']}")
lines.append(i["message"])
lines.append("")
next_steps = fb.get("next_steps", [])
if next_steps:
lines += ["## 📋 Next Steps", ""]
for step in next_steps:
lines.append(f"- {step}")
learning = fb.get("learning_path", [])
if learning:
lines += ["", "## 📚 Learning Path", ""]
for item in learning:
r = item.get("resource", {})
url = r.get("url", "#") if r else "#"
lines.append(f"{item['step']}. **{item['title']}** — {item['description']} [{r.get('title','')}]({url})")
return "\n".join(lines)
def results_to_json(results: Dict[str, Any], indent: int = 2) -> str:
"""Serialise results to pretty-printed JSON."""
return json.dumps(results, indent=indent, default=str)
# ─── Formatting ──────────────────────────────────────────────────────────────
def truncate(text: str, max_len: int = 100) -> str:
return text if len(text) <= max_len else text[:max_len - 3] + "..."
def format_duration(ms: int) -> str:
if ms < 1000:
return f"{ms}ms"
return f"{ms/1000:.1f}s"
def percentage(value: float, total: float) -> str:
if total == 0:
return "0%"
return f"{(value / total * 100):.1f}%"