Switch to config based papers directory.
TODO add papers dir to config on papers init command.
This commit is contained in:
parent
099e764c04
commit
04eeedf8a4
@ -43,7 +43,7 @@ def command(config, ui, bibfile, docfile, copy):
|
|||||||
"""
|
"""
|
||||||
if copy is None:
|
if copy is None:
|
||||||
copy = config.get('papers', 'import-copy')
|
copy = config.get('papers', 'import-copy')
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
p = Paper.load(bibfile)
|
p = Paper.load(bibfile)
|
||||||
# Check if another doc file is specified in bibtex
|
# Check if another doc file is specified in bibtex
|
||||||
docfile2 = extract_doc_path_from_bibdata(p, ui)
|
docfile2 = extract_doc_path_from_bibdata(p, ui)
|
||||||
|
@ -13,6 +13,6 @@ def command(config, ui, bibfile):
|
|||||||
"""
|
"""
|
||||||
:param bibtex bibtex file (in .bib, .bibml or .yaml format.
|
:param bibtex bibtex file (in .bib, .bibml or .yaml format.
|
||||||
"""
|
"""
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
for p in Paper.many_from_bib(bibfile):
|
for p in Paper.many_from_bib(bibfile):
|
||||||
rp.add_paper(p)
|
rp.add_paper(p)
|
||||||
|
@ -14,7 +14,7 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
|
|
||||||
def command(config, ui, reference, meta):
|
def command(config, ui, reference, meta):
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
key = rp.citekey_from_ref(reference, fatal=True)
|
key = rp.citekey_from_ref(reference, fatal=True)
|
||||||
paper = rp.paper_from_citekey(key)
|
paper = rp.paper_from_citekey(key)
|
||||||
to_edit = 'bib'
|
to_edit = 'bib'
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pybtex
|
|
||||||
from pybtex.database import BibliographyData
|
from pybtex.database import BibliographyData
|
||||||
|
|
||||||
from .. import repo
|
from .. import repo
|
||||||
@ -19,7 +18,7 @@ def command(config, ui, bib_format):
|
|||||||
"""
|
"""
|
||||||
:param bib_format (in 'bibtex', 'yaml')
|
:param bib_format (in 'bibtex', 'yaml')
|
||||||
"""
|
"""
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
bib = BibliographyData()
|
bib = BibliographyData()
|
||||||
for p in rp.all_papers():
|
for p in rp.all_papers():
|
||||||
bib.add_entry(p.citekey, p.bibentry)
|
bib.add_entry(p.citekey, p.bibentry)
|
||||||
|
@ -21,7 +21,7 @@ def command(config, ui, bibpath, copy):
|
|||||||
"""
|
"""
|
||||||
if copy is None:
|
if copy is None:
|
||||||
copy = config.get('papers', 'import-copy')
|
copy = config.get('papers', 'import-copy')
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
# Extract papers from bib
|
# Extract papers from bib
|
||||||
papers = Paper.many_from_path(bibpath, fatal=False)
|
papers = Paper.many_from_path(bibpath, fatal=False)
|
||||||
for p in papers:
|
for p in papers:
|
||||||
|
@ -3,25 +3,33 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from ..repo import Repository
|
from ..repo import Repository
|
||||||
from ..color import colored
|
from .. import configs
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('init',
|
parser = subparsers.add_parser('init',
|
||||||
help="initialize the .papers directory")
|
help="initialize the papers directory")
|
||||||
|
parser.add_argument('-p', '--path', default=None,
|
||||||
|
help='path to papers directory (if none, ~/.papers is used)')
|
||||||
|
parser.add_argument('-d', '--doc-dir', default=None,
|
||||||
|
help=('path to document directory '
|
||||||
|
'(if none, documents are stored in the same directory)'))
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(config, ui):
|
def command(config, ui, path, doc_dir):
|
||||||
"""Create a .papers directory"""
|
"""Create a .papers directory"""
|
||||||
papersdir = os.getcwd() + '/.papers'
|
if path is None:
|
||||||
|
papersdir = configs.DEFAULT_PAPER_PATH
|
||||||
|
else:
|
||||||
|
papersdir = os.path.join(os.getcwd(), path)
|
||||||
if not os.path.exists(papersdir):
|
if not os.path.exists(papersdir):
|
||||||
print('Initializing papers in {}'.format(
|
ui.print_('Initializing papers in {}.'.format(
|
||||||
colored(papersdir, 'filepath')))
|
ui.colored(papersdir, 'filepath')))
|
||||||
repo = Repository()
|
repo = Repository()
|
||||||
repo.init(papersdir) # Creates directories
|
repo.init(papersdir) # Creates directories
|
||||||
repo.save() # Saves empty repository description
|
repo.save() # Saves empty repository description
|
||||||
else:
|
else:
|
||||||
ui.error('papers already present in {}.'.format(
|
ui.error('papers already present in {}.'.format(
|
||||||
colored(papersdir, 'filepath')))
|
ui.colored(papersdir, 'filepath')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
@ -8,7 +8,7 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
|
|
||||||
def command(config, ui):
|
def command(config, ui):
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
articles = []
|
articles = []
|
||||||
for n, p in enumerate(rp.all_papers()):
|
for n, p in enumerate(rp.all_papers()):
|
||||||
bibdesc = pretty.bib_oneliner(p.bibentry, color=ui.color)
|
bibdesc = pretty.bib_oneliner(p.bibentry, color=ui.color)
|
||||||
|
@ -14,7 +14,7 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
|
|
||||||
def command(config, ui, citekey):
|
def command(config, ui, citekey):
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
paper = rp.paper_from_ref(citekey, fatal=True)
|
paper = rp.paper_from_ref(citekey, fatal=True)
|
||||||
try:
|
try:
|
||||||
filepath = paper.get_document_path()
|
filepath = paper.get_document_path()
|
||||||
|
@ -9,7 +9,7 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
|
|
||||||
def command(config, ui, reference):
|
def command(config, ui, reference):
|
||||||
rp = repo.Repository.from_directory()
|
rp = repo.Repository.from_directory(config)
|
||||||
key = rp.citekey_from_ref(reference, fatal=True)
|
key = rp.citekey_from_ref(reference, fatal=True)
|
||||||
paper = rp.paper_from_citekey(key)
|
paper = rp.paper_from_citekey(key)
|
||||||
are_you_sure = ("Are you sure you want to delete paper [%s]"
|
are_you_sure = ("Are you sure you want to delete paper [%s]"
|
||||||
|
@ -2,6 +2,7 @@ import os
|
|||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers')
|
||||||
DEFAULT_OPEN_CMD = 'open'
|
DEFAULT_OPEN_CMD = 'open'
|
||||||
try:
|
try:
|
||||||
DEFAULT_EDIT_CMD = os.environ['EDITOR']
|
DEFAULT_EDIT_CMD = os.environ['EDITOR']
|
||||||
@ -14,6 +15,7 @@ DEFAULT_COLOR = 'yes'
|
|||||||
|
|
||||||
|
|
||||||
CONFIG = ConfigParser.SafeConfigParser({
|
CONFIG = ConfigParser.SafeConfigParser({
|
||||||
|
'papers-directory': DEFAULT_PAPERS_DIRECTORY,
|
||||||
'open-cmd': DEFAULT_OPEN_CMD,
|
'open-cmd': DEFAULT_OPEN_CMD,
|
||||||
'edit-cmd': DEFAULT_EDIT_CMD,
|
'edit-cmd': DEFAULT_EDIT_CMD,
|
||||||
'import-copy': DEFAULT_IMPORT_COPY,
|
'import-copy': DEFAULT_IMPORT_COPY,
|
||||||
|
@ -43,30 +43,6 @@ def clean_path(path):
|
|||||||
return os.path.abspath(os.path.expanduser(path))
|
return os.path.abspath(os.path.expanduser(path))
|
||||||
|
|
||||||
|
|
||||||
def find_papersdir():
|
|
||||||
"""Find .papers directory in this directory and the parent directories"""
|
|
||||||
global _papersdir
|
|
||||||
if _papersdir is None:
|
|
||||||
curdir = os.path.abspath('')
|
|
||||||
while curdir != '':
|
|
||||||
curdir_path = os.path.join(clean_path(curdir), '.papers')
|
|
||||||
if (os.path.exists(curdir_path) and os.path.isdir(curdir_path)):
|
|
||||||
_papersdir = curdir + '/.papers'
|
|
||||||
curdir = ''
|
|
||||||
if curdir == '/':
|
|
||||||
curdir = '~'
|
|
||||||
elif curdir == '~':
|
|
||||||
curdir = ''
|
|
||||||
else:
|
|
||||||
curdir = os.path.split(curdir)[0]
|
|
||||||
if _papersdir is None:
|
|
||||||
print (colored('error', 'error')
|
|
||||||
+ ': no papers repo found in this directory or in '
|
|
||||||
'any parent directory.')
|
|
||||||
exit(-1)
|
|
||||||
return _papersdir
|
|
||||||
|
|
||||||
|
|
||||||
def name_from_path(fullpdfpath, verbose=False):
|
def name_from_path(fullpdfpath, verbose=False):
|
||||||
name, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
name, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
||||||
if verbose:
|
if verbose:
|
||||||
|
@ -204,10 +204,10 @@ class Repository(object):
|
|||||||
shutil.copy(doc_file, new_doc_file)
|
shutil.copy(doc_file, new_doc_file)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_directory(cls, papersdir=None):
|
def from_directory(cls, config, papersdir=None):
|
||||||
repo = cls()
|
repo = cls(config=config)
|
||||||
if papersdir is None:
|
if papersdir is None:
|
||||||
papersdir = files.find_papersdir()
|
papersdir = config.get('papers', 'papers-directory')
|
||||||
repo.papersdir = files.clean_path(papersdir)
|
repo.papersdir = files.clean_path(papersdir)
|
||||||
repo.load()
|
repo.load()
|
||||||
return repo
|
return repo
|
||||||
|
Loading…
x
Reference in New Issue
Block a user