Adds plugin capability and call MAIN_SECTION to get configs.
This commit is contained in:
parent
9e02ab0db2
commit
d27e5457ef
@ -19,6 +19,8 @@ import locale
|
||||
import sys
|
||||
from ConfigParser import NoOptionError
|
||||
|
||||
from . import configs
|
||||
|
||||
|
||||
class UserError(Exception):
|
||||
"""UI exception. Commands should throw this in order to display
|
||||
@ -31,7 +33,7 @@ def _encoding(config):
|
||||
"""Tries to guess the encoding used by the terminal."""
|
||||
# Configured override?
|
||||
try:
|
||||
return config.get('papers', 'terminal-encoding')
|
||||
return config.get(configs.MAIN_SECTION, 'terminal-encoding')
|
||||
except NoOptionError:
|
||||
# Determine from locale settings.
|
||||
try:
|
||||
|
@ -1,6 +1,7 @@
|
||||
from .. import repo
|
||||
from .. import files
|
||||
from ..paper import Paper, NoDocumentFile
|
||||
from . import configs
|
||||
|
||||
|
||||
def add_paper_with_docfile(repo, paper, docfile=None, copy=False):
|
||||
@ -20,7 +21,7 @@ def extract_doc_path_from_bibdata(paper, ui):
|
||||
return file_path
|
||||
else:
|
||||
ui.warning("File does not exist for %s (%s)."
|
||||
% (paper.citekey, file_path))
|
||||
% (paper.citekey, file_path))
|
||||
except NoDocumentFile:
|
||||
return None
|
||||
|
||||
@ -42,7 +43,7 @@ def command(config, ui, bibfile, docfile, copy):
|
||||
:param docfile: path (no url yet) to a pdf or ps file
|
||||
"""
|
||||
if copy is None:
|
||||
copy = config.get('papers', 'import-copy')
|
||||
copy = config.get(configs.MAIN_SECTION, 'import-copy')
|
||||
rp = repo.Repository.from_directory(config)
|
||||
p = Paper.load(bibfile)
|
||||
# Check if another doc file is specified in bibtex
|
||||
|
@ -1,6 +1,7 @@
|
||||
from ..files import editor_input
|
||||
from .. import repo
|
||||
from ..paper import get_bibentry_from_string, get_safe_metadata_from_content
|
||||
from . import configs
|
||||
|
||||
|
||||
def parser(subparsers, config):
|
||||
@ -21,7 +22,7 @@ def command(config, ui, reference, meta):
|
||||
if meta:
|
||||
to_edit = 'meta'
|
||||
filepath = rp.path_to_paper_file(key, to_edit)
|
||||
editor = config.get('papers', 'edit-cmd')
|
||||
editor = config.get(configs.MAIN_SECTION, 'edit-cmd')
|
||||
with open(filepath) as f:
|
||||
content = f.read()
|
||||
while True:
|
||||
|
@ -1,6 +1,7 @@
|
||||
from .. import repo
|
||||
from ..paper import Paper
|
||||
from add_cmd import add_paper_with_docfile, extract_doc_path_from_bibdata
|
||||
from . import configs
|
||||
|
||||
|
||||
def parser(subparsers, config):
|
||||
@ -20,7 +21,7 @@ def command(config, ui, bibpath, copy):
|
||||
:param bibpath: path (no url yet) to a bibliography file
|
||||
"""
|
||||
if copy is None:
|
||||
copy = config.get('papers', 'import-copy')
|
||||
copy = config.get(configs.MAIN_SECTION, 'import-copy')
|
||||
rp = repo.Repository.from_directory(config)
|
||||
# Extract papers from bib
|
||||
papers = Paper.many_from_path(bibpath, fatal=False)
|
||||
|
@ -3,6 +3,7 @@ import subprocess
|
||||
from ..color import colored
|
||||
from .. import repo
|
||||
from ..paper import NoDocumentFile
|
||||
from . import configs
|
||||
|
||||
|
||||
def parser(subparsers, config):
|
||||
@ -18,7 +19,7 @@ def command(config, ui, citekey):
|
||||
paper = rp.paper_from_ref(citekey, fatal=True)
|
||||
try:
|
||||
filepath = paper.get_document_path()
|
||||
subprocess.Popen([config.get('papers', 'open-cmd'), filepath])
|
||||
subprocess.Popen([config.get(configs.MAIN_SECTION, 'open-cmd'), filepath])
|
||||
print("%s opened." % colored(filepath, 'filepath'))
|
||||
except NoDocumentFile:
|
||||
print("%s: No document associated to this entry %s."
|
||||
|
@ -1,6 +1,8 @@
|
||||
import os
|
||||
import ConfigParser
|
||||
|
||||
|
||||
MAIN_SECTION = 'papers'
|
||||
CONFIG_PATH = os.path.expanduser('~/.papersrc')
|
||||
DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers')
|
||||
DEFAULT_OPEN_CMD = 'open'
|
||||
@ -12,7 +14,7 @@ except KeyError:
|
||||
DEFAULT_IMPORT_COPY = 'yes'
|
||||
DEFAULT_IMPORT_MOVE = 'no'
|
||||
DEFAULT_COLOR = 'yes'
|
||||
|
||||
DEFAULT_PLUGINS = ''
|
||||
|
||||
CONFIG = ConfigParser.SafeConfigParser({
|
||||
'papers-directory': DEFAULT_PAPERS_DIRECTORY,
|
||||
@ -20,8 +22,9 @@ CONFIG = ConfigParser.SafeConfigParser({
|
||||
'edit-cmd': DEFAULT_EDIT_CMD,
|
||||
'import-copy': DEFAULT_IMPORT_COPY,
|
||||
'import-move': DEFAULT_IMPORT_MOVE,
|
||||
'color': DEFAULT_COLOR})
|
||||
CONFIG.add_section('papers')
|
||||
'color': DEFAULT_COLOR,
|
||||
'plugins': DEFAULT_PLUGINS})
|
||||
CONFIG.add_section(MAIN_SECTION)
|
||||
|
||||
|
||||
def read_config():
|
||||
@ -42,6 +45,10 @@ def add_and_write_option(section, option, value):
|
||||
f.close()
|
||||
|
||||
|
||||
def get_plugins(cfg):
|
||||
return cfg.get(MAIN_SECTION, 'plugins').split()
|
||||
|
||||
|
||||
def get_boolean(value, default):
|
||||
value = str(value).lower()
|
||||
if value in ('yes', 'true', 't', 'y', '1'):
|
||||
|
@ -25,6 +25,14 @@ cmds = collections.OrderedDict([
|
||||
config = configs.read_config()
|
||||
ui = UI(config)
|
||||
|
||||
# Extend with plugin commands
|
||||
plugs = configs.get_plugins(config)
|
||||
for plugname in plugs:
|
||||
module_name = 'papers.plugins.' + plugname + '.' + plugname + '_cmd'
|
||||
plug = __import__(module_name, globals(), locals(),
|
||||
['parser', 'command'], -1)
|
||||
cmds.update(collections.OrderedDict([(plugname, plug)]))
|
||||
|
||||
parser = argparse.ArgumentParser(description="research papers repository")
|
||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||
|
||||
|
0
papers/plugins/__init__.py
Normal file
0
papers/plugins/__init__.py
Normal file
0
papers/plugins/texnote/__init__.py
Normal file
0
papers/plugins/texnote/__init__.py
Normal file
11
papers/plugins/texnote/texnote_cmd.py
Normal file
11
papers/plugins/texnote/texnote_cmd.py
Normal file
@ -0,0 +1,11 @@
|
||||
#from ... import pretty
|
||||
#from ... import repo
|
||||
|
||||
|
||||
def parser(subparsers, config):
|
||||
parser = subparsers.add_parser('texnote', help="edit advance note in latex")
|
||||
return parser
|
||||
|
||||
|
||||
def command(config, ui):
|
||||
ui.print_('texnote test')
|
@ -2,10 +2,10 @@ import os
|
||||
import shutil
|
||||
import glob
|
||||
|
||||
import files
|
||||
from paper import PaperInRepo, NoDocumentFile
|
||||
from color import colored
|
||||
import configs
|
||||
from . import files
|
||||
from .paper import PaperInRepo, NoDocumentFile
|
||||
from .color import colored
|
||||
from . import configs
|
||||
|
||||
|
||||
ALPHABET = 'abcdefghijklmopqrstuvwxyz'
|
||||
@ -173,8 +173,8 @@ class Repository(object):
|
||||
raise(ValueError("%s is not a valid paper file." % file_))
|
||||
|
||||
def get_document_directory(self):
|
||||
if self.config.has_option('papers', 'document-directory'):
|
||||
doc_dir = self.config.get('papers', 'document-directory')
|
||||
if self.config.has_option(configs.MAIN_SECTION, 'document-directory'):
|
||||
doc_dir = self.config.get(configs.MAIN_SECTION, 'document-directory')
|
||||
else:
|
||||
doc_dir = os.path.join(self.papersdir, DOC_DIR)
|
||||
return files.clean_path(doc_dir)
|
||||
@ -207,7 +207,7 @@ class Repository(object):
|
||||
def from_directory(cls, config, papersdir=None):
|
||||
repo = cls(config=config)
|
||||
if papersdir is None:
|
||||
papersdir = config.get('papers', 'papers-directory')
|
||||
papersdir = config.get(configs.MAIN_SECTION, 'papers-directory')
|
||||
repo.papersdir = files.clean_path(papersdir)
|
||||
repo.load()
|
||||
return repo
|
||||
|
@ -1,6 +1,7 @@
|
||||
from beets_ui import _encoding, input_
|
||||
from .beets_ui import _encoding, input_
|
||||
|
||||
from color import colored
|
||||
from .color import colored
|
||||
from . import configs
|
||||
|
||||
|
||||
class UI:
|
||||
@ -9,7 +10,7 @@ class UI:
|
||||
|
||||
def __init__(self, config):
|
||||
self.encoding = _encoding(config)
|
||||
self.color = config.getboolean('papers', 'color')
|
||||
self.color = config.getboolean(configs.MAIN_SECTION, 'color')
|
||||
|
||||
def colored(self, s, *args, **kwargs):
|
||||
if self.color:
|
||||
@ -23,7 +24,7 @@ class UI:
|
||||
replaces it.
|
||||
"""
|
||||
txt = [s.encode(self.encoding, 'replace')
|
||||
if isinstance(s, unicode) else s
|
||||
if isinstance(s, unicode) else s
|
||||
for s in strings]
|
||||
print(' '.join(txt))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user