xenu_nntp/lib/nntp/tiny/config.py
2024-12-02 23:19:01 -05:00

37 lines
777 B
Python

import os
import configparser
from typing import Optional
class ConfigException(Exception):
def __init__(self, path: str):
self.path = path
def __str__(self):
return "Unable to locate file '" + self.path + "'"
class Config():
SEARCH_PATHS = [
'./server.conf',
'/etc/nntp-tiny/server.conf'
]
@staticmethod
def find():
for path in Config.SEARCH_PATHS:
if os.path.exists(path):
return path
return None
def load(path: Optional[str]=None):
if path is None:
path = Config.find()
if path is None:
raise ConfigException(path)
parser = configparser.ConfigParser()
parser.read(path)
return parser