示例#1
0
def _load_config(config_file: str) -> ConfigParser:
    """Load from config_file, or create new with defaults."""

    try:
        return load_config_from_files([config_file])
    except:
        return get_default_config()
示例#2
0
 def test_config_load_from_file(self):
     with mock.patch('dwave.cloud.config.open',
                     iterable_mock_open(self.config_body),
                     create=True):
         config = load_config_from_files(filenames=["filename"])
         self.assertEqual(config.sections(),
                          ['dw2000', 'software', 'alpha'])
         self.assertEqual(config['dw2000']['client'], 'qpu')
         self.assertEqual(config['software']['client'], 'sw')
示例#3
0
 def test_no_config_detected(self):
     """When no config file detected, `load_config_from_files` should return
     empty config."""
     with mock.patch("dwave.cloud.config.get_configfile_paths", lambda: []):
         self.assertFalse(load_config_from_files().sections())
示例#4
0
def create(config_file, profile):
    """Create and/or update cloud client configuration file."""

    # determine the config file path
    if config_file:
        click.echo("Using configuration file: {}".format(config_file))
    else:
        # path not given, try to detect; or use default, but allow user to override
        config_file = get_configfile_path()
        if config_file:
            click.echo(
                "Found existing configuration file: {}".format(config_file))
        else:
            config_file = get_default_configfile_path()
            click.echo(
                "Configuration file not found; the default location is: {}".
                format(config_file))
        config_file = default_text_input("Configuration file path",
                                         config_file)
        config_file = os.path.expanduser(config_file)

    # create config_file path
    config_base = os.path.dirname(config_file)
    if config_base and not os.path.exists(config_base):
        if click.confirm("Configuration file path does not exist. Create it?",
                         abort=True):
            try:
                os.makedirs(config_base)
            except Exception as e:
                click.echo("Error creating configuration path: {}".format(e))
                return 1

    # try loading existing config, or use defaults
    try:
        config = load_config_from_files([config_file])
    except:
        config = get_default_config()

    # determine profile
    if profile:
        click.echo("Using profile: {}".format(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'
        profile = default_text_input("Profile (%s)" % profiles,
                                     default_profile,
                                     optional=False)

    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', 'Authentication token',
        'Default client class (qpu or sw)', 'Default solver'
    ]
    for var, prompt in zip(variables, prompts):
        default_val = config.get(profile, var, fallback=None)
        val = default_text_input(prompt, default_val)
        if val:
            val = os.path.expandvars(val)
        if val != default_val:
            config.set(profile, var, val)

    try:
        with open(config_file, 'w') as fp:
            config.write(fp)
    except Exception as e:
        click.echo("Error writing to configuration file: {}".format(e))
        return 2

    click.echo("Configuration saved.")
    return 0