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_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 test_site_config_list_version_venv_no_create(self): version = '1.0' expected = [ os.path.join(d, '{}_{}'.format(self.app_name, version)) for d in self.base_paths[self.platform]['site_config'] ] result = homebase.site_config_dir_list(self.app_name, app_author=self.app_author, version=version, use_virtualenv=True, create=False) self.assertEqual(expected, result)
def test_site_config_list_no_version_venv_no_create(self): expected = [ os.path.join(d, self.app_name) for d in self.base_paths[self.platform]['site_config'] ] self.assertEqual( expected, homebase.site_config_dir_list(self.app_name, app_author=self.app_author, version=None, use_virtualenv=True, create=False))
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