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
|
import sys
|
||||||
from ConfigParser import NoOptionError
|
from ConfigParser import NoOptionError
|
||||||
|
|
||||||
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
class UserError(Exception):
|
class UserError(Exception):
|
||||||
"""UI exception. Commands should throw this in order to display
|
"""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."""
|
"""Tries to guess the encoding used by the terminal."""
|
||||||
# Configured override?
|
# Configured override?
|
||||||
try:
|
try:
|
||||||
return config.get('papers', 'terminal-encoding')
|
return config.get(configs.MAIN_SECTION, 'terminal-encoding')
|
||||||
except NoOptionError:
|
except NoOptionError:
|
||||||
# Determine from locale settings.
|
# Determine from locale settings.
|
||||||
try:
|
try:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from .. import repo
|
from .. import repo
|
||||||
from .. import files
|
from .. import files
|
||||||
from ..paper import Paper, NoDocumentFile
|
from ..paper import Paper, NoDocumentFile
|
||||||
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
def add_paper_with_docfile(repo, paper, docfile=None, copy=False):
|
def add_paper_with_docfile(repo, paper, docfile=None, copy=False):
|
||||||
@ -42,7 +43,7 @@ def command(config, ui, bibfile, docfile, copy):
|
|||||||
:param docfile: path (no url yet) to a pdf or ps file
|
:param docfile: path (no url yet) to a pdf or ps file
|
||||||
"""
|
"""
|
||||||
if copy is None:
|
if copy is None:
|
||||||
copy = config.get('papers', 'import-copy')
|
copy = config.get(configs.MAIN_SECTION, 'import-copy')
|
||||||
rp = repo.Repository.from_directory(config)
|
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
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from ..files import editor_input
|
from ..files import editor_input
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import get_bibentry_from_string, get_safe_metadata_from_content
|
from ..paper import get_bibentry_from_string, get_safe_metadata_from_content
|
||||||
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -21,7 +22,7 @@ def command(config, ui, reference, meta):
|
|||||||
if meta:
|
if meta:
|
||||||
to_edit = 'meta'
|
to_edit = 'meta'
|
||||||
filepath = rp.path_to_paper_file(key, to_edit)
|
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:
|
with open(filepath) as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
while True:
|
while True:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import Paper
|
from ..paper import Paper
|
||||||
from add_cmd import add_paper_with_docfile, extract_doc_path_from_bibdata
|
from add_cmd import add_paper_with_docfile, extract_doc_path_from_bibdata
|
||||||
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -20,7 +21,7 @@ def command(config, ui, bibpath, copy):
|
|||||||
:param bibpath: path (no url yet) to a bibliography file
|
:param bibpath: path (no url yet) to a bibliography file
|
||||||
"""
|
"""
|
||||||
if copy is None:
|
if copy is None:
|
||||||
copy = config.get('papers', 'import-copy')
|
copy = config.get(configs.MAIN_SECTION, 'import-copy')
|
||||||
rp = repo.Repository.from_directory(config)
|
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)
|
||||||
|
@ -3,6 +3,7 @@ import subprocess
|
|||||||
from ..color import colored
|
from ..color import colored
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import NoDocumentFile
|
from ..paper import NoDocumentFile
|
||||||
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -18,7 +19,7 @@ def command(config, ui, citekey):
|
|||||||
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()
|
||||||
subprocess.Popen([config.get('papers', 'open-cmd'), filepath])
|
subprocess.Popen([config.get(configs.MAIN_SECTION, 'open-cmd'), filepath])
|
||||||
print("%s opened." % colored(filepath, 'filepath'))
|
print("%s opened." % colored(filepath, 'filepath'))
|
||||||
except NoDocumentFile:
|
except NoDocumentFile:
|
||||||
print("%s: No document associated to this entry %s."
|
print("%s: No document associated to this entry %s."
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
MAIN_SECTION = 'papers'
|
||||||
CONFIG_PATH = os.path.expanduser('~/.papersrc')
|
CONFIG_PATH = os.path.expanduser('~/.papersrc')
|
||||||
DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers')
|
DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers')
|
||||||
DEFAULT_OPEN_CMD = 'open'
|
DEFAULT_OPEN_CMD = 'open'
|
||||||
@ -12,7 +14,7 @@ except KeyError:
|
|||||||
DEFAULT_IMPORT_COPY = 'yes'
|
DEFAULT_IMPORT_COPY = 'yes'
|
||||||
DEFAULT_IMPORT_MOVE = 'no'
|
DEFAULT_IMPORT_MOVE = 'no'
|
||||||
DEFAULT_COLOR = 'yes'
|
DEFAULT_COLOR = 'yes'
|
||||||
|
DEFAULT_PLUGINS = ''
|
||||||
|
|
||||||
CONFIG = ConfigParser.SafeConfigParser({
|
CONFIG = ConfigParser.SafeConfigParser({
|
||||||
'papers-directory': DEFAULT_PAPERS_DIRECTORY,
|
'papers-directory': DEFAULT_PAPERS_DIRECTORY,
|
||||||
@ -20,8 +22,9 @@ CONFIG = ConfigParser.SafeConfigParser({
|
|||||||
'edit-cmd': DEFAULT_EDIT_CMD,
|
'edit-cmd': DEFAULT_EDIT_CMD,
|
||||||
'import-copy': DEFAULT_IMPORT_COPY,
|
'import-copy': DEFAULT_IMPORT_COPY,
|
||||||
'import-move': DEFAULT_IMPORT_MOVE,
|
'import-move': DEFAULT_IMPORT_MOVE,
|
||||||
'color': DEFAULT_COLOR})
|
'color': DEFAULT_COLOR,
|
||||||
CONFIG.add_section('papers')
|
'plugins': DEFAULT_PLUGINS})
|
||||||
|
CONFIG.add_section(MAIN_SECTION)
|
||||||
|
|
||||||
|
|
||||||
def read_config():
|
def read_config():
|
||||||
@ -42,6 +45,10 @@ def add_and_write_option(section, option, value):
|
|||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
def get_plugins(cfg):
|
||||||
|
return cfg.get(MAIN_SECTION, 'plugins').split()
|
||||||
|
|
||||||
|
|
||||||
def get_boolean(value, default):
|
def get_boolean(value, default):
|
||||||
value = str(value).lower()
|
value = str(value).lower()
|
||||||
if value in ('yes', 'true', 't', 'y', '1'):
|
if value in ('yes', 'true', 't', 'y', '1'):
|
||||||
|
@ -25,6 +25,14 @@ cmds = collections.OrderedDict([
|
|||||||
config = configs.read_config()
|
config = configs.read_config()
|
||||||
ui = UI(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")
|
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")
|
||||||
|
|
||||||
|
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 shutil
|
||||||
import glob
|
import glob
|
||||||
|
|
||||||
import files
|
from . import files
|
||||||
from paper import PaperInRepo, NoDocumentFile
|
from .paper import PaperInRepo, NoDocumentFile
|
||||||
from color import colored
|
from .color import colored
|
||||||
import configs
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
ALPHABET = 'abcdefghijklmopqrstuvwxyz'
|
ALPHABET = 'abcdefghijklmopqrstuvwxyz'
|
||||||
@ -173,8 +173,8 @@ class Repository(object):
|
|||||||
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):
|
def get_document_directory(self):
|
||||||
if self.config.has_option('papers', 'document-directory'):
|
if self.config.has_option(configs.MAIN_SECTION, 'document-directory'):
|
||||||
doc_dir = self.config.get('papers', 'document-directory')
|
doc_dir = self.config.get(configs.MAIN_SECTION, 'document-directory')
|
||||||
else:
|
else:
|
||||||
doc_dir = os.path.join(self.papersdir, DOC_DIR)
|
doc_dir = os.path.join(self.papersdir, DOC_DIR)
|
||||||
return files.clean_path(doc_dir)
|
return files.clean_path(doc_dir)
|
||||||
@ -207,7 +207,7 @@ class Repository(object):
|
|||||||
def from_directory(cls, config, papersdir=None):
|
def from_directory(cls, config, papersdir=None):
|
||||||
repo = cls(config=config)
|
repo = cls(config=config)
|
||||||
if papersdir is None:
|
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.papersdir = files.clean_path(papersdir)
|
||||||
repo.load()
|
repo.load()
|
||||||
return repo
|
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:
|
class UI:
|
||||||
@ -9,7 +10,7 @@ class UI:
|
|||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.encoding = _encoding(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):
|
def colored(self, s, *args, **kwargs):
|
||||||
if self.color:
|
if self.color:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user