From 04eeedf8a4cfafd3bc1e88fe2c1fc9a8478afb60 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Wed, 5 Jun 2013 19:52:11 +0200 Subject: [PATCH] Switch to config based papers directory. TODO add papers dir to config on papers init command. --- papers/commands/add_cmd.py | 2 +- papers/commands/add_library_cmd.py | 2 +- papers/commands/edit_cmd.py | 2 +- papers/commands/export_cmd.py | 3 +-- papers/commands/import_cmd.py | 2 +- papers/commands/init_cmd.py | 22 +++++++++++++++------- papers/commands/list_cmd.py | 2 +- papers/commands/open_cmd.py | 2 +- papers/commands/remove_cmd.py | 2 +- papers/configs.py | 2 ++ papers/files.py | 24 ------------------------ papers/repo.py | 6 +++--- 12 files changed, 28 insertions(+), 43 deletions(-) diff --git a/papers/commands/add_cmd.py b/papers/commands/add_cmd.py index a77e4ad..c6b4bc8 100644 --- a/papers/commands/add_cmd.py +++ b/papers/commands/add_cmd.py @@ -43,7 +43,7 @@ def command(config, ui, bibfile, docfile, copy): """ if copy is None: copy = config.get('papers', 'import-copy') - rp = repo.Repository.from_directory() + rp = repo.Repository.from_directory(config) p = Paper.load(bibfile) # Check if another doc file is specified in bibtex docfile2 = extract_doc_path_from_bibdata(p, ui) diff --git a/papers/commands/add_library_cmd.py b/papers/commands/add_library_cmd.py index 645dd08..3161d50 100644 --- a/papers/commands/add_library_cmd.py +++ b/papers/commands/add_library_cmd.py @@ -13,6 +13,6 @@ def command(config, ui, bibfile): """ :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): rp.add_paper(p) diff --git a/papers/commands/edit_cmd.py b/papers/commands/edit_cmd.py index d711319..a7ce660 100644 --- a/papers/commands/edit_cmd.py +++ b/papers/commands/edit_cmd.py @@ -14,7 +14,7 @@ def parser(subparsers, config): 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) paper = rp.paper_from_citekey(key) to_edit = 'bib' diff --git a/papers/commands/export_cmd.py b/papers/commands/export_cmd.py index 3a97063..edf4ee0 100644 --- a/papers/commands/export_cmd.py +++ b/papers/commands/export_cmd.py @@ -1,6 +1,5 @@ import sys -import pybtex from pybtex.database import BibliographyData from .. import repo @@ -19,7 +18,7 @@ def command(config, ui, bib_format): """ :param bib_format (in 'bibtex', 'yaml') """ - rp = repo.Repository.from_directory() + rp = repo.Repository.from_directory(config) bib = BibliographyData() for p in rp.all_papers(): bib.add_entry(p.citekey, p.bibentry) diff --git a/papers/commands/import_cmd.py b/papers/commands/import_cmd.py index 4d17853..598a7d3 100644 --- a/papers/commands/import_cmd.py +++ b/papers/commands/import_cmd.py @@ -21,7 +21,7 @@ def command(config, ui, bibpath, copy): """ if copy is None: copy = config.get('papers', 'import-copy') - rp = repo.Repository.from_directory() + rp = repo.Repository.from_directory(config) # Extract papers from bib papers = Paper.many_from_path(bibpath, fatal=False) for p in papers: diff --git a/papers/commands/init_cmd.py b/papers/commands/init_cmd.py index e9455d7..dfe2a71 100644 --- a/papers/commands/init_cmd.py +++ b/papers/commands/init_cmd.py @@ -3,25 +3,33 @@ import os from ..repo import Repository -from ..color import colored +from .. import configs def parser(subparsers, config): 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 -def command(config, ui): +def command(config, ui, path, doc_dir): """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): - print('Initializing papers in {}'.format( - colored(papersdir, 'filepath'))) + ui.print_('Initializing papers in {}.'.format( + ui.colored(papersdir, 'filepath'))) repo = Repository() repo.init(papersdir) # Creates directories repo.save() # Saves empty repository description else: ui.error('papers already present in {}.'.format( - colored(papersdir, 'filepath'))) + ui.colored(papersdir, 'filepath'))) exit(-1) diff --git a/papers/commands/list_cmd.py b/papers/commands/list_cmd.py index 50cff68..d2148a9 100644 --- a/papers/commands/list_cmd.py +++ b/papers/commands/list_cmd.py @@ -8,7 +8,7 @@ def parser(subparsers, config): def command(config, ui): - rp = repo.Repository.from_directory() + rp = repo.Repository.from_directory(config) articles = [] for n, p in enumerate(rp.all_papers()): bibdesc = pretty.bib_oneliner(p.bibentry, color=ui.color) diff --git a/papers/commands/open_cmd.py b/papers/commands/open_cmd.py index d269d00..37de93e 100644 --- a/papers/commands/open_cmd.py +++ b/papers/commands/open_cmd.py @@ -14,7 +14,7 @@ def parser(subparsers, config): def command(config, ui, citekey): - rp = repo.Repository.from_directory() + rp = repo.Repository.from_directory(config) paper = rp.paper_from_ref(citekey, fatal=True) try: filepath = paper.get_document_path() diff --git a/papers/commands/remove_cmd.py b/papers/commands/remove_cmd.py index 7aa75d3..971ea5a 100644 --- a/papers/commands/remove_cmd.py +++ b/papers/commands/remove_cmd.py @@ -9,7 +9,7 @@ def parser(subparsers, config): def command(config, ui, reference): - rp = repo.Repository.from_directory() + rp = repo.Repository.from_directory(config) key = rp.citekey_from_ref(reference, fatal=True) paper = rp.paper_from_citekey(key) are_you_sure = ("Are you sure you want to delete paper [%s]" diff --git a/papers/configs.py b/papers/configs.py index 934458d..29d67f2 100644 --- a/papers/configs.py +++ b/papers/configs.py @@ -2,6 +2,7 @@ import os import ConfigParser +DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers') DEFAULT_OPEN_CMD = 'open' try: DEFAULT_EDIT_CMD = os.environ['EDITOR'] @@ -14,6 +15,7 @@ DEFAULT_COLOR = 'yes' CONFIG = ConfigParser.SafeConfigParser({ + 'papers-directory': DEFAULT_PAPERS_DIRECTORY, 'open-cmd': DEFAULT_OPEN_CMD, 'edit-cmd': DEFAULT_EDIT_CMD, 'import-copy': DEFAULT_IMPORT_COPY, diff --git a/papers/files.py b/papers/files.py index 6c65718..f672b7f 100644 --- a/papers/files.py +++ b/papers/files.py @@ -43,30 +43,6 @@ def clean_path(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): name, ext = os.path.splitext(os.path.split(fullpdfpath)[1]) if verbose: diff --git a/papers/repo.py b/papers/repo.py index 270b1cf..bc43bc0 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -204,10 +204,10 @@ class Repository(object): shutil.copy(doc_file, new_doc_file) @classmethod - def from_directory(cls, papersdir=None): - repo = cls() + def from_directory(cls, config, papersdir=None): + repo = cls(config=config) if papersdir is None: - papersdir = files.find_papersdir() + papersdir = config.get('papers', 'papers-directory') repo.papersdir = files.clean_path(papersdir) repo.load() return repo