me.fier.gawk/module/config.py

54 lines
1.2 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 parseListLine(line,parseComments = True):
line = line.replace("\r","").strip("\n")
if parseComments: line = line.split("#",1)[0]
line = line.strip("\t ")
return line
def readList(path,parseComments = True):
with open(path,encoding="utf-8") as f:
line = f.readline()
while line:
line = parseListLine(line)
if line == "":
line = f.readline()
continue
yield line
line = f.readline()
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()