def set(name, edit, value): """Bind a name to a value.""" items = item.load() original_items = dict(items) old = items[name] if name in items else None if edit: if value is not None: echo_error('Value cannot be present while --editor is set.') return edited = click.edit(text=old) # click.edit() returns None when no changes were made, so account for that value = edited.rstrip() if edited is not None else old else: if value is None: value = sys.stdin.buffer.read() # Modify items items[name] = value item.save(items, original_items) # Output result if old is None: click.echo("Added {} → {}".format(Fore.YELLOW + name + Fore.RESET, format_value(value))) else: if value == old: click.echo(Style.DIM + 'No changes made' + Style.NORMAL) else: click.echo("Updated {} → {}".format( Fore.YELLOW + name + Fore.RESET, format_value(value)))
def test_save_with_new_blob_writes_blob_to_file(tmp_path): original_items = {} items = {'foo': b'Hello'} save(items, original_items) with open(tmp_path / 'blobs' / 'foo', 'rb') as f: blob = f.read() assert blob == b'Hello'
def test_save_remove_blob_removes_file(tmp_path): # Add blob (which creates the file for the blob) test_items = {'foo': b''} save(items=test_items, original_items={}) save(items={}, original_items=test_items) blob_path = tmp_path / 'blobs' blobs = list(blob_path.iterdir()) assert len(blobs) == 1 and blobs[0].name == 'foo'
def test_save_with_new_item_writes_new_item(tmp_path): # Add foo: bar original_items = {} items = {'foo': 'bar'} save(items, original_items) with open(tmp_path / 'items.json', 'r') as f: written = json.load(f) assert written == items
def remove(pattern): """Remove an item.""" items = item.load() to_remove = search(item.load(), pattern) count = len(to_remove) if count == 0: echo_error('No items found') return new_items = { name: value for name, value in items.items() if not name in to_remove } item.save(new_items, items) click.echo('Removed {} item{}'.format(Fore.GREEN + str(count) + Fore.RESET, '' if count == 1 else 's') + Style.RESET_ALL)