File size: 935 Bytes
8a3099e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List

import yaml

CONFIG_PATH = Path(__file__).resolve().parent.parent / "config" / "error_categories.yaml"


@dataclass(frozen=True)
class ErrorCategory:
    id: int
    name: str
    description: str


def load_categories(config_path: Path = CONFIG_PATH) -> List[ErrorCategory]:
    with open(config_path) as f:
        data = yaml.safe_load(f)
    return [
        ErrorCategory(id=c["id"], name=c["name"], description=c["description"])
        for c in data["categories"]
    ]


def id_to_name(categories: List[ErrorCategory] | None = None) -> Dict[int, str]:
    cats = categories or load_categories()
    return {c.id: c.name for c in cats}


def name_to_id(categories: List[ErrorCategory] | None = None) -> Dict[str, int]:
    cats = categories or load_categories()
    return {c.name: c.id for c in cats}