예제 #1
0
파일: config.py 프로젝트: mcayland/remuco
class Config(object):
    """Class for getting and setting player adapter specific configurations.
    
    An instance of Config mirrors the configuration of a specific player
    adapter (usually ~/.config/remuco/PLAYER/conf).
    
    Player adapters are not supposed to create instances of Config. Instead
    use the 'config' attribute of a PlayerAdapter instance to access the
    currently used Config instance.
    
    """
    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, 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, e:
                log.warning("failed to read config %s (%s)" % (self.file, e))
예제 #2
0
파일: config.py 프로젝트: gkfabs/remuco
    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)
예제 #3
0
파일: config.py 프로젝트: cpatulea/remuco
     
     self.__dir_config = os.path.join(xdg_config, "remuco", name_plain)
     self.__dir_cache = os.path.join(xdg_cache, "remuco", name_plain)
     self.__file_config = os.path.join(self.__dir_config, "conf")
     self.__file_log = os.path.join(self.__dir_cache, "log")
 
     try:
         if not os.path.isdir(self.__dir_config):
             os.makedirs(self.__dir_config)
         if not os.path.isdir(self.__dir_cache):
             os.makedirs(self.__dir_cache)
     except OSError, e:
         log.error("failed to make dir: %s", e)
     else:
         if not "--remuco-log-stdout" in sys.argv:
             log.set_file(self.__file_log)
     
     ###### custom volume command ######
     
     cmd = os.path.join(xdg_config, "remuco", "volume")
     if not os.path.isfile(cmd):
         cmd = os.path.join(self.__dir_config, "volume")
     if not os.path.isfile(cmd):
         log.debug("custom volume command does not exist (%s)" % cmd)
         self.__custom_volume_cmd = None
     elif not os.access(cmd, os.X_OK):
         log.warning("custom volume command (%s) is not executable" % cmd)
         self.__custom_volume_cmd = None
     else:
         log.info("using custom volume command (%s)" % cmd)
         self.__custom_volume_cmd = cmd