diff --git a/papers/color.py b/papers/color.py index 7a4a1c7..758b094 100644 --- a/papers/color.py +++ b/papers/color.py @@ -28,6 +28,7 @@ BCOLORS = { # application specific ALIASES = { + 'ok' : 'green', 'error' : 'red', 'normal' : 'grey', 'citekey' : 'purple', diff --git a/papers/commands/__init__.py b/papers/commands/__init__.py index af89798..61d61fb 100644 --- a/papers/commands/__init__.py +++ b/papers/commands/__init__.py @@ -4,4 +4,5 @@ import import_cmd import init_cmd import list_cmd import open_cmd +import edit_cmd import websearch_cmd diff --git a/papers/commands/edit_cmd.py b/papers/commands/edit_cmd.py new file mode 100644 index 0000000..d567705 --- /dev/null +++ b/papers/commands/edit_cmd.py @@ -0,0 +1,29 @@ +import subprocess + +from ..color import colored +from .. import repo + + +def parser(subparsers, config): + parser = subparsers.add_parser('edit', + help=colored('open the paper bibliographic file in an editor', + 'normal')) + parser.add_argument('reference', + help=colored('reference to the paper (citekey or number)', + 'normal')) + return parser + + +def command(config, reference): + rp = repo.Repository.from_directory() + key = rp.citekey_from_ref(reference, fatal=True) + filepath = rp.path_to_paper_file(key, 'bib') + subprocess.call([config.get('papers', 'edit-cmd'), + filepath]) + # TODO use editor_input from files.py instead of directly editing the file. + # Then chack that output file is correctly formmatted and that citekey has + # not changed or is still a valid citekey. + print('{} editing {}.'.format( + colored('Done', 'ok'), + colored(filepath, 'filepath') + )) diff --git a/papers/commands/list_cmd.py b/papers/commands/list_cmd.py index ea8c788..3951cb8 100644 --- a/papers/commands/list_cmd.py +++ b/papers/commands/list_cmd.py @@ -14,9 +14,8 @@ def parser(subparsers, config): def command(config): rp = repo.Repository.from_directory() articles = [] - for n in range(rp.size()): - paper = rp.paper_from_number(n, fatal=True) - bibdesc = pretty.bib_oneliner(paper.bibentry) + for n, p in enumerate(rp.all_papers()): + bibdesc = pretty.bib_oneliner(p.bibentry) articles.append((u'{num:d}: [{citekey}] {descr}'.format( num=int(n), citekey=colored(rp.citekeys[n], 'purple'), diff --git a/papers/commands/open_cmd.py b/papers/commands/open_cmd.py index 391bbe2..01a7266 100644 --- a/papers/commands/open_cmd.py +++ b/papers/commands/open_cmd.py @@ -15,7 +15,7 @@ def parser(subparsers, config): def command(config, citekey): rp = repo.Repository.from_directory() - paper = rp.paper_from_any(citekey, fatal=True) + paper = rp.paper_from_ref(citekey, fatal=True) try: if paper.check_file(): filepath = paper.get_file_path() diff --git a/papers/configs.py b/papers/configs.py index 39c22ea..83bf3b6 100644 --- a/papers/configs.py +++ b/papers/configs.py @@ -3,7 +3,10 @@ import ConfigParser DEFAULT_OPEN_CMD = 'open' -DEFAULT_EDIT_CMD = 'vim' +try: + DEFAULT_EDIT_CMD = os.environ['EDITOR'] +except KeyError: + DEFAULT_EDIT_CMD = 'vi' DEFAULT_IMPORT_COPY = 'yes' DEFAULT_IMPORT_MOVE = 'no' diff --git a/papers/files.py b/papers/files.py index 1508478..d943a95 100644 --- a/papers/files.py +++ b/papers/files.py @@ -26,10 +26,6 @@ except ImportError: _papersdir = None -try: - EDITOR = os.environ['EDITOR'] -except KeyError: - EDITOR = 'nano' BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml'] @@ -151,16 +147,14 @@ def load_externalbibfile(fullbibpath): return bib_data -# vim input - -def vim_input(initial=""): +def editor_input(editor, initial=""): """Use an editor to get input""" with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file: tfile_name = temp_file.name temp_file.write(initial) temp_file.flush() - subprocess.call([EDITOR, tfile_name]) + subprocess.call([editor, tfile_name]) with open(tfile_name) as temp_file: content = temp_file.read() diff --git a/papers/papers b/papers/papers index 01ef4a8..f384621 100755 --- a/papers/papers +++ b/papers/papers @@ -2,8 +2,6 @@ # -*- coding:utf-8 -*- -import os - import argparse import collections @@ -16,6 +14,7 @@ cmds = collections.OrderedDict([ ('add_library', commands.add_library_cmd), ('import', commands.import_cmd), ('list', commands.list_cmd), + ('edit', commands.edit_cmd), ('open', commands.open_cmd), ('websearch', commands.websearch_cmd) ]) diff --git a/papers/repo.py b/papers/repo.py index 4f04fa8..a7cf9cb 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -20,46 +20,32 @@ class Repository(object): # loading existing papers - def paper_from_number(self, number, fatal=True): - try: - citekey = self.citekeys[int(number)] - paper = self.paper_from_citekey(citekey) - return paper - except (IndexError, ValueError): - if fatal: - print(colored('error', 'error') - + ': no paper with number {}'.format( - colored(number, 'citekey'))) - exit(-1) - raise(IOError('file not found')) - - def paper_from_citekey(self, citekey, fatal=True): + def paper_from_citekey(self, citekey): """Load a paper by its citekey from disk, if necessary.""" - if citekey in self.citekeys: - return Paper.load(self.path_to_paper_file(citekey, 'bib'), - metapath=self.path_to_paper_file(citekey, 'meta')) + return Paper.load(self.path_to_paper_file(citekey, 'bib'), + metapath=self.path_to_paper_file(citekey, 'meta')) + + def citekey_from_ref(self, ref, fatal=True): + """Tries to get citekey from given ref. + Ref can be a citekey or a number. + """ + if ref in self.citekeys: + return ref else: - if fatal: - print(colored('error', 'error') - + ': no paper with citekey {}'.format( - colored(citekey, 'citekey'))) - exit(-1) - raise(IOError('file not found')) - - def paper_from_any(self, key, fatal=True): - try: - return self.paper_from_citekey(key, fatal=False) - except IOError: try: - return self.paper_from_number(key, fatal=False) - except IOError: + return self.citekeys[int(ref)] + except (IndexError, ValueError): if fatal: print(colored('error', 'error') - + (': paper with citekey or number %s not found' - % colored(key, 'citekey'))) + + ': no paper with reference {}'.format( + colored(ref, 'citekey'))) exit(-1) raise(IOError('file not found')) + def paper_from_ref(self, ref, fatal=True): + key = self.citekey_from_ref(ref, fatal=fatal) + return self.paper_from_citekey(key) + # creating new papers def add_paper_from_paths(self, docpath, bibpath): @@ -137,6 +123,10 @@ class Repository(object): else: return os.path.join(self.papersdir, DOC_DIR) + def all_papers(self): + for key in self.citekeys: + yield self.paper_from_citekey(key) + @classmethod def from_directory(cls, papersdir=None): repo = cls()