From f528aa8a67062a80dbc6fe5445a9652df90dbb81 Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Tue, 2 Jul 2013 14:05:32 +0100 Subject: [PATCH] updated core. almost all tests are passing --- papers/events.py | 3 +-- papers/papers_cmd.py | 11 +++++++---- papers/plugin.py | 16 ++++++++-------- papers/plugs/texnote/texnote.py | 24 +++++++++++------------- papers/ui.py | 5 +---- 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/papers/events.py b/papers/events.py index efd94d6..29771a6 100644 --- a/papers/events.py +++ b/papers/events.py @@ -28,7 +28,6 @@ class Event(object): class RemoveEvent(Event): - def __init__(self, config, ui, citekey): - self.config = config + def __init__(self, ui, citekey): self.ui = ui self.citekey = citekey diff --git a/papers/papers_cmd.py b/papers/papers_cmd.py index eb59bff..dfa93fa 100644 --- a/papers/papers_cmd.py +++ b/papers/papers_cmd.py @@ -28,11 +28,15 @@ cmds = collections.OrderedDict([ def execute(raw_args = sys.argv): - config = configs.read_config() + # loading config + config = configs.Config() + config.load() + config.as_global() + ui = UI(config) # Extend with plugin commands - plugin.load_plugins(config, ui, configs.get_plugins(config)) + plugin.load_plugins(ui, config.plugins.split()) for p in plugin.get_plugins().values(): cmds.update(collections.OrderedDict([(p.name, p)])) @@ -40,10 +44,9 @@ def execute(raw_args = sys.argv): subparsers = parser.add_subparsers(title="valid commands", dest="command") for cmd_mod in cmds.values(): - subparser = cmd_mod.parser(subparsers, config) # why do we return the subparser ? + subparser = cmd_mod.parser(subparsers) # why do we return the subparser ? args = parser.parse_args(raw_args[1:]) - args.config = config args.ui = ui cmd = args.command diff --git a/papers/plugin.py b/papers/plugin.py index 7794e3f..f5479a6 100644 --- a/papers/plugin.py +++ b/papers/plugin.py @@ -1,4 +1,5 @@ import importlib +from .configs import config PLUGIN_NAMESPACE = 'plugs' @@ -11,20 +12,19 @@ class PapersPlugin(object): functionality by defining a subclass of PapersPlugin and overriding the abstract methods defined here. """ - def __init__(self, config, ui): + def __init__(self, ui): """Perform one-time plugin setup. """ self.name = self.__module__.split('.')[-1] - self.config = config self.ui = ui - #config and ui and given again to stay consistent with the core papers cmd. + #ui and given again to stay consistent with the core papers cmd. #two options: #- create specific cases in script papers/papers - #- do not store self.config and self.ui and use them if needed when command is called + #- do not store self.ui and use them if needed when command is called #this may end up with a lot of function with config/ui in argument #or just keep it that way... - def parser(self, subparsers, config): + def parser(self, subparsers): """ Should retrun the parser with plugins specific command. This is a basic example """ @@ -32,7 +32,7 @@ class PapersPlugin(object): parser.add_argument('strings', nargs='*', help='the strings') return parser - def command(self, config, ui, strings): + def command(self, ui, strings): """This function will be called with argument defined in the parser above This is a basic example """ @@ -47,7 +47,7 @@ class PapersPlugin(object): raise RuntimeError("{} instance not created".format(cls.__name__)) -def load_plugins(config, ui, names): +def load_plugins(ui, names): """Imports the modules for a sequence of plugin names. Each name must be the name of a Python module under the "PLUGIN_NAMESPACE" namespace package in sys.path; the module indicated should contain the @@ -69,7 +69,7 @@ def load_plugins(config, ui, names): if isinstance(obj, type) and issubclass(obj, PapersPlugin) \ and obj != PapersPlugin: _classes.append(obj) - _instances[obj] = obj(config, ui) + _instances[obj] = obj(ui) except: ui.warning('error loading plugin {}'.format(name)) diff --git a/papers/plugs/texnote/texnote.py b/papers/plugs/texnote/texnote.py index d550156..bd33326 100644 --- a/papers/plugs/texnote/texnote.py +++ b/papers/plugs/texnote/texnote.py @@ -3,7 +3,7 @@ import shutil import subprocess from ... import repo -from ... import configs +from ...configs import config from ... import files from ...plugin import PapersPlugin from ...commands.helpers import add_references_argument, parse_reference @@ -18,7 +18,7 @@ TEXNOTE_DIR = 'texnote' class TexnotePlugin(PapersPlugin): - def parser(self, subparsers, config): + def parser(self, subparsers): parser = subparsers.add_parser(self.name, help="edit advance note in latex") sub = parser.add_subparsers(title="valid texnote commands", dest="texcmd") p = sub.add_parser("remove", help="remove a reference") @@ -29,11 +29,11 @@ class TexnotePlugin(PapersPlugin): parser.add_argument('-v', '--view', action='store_true', help='open the paper in a pdf viewer', default=None) return parser - def command(self, config, ui, texcmd, reference, view): + def command(self, ui, texcmd, reference, view): if view is not None: subprocess.Popen(['papers', 'open', reference]) if texcmd == 'edit': - open_texnote(config, ui, reference) + open_texnote(ui, reference) def toto(self): print "toto" @@ -47,7 +47,8 @@ class TexnotePlugin(PapersPlugin): def remove(rmevent): texplug = TexnotePlugin.get_instance() texplug.toto() - rp = repo.Repository.from_directory(rmevent.config) + # HACK : transfer repo via RemoveEvent, do not recreate one + rp = repo.Repository(config()) paper = rp.get_paper(parse_reference(rmevent.ui, rp, rmevent.citekey)) if 'texnote' in paper.metadata: try: @@ -59,8 +60,9 @@ def remove(rmevent): files.save_meta(paper.metadata, metapath) -def open_texnote(config, ui, ref): - rp = repo.Repository.from_directory(config) +def open_texnote(ui, ref): + # HACK : transfer repo via arguments, do not recreate one + rp = repo.Repository(config()) paper = rp.get_paper(parse_reference(ui, rp, ref)) #ugly to recode like for the doc field @@ -83,12 +85,8 @@ def open_texnote(config, ui, ref): autofill_texnote(texnote_path, paper.bibentry) #open the file using the config editor - if config.has_option(TEXNOTE_SECTION, 'edit-cmd'): - #os.system(config.get(TEXNOTE_SECTION, 'edit-cmd') + ' ' + texnote_path + " &") - subprocess.Popen([config.get(TEXNOTE_SECTION, 'edit-cmd'), texnote_path]) - else: - #os.system(config.get(configs.MAIN_SECTION, 'edit-cmd') + ' ' + texnote_path + " &") - subprocess.Popen([config.get(configs.MAIN_SECTION, 'edit-cmd'), texnote_path]) + edit_cmd = config(TEXTNOTE_SECTION).get('edit_cmd', config().edit_cmd) + subprocess.Popen([edit_cmd, texnote_path]) ##### ugly replace by proper ##### diff --git a/papers/ui.py b/papers/ui.py index 50cd382..fa6c1a4 100644 --- a/papers/ui.py +++ b/papers/ui.py @@ -3,8 +3,6 @@ import sys from .beets_ui import _encoding, input_ from . import color -from . import configs - class UI: """UI class. Stores configuration parameters and system information. @@ -12,8 +10,7 @@ class UI: def __init__(self, config): self.encoding = _encoding(config) - color_enable = configs.get_boolean(config.get('papers', 'color')) - color.setup(color_enable) + color.setup(config.color) def print_(self, *strings): """Like print, but rather than raising an error when a character