Browse files

Make settings lazy

Make settings lazy-loadable and remote default settings mock from
package __init__. Also change torneira.settings to
torneira.conf.settings.
  • Loading branch information...
1 parent b80bb20 commit c15b800fdbbf377a031e9e1ec664d11e43024eab @rcmachado rcmachado committed Mar 3, 2013
View
2 tests/core/test_meta.py
@@ -14,7 +14,7 @@
import unittest, fudge
-from torneira import settings
+from torneira.conf import settings
from torneira.core import meta
class TimerProxyTestCase(unittest.TestCase):
View
13 torneira/__init__.py
@@ -15,16 +15,3 @@
# limitations under the License.
#
__version__ = '0.3.3'
-
-try:
- import settings_local as settings
-except ImportError, ie:
- try:
- import settings
- except ImportError, ie:
- class settings(object):
- DEBUG = True
- PROFILING = False
- COOKIE_SECRET = ""
-
-settings = settings
View
2 torneira/cache/extension.py
@@ -20,7 +20,7 @@
from sqlalchemy.orm import attributes, MapperExtension, EXT_CONTINUE, EXT_STOP
from sqlalchemy.orm.session import Session
-from torneira import settings
+from torneira.conf import settings
from torneira.cache.util import get_cache, cache_key
View
2 torneira/cache/util.py
@@ -20,7 +20,7 @@
from tornado import gen
from torneira.cache.backend import MemcachedClass, DummyClass, RedisClass
-from torneira import settings
+from torneira.conf import settings
__cache__ = None
View
46 torneira/conf.py
@@ -0,0 +1,46 @@
+# coding: utf-8
+import os
+
+
+class EmptySettings(object):
+ pass
+
+
+class LazySettings(object):
+ """'Inspired' by Django's LazySettings class"""
+ _wrapped = None
+
+ def _import_module(self):
+ if not self._wrapped:
+ module_name = os.getenv('TORNEIRA_SETTINGS_MODULE', 'settings')
+ try:
+ self._wrapped = __import__(module_name, globals(), locals(), [], -1)
+ except ImportError:
+ self._wrapped = EmptySettings()
+
+ def __getattr__(self, name):
+ if name == "_wrapped":
+ return self.__dict__["_wrapped"]
+ else:
+ self._import_module()
+ return getattr(self._wrapped, name)
+
+ def __setattr__(self, name, value):
+ if name == "_wrapped":
+ self.__dict__["_wrapped"] = value
+ else:
+ self._import_module()
+ setattr(self._wrapped, name, value)
+
+ def __delattr__(self, name):
+ if name == "_wrapped":
+ raise TypeError("can't delete _wrapped.")
+ self._import_module()
+ delattr(self._wrapped, name)
+
+
+settings = LazySettings()
+
+__all__ = (
+ 'settings',
+)
View
3 torneira/controller/base.py
@@ -15,7 +15,7 @@
import functools
from tornado import locale
-from torneira import settings
+from torneira.conf import settings
from torneira.handler import TorneiraHandler
from torneira.template import MakoMixin
@@ -30,6 +30,7 @@
except ImportError:
simplexml = None
+
class BaseController(TorneiraHandler, MakoMixin):
def initialize(self, *args, **kwargs):
super(BaseController, self).initialize(*args, **kwargs)
View
2 torneira/core/meta.py
@@ -19,7 +19,7 @@
from sqlalchemy.interfaces import ConnectionProxy
from sqlalchemy.orm import scoped_session, sessionmaker
-from torneira import settings
+from torneira.conf import settings
class TimerProxy(ConnectionProxy):
View
2 torneira/core/server.py
@@ -17,7 +17,7 @@
from tornado.web import Application, StaticFileHandler, URLSpec
from tornado.ioloop import IOLoop
-from torneira import settings
+from torneira.conf import settings
class TorneiraServer(object):
View
2 torneira/handler.py
@@ -3,7 +3,7 @@
from tornado.web import RequestHandler
-from torneira import settings
+from torneira.conf import settings
class TorneiraHandler(RequestHandler):
View
23 torneira/runner/main.py
@@ -18,17 +18,24 @@ def __init__(self, cli, options, args):
self.args = args
self.cli = cli
- def start(self):
- options = self.options
- # set path
- sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(options.settings_file)), ".."))
- sys.path.insert(0, os.path.dirname(os.path.abspath(options.settings_file)))
+ def setup_import_path(self):
+ filename = os.path.abspath(self.options.settings_file)
+ if not os.path.exists(filename):
+ self.cli.print_error("Settings file %s not found" % filename)
+ return None
- # set setting
- exec("import %s as settings" % os.path.splitext(os.path.basename(options.settings_file))[0])
- torneira.settings = settings
+ settings_dir = os.path.dirname(filename)
+ parent_dir = os.path.abspath(os.path.join(settings_dir, '..'))
+
+ sys.path.insert(0, parent_dir)
+ sys.path.insert(0, settings_dir)
+
+ def start(self):
+ self.setup_import_path()
from torneira.core.server import TorneiraServer
+
+ options = self.options
server = TorneiraServer(
port=options.port,
media_dir=os.path.abspath(options.media_dir),
View
2 torneira/template/mako_engine.py
@@ -3,7 +3,7 @@
from mako.lookup import TemplateLookup
from tornado.web import HTTPError
-from torneira import settings
+from torneira.conf import settings
class MakoMixin(object):
View
2 torneira/testing/client.py
@@ -4,8 +4,8 @@
from tornado.escape import parse_qs_bytes, native_str
from tornado.web import Application, HTTPError
+from torneira.conf import settings
from torneira.core.server import TorneiraHandler
-from torneira import settings
class TestingClient(object):
View
3 torneira/testing/testcase.py
@@ -1,7 +1,8 @@
# coding: utf-8
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application
-from torneira import settings
+
+from torneira.conf import settings
class TestCase(AsyncHTTPTestCase):

0 comments on commit c15b800

Please sign in to comment.