def edit(ctx, key, field_value, destination, no_copy, **kwargs): """ Edit an entry. Use FIELD_VALUE to set fields as follows: ``author=Einstein``, or ``tags=interesting``. Leave the value empty to open in editor. Set the key or type in the same way. Don't forget to set the EDITOR environment variable for this command to work properly. """ file_ = kwargs.pop("file") data = ctx.obj["data"] entry = query.get_by_key(data, key) if file_: internals.set_file(data, entry, file_, destination, no_copy) for fv in field_value: if "=" in fv: field, value = fv.split("=") else: field = fv current_value = entry["fields"].get(field, "") value = internals.editor(text=current_value).strip() if field == "key": internals.unique_key_validation(value, data) entry["key"] = value elif field == "type": entry["type"] = value else: entry["fields"][field] = value pybibs.write_file(data, ctx.obj["database"])
def add(ctx, destination, doi, no_copy, **kwargs): """ Add a new entry to the database. Find a bib entry you would like to add. Copy it to the clipboard, and run the command. It will be opened in your editor for validation or manual editing. Upon saving, the entry is added to the database. Don't forget to set the EDITOR environment variable for this command to work properly. """ file_ = kwargs.pop("file") data = ctx.obj["data"] if doi is not None: url = "http://dx.doi.org/{}".format(doi) headers = {"Accept": "application/x-bibtex"} resp = requests.get(url, headers=headers) assert resp.status_code == 200 raw_bib = resp.text else: raw_bib = pyperclip.paste() bib = internals.editor(text=raw_bib) entry = pybibs.read_entry_string(bib) internals.unique_key_validation(entry["key"], data) data.append(entry) if file_: internals.set_file(data, entry, file_, destination, no_copy) pybibs.write_file(data, ctx.obj["database"])
def todo(ctx, key): data = ctx.obj['data'] entry = bibo.query.get_by_key(data, key) curr_note = entry['fields'].get('todo', '') try: upd_note = click.edit(text=curr_note).strip() except AttributeError: click.echo('Error updating TODO note, did you leave it blank?') return if upd_note == curr_note: click.echo('No changes to TODO note: "{}"'.format(upd_note)) return elif upd_note: entry['fields']['todo'] = upd_note click.echo('Added/Modified TODO note: "{}"'.format(upd_note)) else: del entry['fields']['todo'] click.echo('Removed TODO note: "{}"'.format(curr_note)) pybibs.write_file(data, ctx.obj['database'])
def remove(ctx, key, field): """ Remove an entry from the database or remove a field from an entry. To remove an entry specify its key. To fields specify the key and list all fields for removal. """ data = ctx.obj["data"] entry = query.get_by_key(data, key) if not field: data.remove(entry) elif "fields" in entry: for f in field: if f in entry["fields"]: del entry["fields"][f] else: click.echo('"{}" has no field "{}"'.format(key, f)) else: click.echo('"{}" has no fields'.format(key)) pybibs.write_file(data, ctx.obj["database"])