-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkassis_config.py
49 lines (40 loc) · 1.32 KB
/
kassis_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os.path
import re
import platform
import configparser
import sys
try:
from win32com.shell import shell, shellcon
except ModuleNotFoundError:
pass
def get():
folders = []
ext_name = ".ini"
folders.append(os.path.dirname(os.path.abspath(__file__)))
folders.append(os.path.expanduser("~"))
if platform.system() == "Windows":
folders.append(shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA, None, 0))
else:
folders.append("/etc")
ini_fileepath = None
for f in folders:
inifile = os.path.join(f, f"kassis{ext_name}")
#print(inifile)
if os.path.isfile(inifile):
ini_fileepath = inifile
break
if ini_fileepath is None:
sys.stderr.write("kassis.iniが見つかりません\n")
sys.exit(2)
config = configparser.ConfigParser()
if os.path.exists(ini_fileepath):
config.read(ini_fileepath, encoding='utf8')
else:
sys.stderr.write(ini_fileepath + " が見つかりません\n")
sys.exit(2)
for s in config.sections():
for i in config[s]:
for m in re.finditer(r"(\$)(\w+)", config[s][i]):
if os.environ[m.group(2)] is not None:
config[s][i] = re.sub(r"(\$)(\w+)", os.environ[m.group(2)], config[s][i])
return config