Files
Autoparts-DB/console/utils/formatting.py
consultoria-as 7b2a904498 feat: migrate to PostgreSQL + SQLAlchemy ORM, rebrand to Nexus Autoparts
- Migrate from SQLite to PostgreSQL with normalized schema
- Add 11 lookup tables (fuel_type, body_type, drivetrain, transmission,
  materials, position_part, manufacture_type, quality_tier, countries,
  reference_type, shapes)
- Rewrite dashboard/server.py (76 routes) using SQLAlchemy text() queries
- Rewrite console/db.py (27 methods) using SQLAlchemy ORM
- Add models.py with 27 SQLAlchemy model definitions
- Add config.py for centralized DB_URL configuration
- Add migrate_to_postgres.py migration script
- Add docs/METABASE_GUIDE.md with complete data entry guide
- Rebrand from "AUTOPARTS DB" to "NEXUS AUTOPARTS"
- Fill vehicle data gaps via NHTSA API + heuristics:
  engines (cylinders, power, torque), brands (country, founded_year),
  models (body_type, production years), MYE (drivetrain, transmission, trim)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 05:24:47 +00:00

87 lines
2.3 KiB
Python

"""
Display formatting utilities for the NEXUS AUTOPARTS console application.
Functions for currency, numbers, text truncation, table layout, and
quality-tier visual bars.
"""
def format_currency(value) -> str:
"""Format a numeric value as USD currency.
None -> '──'
0 -> '$0.00'
45.99 -> '$45.99'
"""
if value is None:
return "──"
return f"${value:,.2f}"
def format_number(value) -> str:
"""Format an integer with thousands separators.
None -> '0'
13685 -> '13,685'
"""
if value is None:
return "0"
return f"{value:,}"
def truncate(text, max_len) -> str:
"""Truncate text to *max_len* characters, appending '...' if trimmed.
None -> ''
fits -> text unchanged
too long -> text[:max_len-3] + '...'
"""
if text is None:
return ""
if len(text) <= max_len:
return text
return text[: max_len - 3] + "..."
def pad_right(text, width) -> str:
"""Pad *text* to *width* with spaces on the right, or truncate if longer.
None -> ''
fits -> ljust(width)
too long -> text[:width]
"""
if text is None:
return ""
if len(text) > width:
return text[:width]
return text.ljust(width)
def format_table_row(values, widths, separator="") -> str:
"""Join *values* padded to corresponding *widths* with *separator*.
Each value is passed through :func:`pad_right` to ensure uniform column
widths, then all columns are joined by the separator string.
"""
cells = [pad_right(str(v), w) for v, w in zip(values, widths)]
return separator.join(cells)
# ── Quality-tier bars ──────────────────────────────────────────────────
_QUALITY_BARS = {
"oem": "███████████",
"premium": "██████████░",
"standard": "███████░░░░",
"economy": "█████░░░░░░",
}
def quality_bar(tier) -> str:
"""Return a Unicode block-bar representing a quality tier.
Recognised tiers: oem, premium, standard, economy.
Unknown tiers fall back to a minimal bar.
"""
return _QUALITY_BARS.get(tier, "░░░░░░░░░░░")