Adds completion for citekeys.
This commit is contained in:
parent
a5466c940e
commit
02c11aaaea
@ -9,7 +9,7 @@ from .. import color
|
|||||||
from .. import pretty
|
from .. import pretty
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('add', help='add a paper to the repository')
|
parser = subparsers.add_parser('add', help='add a paper to the repository')
|
||||||
parser.add_argument('bibfile', nargs='?', default=None,
|
parser.add_argument('bibfile', nargs='?', default=None,
|
||||||
help='bibtex file')
|
help='bibtex file')
|
||||||
|
@ -3,7 +3,7 @@ from .. import config
|
|||||||
from .. import content
|
from .. import content
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('conf',
|
parser = subparsers.add_parser('conf',
|
||||||
help='open the configuration in an editor')
|
help='open the configuration in an editor')
|
||||||
return parser
|
return parser
|
||||||
|
@ -6,6 +6,8 @@ from .. import color
|
|||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from .. import content
|
from .. import content
|
||||||
from ..utils import resolve_citekey, resolve_citekey_list
|
from ..utils import resolve_citekey, resolve_citekey_list
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
|
|
||||||
# doc --+- add $file $key [[-L|--link] | [-M|--move]] [-f|--force]
|
# doc --+- add $file $key [[-L|--link] | [-M|--move]] [-f|--force]
|
||||||
# +- remove $key [$key [...]] [-f|--force]
|
# +- remove $key [$key [...]] [-f|--force]
|
||||||
@ -13,35 +15,46 @@ from ..utils import resolve_citekey, resolve_citekey_list
|
|||||||
# +- open $key [-w|--with $cmd]
|
# +- open $key [-w|--with $cmd]
|
||||||
# supplements attach, open
|
# supplements attach, open
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
doc_parser = subparsers.add_parser('doc', help='manage the document relating to a publication')
|
doc_parser = subparsers.add_parser(
|
||||||
doc_subparsers = doc_parser.add_subparsers(title='document actions', help='actions to interact with the documents',
|
'doc',
|
||||||
dest='action')
|
help='manage the document relating to a publication')
|
||||||
|
doc_subparsers = doc_parser.add_subparsers(
|
||||||
|
title='document actions', dest='action',
|
||||||
|
help='actions to interact with the documents')
|
||||||
doc_subparsers.required = True
|
doc_subparsers.required = True
|
||||||
|
|
||||||
add_parser = doc_subparsers.add_parser('add', help='add a document to a publication')
|
add_parser = doc_subparsers.add_parser('add', help='add a document to a publication')
|
||||||
add_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False,
|
add_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False,
|
||||||
help='force overwriting an already assigned document')
|
help='force overwriting an already assigned document')
|
||||||
add_parser.add_argument('document', nargs=1, help='document file to assign')
|
add_parser.add_argument('document', nargs=1, help='document file to assign')
|
||||||
add_parser.add_argument('citekey', nargs=1, help='citekey of the publication')
|
add_parser.add_argument('citekey', nargs=1, help='citekey of the publication'
|
||||||
|
).completer = CiteKeyCompletion(conf)
|
||||||
add_exclusives = add_parser.add_mutually_exclusive_group()
|
add_exclusives = add_parser.add_mutually_exclusive_group()
|
||||||
add_exclusives.add_argument('-L', '--link', action='store_false', dest='link', default=False,
|
add_exclusives.add_argument(
|
||||||
help='do not copy document files, just create a link')
|
'-L', '--link', action='store_false', dest='link', default=False,
|
||||||
add_exclusives.add_argument('-M', '--move', action='store_true', dest='move', default=False,
|
help='do not copy document files, just create a link')
|
||||||
help='move document instead of of copying (ignored if --link)')
|
add_exclusives.add_argument(
|
||||||
|
'-M', '--move', action='store_true', dest='move', default=False,
|
||||||
|
help='move document instead of of copying (ignored if --link)')
|
||||||
|
|
||||||
remove_parser = doc_subparsers.add_parser('remove', help='remove assigned documents from publications')
|
remove_parser = doc_subparsers.add_parser('remove', help='remove assigned documents from publications')
|
||||||
remove_parser.add_argument('citekeys', nargs='+', help='citekeys of the publications')
|
remove_parser.add_argument('citekeys', nargs='+', help='citekeys of the publications'
|
||||||
remove_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False,
|
).completer = CiteKeyCompletion(conf)
|
||||||
help='force removing assigned documents')
|
remove_parser.add_argument('-f', '--force', action='store_true', dest='force',
|
||||||
|
default=False,
|
||||||
|
help='force removing assigned documents')
|
||||||
|
|
||||||
# favor key+ path over: key
|
# favor key+ path over: key
|
||||||
export_parser = doc_subparsers.add_parser('export', help='export assigned documents to given path')
|
export_parser = doc_subparsers.add_parser('export', help='export assigned documents to given path')
|
||||||
export_parser.add_argument('citekeys', nargs='+', help='citekeys of the documents to export')
|
export_parser.add_argument('citekeys', nargs='+',
|
||||||
|
help='citekeys of the documents to export'
|
||||||
|
).completer = CiteKeyCompletion(conf)
|
||||||
export_parser.add_argument('path', nargs=1, help='directory to export the files to')
|
export_parser.add_argument('path', nargs=1, help='directory to export the files to')
|
||||||
|
|
||||||
open_parser = doc_subparsers.add_parser('open', help='open an assigned document')
|
open_parser = doc_subparsers.add_parser('open', help='open an assigned document')
|
||||||
open_parser.add_argument('citekey', nargs=1, help='citekey of the document to open')
|
open_parser.add_argument('citekey', nargs=1, help='citekey of the document to open'
|
||||||
|
).completer = CiteKeyCompletion(conf)
|
||||||
open_parser.add_argument('-w', '--with', dest='cmd', help='command to open the file with')
|
open_parser.add_argument('-w', '--with', dest='cmd', help='command to open the file with')
|
||||||
|
|
||||||
return doc_parser
|
return doc_parser
|
||||||
|
@ -4,15 +4,19 @@ from .. import repo
|
|||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from ..endecoder import EnDecoder
|
from ..endecoder import EnDecoder
|
||||||
from ..utils import resolve_citekey
|
from ..utils import resolve_citekey
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('edit',
|
parser = subparsers.add_parser(
|
||||||
help='open the paper bibliographic file in an editor')
|
'edit',
|
||||||
parser.add_argument('-m', '--meta', action='store_true', default=False,
|
help='open the paper bibliographic file in an editor')
|
||||||
help='edit metadata')
|
parser.add_argument(
|
||||||
parser.add_argument('citekey',
|
'-m', '--meta', action='store_true', default=False,
|
||||||
help='citekey of the paper')
|
help='edit metadata')
|
||||||
|
parser.add_argument(
|
||||||
|
'citekey',
|
||||||
|
help='citekey of the paper').completer = CiteKeyCompletion(conf)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,12 +4,15 @@ from .. import repo
|
|||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from .. import endecoder
|
from .. import endecoder
|
||||||
from ..utils import resolve_citekey_list
|
from ..utils import resolve_citekey_list
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
def parser(subparsers):
|
|
||||||
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('export', help='export bibliography')
|
parser = subparsers.add_parser('export', help='export bibliography')
|
||||||
# parser.add_argument('-f', '--bib-format', default='bibtex',
|
# parser.add_argument('-f', '--bib-format', default='bibtex',
|
||||||
# help='export format')
|
# help='export format')
|
||||||
parser.add_argument('citekeys', nargs='*', help='one or several citekeys')
|
parser.add_argument('citekeys', nargs='*', help='one or several citekeys'
|
||||||
|
).completer = CiteKeyCompletion(conf)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ from ..uis import get_ui
|
|||||||
from ..content import system_path, read_text_file
|
from ..content import system_path, read_text_file
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('import',
|
parser = subparsers.add_parser('import',
|
||||||
help='import paper(s) to the repository')
|
help='import paper(s) to the repository')
|
||||||
parser.add_argument('bibpath',
|
parser.add_argument('bibpath',
|
||||||
|
@ -9,7 +9,8 @@ from ..repo import Repository
|
|||||||
from ..content import system_path, check_directory
|
from ..content import system_path, check_directory
|
||||||
from .. import config
|
from .. import config
|
||||||
|
|
||||||
def parser(subparsers):
|
|
||||||
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('init',
|
parser = subparsers.add_parser('init',
|
||||||
help="initialize the pubs directory")
|
help="initialize the pubs directory")
|
||||||
parser.add_argument('-p', '--pubsdir', default=None,
|
parser.add_argument('-p', '--pubsdir', default=None,
|
||||||
|
@ -10,7 +10,7 @@ class InvalidQuery(ValueError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('list', help="list papers")
|
parser = subparsers.add_parser('list', help="list papers")
|
||||||
parser.add_argument('-k', '--citekeys-only', action='store_true',
|
parser.add_argument('-k', '--citekeys-only', action='store_true',
|
||||||
default=False, dest='citekeys',
|
default=False, dest='citekeys',
|
||||||
|
@ -2,19 +2,19 @@ from .. import repo
|
|||||||
from .. import content
|
from .. import content
|
||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from ..utils import resolve_citekey
|
from ..utils import resolve_citekey
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('note',
|
parser = subparsers.add_parser('note',
|
||||||
help='edit the note attached to a paper')
|
help='edit the note attached to a paper')
|
||||||
parser.add_argument('citekey',
|
parser.add_argument('citekey',
|
||||||
help='citekey of the paper')
|
help='citekey of the paper'
|
||||||
|
).completer = CiteKeyCompletion(conf)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(conf, args):
|
def command(conf, args):
|
||||||
"""
|
|
||||||
"""
|
|
||||||
|
|
||||||
ui = get_ui()
|
ui = get_ui()
|
||||||
rp = repo.Repository(conf)
|
rp = repo.Repository(conf)
|
||||||
|
@ -3,13 +3,16 @@ from .. import color
|
|||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
from ..utils import resolve_citekey_list
|
from ..utils import resolve_citekey_list
|
||||||
from ..p3 import ustr
|
from ..p3 import ustr
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
def parser(subparsers):
|
|
||||||
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('remove', help='removes a publication')
|
parser = subparsers.add_parser('remove', help='removes a publication')
|
||||||
parser.add_argument('-f', '--force', action='store_true', default=None,
|
parser.add_argument('-f', '--force', action='store_true', default=None,
|
||||||
help="does not prompt for confirmation.")
|
help="does not prompt for confirmation.")
|
||||||
parser.add_argument('citekeys', nargs='+',
|
parser.add_argument('citekeys', nargs='+',
|
||||||
help="one or several citekeys")
|
help="one or several citekeys"
|
||||||
|
).completer = CiteKeyCompletion(conf)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,13 +2,15 @@ from ..uis import get_ui
|
|||||||
from .. import color
|
from .. import color
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from ..utils import resolve_citekey
|
from ..utils import resolve_citekey
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
def parser(subparsers):
|
|
||||||
parser = subparsers.add_parser('rename', help='rename the citekey of a repository')
|
def parser(subparsers, conf):
|
||||||
parser.add_argument('citekey',
|
parser = subparsers.add_parser('rename',
|
||||||
help='current citekey')
|
help='rename the citekey of a repository')
|
||||||
parser.add_argument('new_citekey',
|
parser.add_argument('citekey', help='current citekey'
|
||||||
help='new citekey')
|
).completer = CiteKeyCompletion(conf)
|
||||||
|
parser.add_argument('new_citekey', help='new citekey')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +28,7 @@ def command(conf, args):
|
|||||||
paper = rp.pull_paper(key)
|
paper = rp.pull_paper(key)
|
||||||
rp.rename_paper(paper, args.new_citekey)
|
rp.rename_paper(paper, args.new_citekey)
|
||||||
ui.message("The '{}' citekey has been renamed into '{}'".format(
|
ui.message("The '{}' citekey has been renamed into '{}'".format(
|
||||||
color.dye_out(args.citekey, 'citekey'),
|
color.dye_out(args.citekey, 'citekey'),
|
||||||
color.dye_out(args.new_citekey, 'citekey')))
|
color.dye_out(args.new_citekey, 'citekey')))
|
||||||
|
|
||||||
rp.close()
|
rp.close()
|
||||||
|
@ -24,12 +24,13 @@ from ..uis import get_ui
|
|||||||
from .. import pretty
|
from .. import pretty
|
||||||
from .. import color
|
from .. import color
|
||||||
from ..utils import resolve_citekey
|
from ..utils import resolve_citekey
|
||||||
|
from ..completion import CiteKeyCompletion
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('tag', help="add, remove and show tags")
|
parser = subparsers.add_parser('tag', help="add, remove and show tags")
|
||||||
parser.add_argument('citekeyOrTag', nargs='?', default=None,
|
parser.add_argument('citekeyOrTag', nargs='?', default=None,
|
||||||
help='citekey or tag.')
|
help='citekey or tag.').completer = CiteKeyCompletion(conf)
|
||||||
parser.add_argument('tags', nargs='?', default=None,
|
parser.add_argument('tags', nargs='?', default=None,
|
||||||
help='If the previous argument was a citekey, then '
|
help='If the previous argument was a citekey, then '
|
||||||
'a list of tags separated by a +.')
|
'a list of tags separated by a +.')
|
||||||
|
@ -3,7 +3,8 @@ import urllib
|
|||||||
|
|
||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
|
|
||||||
def parser(subparsers):
|
|
||||||
|
def parser(subparsers, conf):
|
||||||
parser = subparsers.add_parser('websearch',
|
parser = subparsers.add_parser('websearch',
|
||||||
help="launch a search on Google Scholar")
|
help="launch a search on Google Scholar")
|
||||||
parser.add_argument("search_string", nargs = '*',
|
parser.add_argument("search_string", nargs = '*',
|
||||||
|
38
pubs/completion.py
Normal file
38
pubs/completion.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
from . import repo
|
||||||
|
try:
|
||||||
|
import argcomplete
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
|
||||||
|
class FakeModule:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _fun(**kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __getattr__(self, _):
|
||||||
|
return self._fun
|
||||||
|
|
||||||
|
argcomplete = FakeModule()
|
||||||
|
|
||||||
|
|
||||||
|
def autocomplete(parser):
|
||||||
|
argcomplete.autocomplete(parser)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseCompleter(object):
|
||||||
|
|
||||||
|
def __init__(self, conf):
|
||||||
|
self.conf = conf
|
||||||
|
|
||||||
|
def __call__(self, **kwargs):
|
||||||
|
try:
|
||||||
|
return self._complete(**kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
argcomplete.warn(e)
|
||||||
|
|
||||||
|
|
||||||
|
class CiteKeyCompletion(BaseCompleter):
|
||||||
|
|
||||||
|
def _complete(self, **kwargs):
|
||||||
|
rp = repo.Repository(self.conf)
|
||||||
|
return rp.citekeys
|
@ -7,10 +7,7 @@ from . import commands
|
|||||||
from . import update
|
from . import update
|
||||||
from . import plugins
|
from . import plugins
|
||||||
from .__init__ import __version__
|
from .__init__ import __version__
|
||||||
try:
|
from .completion import autocomplete
|
||||||
import argcomplete
|
|
||||||
except ModuleNotFoundError:
|
|
||||||
argcomplete = None
|
|
||||||
|
|
||||||
|
|
||||||
CORE_CMDS = collections.OrderedDict([
|
CORE_CMDS = collections.OrderedDict([
|
||||||
@ -77,7 +74,7 @@ def execute(raw_args=sys.argv):
|
|||||||
|
|
||||||
# Populate the parser with core commands
|
# Populate the parser with core commands
|
||||||
for cmd_name, cmd_mod in CORE_CMDS.items():
|
for cmd_name, cmd_mod in CORE_CMDS.items():
|
||||||
cmd_parser = cmd_mod.parser(subparsers)
|
cmd_parser = cmd_mod.parser(subparsers, conf)
|
||||||
cmd_parser.set_defaults(func=cmd_mod.command)
|
cmd_parser.set_defaults(func=cmd_mod.command)
|
||||||
|
|
||||||
# Extend with plugin commands
|
# Extend with plugin commands
|
||||||
@ -85,8 +82,8 @@ def execute(raw_args=sys.argv):
|
|||||||
for p in plugins.get_plugins().values():
|
for p in plugins.get_plugins().values():
|
||||||
p.update_parser(subparsers)
|
p.update_parser(subparsers)
|
||||||
|
|
||||||
if argcomplete is not None:
|
# Eventually autocomplete
|
||||||
argcomplete.autocomplete(parser)
|
autocomplete(parser)
|
||||||
# Parse and run appropriate command
|
# Parse and run appropriate command
|
||||||
args = parser.parse_args(remaining_args)
|
args = parser.parse_args(remaining_args)
|
||||||
args.prog = "pubs" # FIXME?
|
args.prog = "pubs" # FIXME?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user