def test_update_yaml_dict_handles_file_errors(self): with CliRunner().isolated_filesystem(): self.assertFalse(os.path.isfile('didnt_exist.yml')) with utils.update_yaml_dict('didnt_exist.yml') as conf: pass self.assertTrue(os.path.isfile('didnt_exist.yml')) os.mkdir('a_directory') with self.assertRaises(IOError): with utils.update_yaml_dict('a_directory'): pass
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 utils.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(GLOBAL_SCRAPINGHUB_YML_PATH) 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(GLOBAL_SCRAPINGHUB_YML_PATH) as conf: conf.setdefault('apikeys', {}) conf['apikeys']['default'] = key
def save(self, path=None, options=None): def _project_id_as_int(project): """Copy project and return it with the ID casted to int to make sure it is exported as "123" and not "'123'" """ try: if isinstance(project, dict): project = project.copy() project['id'] = int(project['id']) else: project = int(project) except ValueError: # Happens when project ID contains endpoint (e.g. vagrant/123), # in that case it will be exported without quotation marks # anyway pass return project with update_yaml_dict(path) as yml: options = options or list(self.SHORTCUTS) for option in options: shortcut = self.SHORTCUTS[option] conf = getattr(self, option) if option == 'endpoints' and conf == ShubConfig().endpoints: # Don't write default endpoint continue elif option == 'projects': conf = {k: _project_id_as_int(v) for k, v in conf.items()} if list(conf.keys()) == ['default']: yml[shortcut] = conf['default'] yml.pop(option, None) else: yml[option] = conf yml.pop(shortcut, None) if self.version != 'AUTO': yml['version'] = self.version if self.eggs: yml.setdefault('requirements', {})['eggs'] = self.eggs if self.requirements_file: yml.setdefault('requirements', {})['file'] = (self.requirements_file)
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 utils.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 call_update_yaml_dict(): with utils.update_yaml_dict(): pass