Files
balanceboard/utils.py
chelsea e821a26b48 Initial commit: BalanceBoard - Reddit-style content aggregator
- Flask-based web application with PostgreSQL
- User authentication and session management
- Content moderation and filtering
- Docker deployment with docker-compose
- Admin interface for content management

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-11 16:11:13 -05:00

58 lines
1.5 KiB
Python

"""
Utilities Library
Generic utility functions shared across modules.
"""
import uuid
import json
from pathlib import Path
from typing import Dict, Any
def generate_uuid() -> str:
"""Generate a new UUID string"""
return str(uuid.uuid4())
def load_json_file(file_path: str) -> Any:
"""Load JSON from file"""
with open(file_path, 'r') as f:
return json.load(f)
def save_json_file(data: Any, file_path: str, indent: int = 2):
"""Save data to JSON file"""
path = Path(file_path)
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, 'w') as f:
json.dump(data, f, indent=indent)
def ensure_directory(dir_path: str) -> Path:
"""Create directory if it doesn't exist, return Path object"""
path = Path(dir_path)
path.mkdir(parents=True, exist_ok=True)
return path
def load_json_files_from_dir(dir_path: str, pattern: str = "*.json") -> Dict[str, Any]:
"""Load all JSON files from directory into dict keyed by filename (without extension)"""
directory = Path(dir_path)
data = {}
if directory.exists():
for file_path in directory.glob(pattern):
key = file_path.stem # filename without extension
data[key] = load_json_file(str(file_path))
return data
def count_files(dir_path: str, pattern: str = "*.json") -> int:
"""Count files matching pattern in directory"""
directory = Path(dir_path)
if not directory.exists():
return 0
return len(list(directory.glob(pattern)))