add config path search order
This commit is contained in:
20
config.py
20
config.py
@@ -2,15 +2,29 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import configparser
|
import configparser
|
||||||
import codecs
|
import codecs
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self, path: str = "config.ini"):
|
def __init__(self, path: str = "config.ini"):
|
||||||
if os.path.exists(path):
|
path_search_order = [
|
||||||
|
path,
|
||||||
|
"./config.ini",
|
||||||
|
os.path.join(Path.home(), "avdc.ini"),
|
||||||
|
os.path.join(Path.home(), ".avdc.ini"),
|
||||||
|
os.path.join(Path.home(), ".avdc/config.ini"),
|
||||||
|
os.path.join(Path.home(), ".config/avdc/config.ini")
|
||||||
|
]
|
||||||
|
ini_path = None
|
||||||
|
for p in path_search_order:
|
||||||
|
if os.path.exists(p):
|
||||||
|
ini_path = p
|
||||||
|
break
|
||||||
|
if ini_path:
|
||||||
self.conf = configparser.ConfigParser()
|
self.conf = configparser.ConfigParser()
|
||||||
try:
|
try:
|
||||||
self.conf.read(path, encoding="utf-8-sig")
|
self.conf.read(ini_path, encoding="utf-8-sig")
|
||||||
except:
|
except:
|
||||||
self.conf.read(path, encoding="utf-8")
|
self.conf.read(ini_path, encoding="utf-8")
|
||||||
else:
|
else:
|
||||||
print("[-]Config file not found!")
|
print("[-]Config file not found!")
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|||||||
Reference in New Issue
Block a user