47 lines
1.2 KiB
Python
Raw Normal View History

2019-01-27 20:26:50 -08:00
"""Module for config file helper functions."""
import configparser
2019-03-30 08:00:17 -07:00
from pathlib import Path
2019-01-27 20:26:50 -08:00
2019-01-30 17:05:49 -08:00
# ========== Constants ==========
2019-03-30 08:00:17 -07:00
PACMAN_CONF = Path("/etc/pacman.conf")
2019-01-27 20:26:50 -08:00
2019-01-30 17:05:49 -08:00
# ========== Functions ==========
2019-01-27 20:26:50 -08:00
def parse_configfile(filepath):
"""Parse a config file given its path and return
a ConfigParser object.
:param path: path to config file to parse
:type path: str, bytes, or path-like object
:returns: object used to parse config file
:rtype: ConfigParser object
2019-04-14 08:51:38 -07:00
:raises FileNotFoundError: if path does not exist
2019-01-27 20:26:50 -08:00
"""
2019-10-24 09:36:58 -07:00
if not Path(filepath).is_file():
2019-01-27 20:26:50 -08:00
raise FileNotFoundError(f"{filepath} does not exist")
config_reader = configparser.ConfigParser(strict=False, allow_no_value=True)
2019-01-27 20:26:50 -08:00
config_reader.read(filepath)
return config_reader
def list_configured_repos():
"""Read /etc/pacman.conf to list all configured repositories.
:raises FileNotFoundError: if config file does not exist
:returns: all repos configured on the system
:rtype: list
"""
parsed_config = parse_configfile(PACMAN_CONF)
repos = parsed_config.sections()
# remove the 'options' entry from the list
del repos[repos.index("options")]
2019-01-27 20:26:50 -08:00
repos.sort()
return repos