From d2e965fe54a15b78aed86bd228927e0502c39767 Mon Sep 17 00:00:00 2001 From: Alberto Sottile Date: Sat, 9 Jun 2018 15:29:08 +0200 Subject: [PATCH] Change config path logic to support XDG --- syncplay/constants.py | 3 +-- syncplay/ui/ConfigurationGetter.py | 36 ++++++++++++++++++------------ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/syncplay/constants.py b/syncplay/constants.py index 00ec2cf..4a60c5e 100755 --- a/syncplay/constants.py +++ b/syncplay/constants.py @@ -8,8 +8,7 @@ MPC_OSD_POSITION = 1 #Right corner, 1 for left MPLAYER_OSD_LEVEL = 1 UI_TIME_FORMAT = "[%X] " CONFIG_NAMES = [".syncplay", "syncplay.ini"] #Syncplay searches first to last -DEFAULT_CONFIG_NAME_WINDOWS = "syncplay.ini" -DEFAULT_CONFIG_NAME_LINUX = ".syncplay" +DEFAULT_CONFIG_NAME = "syncplay.ini" RECENT_CLIENT_THRESHOLD = "1.5.3" #This and higher considered 'recent' clients (no warnings) WARN_OLD_CLIENTS = True #Use MOTD to inform old clients to upgrade LIST_RELATIVE_CONFIGS = True # Print list of relative configs loaded diff --git a/syncplay/ui/ConfigurationGetter.py b/syncplay/ui/ConfigurationGetter.py index 7d1e33b..a4fa113 100755 --- a/syncplay/ui/ConfigurationGetter.py +++ b/syncplay/ui/ConfigurationGetter.py @@ -322,21 +322,29 @@ class ConfigurationGetter(object): def _getConfigurationFilePath(self): configFile = self._checkForPortableFile() - if not configFile: - for name in constants.CONFIG_NAMES: - if configFile and os.path.isfile(configFile): - break - if os.name != 'nt': - configFile = os.path.join(os.getenv('HOME', '.'), name) - else: - configFile = os.path.join(os.getenv('APPDATA', '.'), name) - if configFile and not os.path.isfile(configFile): - if os.name != 'nt': - configFile = os.path.join(os.getenv('HOME', '.'), constants.DEFAULT_CONFIG_NAME_LINUX) - else: - configFile = os.path.join(os.getenv('APPDATA', '.'), constants.DEFAULT_CONFIG_NAME_WINDOWS) + if configFile: + return configFile + for name in constants.CONFIG_NAMES: + configFile = self._expandConfigPath(name, xdg = False) + if os.path.isfile(configFile): + return configFile + return self._expandConfigPath() - return configFile + def _expandConfigPath(self, name = None, xdg = True): + if os.name != 'nt': + if xdg: + prefix = self._getXdgConfigHome() + else: + prefix = os.getenv('HOME', '.') + else: + prefix = os.getenv('APPDATA', '.') + return os.path.join(prefix, name or constants.DEFAULT_CONFIG_NAME) + + def _getXdgConfigHome(self): + path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) + if not os.path.isdir(path): + os.mkdir(path, 0o755) + return path def _parseConfigFile(self, iniPath, createConfig=True): parser = SafeConfigParserUnicode()