diff options
author | Aurélien Bompard <aurelien@bompard.org> | 2013-07-31 16:28:17 +0200 |
---|---|---|
committer | Aurélien Bompard <aurelien@bompard.org> | 2013-07-31 16:29:17 +0200 |
commit | 6c2bc5b4925dec43d402713b4fdc464ba5c71677 (patch) | |
tree | 9d6c93366ef01da230857328dbfc38d4a47adb2c | |
parent | 6c34df90b8f8eac80a417b155f039f771b6d8520 (diff) | |
download | kittystore-6c2bc5b4925dec43d402713b4fdc464ba5c71677.tar.gz kittystore-6c2bc5b4925dec43d402713b4fdc464ba5c71677.tar.xz kittystore-6c2bc5b4925dec43d402713b4fdc464ba5c71677.zip |
Only accept a Django-like settings module to configure
-rw-r--r-- | kittystore/__init__.py | 16 | ||||
-rw-r--r-- | kittystore/import.py | 7 | ||||
-rw-r--r-- | kittystore/scripts.py | 46 | ||||
-rw-r--r-- | kittystore/storm/__init__.py | 10 | ||||
-rw-r--r-- | kittystore/storm/store.py | 3 | ||||
-rw-r--r-- | kittystore/test/__init__.py | 7 | ||||
-rw-r--r-- | kittystore/test/test_analysis.py | 4 | ||||
-rw-r--r-- | kittystore/test/test_storm_model.py | 4 | ||||
-rw-r--r-- | kittystore/test/test_storm_store.py | 4 | ||||
-rw-r--r-- | kittystore/test/test_storm_store_fetch.py | 4 |
10 files changed, 50 insertions, 55 deletions
diff --git a/kittystore/__init__.py b/kittystore/__init__.py index 6626a70..6210c95 100644 --- a/kittystore/__init__.py +++ b/kittystore/__init__.py @@ -18,17 +18,25 @@ license. __all__ = ("get_store", "MessageNotFound", ) -def get_store(url, search=None, debug=False): +def get_store(settings, debug=None): """Factory for a KittyStore subclass""" - if url.startswith("mongo://"): + required_keys = ("KITTYSTORE_URL", "KITTYSTORE_SEARCH_INDEX") + for req_key in required_keys: + try: + getattr(settings, req_key) + except AttributeError: + raise AttributeError("The settings file is missing the \"%s\" key" % req_key) + if debug is None: + debug = getattr(settings, "KITTYSTORE_DEBUG", False) + if settings.KITTYSTORE_URL.startswith("mongo://"): raise NotImplementedError #else: # from kittystore.sa import KittySAStore # return KittySAStore(url, debug) else: from kittystore.storm import get_storm_store - store = get_storm_store(url, search, debug) - if search is not None: + store = get_storm_store(settings, debug) + if settings.KITTYSTORE_SEARCH_INDEX is not None: store.search_index.initialize_with(store) return store diff --git a/kittystore/import.py b/kittystore/import.py index ab78dd6..c33cacf 100644 --- a/kittystore/import.py +++ b/kittystore/import.py @@ -222,14 +222,11 @@ class DbImporter(object): def parse_args(): usage = "%prog -s store_url -l list_name mbox_file [mbox_file ...]" parser = OptionParser(usage=usage) - parser.add_option("-s", "--store", help="the URL to the store database") parser.add_option("-l", "--list-name", help="the fully-qualified list " "name (including the '@' symbol and the domain name") - parser.add_option("-i", "--search-index", metavar="PATH", - help="the path to the search index") - parser.add_option("--settings", + parser.add_option("-s", "--settings", help="the Python path to a Django settings module") - parser.add_option("--pythonpath", + parser.add_option("-p", "--pythonpath", help="a directory to add to the Python path") parser.add_option("-v", "--verbose", action="store_true", help="show more output") diff --git a/kittystore/scripts.py b/kittystore/scripts.py index 3257b2c..e12a174 100644 --- a/kittystore/scripts.py +++ b/kittystore/scripts.py @@ -40,33 +40,19 @@ class StoreFromOptionsError(Exception): pass def get_store_from_options(opts): """ Returns a Store instance from an options object. Known options are; - - "store": the store URL - - "search_index": the search index path - "settings": the Django settings module - "pythonpath": an additional Python path to import the Django settings """ - django_settings = None - if opts.settings is not None: - if opts.pythonpath is not None: - sys.path.append(opts.pythonpath) - try: - django_settings = importlib.import_module(opts.settings) - except ImportError as e: - raise StoreFromOptionsError( - "could not import settings '%s' (Is it on " - "sys.path?): %s" % (opts.settings, e)) - if opts.store is not None: - store_url = opts.store - elif getattr(django_settings, "KITTYSTORE_URL", None) is not None: - store_url = django_settings.KITTYSTORE_URL - else: + settings = None + if opts.pythonpath is not None: + sys.path.append(opts.pythonpath) + try: + settings = importlib.import_module(opts.settings) + except ImportError as e: raise StoreFromOptionsError( - "you must either specify a store URL (eg: " - "sqlite:///kittystore.sqlite) or a Django configuration " - "module (Python path to the settings module)") - if opts.search_index is None: - opts.search_index = getattr(django_settings, "KITTYSTORE_SEARCH_INDEX", None) - return get_store(store_url, search=opts.search_index, debug=opts.debug) + "could not import settings '%s' (Is it on " + "sys.path?): %s" % (opts.settings, e)) + return get_store(settings, debug=opts.debug) # @@ -74,14 +60,10 @@ def get_store_from_options(opts): # def updatedb(): - parser = OptionParser(usage="%prog -s store_url") - parser.add_option("-s", "--store", metavar="URL", - help="the URL to the store database") - parser.add_option("-i", "--search-index", metavar="PATH", - help="the path to the search index") - parser.add_option("--settings", - help="the Python path to a Django settings module") - parser.add_option("--pythonpath", + parser = OptionParser(usage="%prog -s settings_module") + parser.add_option("-s", "--settings", + help="the Python path to a Django-like settings module") + parser.add_option("-p", "--pythonpath", help="a directory to add to the Python path") parser.add_option("-d", "--debug", action="store_true", help="show SQL queries") @@ -92,7 +74,7 @@ def updatedb(): 'the search index if necessary...' try: store = get_store_from_options(opts) - except StoreFromOptionsError, e: + except (StoreFromOptionsError, AttributeError), e: parser.error(e.args[0]) version = list(store.db.execute( "SELECT patch.version FROM patch " diff --git a/kittystore/storm/__init__.py b/kittystore/storm/__init__.py index 8e1c744..2ea90ee 100644 --- a/kittystore/storm/__init__.py +++ b/kittystore/storm/__init__.py @@ -36,23 +36,25 @@ class ThreadSafeStorePool(object): return self._local.store -def create_store(url, search, debug): +def create_store(settings, debug): if debug: storm.tracer.debug(True, stream=sys.stdout) + url = settings.KITTYSTORE_URL database = create_database(url) dbtype = url.partition(":")[0] store = Store(database) dbschema = Schema(schema.CREATES[dbtype], [], [], schema) dbschema.upgrade(store) + search = settings.KITTYSTORE_SEARCH_INDEX if search is not None: search_index = SearchEngine(search) else: search_index = None - return StormStore(store, search_index, debug) + return StormStore(store, search_index, settings, debug) -def get_storm_store(url, search=None, debug=False): +def get_storm_store(settings, debug=False): # Thread safety is managed by the middleware #store_pool = ThreadSafeStorePool(url, debug) #return store_pool.get() - return create_store(url, search, debug) + return create_store(settings, debug) diff --git a/kittystore/storm/store.py b/kittystore/storm/store.py index 1a19d58..4d09a1b 100644 --- a/kittystore/storm/store.py +++ b/kittystore/storm/store.py @@ -40,7 +40,7 @@ class StormStore(object): implements(IMessageStore) - def __init__(self, db, search_index=None, debug=False): + def __init__(self, db, search_index, settings, debug=False): """ Constructor. Create the session using the engine defined in the url. @@ -50,6 +50,7 @@ class StormStore(object): self.db = db self.debug = debug self.search_index = search_index + self.settings = settings # IMessageStore methods diff --git a/kittystore/test/__init__.py b/kittystore/test/__init__.py index 4250b59..170473e 100644 --- a/kittystore/test/__init__.py +++ b/kittystore/test/__init__.py @@ -16,4 +16,9 @@ class FakeList(object): self.display_name = None self.subject_prefix = None - +class SettingsModule: + KITTYSTORE_URL = "sqlite:" + KITTYSTORE_SEARCH_INDEX = None + MAILMAN_REST_SERVER = "http://localhost:8001" + MAILMAN_API_USER = "testrestuser" + MAILMAN_API_PASS = "testrestpass" diff --git a/kittystore/test/test_analysis.py b/kittystore/test/test_analysis.py index bd6cef2..16d0d01 100644 --- a/kittystore/test/test_analysis.py +++ b/kittystore/test/test_analysis.py @@ -12,7 +12,7 @@ from kittystore.storm import get_storm_store from kittystore.storm.model import Email, Thread from kittystore.analysis import compute_thread_order_and_depth -from kittystore.test import FakeList +from kittystore.test import FakeList, SettingsModule def make_fake_email(num=1, list_name="example-list", date=None): @@ -33,7 +33,7 @@ def make_fake_email(num=1, list_name="example-list", date=None): class TestThreadOrderDepth(unittest.TestCase): def setUp(self): - self.store = get_storm_store("sqlite:") + self.store = get_storm_store(SettingsModule()) def tearDown(self): self.store.flush() diff --git a/kittystore/test/test_storm_model.py b/kittystore/test/test_storm_model.py index c1507c1..d4bc558 100644 --- a/kittystore/test/test_storm_model.py +++ b/kittystore/test/test_storm_model.py @@ -10,13 +10,13 @@ from mailman.email.message import Message from kittystore.storm import get_storm_store from kittystore.storm.model import Email, Thread -from kittystore.test import get_test_file, FakeList +from kittystore.test import get_test_file, FakeList, SettingsModule class TestStormModel(unittest.TestCase): def setUp(self): - self.store = get_storm_store("sqlite:") + self.store = get_storm_store(SettingsModule()) #self.store = get_storm_store("postgres://kittystore:kittystore@localhost/kittystore_test", True) #self.store = get_storm_store("mysql://kittystore:kittystore@localhost/kittystore_test", True) diff --git a/kittystore/test/test_storm_store.py b/kittystore/test/test_storm_store.py index b3d2903..6b1f798 100644 --- a/kittystore/test/test_storm_store.py +++ b/kittystore/test/test_storm_store.py @@ -14,13 +14,13 @@ from kittystore.storm import get_storm_store from kittystore.storm.model import Email, Attachment, List from kittystore.utils import get_message_id_hash -from kittystore.test import get_test_file, FakeList +from kittystore.test import get_test_file, FakeList, SettingsModule class TestStormStore(unittest.TestCase): def setUp(self): - self.store = get_storm_store("sqlite:") + self.store = get_storm_store(SettingsModule()) def tearDown(self): self.store.close() diff --git a/kittystore/test/test_storm_store_fetch.py b/kittystore/test/test_storm_store_fetch.py index 2ee4faf..e8db480 100644 --- a/kittystore/test/test_storm_store_fetch.py +++ b/kittystore/test/test_storm_store_fetch.py @@ -7,13 +7,13 @@ from mailman.email.message import Message from kittystore.storm import get_storm_store -from kittystore.test import FakeList +from kittystore.test import FakeList, SettingsModule class TestStormStoreFetch(unittest.TestCase): def setUp(self): - self.store = get_storm_store("sqlite:") + self.store = get_storm_store(SettingsModule()) self.listname, self.m_hash = self.add_fetch_data() def tearDown(self): |