From 84fd5d76493f3d74789340c67519e1b022f11697 Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Sun, 20 Apr 2014 02:41:20 +0200 Subject: [PATCH] fix #17 --- pubs/commands/init_cmd.py | 2 +- pubs/configs.py | 15 ++++++++++++--- pubs/content.py | 2 +- pubs/pubs_cmd.py | 7 ++++++- tests/test_databroker.py | 4 ++-- tests/test_usecase.py | 19 +++++++++++++------ 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/pubs/commands/init_cmd.py b/pubs/commands/init_cmd.py index b6aa0ee..5b55121 100644 --- a/pubs/commands/init_cmd.py +++ b/pubs/commands/init_cmd.py @@ -37,7 +37,7 @@ def command(args): color.dye(pubsdir, color.filepath))) ui.exit() - ui.print_('Initializing pubs in {}.'.format( + ui.print_('Initializing pubs in {}'.format( color.dye(pubsdir, color.filepath))) config().pubsdir = pubsdir diff --git a/pubs/configs.py b/pubs/configs.py index 9724f92..bfb51dc 100644 --- a/pubs/configs.py +++ b/pubs/configs.py @@ -2,7 +2,10 @@ import os import collections from .p3 import configparser -from .content import system_path +from . import content + +from .content import system_path, check_file + # constant stuff (DFT = DEFAULT) @@ -61,11 +64,17 @@ class Config(object): _config = self def load(self, path=DFT_CONFIG_PATH): - self._cfg.read(path) + if not content.check_file(path, fail=False): + raise IOError(("The configuration file {} does not exist." + " Did you run 'pubs init' ?").format(path)) + with open(content.system_path(path), 'r') as f: + read = self._cfg.readfp(f) + # if len(read) == 0: + # raise IOError("Syntax error in {} config file. Aborting.".format(path)) return self def save(self, path=DFT_CONFIG_PATH): - with open(system_path(path), 'w') as f: + with open(content.system_path(path), 'w') as f: self._cfg.write(f) def __setattr__(self, name, value): diff --git a/pubs/content.py b/pubs/content.py index f113f9b..621ab32 100644 --- a/pubs/content.py +++ b/pubs/content.py @@ -11,7 +11,7 @@ from .p3 import urlparse, HTTPConnection, urlopen def _check_system_path_exists(path, fail=True): answer = os.path.exists(path) if not answer and fail: - raise IOError("File does not exist: {}.".format(path)) + raise IOError("File does not exist: {}".format(path)) else: return answer diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 7be1dbd..0fba8ff 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -58,7 +58,12 @@ def _update_check(config, ui): def execute(raw_args=sys.argv): # loading config config = configs.Config() - config.load() + if raw_args[1] != 'init': + try: + config.load() + except IOError as e: + print('error: {}'.format(str(e))) + sys.exit() config.as_global() uis.init_ui(config) diff --git a/tests/test_databroker.py b/tests/test_databroker.py index c83190c..0e50367 100644 --- a/tests/test_databroker.py +++ b/tests/test_databroker.py @@ -5,7 +5,7 @@ import os import dotdot import fake_env -from pubs import content, filebroker, databroker, datacache +from pubs import content, filebroker, databroker, datacache, configs import str_fixtures from pubs import endecoder @@ -20,7 +20,7 @@ class TestDataBroker(unittest.TestCase): page99_bibdata = ende.decode_bibdata(str_fixtures.bibtex_raw0) for db_class in [databroker.DataBroker, datacache.DataCache]: - self.fs = fake_env.create_fake_fs([content, filebroker]) + self.fs = fake_env.create_fake_fs([content, filebroker, configs]) db = db_class('tmp', create=True) diff --git a/tests/test_usecase.py b/tests/test_usecase.py index bf60fe6..5032ffb 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -7,14 +7,19 @@ import dotdot import fake_env from pubs import pubs_cmd -from pubs import color, content, filebroker, uis, beets_ui, p3, endecoder +from pubs import color, content, filebroker, uis, beets_ui, p3, endecoder, configs import str_fixtures import fixtures - from pubs.commands import init_cmd, import_cmd + +# makes the tests very noisy +PRINT_OUTPUT=False +CAPTURE_OUTPUT=True + + # code for fake fs class TestFakeInput(unittest.TestCase): @@ -50,9 +55,9 @@ class CommandTestCase(unittest.TestCase): maxDiff = None def setUp(self): - self.fs = fake_env.create_fake_fs([content, filebroker, init_cmd, import_cmd]) + self.fs = fake_env.create_fake_fs([content, filebroker, configs, init_cmd, import_cmd]) - def execute_cmds(self, cmds, fs=None, capture_output=True): + def execute_cmds(self, cmds, fs=None, capture_output=CAPTURE_OUTPUT): """ Execute a list of commands, and capture their output A command can be a string, or a tuple of size 2 or 3. @@ -88,10 +93,12 @@ class CommandTestCase(unittest.TestCase): if capture_output: assert(stderr.getvalue() == '') outs.append(color.undye(stdout.getvalue())) + if PRINT_OUTPUT: + print(outs) return outs def tearDown(self): - fake_env.unset_fake_fs([content, filebroker]) + fake_env.unset_fake_fs([content, filebroker, configs, init_cmd, import_cmd]) class DataCommandTestCase(CommandTestCase): @@ -206,7 +213,7 @@ class TestList(DataCommandTestCase): class TestUsecase(DataCommandTestCase): def test_first(self): - correct = ['Initializing pubs in /paper_first.\n', + correct = ['Initializing pubs in /paper_first\n', '', '[Page99] Page, Lawrence et al. "The PageRank Citation Ranking: Bringing Order to the Web." (1999) \n', '',