Adds edit command.
This commit is contained in:
parent
f734e2f172
commit
748717ff97
@ -28,6 +28,7 @@ BCOLORS = {
|
|||||||
|
|
||||||
# application specific
|
# application specific
|
||||||
ALIASES = {
|
ALIASES = {
|
||||||
|
'ok' : 'green',
|
||||||
'error' : 'red',
|
'error' : 'red',
|
||||||
'normal' : 'grey',
|
'normal' : 'grey',
|
||||||
'citekey' : 'purple',
|
'citekey' : 'purple',
|
||||||
|
@ -4,4 +4,5 @@ import import_cmd
|
|||||||
import init_cmd
|
import init_cmd
|
||||||
import list_cmd
|
import list_cmd
|
||||||
import open_cmd
|
import open_cmd
|
||||||
|
import edit_cmd
|
||||||
import websearch_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):
|
def command(config):
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory()
|
||||||
articles = []
|
articles = []
|
||||||
for n in range(rp.size()):
|
for n, p in enumerate(rp.all_papers()):
|
||||||
paper = rp.paper_from_number(n, fatal=True)
|
bibdesc = pretty.bib_oneliner(p.bibentry)
|
||||||
bibdesc = pretty.bib_oneliner(paper.bibentry)
|
|
||||||
articles.append((u'{num:d}: [{citekey}] {descr}'.format(
|
articles.append((u'{num:d}: [{citekey}] {descr}'.format(
|
||||||
num=int(n),
|
num=int(n),
|
||||||
citekey=colored(rp.citekeys[n], 'purple'),
|
citekey=colored(rp.citekeys[n], 'purple'),
|
||||||
|
@ -15,7 +15,7 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
def command(config, citekey):
|
def command(config, citekey):
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory()
|
||||||
paper = rp.paper_from_any(citekey, fatal=True)
|
paper = rp.paper_from_ref(citekey, fatal=True)
|
||||||
try:
|
try:
|
||||||
if paper.check_file():
|
if paper.check_file():
|
||||||
filepath = paper.get_file_path()
|
filepath = paper.get_file_path()
|
||||||
|
@ -3,7 +3,10 @@ import ConfigParser
|
|||||||
|
|
||||||
|
|
||||||
DEFAULT_OPEN_CMD = 'open'
|
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_COPY = 'yes'
|
||||||
DEFAULT_IMPORT_MOVE = 'no'
|
DEFAULT_IMPORT_MOVE = 'no'
|
||||||
|
@ -26,10 +26,6 @@ except ImportError:
|
|||||||
|
|
||||||
_papersdir = None
|
_papersdir = None
|
||||||
|
|
||||||
try:
|
|
||||||
EDITOR = os.environ['EDITOR']
|
|
||||||
except KeyError:
|
|
||||||
EDITOR = 'nano'
|
|
||||||
BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml']
|
BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml']
|
||||||
|
|
||||||
|
|
||||||
@ -151,16 +147,14 @@ def load_externalbibfile(fullbibpath):
|
|||||||
return bib_data
|
return bib_data
|
||||||
|
|
||||||
|
|
||||||
# vim input
|
def editor_input(editor, initial=""):
|
||||||
|
|
||||||
def vim_input(initial=""):
|
|
||||||
"""Use an editor to get input"""
|
"""Use an editor to get input"""
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file:
|
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file:
|
||||||
tfile_name = temp_file.name
|
tfile_name = temp_file.name
|
||||||
temp_file.write(initial)
|
temp_file.write(initial)
|
||||||
temp_file.flush()
|
temp_file.flush()
|
||||||
subprocess.call([EDITOR, tfile_name])
|
subprocess.call([editor, tfile_name])
|
||||||
|
|
||||||
with open(tfile_name) as temp_file:
|
with open(tfile_name) as temp_file:
|
||||||
content = temp_file.read()
|
content = temp_file.read()
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
@ -16,6 +14,7 @@ cmds = collections.OrderedDict([
|
|||||||
('add_library', commands.add_library_cmd),
|
('add_library', commands.add_library_cmd),
|
||||||
('import', commands.import_cmd),
|
('import', commands.import_cmd),
|
||||||
('list', commands.list_cmd),
|
('list', commands.list_cmd),
|
||||||
|
('edit', commands.edit_cmd),
|
||||||
('open', commands.open_cmd),
|
('open', commands.open_cmd),
|
||||||
('websearch', commands.websearch_cmd)
|
('websearch', commands.websearch_cmd)
|
||||||
])
|
])
|
||||||
|
@ -20,45 +20,31 @@ class Repository(object):
|
|||||||
|
|
||||||
# loading existing papers
|
# loading existing papers
|
||||||
|
|
||||||
def paper_from_number(self, number, fatal=True):
|
def paper_from_citekey(self, citekey):
|
||||||
|
"""Load a paper by its citekey from disk, if necessary."""
|
||||||
|
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:
|
||||||
try:
|
try:
|
||||||
citekey = self.citekeys[int(number)]
|
return self.citekeys[int(ref)]
|
||||||
paper = self.paper_from_citekey(citekey)
|
|
||||||
return paper
|
|
||||||
except (IndexError, ValueError):
|
except (IndexError, ValueError):
|
||||||
if fatal:
|
if fatal:
|
||||||
print(colored('error', 'error')
|
print(colored('error', 'error')
|
||||||
+ ': no paper with number {}'.format(
|
+ ': no paper with reference {}'.format(
|
||||||
colored(number, 'citekey')))
|
colored(ref, 'citekey')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
raise(IOError('file not found'))
|
raise(IOError('file not found'))
|
||||||
|
|
||||||
def paper_from_citekey(self, citekey, fatal=True):
|
def paper_from_ref(self, ref, fatal=True):
|
||||||
"""Load a paper by its citekey from disk, if necessary."""
|
key = self.citekey_from_ref(ref, fatal=fatal)
|
||||||
if citekey in self.citekeys:
|
return self.paper_from_citekey(key)
|
||||||
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'))
|
|
||||||
|
|
||||||
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:
|
|
||||||
if fatal:
|
|
||||||
print(colored('error', 'error')
|
|
||||||
+ (': paper with citekey or number %s not found'
|
|
||||||
% colored(key, 'citekey')))
|
|
||||||
exit(-1)
|
|
||||||
raise(IOError('file not found'))
|
|
||||||
|
|
||||||
# creating new papers
|
# creating new papers
|
||||||
|
|
||||||
@ -137,6 +123,10 @@ class Repository(object):
|
|||||||
else:
|
else:
|
||||||
return os.path.join(self.papersdir, DOC_DIR)
|
return os.path.join(self.papersdir, DOC_DIR)
|
||||||
|
|
||||||
|
def all_papers(self):
|
||||||
|
for key in self.citekeys:
|
||||||
|
yield self.paper_from_citekey(key)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_directory(cls, papersdir=None):
|
def from_directory(cls, papersdir=None):
|
||||||
repo = cls()
|
repo = cls()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user