Exemple #1
0
    def register(self, name, vtype, descr, default=None, validator=None):
        cfgname = get_config_name()
        c = new_config()
        value = None
        if len(c.read(cfgname)) != 0:
            value = get_option(c, CONFIG_SECTION, name, None, None)
        if vtype == str:
            pass
        elif vtype == bool:
            if value != None:
                value = value.upper()
            if value == '0' or value == 'NO' or value == 'FALSE':
                value = False
            elif value == '1' or value == 'YES' or value == 'TRUE':
                value = True
            else:
                value = None
        elif vtype == int:
            if value != None:
                value = str_to_int(value, None)

        if value == None:
            value = default

        s = Setting(name, vtype, descr, value, validator)
        self.settings.append(s)

        for cb in self.new_setting_cb:
            cb(s)
        return s
Exemple #2
0
    def read_config_section(self, c):
        if c.has_section(self.metasection) == False:
            return False
        self.reset_defaults(trusted=False)

        version = self.d['v']

        for (name, ma) in self.metaattributes.items():
            if ma.required and get_option(self.metasection, name) == None:
                warning('Missing required config attribute: %s\n' % name)
                return False

        for (name, value) in c.items(self.metasection):
            ma = self.metaattributes.get(name)
            if ma == None:
                continue
            if ma.public == False:
                warning('Ignoring %s:%s in %s\n' %(name, value, self.metasection))
                continue

            if ma.vtype == str:
                pass
            elif ma.vtype == bool:
                value = value.upper()
                if value == '0' or value == 'NO' or value == 'FALSE':
                    value = False
                elif value == '1' or value == 'YES' or value == 'TRUE':
                    value = True
                else:
                    continue
            elif ma.vtype == int:
                value = str_to_int(value, None)
                if value == None:
                    continue
            else:
                warning('Unknown vtype for %s in %s (type %s)\n' %(name, self.metasection, str(ma.vtype)))
                continue

            if self.set(name, value):
                if name == 'v':
                    version = value

        self.d['v'] = version
        return True