1ef6f59970940c2742a8385d7c229ab9205dc04b max Fri Oct 23 04:18:47 2015 -0700 adding hg.conf parsing functions to Chris's common.py. Not using yet, as I don't want to break his code. diff --git src/pyLib/common.py src/pyLib/common.py index 259d5b1..9706a57 100755 --- src/pyLib/common.py +++ src/pyLib/common.py @@ -54,30 +54,67 @@ result = dict() firstLine = True for line in file: if firstLine: firstLine = False continue splitLine = line[:-1].split(separator) count = 0 list = [] for item in splitLine: list.append(item) result.setdefault(splitLine[keyColumn],list) return result +def parseConf(fname): + """ parse a hg.conf style file, + return a dict key -> value (both are strings) + """ + + conf = {} + for line in open(fname): + line = line.strip() + if line.startswith("#"): + continue + elif line.startswith("include "): + inclFname = line.split()[1] + inclPath = join(dirname(fname), inclFname) + if isfile(inclPath): + inclDict = parseConf(inclPath) + conf.update(inclDict) + elif "=" in line: # string search for "=" + key, value = line.split("=") + conf[key] = value + return conf + +hgConf = None + +def parseHgConf(confDir="."): + """ return hg.conf as dict key:value """ + global hgConf + if hgConf is not None: + return hgConf + + hgConf = dict() # python dict = hash table + fname = join(currDir, confDir, "hg.conf") + if not isfile(fname): + return {} + hgConf = parseConf(fname) + + return hgConf + def getSQLLoginInfo(): """ Output: result - An object with three elements, host, user, password. Grab the SQL login info from the users home directory hg.conf file. """ hst = "" usr = "" pw = "" for line in open("/cluster/home/" + getpass.getuser() + "/.hg.conf","r"): splitLine=line.split("=") if (splitLine[0]== "db.host"): hst = splitLine[1][:-1] if (splitLine[0]== "db.user"): usr = splitLine[1][:-1] if (splitLine[0]== "db.password"): pw = splitLine[1][:-1] return (hst, usr, pw)