def saveRealm(fi): if not defaults.get("set",False): setDefaults() lines = [] if fi is None or fi == "": fi = "default" print "Saving realm %s"% fi fi = "realms/%s.rlm"% fi line = getline(fi,1) comp = {} matchlike = config.get("matchlike",False) if "likerealm" in line: values = [a.strip() for a in line.split('=')] comp = loadRealm("realms/%s.rlm"% values[1]) lines.append(line) write = False for key in config.keys(): if key in rlmkeys and config[key] != defaults.get(key): write = False if matchlike == 2: write = True elif matchlike == 1 and config.get(key) != comp.get(key): write = True elif matchlike == 0 and key in rlmsavekeys: write = True if write: lines.append("%s = %s\n" % (key,config[key])) try: f = open(os.path.abspath(fi), 'w') if config['debug'] > 0: printPretty(lines) f.writelines(lines) except IOError as e: print " Could not write configuration file: %s" % e finally: f.close()
def saveConfig(fn): if not defaults.get("set",False): setDefaults() lines = [] for key in config.keys(): if (key in cfgkeys or config['rlmincfg']) and config[key] != defaults.get(key): lines.append("%s = %s\n" % (key,config[key])) if config['debug'] > 0: print lines try: f = open(os.path.abspath(fn), 'w') f.writelines(lines) f.close() except IOError as e: print " Could not write configuration file: %s" % e
def criticalDefaults(): missing = {} try: if not defaults.get("set",False): setDefaults() except NameError: setDefaults() critical = [] for key in cfgkeys: critical.append(key) for key in rlmkeys: critical.append(key) for key in critical: try: x = config[key] except KeyError: missing[key] = defaults[key] return missing
def loadConfig(fn = None,recursion = 0): """Returns a dict containing the config options in the Minette config file.""" maxrecursion = 3 lines = [] global config global defaults if fn is None: fn = "default.cfg" # using 3-letter extension for MSWin compatibility, I hope. (found,fn) = findFile(lineno(),fn) if not found: print " [W] Config %s not loaded." % fn if not defaults.get("set",False): setDefaults() config.update(defaults) return config lines = readfile(fn) for line in lines: try: line = line.strip() if line: values = [x.strip() for x in line.split('=')] if values[0] != "loadconfig": if not config.get(values[0]): config[values[0]] = values[1] # existing options will not be clobbered elif recursion < maxrecursion and os.path.exists(values[1]): # loadconfig must be first option, or its options may be ignored. recursion += 1 loadConfig(values[1],recursion) except Exception as e: print " [E] There was an error in the configuration file: %s" % e config['file'] = fn config = validateConfig(config) config['realmfile'] = "" if len(config.get("loadrealm","")) > 0 and recursion <= maxrecursion: rf = "realms/%s.rlm" % config['loadrealm'] realm = loadRealm(rf) config.update(realm) else: config['realmloaded'] = False config.update(criticalDefaults()) return config
def validateConfig(config): """Checks some config values for validity. Returns adjusted dictionary.""" try: if not defaults.get("set",False): setDefaults() except NameError: setDefaults() keylist = [] keylist.extend(cfgkeys) if config.get("rlmincfg",False): keylist.extend(rlmkeys) for key in keylist: config[key] = config.get(key,defaults[key]) if config[key] == "True": config[key] = True if config[key] == "False": config[key] = False pos = config.get("pos",defaults['pos']) # default position siz = config.get("size",defaults['size']) # default size pattern = re.compile(r'\(\s?(\d+)\s?,\s?(\d+)\s?\)') match = False if pos == str(pos): # otherwise, must be reloading config, no need to reprocess match = pattern.search(pos) if match: config['pos'] = (int(match.group(1)),int(match.group(2))) else: config['pos'] = (0,0) match = False if siz == str(siz): # otherwise, must be reloading config, no need to reprocess match = pattern.search(siz) if match: config['size'] = (int(match.group(1)),int(match.group(2))) else: config['size'] = (620,440) keys = ["debug","matchlike",] for k in keys: config[k] = int(config.get(k,0)) return config