me.fier.gawk/module/config.py

53 lines
1.1 KiB
Python

import os
try:
import configparser
except Exception:
import ConfigParser as configparser
paths = []
def find(path,method = os.path.isfile):
for p in paths:
fp = mfp.p(p,path)
if method(fp): return fp
raise Exception("Config file not found: " +path)
def findAll(path,method = os.path.isfile):
rtn = []
for p in paths:
fp = mfp.p(p,path)
if method(fp): rtn.append(fp)
return rtn
def readList(path):
with open(path,encoding="utf-8") as f:
content = f.read().replace("\r","").strip("\n").split("\n")
index = 0
length = len(content)
while index < length:
line = content[index]
line.strip("\t ")
if line == "":
del content[index]
length -= 1
continue
content[index] = line
index += 1
return content
def readIni(path,config = None):
if config == None:
config = configparser.ConfigParser()
config.optionxform = str
config.read(path)
return config
def init():
import sys
if sys.platform == "windows":
paths.append(mfp.p(os.environ["APPDATA"],mfp.programName))
else:
paths.append(mfp.p(os.environ["HOME"],".config",mfp.programName))
paths.append(mfp.p(mfp.sd,"user"))
init()