From 759b304cb50b588993714cd49e149fe8b6c6ad9c Mon Sep 17 00:00:00 2001 From: posativ Date: Wed, 17 Oct 2012 18:32:53 +0200 Subject: [PATCH] refactor configuration variables --- isso/__init__.py | 11 +++++++++-- isso/db.py | 8 ++++---- specs/test_db.py | 3 ++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/isso/__init__.py b/isso/__init__.py index afac795..e695feb 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -50,9 +50,16 @@ url_map = Map([ class Isso: + SECRET_KEY = ',\x1e\xbaY\xbb\xdf\xe7@\x85\xe3\xd9\xb4A9\xe4G\xa6O' + MODERATION = False + SQLITE = None + def __init__(self, conf): - self.conf = conf - self.db = db.SQLite(conf) + + self.__dict__.update(dict((k, v) for k, v in conf.iteritems() if k.isupper())) + + if self.SQLITE: + self.db = db.SQLite(self) def dispatch(self, request, start_response): adapter = url_map.bind_to_environ(request.environ) diff --git a/isso/db.py b/isso/db.py index ad67f41..6aa2d3a 100644 --- a/isso/db.py +++ b/isso/db.py @@ -11,7 +11,7 @@ class Abstract: __metaclass__ = abc.ABCMeta @abc.abstractmethod - def __init__(self, conf): + def __init__(self, app): return @abc.abstractmethod @@ -57,10 +57,10 @@ class SQLite(Abstract): 'text', 'author', 'email', 'website', 'parent', 'mode' ] - def __init__(self, conf): + def __init__(self, app): - self.dbpath = conf['SQLITE'] - self.mode = 1 if conf.get('MODERATION') else 0 + self.dbpath = app.SQLITE + self.mode = 1 if app.MODERATION else 0 with sqlite3.connect(self.dbpath) as con: sql = ('main.comments (id INTEGER NOT NULL, path VARCHAR(255) NOT NULL,' diff --git a/specs/test_db.py b/specs/test_db.py index 93e2b73..a6ae17e 100644 --- a/specs/test_db.py +++ b/specs/test_db.py @@ -5,6 +5,7 @@ import time import tempfile import unittest +import isso from isso.models import Comment from isso.db import SQLite @@ -18,7 +19,7 @@ class TestSQLite(unittest.TestCase): def setUp(self): fd, self.path = tempfile.mkstemp() - self.db = SQLite({'SQLITE': self.path}) + self.db = SQLite(isso.Isso({'SQLITE': self.path})) def test_get(self):