Spaces:
Runtime error
Runtime error
Create FixImports.py
Browse files- pages/FixImports.py +63 -0
pages/FixImports.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
st.title("🧩 Import Path Fixer")
|
| 6 |
+
st.write("Automatically patch missing `utils` imports across the Omniscient Framework.")
|
| 7 |
+
|
| 8 |
+
# ─── Configuration ─────────────────────────────────────────────
|
| 9 |
+
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
| 10 |
+
SEARCH_PATHS = [
|
| 11 |
+
os.path.join(PROJECT_ROOT, "pages"),
|
| 12 |
+
os.path.join(PROJECT_ROOT, "utils"),
|
| 13 |
+
PROJECT_ROOT,
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
# ─── Helper Function ───────────────────────────────────────────
|
| 17 |
+
def fix_imports_in_file(file_path):
|
| 18 |
+
"""
|
| 19 |
+
Scans a .py file and replaces any `from utils.` imports
|
| 20 |
+
with `from omniscientframework.utils.` for portability.
|
| 21 |
+
"""
|
| 22 |
+
try:
|
| 23 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 24 |
+
content = f.read()
|
| 25 |
+
|
| 26 |
+
new_content = re.sub(
|
| 27 |
+
r"from\s+utils(\.[\w_]+)?\s+import",
|
| 28 |
+
r"from omniscientframework.utils\1 import",
|
| 29 |
+
content,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if new_content != content:
|
| 33 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 34 |
+
f.write(new_content)
|
| 35 |
+
return True
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.error(f"⚠️ Error fixing {file_path}: {e}")
|
| 40 |
+
return False
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ─── UI ────────────────────────────────────────────────────────
|
| 44 |
+
if st.button("🔧 Fix All Imports Now"):
|
| 45 |
+
modified_files = []
|
| 46 |
+
for search_dir in SEARCH_PATHS:
|
| 47 |
+
for root, _, files in os.walk(search_dir):
|
| 48 |
+
for fname in files:
|
| 49 |
+
if fname.endswith(".py"):
|
| 50 |
+
fpath = os.path.join(root, fname)
|
| 51 |
+
if fix_imports_in_file(fpath):
|
| 52 |
+
modified_files.append(fpath)
|
| 53 |
+
|
| 54 |
+
if modified_files:
|
| 55 |
+
st.success(f"✅ Fixed imports in {len(modified_files)} files.")
|
| 56 |
+
st.write("### Modified Files:")
|
| 57 |
+
for f in modified_files:
|
| 58 |
+
st.code(f.replace(PROJECT_ROOT, ""), language="bash")
|
| 59 |
+
else:
|
| 60 |
+
st.info("✅ All imports already use the correct `omniscientframework.utils` format.")
|
| 61 |
+
|
| 62 |
+
st.markdown("---")
|
| 63 |
+
st.caption("⚙️ Import Fixer v1.0 — Omniscient Framework internal utility.")
|