source_metadata.py
Ricky/RAG_project/processors/source_metadata.py
"""
Source metadata helpers: normalize and standardize file-origin metadata
so all downstream modules can rely on consistent field names.
"""
import os
from pathlib import Path
PROJECT_ROOT = str(Path(__file__).resolve().parent.parent)
def normalize_source_path(filepath):
absolute = os.path.abspath(filepath)
try:
relative = os.path.relpath(absolute, PROJECT_ROOT)
except ValueError:
relative = absolute
return relative.replace("\\", "/")
def build_source_metadata(filepath, source_type, **extra):
source_name = os.path.basename(filepath)
metadata = {
"source": source_name,
"source_name": source_name,
"source_path": normalize_source_path(filepath),
"source_type": source_type,
}
for key, value in extra.items():
if value is not None and value != "":
metadata[key] = value
return metadata
def ensure_source_metadata(metadata, source_type=None):
meta = dict(metadata or {})
source_path = meta.get("source_path") or meta.get("source")
if isinstance(source_path, str) and source_path:
source_path = source_path.replace("\\", "/")
meta["source_path"] = source_path
meta["source"] = os.path.basename(source_path)
meta.setdefault("source_name", meta["source"])
else:
meta.setdefault("source", "unknown")
meta.setdefault("source_name", meta["source"])
meta.setdefault("source_path", meta["source"])
if source_type:
meta["source_type"] = source_type
else:
meta.setdefault("source_type", "unknown")
return meta
Related articles
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
Read article →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
Read article →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
Read article →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
Read article →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
Read article →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
Read article →