def isposfloat(self, what): self.hasvalue(what) try: res = float(self[what]) if res <= 0: raise derrors.CfgErr( "for setting %s: value is zero or negative: %f" % (what, res)) self[what] = res except ValueError: raise derrors.CfgErr("for setting %s: value is not a float: '%s'" % (what, self[what]))
def load(fname, cfg=None): try: fp = contread.openfile(fname) res = _loadfile(fp, cfg) fp.close() return res except EnvironmentError as e: raise derrors.IOErr("could not read config file %s: %s" % (fname, str(e))) except contread.StartingContinuedLine as e: raise derrors.CfgErr("configuration file %s: %s" % (fname, str(e))) except derrors.CfgErr as e: raise derrors.CfgErr("configuration file %s: %s" % (fname, str(e)))
def _loadfile(fp, cfg=None): if cfg is None: cfg = Configuration() for line in fp: # (this removes any trailing newline) line = line.strip() tl = line.split(None, 1) if len(tl) == 1: tl.append(True) else: tl[1] = tl[1].strip() if tl[0] in cfg.wordlist_items: cfg.addWordListItem(tl[0], tl[1]) elif tl[0] in cfg.list_items: cfg.addListItem(tl[0], tl[1]) elif tl[0] in cfg.ip_ranges: cfg.addIpRanges(tl[0], tl[1]) elif tl[0] in cfg: raise derrors.CfgErr("setting %s supplied more than once" % tl[0]) else: cfg[tl[0]] = tl[1] cfg.checkGoodConfig() return cfg
def loadAuthSeed(self): fname = self['global-authseed-file'] try: fp = open(fname, 'rb') self['global-authseed'] = fp.read() fp.close() except EnvironmentError as e: raise derrors.CfgErr("cannot read global-authseed-file '%s': %s" % (fname, str(e)))
def isIntOrTimestr(self, what): self.hasvalue(what) try: res = int(self[what]) if res < 0: raise derrors.CfgErr("for setting %s: value is negative: %d" % (what, res)) self[what] = res return except ValueError: pass for tf in timeFmts: try: r = time.strptime(self[what], tf) self[what] = time.mktime(r) return except ValueError: pass raise derrors.CfgErr( "for setting %s: value is neither an integer not a recognizable timestamp: '%s'" % (what, self[what]))
def isabsfile(self, what): self.isabspath(what) if not os.path.isfile(self[what]): raise derrors.CfgErr("for setting %s: no file '%s'" % (what, self[what]))
def isabsdir(self, what): self.isabspath(what) if not os.path.isdir(self[what]): raise derrors.CfgErr("for setting %s: no directory '%s'" % (what, self[what]))
def isabspath(self, what): self.hasvalue(what) if self[what][0] != '/': raise derrors.CfgErr( "for setting %s: path '%s' is not an absolute path" % (what, self[what]))
def hasvalue(self, what): if what not in self: raise derrors.CfgErr("setting %s is not specified" % what) if self[what] is True: raise derrors.CfgErr("for setting %s: no value specified" % what)
def mustDiffer(self, one, two): if self[one] == self[two]: raise derrors.CfgErr("for setting %s and %s: same value '%s'" % (one, two, self[one]))