Пример #1
0
    def __init__(self, player_name):
        """Create a new instance for the given player (adapter)."""

        super(Config, self).__init__()
        
        # convert descriptive name to a plain canonical one
        self.player = re.sub(r'[^\w-]', '', player_name).lower()
        
        # paths
        self.dir = join(user_config_dir, "remuco")
        self.cache = join(user_cache_dir, "remuco")
        self.file = join(self.dir, "remuco.cfg")

        # remove old stuff
        self.__cleanup()

        # create directories
        for dname in (self.dir, self.cache):
            try:
                if not isdir(dname):
                    os.makedirs(dname)
            except OSError as e:
                log.error("failed to make dir: %s", e)
        if not "REMUCO_LOG_STDOUT" in os.environ and isdir(self.cache):
            log.set_file(join(self.cache, "%s.log" % self.player))

        # load
        cp = configparser.RawConfigParser(_DEFAULTS, _odict)
        if not cp.has_section(self.player):
            cp.add_section(self.player)
        if exists(self.file):
            try:
                cp.read(self.file)
            except configparser.Error as e:
                log.warning("failed to read config %s (%s)" % (self.file, e))

        # reset on version change
        if cp.get(configparser.DEFAULTSECT, "config-version") != _CONFIG_VERSION:
            sections = cp.sections() # keep already existing player sections
            cp = configparser.RawConfigParser(_DEFAULTS, _odict)
            for sec in sections:
                cp.add_section(sec)
            if exists(self.file):
                bak = "%s.%s.backup" % (self.file, _TS)
                log.info("reset config (major changes, backup: %s)" % bak)
                shutil.copy(self.file, bak)
            
        # remove unknown options in all sections
        for sec in cp.sections() + [configparser.DEFAULTSECT]:
            for key, value in cp.items(sec):
                if key not in _DEFAULTS and not key.startswith("x-"):
                    cp.remove_option(sec, key)
                    
        # add not yet existing options to default section
        for key, value in _DEFAULTS.items():
            if not cp.has_option(configparser.DEFAULTSECT, key):
                cp.set(configparser.DEFAULTSECT, key, value)
        
        # update version
        cp.set(configparser.DEFAULTSECT, "config-version", _CONFIG_VERSION)

        self.__cp = cp
        
        # save to always have a clean file
        self.__save()

        log.set_level(self.log_level)
        
        log.info("remuco version: %s" % defs.REMUCO_VERSION)
Пример #2
0
                    cp.remove_option(sec, key)
                    
        # add not yet existing options to default section
        for key, value in _DEFAULTS.items():
            if not cp.has_option(ConfigParser.DEFAULTSECT, key):
                cp.set(ConfigParser.DEFAULTSECT, key, value)
        
        # update version
        cp.set(ConfigParser.DEFAULTSECT, "config-version", _CONFIG_VERSION)

        self.__cp = cp
        
        # save to always have a clean file
        self.__save()

        log.set_level(self.log_level)
        
        log.info("remuco version: %s" % defs.REMUCO_VERSION)
        
    def __getattribute__(self, attr):
        """Attribute-style access to standard options."""
        
        try:
            return super(Config, self).__getattribute__(attr)
        except AttributeError, e:
            _attr = attr.replace("_", "-")
            if _attr in _OPTIONS:
                attr = _attr
            elif attr not in _OPTIONS:
                raise e
            value = self.__cp.get(self.player, attr)
Пример #3
0
                    cp.remove_option(sec, key)

        # add not yet existing options to default section
        for key, value in _DEFAULTS.items():
            if not cp.has_option(ConfigParser.DEFAULTSECT, key):
                cp.set(ConfigParser.DEFAULTSECT, key, value)

        # update version
        cp.set(ConfigParser.DEFAULTSECT, "config-version", _CONFIG_VERSION)

        self.__cp = cp

        # save to always have a clean file
        self.__save()

        log.set_level(self.log_level)

        log.info("remuco version: %s" % defs.REMUCO_VERSION)

    def __getattribute__(self, attr):
        """Attribute-style access to standard options."""

        try:
            return super(Config, self).__getattribute__(attr)
        except AttributeError, e:
            _attr = attr.replace("_", "-")
            if _attr in _OPTIONS:
                attr = _attr
            elif attr not in _OPTIONS:
                raise e
            value = self.__cp.get(self.player, attr)