Adds basic import command with document file copy.
Still lot to be improve on ui and features.
This commit is contained in:
parent
47e3ce88e8
commit
3af7590827
@ -1,5 +1,6 @@
|
|||||||
import add_cmd
|
import add_cmd
|
||||||
import add_library_cmd
|
import add_library_cmd
|
||||||
|
import import_cmd
|
||||||
import init_cmd
|
import init_cmd
|
||||||
import list_cmd
|
import list_cmd
|
||||||
import open_cmd
|
import open_cmd
|
||||||
|
55
papers/commands/import_cmd.py
Normal file
55
papers/commands/import_cmd.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from .. import repo
|
||||||
|
from ..paper import Paper, NoDocumentFile
|
||||||
|
|
||||||
|
|
||||||
|
def parser(subparsers, config):
|
||||||
|
parser = subparsers.add_parser('import',
|
||||||
|
help='import paper(s) to the repository')
|
||||||
|
parser.add_argument('bibpath',
|
||||||
|
help='path to bibtex, bibtexml or bibyaml file (or directory)')
|
||||||
|
parser.add_argument('-c', '--copy', action='store_true', default=None,
|
||||||
|
help="copy document files into library directory (default)")
|
||||||
|
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
|
||||||
|
help="don't copy document files (opposite of -c)")
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def command(config, bibpath, copy):
|
||||||
|
"""
|
||||||
|
:param pdffilepath path (no url yet) to a pdf or ps file
|
||||||
|
:param bibtex bibtex file (in .bib, .bibml or .yaml format.
|
||||||
|
"""
|
||||||
|
if copy is None:
|
||||||
|
copy = config.get('papers', 'import-copy')
|
||||||
|
rp = repo.Repository.from_directory()
|
||||||
|
# Get directory for document
|
||||||
|
doc_path = rp.get_document_directory(config)
|
||||||
|
if not (os.path.exists(doc_path) and os.path.isdir(doc_path)):
|
||||||
|
print "Document directory %s, does not exist." % doc_path
|
||||||
|
sys.exit(1)
|
||||||
|
# Extract papers from bib
|
||||||
|
papers = Paper.many_from_path(bibpath)
|
||||||
|
for p in papers:
|
||||||
|
doc_file = None
|
||||||
|
try:
|
||||||
|
file_path = p.get_document_file_from_bibdata(remove=True)
|
||||||
|
if os.path.exists(file_path):
|
||||||
|
doc_file = file_path
|
||||||
|
else:
|
||||||
|
print "File does not exist for %s." % p.citekey
|
||||||
|
except NoDocumentFile:
|
||||||
|
print "No file for %s." % p.citekey
|
||||||
|
rp.add_paper(p)
|
||||||
|
if doc_file:
|
||||||
|
if copy:
|
||||||
|
ext = os.path.splitext(doc_file)[1]
|
||||||
|
new_doc_file = os.path.join(doc_path, p.citekey + ext)
|
||||||
|
shutil.copy(doc_file, new_doc_file)
|
||||||
|
else:
|
||||||
|
new_doc_file = doc_file
|
||||||
|
p.set_document(new_doc_file)
|
||||||
|
rp.add_or_update(p)
|
@ -12,7 +12,6 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
|
|
||||||
def command(config, citekey):
|
def command(config, citekey):
|
||||||
print config.get('papers', 'open-cmd')
|
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory()
|
||||||
paper = rp.paper_from_any(citekey, fatal=True)
|
paper = rp.paper_from_any(citekey, fatal=True)
|
||||||
try:
|
try:
|
||||||
|
32
papers/configs.py
Normal file
32
papers/configs.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import os
|
||||||
|
import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_OPEN_CMD = 'open'
|
||||||
|
DEFAULT_EDIT_CMD = 'vim'
|
||||||
|
|
||||||
|
DEFAULT_IMPORT_COPY = 'yes'
|
||||||
|
DEFAULT_IMPORT_MOVE = 'no'
|
||||||
|
|
||||||
|
CONFIG = ConfigParser.SafeConfigParser({
|
||||||
|
'open-cmd': DEFAULT_OPEN_CMD,
|
||||||
|
'edit-cmd': DEFAULT_EDIT_CMD,
|
||||||
|
'import-copy': DEFAULT_IMPORT_COPY,
|
||||||
|
'import-move': DEFAULT_IMPORT_MOVE,
|
||||||
|
})
|
||||||
|
CONFIG.add_section('papers')
|
||||||
|
|
||||||
|
|
||||||
|
def read_config():
|
||||||
|
CONFIG.read(os.path.expanduser('~/.papersrc'))
|
||||||
|
return CONFIG
|
||||||
|
|
||||||
|
|
||||||
|
def get_boolean(value, default):
|
||||||
|
value = str(value).lower()
|
||||||
|
if value in ('yes', 'true', 't', 'y', '1'):
|
||||||
|
return True
|
||||||
|
elif value in ('no', 'false', 'f', 'n', '0'):
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return 0
|
@ -29,13 +29,18 @@ try:
|
|||||||
EDITOR = os.environ['EDITOR']
|
EDITOR = os.environ['EDITOR']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
EDITOR = 'nano'
|
EDITOR = 'nano'
|
||||||
|
BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml']
|
||||||
|
|
||||||
|
|
||||||
|
def clean_path(path):
|
||||||
|
return os.path.abspath(os.path.expanduser(path))
|
||||||
|
|
||||||
|
|
||||||
def find_papersdir():
|
def find_papersdir():
|
||||||
"""Find .papers directory in this directory and the parent directories"""
|
"""Find .papers directory in this directory and the parent directories"""
|
||||||
global _papersdir
|
global _papersdir
|
||||||
if _papersdir is None:
|
if _papersdir is None:
|
||||||
curdir = os.path.abspath(os.getcwd())
|
curdir = os.path.abspath('')
|
||||||
while curdir != '':
|
while curdir != '':
|
||||||
if (os.path.exists(curdir + '/.papers')
|
if (os.path.exists(curdir + '/.papers')
|
||||||
and os.path.isdir(curdir + '/.papers')):
|
and os.path.isdir(curdir + '/.papers')):
|
||||||
|
@ -105,10 +105,10 @@ class Paper(object):
|
|||||||
citekey = u'{}{}'.format(u''.join(first_author.last()), year)
|
citekey = u'{}{}'.format(u''.join(first_author.last()), year)
|
||||||
return str2citekey(citekey)
|
return str2citekey(citekey)
|
||||||
|
|
||||||
def set_pdf(self, pdfpath):
|
def set_document(self, docpath):
|
||||||
fullpdfpath = os.path.abspath(pdfpath)
|
fullpdfpath = os.path.abspath(docpath)
|
||||||
files.check_file(fullpdfpath)
|
files.check_file(fullpdfpath)
|
||||||
name, ext = files.name_from_path(pdfpath)
|
name, ext = files.name_from_path(docpath)
|
||||||
self.metadata['filename'] = name
|
self.metadata['filename'] = name
|
||||||
self.metadata['extension'] = ext
|
self.metadata['extension'] = ext
|
||||||
self.metadata['path'] = fullpdfpath
|
self.metadata['path'] = fullpdfpath
|
||||||
@ -124,6 +124,30 @@ class Paper(object):
|
|||||||
files.save_bibdata(bibdata, bib_filepath)
|
files.save_bibdata(bibdata, bib_filepath)
|
||||||
files.save_meta(self.metadata, meta_filepath)
|
files.save_meta(self.metadata, meta_filepath)
|
||||||
|
|
||||||
|
def get_document_file_from_bibdata(self, remove=False):
|
||||||
|
"""Try extracting document file from bib data.
|
||||||
|
Raises NoDocumentFile if not found.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
-----------
|
||||||
|
remove: default: False
|
||||||
|
remove field after extracting information
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
field = self.bibentry.fields['file']
|
||||||
|
# Check if this is mendeley specific
|
||||||
|
for f in field.split(':'):
|
||||||
|
if len(f) > 0:
|
||||||
|
break
|
||||||
|
if remove:
|
||||||
|
self.bibentry.fields.pop('file')
|
||||||
|
# This is a hck for Mendeley. Make clean
|
||||||
|
if f[0] != '/':
|
||||||
|
f = '/' + f
|
||||||
|
return f
|
||||||
|
except (KeyError, IndexError):
|
||||||
|
raise(NoDocumentFile('No file found in bib data.'))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, bibpath, metapath=None):
|
def load(cls, bibpath, metapath=None):
|
||||||
key, entry = cls.get_bibentry(bibpath)
|
key, entry = cls.get_bibentry(bibpath)
|
||||||
@ -148,7 +172,15 @@ class Paper(object):
|
|||||||
return BASE_META.copy()
|
return BASE_META.copy()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def many_from_bib(cls, bibpath):
|
def many_from_path(cls, bibpath):
|
||||||
bib_data = files.load_externalbibfile(bibpath)
|
"""Extract list of papers found in bibliographic files in path.
|
||||||
return [Paper(bibentry=bib_data.entries[k], citekey=k)
|
"""
|
||||||
for k in bib_data.entries]
|
bibpath = files.clean_path(bibpath)
|
||||||
|
if os.path.isdir(bibpath):
|
||||||
|
all_files = [os.path.join(bibpath, f) for f in os.listdir(bibpath)
|
||||||
|
if os.path.splitext(f)[-1] in files.BIB_EXTENSIONS]
|
||||||
|
else:
|
||||||
|
all_files = [bibpath]
|
||||||
|
bib_data = [files.load_externalbibfile(f) for f in all_files]
|
||||||
|
return [Paper(bibentry=b.entries[k], citekey=k)
|
||||||
|
for b in bib_data for k in b.entries]
|
||||||
|
@ -6,24 +6,21 @@ import os
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import collections
|
import collections
|
||||||
import ConfigParser
|
|
||||||
|
|
||||||
|
from papers import configs
|
||||||
from papers import commands
|
from papers import commands
|
||||||
|
|
||||||
cmds = collections.OrderedDict([
|
cmds = collections.OrderedDict([
|
||||||
('init', commands.init_cmd),
|
('init', commands.init_cmd),
|
||||||
('add', commands.add_cmd),
|
('add', commands.add_cmd),
|
||||||
('add_library', commands.add_library_cmd),
|
('add_library', commands.add_library_cmd),
|
||||||
|
('import', commands.import_cmd),
|
||||||
('list', commands.list_cmd),
|
('list', commands.list_cmd),
|
||||||
('open', commands.open_cmd),
|
('open', commands.open_cmd),
|
||||||
('websearch', commands.websearch_cmd)
|
('websearch', commands.websearch_cmd)
|
||||||
])
|
])
|
||||||
|
|
||||||
config = ConfigParser.SafeConfigParser()
|
config = configs.read_config()
|
||||||
config.add_section('papers')
|
|
||||||
config.set('papers', 'open-cmd', 'open')
|
|
||||||
config.set('papers', 'edit-cmd', 'vim')
|
|
||||||
config.read(os.path.expanduser('~/.papersrc'))
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="research papers repository")
|
parser = argparse.ArgumentParser(description="research papers repository")
|
||||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||||
|
@ -9,6 +9,7 @@ ALPHABET = 'abcdefghijklmopqrstuvwxyz'
|
|||||||
BASE_FILE = 'papers.yaml'
|
BASE_FILE = 'papers.yaml'
|
||||||
BIB_DIR = 'bibdata'
|
BIB_DIR = 'bibdata'
|
||||||
META_DIR = 'meta'
|
META_DIR = 'meta'
|
||||||
|
DOC_DIR = 'doc'
|
||||||
|
|
||||||
|
|
||||||
class Repository(object):
|
class Repository(object):
|
||||||
@ -60,9 +61,9 @@ class Repository(object):
|
|||||||
|
|
||||||
# creating new papers
|
# creating new papers
|
||||||
|
|
||||||
def add_paper_from_paths(self, pdfpath, bibpath):
|
def add_paper_from_paths(self, docpath, bibpath):
|
||||||
p = Paper.load(bibpath)
|
p = Paper.load(bibpath)
|
||||||
p.set_pdf(pdfpath)
|
p.set_document(docpath)
|
||||||
self.add_paper(p)
|
self.add_paper(p)
|
||||||
|
|
||||||
def add_paper(self, p):
|
def add_paper(self, p):
|
||||||
@ -83,7 +84,7 @@ class Repository(object):
|
|||||||
if not paper.citekey in self.citekeys:
|
if not paper.citekey in self.citekeys:
|
||||||
self.add_paper(paper)
|
self.add_paper(paper)
|
||||||
else:
|
else:
|
||||||
paper.save_paper(paper)
|
self.save_paper(paper)
|
||||||
|
|
||||||
def save_paper(self, paper):
|
def save_paper(self, paper):
|
||||||
if not paper.citekey in self.citekeys:
|
if not paper.citekey in self.citekeys:
|
||||||
@ -129,6 +130,12 @@ class Repository(object):
|
|||||||
else:
|
else:
|
||||||
raise(ValueError("%s is not a valid paper file." % file_))
|
raise(ValueError("%s is not a valid paper file." % file_))
|
||||||
|
|
||||||
|
def get_document_directory(self, config):
|
||||||
|
if config.has_option('papers', 'document-directory'):
|
||||||
|
return config.get('papers', 'document-directory')
|
||||||
|
else:
|
||||||
|
return os.path.join(self.papersdir, DOC_DIR)
|
||||||
|
|
||||||
@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