codekingpro/portable-devtools
114k
1import os2import platform3import pathlib4 5 6def _settings_root_XP():7 return os.path.join(os.environ['USERPROFILE'], 'Local Settings')8 9 10def _settings_root_Vista():11 return os.environ.get('LOCALAPPDATA', os.environ.get('ProgramData', '.'))12 13 14def _data_root_Windows():15 release, version, csd, ptype = platform.win32_ver()16 root = _settings_root_XP() if release == 'XP' else _settings_root_Vista()17 return pathlib.Path(root, 'Python Keyring')18 19 20def _data_root_Linux():21 """22 Use freedesktop.org Base Dir Specification to determine storage23 location.24 """25 fallback = pathlib.Path.home() / '.local/share'26 root = os.environ.get('XDG_DATA_HOME', None) or fallback27 return pathlib.Path(root, 'python_keyring')28 29 30_config_root_Windows = _data_root_Windows31 32 33def _check_old_config_root():34 """35 Prior versions of keyring would search for the config36 in XDG_DATA_HOME, but should probably have been37 searching for config in XDG_CONFIG_HOME. If the38 config exists in the former but not in the latter,39 raise a RuntimeError to force the change.40 """41 # disable the check - once is enough and avoids infinite loop42 globals()['_check_old_config_root'] = lambda: None43 config_file_new = os.path.join(_config_root_Linux(), 'keyringrc.cfg')44 config_file_old = os.path.join(_data_root_Linux(), 'keyringrc.cfg')45 if os.path.isfile(config_file_old) and not os.path.isfile(config_file_new):46 msg = (47 "Keyring config exists only in the old location "48 f"{config_file_old} and should be moved to {config_file_new} "49 "to work with this version of keyring."50 )51 raise RuntimeError(msg)52 53 54def _config_root_Linux():55 """56 Use freedesktop.org Base Dir Specification to determine config57 location.58 """59 _check_old_config_root()60 fallback = pathlib.Path.home() / '.config'61 key = 'XDG_CONFIG_HOME'62 root = os.environ.get(key, None) or fallback63 return pathlib.Path(root, 'python_keyring')64 65 66# by default, use Unix convention67data_root = globals().get('_data_root_' + platform.system(), _data_root_Linux)68config_root = globals().get('_config_root_' + platform.system(), _config_root_Linux)69 