def test_config_file_detection_system(self):
        if sys.platform == 'win32':
            # TODO
            pass
        elif sys.platform == 'darwin':
            configpath = os.path.expanduser(
                "/Library/Application Support/dwave/dwave.conf")
        else:
            configpath = "/etc/xdg/dwave/dwave.conf"

        with mock.patch("os.path.exists", lambda path: path == configpath):
            self.assertEqual(detect_configfile_path(), configpath)
Esempio n. 2
0
def configure(config_file, profile):
    """Create and/or update cloud client configuration file."""

    # determine the config file path
    if config_file:
        print("Using config file:", config_file)
    else:
        # path not given, try to detect; or use default, but allow user to override
        config_file = detect_configfile_path()
        if config_file:
            print("Found existing config file:", config_file)
        else:
            config_file = get_default_configfile_path()
            print("Config file not found, the default location is:",
                  config_file)
        config_file = readline_input("Confirm config file path (editable): ",
                                     config_file)

    # try loading existing config, or use defaults
    try:
        config = load_config_from_file(config_file)
    except ValueError:
        config = get_default_config()

    # determine profile
    if profile:
        print("Using profile:", profile)
    else:
        existing = config.sections()
        if existing:
            profiles = 'create new or choose from: {}'.format(
                ', '.join(existing))
            default_profile = ''
        else:
            profiles = 'create new'
            default_profile = 'prod'
        while not profile:
            profile = readline_input("Profile (%s): " % profiles,
                                     default_profile)
            if not profile:
                print("Profile name can't be empty.")

    if not config.has_section(profile):
        config.add_section(profile)

    # fill out the profile variables
    variables = 'endpoint token client solver proxy'.split()
    prompts = [
        'API endpoint URL (editable): ', 'Auth token (editable): ',
        'Client class (qpu or sw): ', 'Solver (can be left blank): ',
        'Proxy URL (can be left blank): '
    ]
    for var, prompt in zip(variables, prompts):
        default_val = config.get(profile, var, fallback=None)
        val = readline_input(prompt, default_val)
        if val != default_val:
            config.set(profile, var, val)

    with open(config_file, 'w') as fp:
        config.write(fp)

    print("Config saved.")
    return 0
 def test_config_file_detection_nonexisting(self):
     with mock.patch("os.path.exists", lambda path: False):
         self.assertEqual(detect_configfile_path(), None)
 def test_config_file_detection_cwd(self):
     configpath = "./dwave.conf"
     with mock.patch("os.path.exists", lambda path: path == configpath):
         self.assertEqual(detect_configfile_path(), configpath)