From 970653c22d8996ca6d346ba3477529805ab12d02 Mon Sep 17 00:00:00 2001 From: gryf Date: Thu, 19 Oct 2023 19:48:38 +0200 Subject: [PATCH] Added new class for configuration reading. --- extfslib.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- setup.cfg | 9 ++++----- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/extfslib.py b/extfslib.py index 70bebba..f2aa72b 100644 --- a/extfslib.py +++ b/extfslib.py @@ -2,24 +2,70 @@ extfslib is a library which contains Archive class to support writing extfs plugins for Midnight Commander. -Tested against python 3.6 and mc 4.8.22 +Tested against python 3.11 and mc 4.8.29 Changelog: + 1.3 Added ability to read config from mc/ini file. 1.2 Switch to python3 1.1 Added item pattern, and common git/uid attrs 1.0 Initial release Author: Roman 'gryf' Dobosz -Date: 2019-06-30 -Version: 1.2 +Date: 2023-10-18 +Version: 1.3 Licence: BSD """ import argparse +import configparser import os import re import subprocess import sys +XDG_CONF_DIR = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) + + +class Config: + """An optional config class, a helper to get and parse MC ini file""" + + def __init__(self, caller): + self._config = self._get_config() + self._class_name = caller.__class__.__name__.lower() + + def _get_config(self): + """Read MC main config file""" + conf_file = os.path.join(XDG_CONF_DIR, 'mc/ini') + conf_parser = configparser.ConfigParser() + conf_parser.read(conf_file) + return conf_parser + + def getboolean(self, name): + try: + return self._config.getboolean(self._class_name, name) + except (configparser.NoOptionError, configparser.NoSectionError): + pass + + def getint(self, name): + try: + return self._config.getint(self._class_name, name) + except (configparser.NoOptionError, configparser.NoSectionError): + pass + + def getfloat(self, name): + try: + return self._config.getfloat(self._class_name, name) + except (configparser.NoOptionError, configparser.NoSectionError): + pass + + def get(self, name): + return getattr(self, name) + + def __getattr__(self, name): + try: + return self._config.get(self._class_name, name) + except (configparser.NoOptionError, configparser.NoSectionError): + pass + class Archive(object): """Archive handle. Provides interface to MC's extfs subsystem""" diff --git a/setup.cfg b/setup.cfg index e433f82..2053336 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,19 +9,18 @@ classifiers = Operating System :: OS Independent Programming Language :: Python Programming Language :: Python :: 3 - Programming Language :: Python :: 3.6 - Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 Topic :: Utilities description = Midnight Commander extfslib helper library for writing extfs archive plugins. -license = BSD 3-Clause -license_file = LICENSE +license = BSD 3-Clause License +license_files = LICENSE long_description = file: README.rst name = extfslib url = https://github.com/gryf/mc_extfslib -version = 1.1 +version = 1.3 [options] py_modules = extfslib