ref: Parse conf file with dict comprehension and named tuple

This commit is contained in:
Marty Oehme 2025-04-08 20:06:25 +02:00
parent 9d5fa3e244
commit 4642b24c6b
Signed by: Marty
GPG key ID: 4E535BC19C61886E

View file

@ -21,6 +21,7 @@ import configparser
import os import os
import subprocess import subprocess
import sys import sys
from collections import namedtuple
from dataclasses import asdict, dataclass, field from dataclasses import asdict, dataclass, field
from pathlib import Path from pathlib import Path
from typing import Any, Self, cast from typing import Any, Self, cast
@ -241,19 +242,21 @@ def parse_conf(conf_file: Path) -> dict:
with open(conf_file.expanduser()) as f: with open(conf_file.expanduser()) as f:
c.read_string("[GENERAL]\n" + f.read()) c.read_string("[GENERAL]\n" + f.read())
res = {} ConfTrans = namedtuple("ParsedToTConf", ["name", "tconf_name"])
for option in [ return _filtered_dict(
# tuples with: (conf option name, TConf member name) {
("data.location", "task_data"), opt.tconf_name: c.get("GENERAL", opt.name)
("notes.dir", "notes_dir"), for opt in [
("notes.ext", "notes_ext"), ConfTrans("data.location", "task_data"),
("notes.annot", "notes_annot"), ConfTrans("notes.dir", "notes_dir"),
("notes.editor", "notes_editor"), ConfTrans("notes.ext", "notes_ext"),
("notes.quiet", "notes_quiet"), ConfTrans("notes.annot", "notes_annot"),
]: ConfTrans("notes.editor", "notes_editor"),
if c.has_option("GENERAL", option[0]): ConfTrans("notes.quiet", "notes_quiet"),
res[option[1]] = c.get("GENERAL", option[0]) ]
return _filtered_dict(res) if c.has_option("GENERAL", opt.name)
}
)
IS_QUIET = False IS_QUIET = False