Skip to content

Commit

Permalink
Merge pull request #61 from Kappers/fileautocomplete
Browse files Browse the repository at this point in the history
internals: add path completion w tests
  • Loading branch information
Nagasaki45 authored Mar 29, 2020
2 parents 2592dba + 851929b commit bd761c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bibo/bibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
'--file',
help='File to link to this entry.',
type=click.Path(exists=True, readable=True, dir_okay=False),
autocompletion=internals.complete_path,
),
click.option(
'--destination',
help='A folder to put the file in.',
type=click.Path(exists=True, readable=True, dir_okay=True, file_okay=False),
autocompletion=internals.complete_path,
),
click.option(
'--no-copy',
Expand Down Expand Up @@ -56,7 +58,8 @@
@click.group(help=__doc__)
@click.version_option()
@click.option('--database', envvar=internals.BIBO_DATABASE_ENV_VAR,
help='A .bib file.', required=True, type=PATH_OPTION)
help='A .bib file.', required=True, type=PATH_OPTION,
autocompletion=internals.complete_path)
@click.pass_context
def cli(ctx, database):
ctx.ensure_object(dict)
Expand Down
14 changes: 14 additions & 0 deletions bibo/internals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8

import collections
import glob
import itertools
import os
import re
Expand Down Expand Up @@ -177,3 +178,16 @@ def bib_entries(entries):
def unique_key_validation(new_key, data):
if new_key in (e['key'] for e in bib_entries(data)):
raise click.ClickException('Duplicate key, command aborted')


def complete_path(ctx, args, incomplete):
'''
Autocompletion for files, matches the glob of incomplete argument, and
provide basename prompts.
'''
wildc_path = os.path.expanduser(os.path.expandvars(incomplete)) + '*'
options = []
for path in glob.glob(wildc_path):
if os.access(path, os.R_OK):
options.append((path, os.path.basename(path)))
return options
27 changes: 27 additions & 0 deletions tests/bibo/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,30 @@ def test_format_entry(data):
entry = data[0]
assert internals.format_entry(entry, '$year') == '1937'
assert internals.format_entry(entry, '$year: $title') == '1937: The Hobbit'

def test_complete_path(tmpdir):
# Create dummy files and a sub-dir with file
ps = []
for i in range(10):
name = 'even' if i % 2 else 'odd'
p = tmpdir / "journal{}_{}.txt".format(name, i)
p.write(i)
ps.append(p)
n_top_level_journals = len(ps)
subp = tmpdir.mkdir("subdir").join("subjournal.txt")
subp.write('findme')
assert len(tmpdir.listdir()) == 1 + n_top_level_journals

incomplete = lambda paths: os.path.join(tmpdir.strpath, *paths)

# Check all matches incl subdir
matches = internals.complete_path(None, None, incomplete(['']))
assert len(matches) == 1 + n_top_level_journals
# Check match pattern
ms_even = internals.complete_path(None, None, incomplete(['journaleven']))
assert len(ms_even) == n_top_level_journals // 2
# Check subdir match
ms_subdir = internals.complete_path(None, None, incomplete(['subdir', '']))
assert len(ms_subdir) == 1
# Check for completion help string as basename
assert ms_subdir[0] == (subp.strpath, subp.basename)

0 comments on commit bd761c7

Please sign in to comment.