Adds edit command.
This commit is contained in:
parent
f734e2f172
commit
748717ff97
@ -28,6 +28,7 @@ BCOLORS = {
|
||||
|
||||
# application specific
|
||||
ALIASES = {
|
||||
'ok' : 'green',
|
||||
'error' : 'red',
|
||||
'normal' : 'grey',
|
||||
'citekey' : 'purple',
|
||||
|
@ -4,4 +4,5 @@ import import_cmd
|
||||
import init_cmd
|
||||
import list_cmd
|
||||
import open_cmd
|
||||
import edit_cmd
|
||||
import websearch_cmd
|
||||
|
29
papers/commands/edit_cmd.py
Normal file
29
papers/commands/edit_cmd.py
Normal file
@ -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')
|
||||
))
|
@ -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'),
|
||||
|
@ -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()
|
||||
|
@ -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'
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
])
|
||||
|
@ -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'))
|
||||
else:
|
||||
if fatal:
|
||||
print(colored('error', 'error')
|
||||
+ ': no paper with citekey {}'.format(
|
||||
colored(citekey, 'citekey')))
|
||||
exit(-1)
|
||||
raise(IOError('file not found'))
|
||||
return Paper.load(self.path_to_paper_file(citekey, 'bib'),
|
||||
metapath=self.path_to_paper_file(citekey, 'meta'))
|
||||
|
||||
def paper_from_any(self, key, fatal=True):
|
||||
try:
|
||||
return self.paper_from_citekey(key, fatal=False)
|
||||
except IOError:
|
||||
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:
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user