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 ls(pattern=None): """Show all items that match `pattern`.""" items = item.load() if items: if pattern is not None: # Search for keys that match pattern items = search(items, pattern) if len(items) == 0: return max_len = max([len(key) for key in items]) def format_name(name): return Fore.YELLOW + name + Fore.RESET def spacing(name): return (max_len - len(name)) + KEY_VALUE_SPACING lines = [ '{}{}{}'.format(format_name(name), ' ' * spacing(name), format_value(value)) for name, value in items.items() ] lines.sort() click.echo('\n'.join(lines)) else: click.echo(Style.DIM + 'No items' + Style.NORMAL)
def get(name): """Print the value of an item.""" items = item.load() if not name in items: echo_error('No such item: {}'.format(name)) return click.echo(items[name])
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)
def test_load_with_one_blob_returns_dictionary_with_blob_contents(tmp_path): contents = b'Hello' with open(tmp_path / 'items.json', 'w') as f: json.dump({'foo': None}, f) blob_path = tmp_path / 'blobs' blob_path.mkdir() with open(blob_path / 'foo', 'wb') as f: f.write(contents) assert load() == {'foo': contents}
def autocomplete_name(ctx, args, incomplete): return [name for name in item.load().keys() if name.startswith(incomplete)]
def test_load_with_one_item_returns_dictionary_with_one_item(tmp_path): with open(tmp_path / 'items.json', 'w') as f: json.dump({'foo': 'bar'}, f) assert load() == {'foo': 'bar'}
def test_load_with_no_items_returns_empty_dictionary(tmp_path): with open(tmp_path / 'items.json', 'w') as f: json.dump({}, f) assert load() == {}