Source code for magboltz_gui.data.database
"""Static gas catalogue used by the mixture editor."""
from csv import DictReader
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional
[docs]
@dataclass
class GasItem:
"""One gas entry loaded from the bundled CSV catalogue."""
id: int
name: str
formula: Optional[str]
year: Optional[int]
note: Optional[str]
rating: Optional[int]
@property
def as_name(self) -> str:
return self.name
@property
def as_name_formula(self) -> str:
return f"{self.name} ({self.formula})" if self.formula else self.name
@property
def as_formula(self) -> str:
return self.formula if self.formula else self.name
[docs]
@dataclass
class GasDatabase:
"""In-memory lookup table for gas definitions."""
content: List[GasItem] = field(default_factory=list)
[docs]
def load(self, filepath: Path) -> None:
"""Load gas definitions from a CSV file."""
with open(filepath, newline="", encoding="utf-8") as csvfile:
reader = DictReader(csvfile)
for row in reader:
gas = GasItem(
id=int(row["id"]),
name=row["name"].strip(),
formula=row["formula"].strip() if row["formula"] else None,
year=int(row["year"]) if row["year"] else None,
note=row["note"].strip() if row["note"] else None,
rating=int(row.get("rating", "")) if row["rating"] else None,
)
self.content.append(gas)
[docs]
def get(self, id: int) -> GasItem:
"""Return one gas definition by numeric Magboltz id."""
for item in self.content:
if item.id == id:
return item
raise KeyError(f"Gas with id {id} not found")