File size: 1,688 Bytes
65b86c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared utilities for scripts/. Reduces duplication across data/model scripts.
"""
from __future__ import annotations

import logging
import sys
from pathlib import Path

# Ensure project root on path for config imports
_PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(_PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(_PROJECT_ROOT))


def get_project_root() -> Path:
    """Project root directory."""
    return _PROJECT_ROOT


def get_data_dir() -> Path:
    """Data directory (data/)."""
    return _PROJECT_ROOT / "data"


def setup_script_logger(
    name: str,
    level: int = logging.INFO,
    format_str: str = "%(asctime)s | %(levelname)s | %(name)s | %(message)s",
    datefmt: str = "%H:%M:%S",
) -> logging.Logger:
    """
    Configure logging for a script. Use instead of ad-hoc logging.basicConfig.
    """
    logger = logging.getLogger(name)
    if not logger.handlers:
        handler = logging.StreamHandler()
        handler.setFormatter(logging.Formatter(format_str, datefmt=datefmt))
        logger.addHandler(handler)
    logger.setLevel(level)
    return logger


def load_data_config():
    """Lazy-load config.data_config paths. Use when script needs DATA_DIR, BOOKS_PROCESSED, etc."""
    from config.data_config import (
        DATA_DIR,
        RAW_DIR,
        BOOKS_PROCESSED,
        BOOKS_BASIC_INFO,
        REC_DIR,
        RAW_BOOKS,
        RAW_RATINGS,
    )
    return {
        "data_dir": DATA_DIR,
        "raw_dir": RAW_DIR,
        "books_processed": BOOKS_PROCESSED,
        "books_basic_info": BOOKS_BASIC_INFO,
        "rec_dir": REC_DIR,
        "raw_books": RAW_BOOKS,
        "raw_ratings": RAW_RATINGS,
    }