-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor DBC file handling and path resolution #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |||||
| from typing import List, Optional, IO, Callable, Dict, Set | ||||||
| from zoneinfo import ZoneInfo | ||||||
| from dataclasses import dataclass, asdict | ||||||
| from pathlib import Path | ||||||
| import cantools | ||||||
| from influxdb_client import InfluxDBClient, WriteOptions | ||||||
|
|
||||||
|
|
@@ -53,6 +54,9 @@ def _env_int(var_name: str, default: int) -> int: | |||||
| INFLUX_TOKEN = os.getenv("INFLUXDB_TOKEN", "apiv3_dev-influxdb-admin-token") | ||||||
| INFLUX_ORG = "WFR" | ||||||
| INFLUX_BUCKET = "WFR25" | ||||||
| DBC_ENV_VAR = "DBC_FILE_PATH" | ||||||
| DBC_FILENAME = "example.dbc" | ||||||
| INSTALLER_ROOT = Path(__file__).resolve().parent.parent | ||||||
|
|
||||||
| # Mode switch | ||||||
| BACKFILL_MODE = os.getenv("BACKFILL", "0") == "1" | ||||||
|
|
@@ -169,6 +173,39 @@ def compute_file_hash(file_path: str) -> str: | |||||
| return hash_md5.hexdigest() | ||||||
|
|
||||||
|
|
||||||
| def _resolve_dbc_path() -> Path: | ||||||
| """Resolve the DBC path using env override, shared installer copy or local fallback.""" | ||||||
| env_override = os.getenv(DBC_ENV_VAR) | ||||||
| if env_override: | ||||||
| env_path = Path(env_override).expanduser() | ||||||
| if env_path.exists(): | ||||||
| return env_path | ||||||
| print(f"⚠️ {DBC_ENV_VAR}={env_override} not found; falling back to default lookup.") | ||||||
|
|
||||||
| shared_candidates = [ | ||||||
| INSTALLER_ROOT / DBC_FILENAME, | ||||||
| Path("/installer") / DBC_FILENAME, | ||||||
| ] | ||||||
| for candidate in shared_candidates: | ||||||
| if candidate.exists(): | ||||||
| return candidate | ||||||
|
|
||||||
| # Final fallback: look for local .dbc files (maintains backwards compatibility) | ||||||
| current_dir = Path(__file__).resolve().parent | ||||||
| dbc_candidates = sorted( | ||||||
| current_dir.glob("*.dbc"), | ||||||
| key=lambda file_path: file_path.stat().st_mtime, | ||||||
| reverse=True | ||||||
| ) | ||||||
| if dbc_candidates: | ||||||
| return dbc_candidates[0] | ||||||
|
|
||||||
| raise FileNotFoundError( | ||||||
| f"Could not locate {DBC_FILENAME}. Place it in the installer root " | ||||||
| f"or set {DBC_ENV_VAR} to the desired path." | ||||||
|
||||||
| f"or set {DBC_ENV_VAR} to the desired path." | |
| f"(INSTALLER_ROOT) or set {DBC_ENV_VAR} to the desired path." |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _resolve_dbc_path() function is duplicated between load_data.py and helper.py with nearly identical logic. This violates the DRY (Don't Repeat Yourself) principle and makes maintenance harder—any bug fix or enhancement needs to be applied in two places.
Consider extracting this shared logic into a common utility module that both files can import, or if the files are always used together, consolidate the function in one location and import it from the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constants
DBC_ENV_VAR,DBC_FILENAME, andINSTALLER_ROOTare duplicated betweenload_data.pyandhelper.py. Like the_resolve_dbc_path()function, these should be defined in a shared location to avoid potential inconsistencies and maintenance issues.If these constants ever need to be updated (e.g., changing the default filename or environment variable name), the change would need to be made in multiple files.