fenen/config.py

45 lines
1.6 KiB
Python

import os
class Config:
def __init__(self):
self.config = {}
self._set_defaults()
def _set_defaults(self):
if "XDG_DATA_HOME" in os.environ:
datadir = os.environ["XDG_DATA_HOME"]
elif os.path.isdir(os.environ["HOME"] + "/.local/share"):
datadir = os.environ["HOME"] + "/.local/share"
else:
datadir = os.environ["HOME"] + "/.fenen"
if not os.path.isdir(datadir):
os.mkdir(datadir)
self.config["db"] = datadir + "/fenen.db"
self.config["threads"] = 4
self.config["browser"] = "firefox"
self.config["pager"] = "less"
self.config["downloader"] = "curl -O"
def parse_config(self):
if "XDG_CONFIG_HOME" in os.environ and os.path.isfile(
os.environ["XDG_CONFIG_HOME"] + "/fenen.conf"
):
configfile = os.environ["XDG_CONFIG_HOME"] + "/fenen.conf"
elif os.path.isfile(os.environ["HOME"] + "/.config/fenen.conf"):
configfile = os.environ["HOME"] + "/.config/fenen.conf"
elif os.path.isfile(os.environ["HOME"] + "/.fenen/fenen.conf"):
configfile = os.environ["HOME"] + "/.fenen/fenen.conf"
else:
return
with open(configfile, "r") as f:
for line in f:
kv = list(map(str.strip, line.split("=", 1)))
if kv[0] in self.config:
self.config[kv[0]] = kv[1]
else:
print(f"Unknown config option: {kv[0]}")
def get_value(self, key):
return self.config[key] if key in self.config else None