def detect_configfile_path(): """Returns the first existing file that finds in a list of possible candidates, and `None` if the list was exhausted, but no candidate config file exists. For details, see :func:`load_config_from_file`. """ app = "dwave" author = "dwavesystem" filename = "dwave.conf" # look for `./dwave.conf` candidates = ["."] # then for something like `~/.config/dwave/dwave.conf` candidates.append( homebase.user_config_dir(app_author=author, app_name=app, roaming=False, use_virtualenv=False, create=False)) # and finally for e.g. `/etc/dwave/dwave.conf` candidates.extend( homebase.site_config_dir_list(app_author=author, app_name=app, use_virtualenv=False, create=False)) for base in candidates: path = os.path.join(base, filename) if os.path.exists(path): return path return None
def get_default_configfile_path(): """Return the default configuration-file path. Typically returns a user-local configuration file; e.g: ``~/.config/dwave/dwave.conf``. Returns: str: Configuration file path. Examples: This example displays the default configuration file on an Ubuntu Linux system. >>> from dwave.cloud import config >>> # Display paths >>> config.get_configfile_paths(only_existing=False) # doctest: +SKIP ['/etc/xdg/xdg-ubuntu/dwave/dwave.conf', '/usr/share/upstart/xdg/dwave/dwave.conf', '/etc/xdg/dwave/dwave.conf', '/home/mary/.config/dwave/dwave.conf', './dwave.conf'] >>> # Find default configuration path >>> config.get_default_configfile_path() # doctest: +SKIP '/home/mary/.config/dwave/dwave.conf' """ base = homebase.user_config_dir(app_author=CONF_AUTHOR, app_name=CONF_APP, roaming=False, use_virtualenv=False, create=False) path = os.path.join(base, CONF_FILENAME) return path
def test_user_config_roaming(self): expected = os.path.join(self.roaming_base_path, self.app_author, self.app_name) self.assertEqual( expected, homebase.user_config_dir(self.app_name, self.app_author, roaming=True))
def get_configfile_paths(system=True, user=True, local=True, only_existing=True): """Returns a list of (existing) config files found on disk. Candidates examined depend on the OS, but for Linux possible list is: ``dwave.conf`` in CWD, user-local ``.config/dwave/``, system-wide ``/etc/dwave/``. For details, see :func:`load_config_from_files`. Args: system (boolean, default=True): Search for system-wide config files. user (boolean, default=True): Search for user-local config files. local (boolean, default=True): Search for local config files (in CWD). only_existing (boolean, default=True): Return only paths for files that exist on disk. Returns: list[str]: A list of config file paths. """ candidates = [] # system-wide has the lowest priority, `/etc/dwave/dwave.conf` if system: candidates.extend( homebase.site_config_dir_list(app_author=CONF_AUTHOR, app_name=CONF_APP, use_virtualenv=False, create=False)) # user-local will override it, `~/.config/dwave/dwave.conf` if user: candidates.append( homebase.user_config_dir(app_author=CONF_AUTHOR, app_name=CONF_APP, roaming=False, use_virtualenv=False, create=False)) # highest priority (overrides all): `./dwave.conf` if local: candidates.append(".") paths = [os.path.join(base, CONF_FILENAME) for base in candidates] if only_existing: paths = list(filter(os.path.exists, paths)) return paths
def get_default_configfile_path(): """Returns the preferred config file path: a user-local config, e.g: ``~/.config/dwave/dwave.conf``.""" base = homebase.user_config_dir(app_author=CONF_AUTHOR, app_name=CONF_APP, roaming=False, use_virtualenv=False, create=False) path = os.path.join(base, CONF_FILENAME) return path
def test_user_config_no_version_venv_no_create(self): expected = os.path.join( self.base_paths[self.platform]['user_config_venv'], self.app_name) self.assertEqual( expected, homebase.user_config_dir(self.app_name, app_author=self.app_author, version=None, use_virtualenv=True, create=False))
def test_user_config_version_venv_no_create(self): version = '1.0' expected = os.path.join( self.base_paths[self.platform]['user_config_venv'], '{}_{}'.format(self.app_name, version)) result = homebase.user_config_dir(self.app_name, app_author=self.app_author, version=version, use_virtualenv=True, create=False) self.assertEqual(expected, result)
def test_user_config_version_no_venv_create(self): version = "1.0" expected = os.path.join(self.base_paths[self.platform]['user_config'], '{}_{}'.format(self.app_name, version)) if os.path.exists(expected): shutil.rmtree(expected) result = homebase.user_config_dir(self.app_name, app_author=self.app_author, version=version, use_virtualenv=False, create=True) self.assertEqual(expected, result) self.assertTrue(os.path.exists(expected)) shutil.rmtree(expected)
def test_user_config_no_version_venv_create(self): expected = os.path.join( self.base_paths[self.platform]['user_config_venv'], self.app_name) if os.path.exists(expected): shutil.rmtree(expected) self.assertEqual( expected, homebase.user_config_dir(self.app_name, app_author=self.app_author, version=None, use_virtualenv=True, create=True)) self.assertTrue(os.path.exists(expected)) shutil.rmtree(expected)
def get_configfile_paths(system=True, user=True, local=True, only_existing=True): """Return a list of local configuration file paths. Search paths for configuration files on the local system are based on homebase_ and depend on operating system; for example, for Linux systems these might include ``dwave.conf`` in the current working directory (CWD), user-local ``.config/dwave/``, and system-wide ``/etc/dwave/``. .. _homebase: https://github.com/dwavesystems/homebase Args: system (boolean, default=True): Search for system-wide configuration files. user (boolean, default=True): Search for user-local configuration files. local (boolean, default=True): Search for local configuration files (in CWD). only_existing (boolean, default=True): Return only paths for files that exist on the local system. Returns: list[str]: List of configuration file paths. Examples: This example displays all paths to configuration files on a Windows system running Python 2.7 and then finds the single existing configuration file. >>> from dwave.cloud.config import get_configfile_paths >>> # Display paths >>> get_configfile_paths(only_existing=False) # doctest: +SKIP ['C:\\ProgramData\\dwavesystem\\dwave\\dwave.conf', 'C:\\Users\\jane\\AppData\\Local\\dwavesystem\\dwave\\dwave.conf', '.\\dwave.conf'] >>> # Find existing files >>> get_configfile_paths() # doctest: +SKIP ['C:\\Users\\jane\\AppData\\Local\\dwavesystem\\dwave\\dwave.conf'] """ candidates = [] # system-wide has the lowest priority, `/etc/dwave/dwave.conf` if system: candidates.extend( homebase.site_config_dir_list(app_author=CONF_AUTHOR, app_name=CONF_APP, use_virtualenv=False, create=False)) # user-local will override it, `~/.config/dwave/dwave.conf` if user: candidates.append( homebase.user_config_dir(app_author=CONF_AUTHOR, app_name=CONF_APP, roaming=False, use_virtualenv=False, create=False)) # highest priority (overrides all): `./dwave.conf` if local: candidates.append(".") paths = [os.path.join(base, CONF_FILENAME) for base in candidates] if only_existing: paths = list(filter(os.path.exists, paths)) return paths
def test_user_config_no_app_author(self): with self.assertRaises(RuntimeError): homebase.user_config_dir(self.app_name, app_author=None)