|
@@ -14,12 +14,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
-import sys
|
|
|
import os
|
|
|
+import platform
|
|
|
+import sys
|
|
|
+from functools import lru_cache
|
|
|
+
|
|
|
|
|
|
if sys.version_info.major > 2:
|
|
|
from configparser import (ConfigParser, ParsingError, NoOptionError,
|
|
@@ -28,7 +31,60 @@ else: # pragma: no cover
|
|
|
from ConfigParser import (ConfigParser, ParsingError, NoOptionError,
|
|
|
NoSectionError, MissingSectionHeaderError)
|
|
|
|
|
|
-config_dir = os.path.expanduser("~/.pwman")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def _getenv(variable: str, default: str) -> str:
|
|
|
+ """Get an environment variable.
|
|
|
+ Parameters
|
|
|
+ ----------
|
|
|
+ variable : str
|
|
|
+ The environment variable.
|
|
|
+ default : str
|
|
|
+ A default value that will be returned if the environment
|
|
|
+ variable is unset or empty.
|
|
|
+ Returns
|
|
|
+ -------
|
|
|
+ str
|
|
|
+ The value of the environment variable, or the default value.
|
|
|
+ """
|
|
|
+ return os.environ.get(variable) or default
|
|
|
+
|
|
|
+
|
|
|
+XDG_CACHE_HOME = _getenv('XDG_CACHE_HOME',
|
|
|
+ os.path.expandvars(os.path.join('$HOME', '.cache')))
|
|
|
+XDG_CONFIG_DIRS = _getenv('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
|
|
|
+XDG_CONFIG_HOME = _getenv('XDG_CONFIG_HOME',
|
|
|
+ os.path.expandvars(os.path.join('$HOME', '.config')))
|
|
|
+XDG_DATA_DIRS = _getenv('XDG_DATA_DIRS',
|
|
|
+ '/usr/local/share/:/usr/share/').split(':')
|
|
|
+XDG_DATA_HOME = _getenv('XDG_DATA_HOME',
|
|
|
+ os.path.expandvars(
|
|
|
+ os.path.join('$HOME', '.local', 'share')))
|
|
|
+XDG_RUNTIME_DIR = os.getenv('XDG_RUNTIME_DIR')
|
|
|
+
|
|
|
+
|
|
|
+@lru_cache(maxsize=None)
|
|
|
+def find_config_dir(appname):
|
|
|
+ """
|
|
|
+ Backward compatibly config dir finder
|
|
|
+
|
|
|
+ If ~/.appname is not found define a new XDG compat one
|
|
|
+ """
|
|
|
+ config_dir = os.path.expanduser("~/.%s" % appname)
|
|
|
+
|
|
|
+ if os.path.exists(config_dir):
|
|
|
+ return config_dir
|
|
|
+ elif platform.system() == 'Windows':
|
|
|
+ return os.path.expandvars(os.path.join('$APPDATA', appname))
|
|
|
+ else:
|
|
|
+ return os.path.join(XDG_CONFIG_HOME, appname)
|
|
|
+
|
|
|
+
|
|
|
+config_dir = find_config_dir('pwman')
|
|
|
+
|
|
|
|
|
|
default_config = {'Global': {'umask': '0100', 'colors': 'yes',
|
|
|
'cls_timeout': '10', 'cp_timeout': '5',
|