Added new class for configuration reading.

This commit is contained in:
2023-10-19 19:48:38 +02:00
parent 5f52e61a4e
commit 970653c22d
2 changed files with 53 additions and 8 deletions

View File

@@ -2,24 +2,70 @@
extfslib is a library which contains Archive class to support writing extfs extfslib is a library which contains Archive class to support writing extfs
plugins for Midnight Commander. 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: Changelog:
1.3 Added ability to read config from mc/ini file.
1.2 Switch to python3 1.2 Switch to python3
1.1 Added item pattern, and common git/uid attrs 1.1 Added item pattern, and common git/uid attrs
1.0 Initial release 1.0 Initial release
Author: Roman 'gryf' Dobosz <gryf73@gmail.com> Author: Roman 'gryf' Dobosz <gryf73@gmail.com>
Date: 2019-06-30 Date: 2023-10-18
Version: 1.2 Version: 1.3
Licence: BSD Licence: BSD
""" """
import argparse import argparse
import configparser
import os import os
import re import re
import subprocess import subprocess
import sys 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): class Archive(object):
"""Archive handle. Provides interface to MC's extfs subsystem""" """Archive handle. Provides interface to MC's extfs subsystem"""

View File

@@ -9,19 +9,18 @@ classifiers =
Operating System :: OS Independent Operating System :: OS Independent
Programming Language :: Python Programming Language :: Python
Programming Language :: Python :: 3 Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10 Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Topic :: Utilities Topic :: Utilities
description = Midnight Commander extfslib helper library for writing extfs archive plugins. description = Midnight Commander extfslib helper library for writing extfs archive plugins.
license = BSD 3-Clause license = BSD 3-Clause License
license_file = LICENSE license_files = LICENSE
long_description = file: README.rst long_description = file: README.rst
name = extfslib name = extfslib
url = https://github.com/gryf/mc_extfslib url = https://github.com/gryf/mc_extfslib
version = 1.1 version = 1.3
[options] [options]
py_modules = extfslib py_modules = extfslib