setools/setoolsgui/config.py
Chris PeBenito cd24446963 setoolsgui: Address new errors in mypy 1.6.0.
Most come from the union-attr check, throwing errors because PyQt returns,
for example, "QObjecet | None", so code is using attributes on the PyQt
object that don't exist on None.

In some cases, classes gained new overridden method implementations that
do type narrowing to ensure a non-None object is returned.

This also includes a new QListView subclass with the above overrides.  With
the new class, there is some light refactoring in ListCriteriaWidget,
moving the selection methods to the new class.

The test code (unit tests and module __main__) simply ignore union-attr
errors, since we want that kind of runtime error to pop loudly.

There are some remaining issues, but they seem to be issues in the PyQt5
typing.

Signed-off-by: Chris PeBenito <pebenito@ieee.org>
2024-02-14 09:11:35 -05:00

68 lines
1.8 KiB
Python

# Copyright 2019, Chris PeBenito <pebenito@ieee.org>
#
# SPDX-License-Identifier: LGPL-2.1-only
#
#
import os
import logging
import configparser
import threading
import typing
#
# Configfile constants
#
APOLCONFIG: typing.Final[str] = "~/.config/setools/apol.conf"
HELP_SECTION: typing.Final[str] = "Help"
HELP_PGM: typing.Final[str] = "assistant"
DEFAULT_HELP_PGM: typing.Final[str] = "/usr/bin/assistant"
class ApolConfig:
"""Apol configuration file."""
def __init__(self) -> None:
self.log: typing.Final = logging.getLogger(__name__)
self._lock = threading.Lock()
self.path: typing.Final = os.path.expanduser(APOLCONFIG)
self._config = configparser.ConfigParser()
save = False
if not self._config.read((self.path,)):
save = True
if not self._config.has_section(HELP_SECTION):
self._config.add_section(HELP_SECTION)
self._config.set(HELP_SECTION, HELP_PGM, DEFAULT_HELP_PGM)
save = True
if save:
self.save()
def save(self) -> None:
"""Save configuration file."""
with self._lock:
try:
os.makedirs(os.path.dirname(self.path), mode=0o755, exist_ok=True)
with open(self.path, "w") as fd:
self._config.write(fd)
except Exception:
self.log.critical(f"Failed to save configuration file \"{self.path}\"")
self.log.debug("Backtrace", exc_info=True)
@property
def assistant(self) -> str:
"""Return the help program executable path."""
with self._lock:
return self._config.get(HELP_SECTION, HELP_PGM, fallback=DEFAULT_HELP_PGM)
@assistant.setter
def assistant(self, value: str) -> None:
with self._lock:
self._config.set(HELP_SECTION, HELP_PGM, value)