def test_update_yaml_dict(self): YAML_BEFORE = textwrap.dedent("""\ z_first: unrelated: dict a_second: key1: val1 # some comment key2: val2 """) YAML_EXPECTED = textwrap.dedent("""\ z_first: unrelated: dict a_second: key1: newval1 # some comment key2: val2 key3: val3 """) runner = CliRunner() with runner.isolated_filesystem(): with open('conf.yml', 'w') as f: f.write(YAML_BEFORE) with update_yaml_dict('conf.yml') as conf: conf['a_second']['key1'] = 'newval1' conf['a_second']['key3'] = 'val3' with open('conf.yml', 'r') as f: self.assertEqual(f.read(), YAML_EXPECTED)
def test_update_yaml_dict(self): YAML_BEFORE = textwrap.dedent("""\ a: unrelated: dict b: key1: val1 key2: val2 """) DICT_EXPECTED = { 'a': { 'unrelated': 'dict' }, 'b': { 'key1': 'newval1', 'key2': 'val2', 'key3': 'val3' } } runner = CliRunner() with runner.isolated_filesystem(): with open('conf.yml', 'w') as f: f.write(YAML_BEFORE) with update_yaml_dict('conf.yml') as conf: conf['b']['key1'] = 'newval1' conf['b']['key3'] = 'val3' with open('conf.yml', 'r') as f: self.assertEqual(yaml.safe_load(f), DICT_EXPECTED) f.seek(0) self.assertIn("key1: newval1", f.read())
def cli(): global_conf = load_shub_config(load_local=False, load_env=False) if 'default' not in global_conf.apikeys: click.echo("You are not logged in.") return 0 with update_yaml_dict() as conf: del conf['apikeys']['default']
def cli(): global_conf = load_shub_config(load_local=False, load_env=False) if 'default' in global_conf.apikeys: raise AlreadyLoggedInException conf = load_shub_config() key = _get_apikey( suggestion=conf.apikeys.get('default'), endpoint=global_conf.endpoints.get('default'), ) with update_yaml_dict() as conf: conf.setdefault('apikeys', {}) conf['apikeys']['default'] = key
def _deploy_wizard(conf, target='default'): """ Ask user for project ID, ensure they have access to that project, and save it to given ``target`` in local ``scrapinghub.yml`` if desired. """ closest_scrapycfg = closest_file('scrapy.cfg') # Double-checking to make deploy_wizard() independent of cli() if not closest_scrapycfg: raise NotFoundException("No Scrapy project found in this location.") closest_sh_yml = os.path.join(os.path.dirname(closest_scrapycfg), 'scrapinghub.yml') # Get default endpoint and API key (meanwhile making sure the user is # logged in) endpoint, apikey = conf.get_endpoint(0), conf.get_apikey(0) project = click.prompt("Target project ID", type=int) if not _has_project_access(project, endpoint, apikey): raise InvalidAuthException( "The account you logged in to has no access to project {}. Please " "double-check the project ID and make sure you logged in to the " "correct acount.".format(project), ) conf.projects[target] = project if click.confirm("Save as default", default=True): try: with update_yaml_dict(closest_sh_yml) as conf_yml: default_entry = {'default': project} if 'projects' in conf_yml: conf_yml['projects'].update(default_entry) else: conf_yml['projects'] = default_entry except Exception: click.echo( "There was an error while trying to write to scrapinghub.yml. " "Could not save project {} as default.".format(project), ) else: click.echo( "Project {} was set as default in scrapinghub.yml. You can " "deploy to it via 'shub deploy' from now on.".format(project), )
def test_update_yaml_dict(self): YAML_BEFORE = textwrap.dedent("""\ a: unrelated: dict b: key1: val1 key2: val2 """) DICT_EXPECTED = { 'a': {'unrelated': 'dict'}, 'b': {'key1': 'newval1', 'key2': 'val2', 'key3': 'val3'} } runner = CliRunner() with runner.isolated_filesystem(): with open('conf.yml', 'w') as f: f.write(YAML_BEFORE) with update_yaml_dict('conf.yml') as conf: conf['b']['key1'] = 'newval1' conf['b']['key3'] = 'val3' with open('conf.yml', 'r') as f: self.assertEqual(yaml.safe_load(f), DICT_EXPECTED) f.seek(0) self.assertIn("key1: newval1", f.read())