Browse files

Adds funfactory to fix logging hell

  • Loading branch information...
1 parent 0de48a7 commit b5811a34f898a422b2d3436003a0b9b4153eac6b @davedash davedash committed Nov 11, 2011
Showing with 31 additions and 274 deletions.
  1. +1 −1 apps/feedback/cron.py
  2. +3 −2 apps/input/tests/test_middleware.py
  3. +5 −2 configs/sphinx/localsettings_django.py
  4. +0 −69 log_settings.py
  5. +12 −44 manage.py
  6. +10 −156 settings.py
View
2 apps/feedback/cron.py
@@ -76,7 +76,7 @@ def populate(num_opinions=None, product='mobile', type=None, locale=None):
type = random.choice(TYPES).id
o = Opinion(_type=type,
url=random.choice(URLS),
- locale=locale or random.choice(settings.INPUT_LANGUAGES),
+ locale=locale or random.choice(settings.PROD_LANGUAGES),
user_agent=random.choice(UA_STRINGS[product]))
o.description = sample()
View
5 apps/input/tests/test_middleware.py
@@ -39,7 +39,7 @@ def test_locale_fallback(self):
def test_mobilesite_nohost(self):
"""Make sure we serve the desktop site if there's no HTTP_HOST set."""
# This won't contain HTTP_HOST. Must not fail.
- self.client.get('/')
+ self.client.get('/', follow=True)
eq_(settings.SITE_ID, settings.DESKTOP_SITE_ID)
@patch('django.contrib.sites.models.Site.objects.get')
@@ -48,12 +48,13 @@ def test_mobilesite_detection(self, mock):
def side_effect(*args, **kwargs):
class FakeSite(object):
id = settings.MOBILE_SITE_ID
+ domain = ''
return FakeSite()
mock.side_effect = side_effect
# Get the front page. Since we mocked the Site model, the URL we
# pass here does not matter.
- self.client.get('/', HTTP_HOST='m.example.com')
+ self.client.get('/', HTTP_HOST='m.example.com', follow=True)
eq_(settings.SITE_ID, settings.MOBILE_SITE_ID)
def test_redirect_with_querystring(self):
View
7 configs/sphinx/localsettings_django.py
@@ -1,12 +1,15 @@
import os
import sys
+import site
SETTINGS_DIR = os.path.realpath(
os.path.join(os.path.dirname(__file__), os.path.sep.join(('..',) * 2)))
sys.path.append(SETTINGS_DIR)
-
-from manage import settings
+site.addsitedir(SETTINGS_DIR + '/vendor')
+from funfactory import manage
+manage.ROOT = SETTINGS_DIR
+import settings_local as settings
s = settings.DATABASES['default']
MYSQL_PASS = s['PASSWORD']
View
69 log_settings.py
@@ -1,69 +0,0 @@
-import logging
-import logging.handlers
-
-from django.conf import settings
-
-import commonware.log
-import dictconfig
-
-
-class NullHandler(logging.Handler):
-
- def emit(self, record):
- pass
-
-
-base_fmt = ('%(name)s:%(levelname)s %(message)s '
- ':%(pathname)s:%(lineno)s')
-
-cfg = {
- 'version': 1,
- 'filters': {},
- 'formatters': {
- 'debug': {
- '()': commonware.log.Formatter,
- 'datefmt': '%H:%M:%s',
- 'format': '%(asctime)s ' + base_fmt,
- },
- 'prod': {
- '()': commonware.log.Formatter,
- 'datefmt': '%H:%M:%s',
- 'format': '%s: [%%(REMOTE_ADDR)s] %s' % (settings.SYSLOG_TAG,
- base_fmt),
- },
- },
- 'handlers': {
- 'console': {
- '()': logging.StreamHandler,
- 'formatter': 'debug',
- },
- 'syslog': {
- '()': logging.handlers.SysLogHandler,
- 'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
- 'formatter': 'prod',
- },
- 'null': {
- '()': NullHandler,
- }
- },
- 'loggers': {
- 'i': {},
- },
- 'root': {},
-}
-
-for key, value in settings.LOGGING.items():
- cfg[key].update(value)
-
-# Set the level and handlers for all loggers.
-for logger in cfg['loggers'].values() + [cfg['root']]:
- syslog = settings.HAS_SYSLOG and not settings.DEBUG
- if 'handlers' not in logger:
- logger['handlers'] = ['syslog' if syslog else 'console']
- if 'level' not in logger:
- logger['level'] = settings.LOG_LEVEL
- if logger is not cfg['root'] and 'propagate' not in logger:
- logger['propagate'] = False
-
-dictconfig.dictConfig(cfg)
-
View
56 manage.py
@@ -1,56 +1,24 @@
#!/usr/bin/env python
import os
-import site
import sys
-
-ROOT = os.path.dirname(os.path.abspath(__file__))
-
-path = lambda *a: os.path.join(ROOT, *a)
-
-prev_sys_path = list(sys.path)
-
-site.addsitedir(path('apps'))
-site.addsitedir(path('lib'))
-site.addsitedir(path('vendor'))
-site.addsitedir(path('vendor/lib/python'))
-
-# Move the new items to the front of sys.path. (via virtualenv)
-new_sys_path = []
-for item in list(sys.path):
- if item not in prev_sys_path:
- new_sys_path.append(item)
- sys.path.remove(item)
-sys.path[:0] = new_sys_path
-
-# No third-party imports until we've added all our sitedirs!
-from django.core.management import execute_manager, setup_environ
-
try:
- import settings_local as settings
+ # For local development in a virtualenv:
+ from funfactory import manage
except ImportError:
- try:
- import settings
- except ImportError:
- import sys
- sys.stderr.write(
- "Error: Tried importing 'settings_local.py' and 'settings.py' "
- "but neither could be found (or they're throwing an ImportError)."
- " Please come back and try again later.")
- raise
+ # Production:
+ # Add a temporary path so that we can import the funfactory
+ tmp_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
+ 'vendor', 'src', 'funfactory')
+ sys.path.append(tmp_path)
-# The first thing execute_manager does is call `setup_environ`. Logging config
-# needs to access settings, so we'll setup the environ early.
-setup_environ(settings)
+ from funfactory import manage
-# Import for side-effect: configures our logging handlers.
-# pylint: disable-msg=W0611
-import log_settings
+ # Let the path magic happen in setup_environ() !
+ sys.path.remove(tmp_path)
-# Monkey-patch django forms to avoid having to use Jinja2's |safe everywhere.
-import safe_django_forms
-safe_django_forms.monkeypatch()
+manage.setup_environ(__file__)
if __name__ == "__main__":
- execute_manager(settings)
+ manage.main()
View
166 settings.py
@@ -1,61 +1,13 @@
-# Django settings for the reporter project.
+from funfactory.settings_base import *
-import os
-import logging
-
-from django.utils.functional import lazy
-
-
-# Make filepaths relative to settings.
-ROOT = os.path.dirname(os.path.abspath(__file__))
-path = lambda *a: os.path.join(ROOT, *a)
-
-ROOT_PACKAGE = os.path.basename(ROOT)
-
-
-DEBUG = False
-TEMPLATE_DEBUG = DEBUG
-
-## Log settings
-LOG_LEVEL = logging.DEBUG
-HAS_SYSLOG = True
SYSLOG_TAG = "http_app_reporter"
-LOGGING_CONFIG = None
-LOGGING = {
- 'loggers': {
- 'i.sphinx': {'level': logging.INFO},
- },
-}
-
-ADMINS = (
- # ('Your Name', 'your_email@domain.com'),
-)
-
-MANAGERS = ADMINS
DATABASE_ROUTERS = ('website_issues.db.DatabaseRouter',
'multidb.MasterSlaveRouter',)
-SLAVE_DATABASES = []
-# Caching
-#CACHE_BACKEND = 'caching.backends.memcached://127.0.0.1:11211/'
CACHE_DEFAULT_PERIOD = CACHE_MIDDLEWARE_SECONDS = 60 * 5 # 5 minutes
CACHE_COUNT_TIMEOUT = 60 # seconds
CACHE_PREFIX = CACHE_MIDDLEWARE_KEY_PREFIX = 'reporter:'
-# L10n
-
-# Local time zone for this installation. Choices can be found here:
-# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
-# although not all choices may be available on all operating systems.
-# On Unix systems, a value of None will cause Django to use the same
-# timezone as the operating system.
-# If running in a Windows environment this must be set to the same as your
-# system time zone.
-TIME_ZONE = 'America/Los_Angeles'
-
-# Language code for this installation. All choices can be found here:
-# http://www.i18nguy.com/unicode/language-identifiers.html
-LANGUAGE_CODE = 'en-US'
# Site ID.
# Site 1 is the desktop site, site == MOBILE_SITE_ID is the mobile site. This
@@ -66,99 +18,34 @@
# The desktop version is the default.
SITE_ID = DESKTOP_SITE_ID
-# If you set this to False, Django will make some optimizations so as not
-# to load the internationalization machinery.
-USE_I18N = True
-
-# If you set this to False, Django will not format dates, numbers and
-# calendars according to the current locale
-USE_L10N = True
-
# Accepted locales
-INPUT_LANGUAGES = ('ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en-US', 'es',
+PROD_LANGUAGES = ('ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en-US', 'es',
'fr', 'fy-NL', 'ga-IE', 'gl', 'he', 'hr', 'hu', 'id', 'it',
'ja', 'ko', 'nb-NO', 'nl', 'pl', 'pt-BR', 'pt-PT', 'ro',
'ru', 'sk', 'sl', 'sq', 'uk', 'vi', 'zh-TW', 'zh-CN')
RTL_LANGUAGES = ('ar', 'he',) # ('fa', 'fa-IR')
# Fallbacks for locales that are not recognized by Babel. Bug 596981.
BABEL_FALLBACK = {'fy-nl': 'nl'}
-
-# Override Django's built-in with our native names
-class LazyLangs(dict):
- def __new__(self):
- from product_details import product_details
- return dict([(lang.lower(), product_details.languages[lang]['native'])
- for lang in INPUT_LANGUAGES])
-LANGUAGES = lazy(LazyLangs, dict)()
-
-LANGUAGE_URL_MAP = dict((i[:2], i) for i in INPUT_LANGUAGES if '-' in i)
-LANGUAGE_URL_MAP.update((i.lower(), i) for i in INPUT_LANGUAGES)
+LANGUAGE_URL_MAP = dict((i[:2], i) for i in PROD_LANGUAGES if '-' in i)
+LANGUAGE_URL_MAP.update((i.lower(), i) for i in PROD_LANGUAGES)
# Paths that don't require a locale prefix.
SUPPORTED_NONLOCALES = ('media', 'admin')
-TEXT_DOMAIN = 'messages'
-STANDALONE_DOMAINS = [TEXT_DOMAIN, 'javascript']
-TOWER_KEYWORDS = {'_lazy': None}
-
-# Tells the extract script what files to look for l10n in and what function
-# handles the extraction. The Tower library expects this.
-DOMAIN_METHODS = {
- 'messages': [
- ('apps/**.py',
- 'tower.management.commands.extract.extract_tower_python'),
- ('**/templates/**.html',
- 'tower.management.commands.extract.extract_tower_template'),
- ],
-}
-
-
-# Media
-
-# Absolute path to the directory that holds media.
-# Example: "/home/media/media.lawrence.com/"
-MEDIA_ROOT = path('media')
-
-# URL that handles the media served from MEDIA_ROOT. Make sure to use a
-# trailing slash if there is a path component (optional in other cases).
-# Examples: "http://media.lawrence.com", "http://example.com/media/"
-MEDIA_URL = '/media/'
-
-# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
-# trailing slash.
-# Examples: "http://foo.com/media/", "/media/".
-ADMIN_MEDIA_PREFIX = '/admin-media/'
-
-# Ignore me!
-SECRET_KEY = '^e*0du@u83$de+==+x$5k%x#+4v7&nm-_sggrr(t!&@kufz87n'
-
# Templates
CSRF_FAILURE_VIEW = '%s.urls.handler_csrf' % ROOT_PACKAGE
-TEMPLATE_DIRS = (path('templates'),)
-
-# List of callables that know how to import templates from various sources.
-TEMPLATE_LOADERS = (
- 'jingo.Loader',
- 'django.template.loaders.filesystem.Loader',
- 'django.template.loaders.app_directories.Loader',
-)
-
-TEMPLATE_CONTEXT_PROCESSORS = (
- 'django.contrib.auth.context_processors.auth',
- 'django.core.context_processors.debug',
- 'django.core.context_processors.media',
- 'django.core.context_processors.request',
- 'django.core.context_processors.csrf',
+TEMPLATE_CONTEXT_PROCESSORS = list(TEMPLATE_CONTEXT_PROCESSORS) + [
+ # TODO: is this needed?
'input.context_processors.i18n',
'input.context_processors.input',
'input.context_processors.mobile',
'input.context_processors.opinion_types',
'search.context_processors.product_versions',
'jingo_minify.helpers.build_ids',
-)
+]
# Bundles is a dictionary of two dictionaries, css and js, which list css files
# and js files that can be bundled together by the jingo-minify app.
@@ -232,33 +119,15 @@ def __new__(self):
}
JAVA_BIN = '/usr/bin/java'
-
-def JINJA_CONFIG():
- import jinja2
- config = {'extensions': ['tower.template.i18n', 'jinja2.ext.loopcontrols',
- 'jinja2.ext.with_', 'caching.ext.cache'],
- 'finalize': lambda x: x if x is not None else ''}
- return config
-
JINGO_EXCLUDE_APPS = ['debug_toolbar', 'admin']
-MIDDLEWARE_CLASSES = (
+MIDDLEWARE_CLASSES = list(MIDDLEWARE_CLASSES) + [
'input.middleware.MobileSiteMiddleware',
- 'input.middleware.LocaleURLMiddleware',
-
- 'django.middleware.common.CommonMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
'commonware.response.middleware.GraphiteMiddleware',
'commonware.response.middleware.GraphiteRequestTimingMiddleware',
- 'commonware.middleware.FrameOptionsHeader',
-)
-
-ROOT_URLCONF = '%s.urls' % ROOT_PACKAGE
+]
-INSTALLED_APPS = [
+INSTALLED_APPS = list(INSTALLED_APPS) + [
'input', # comes first so it always takes precedence.
'api',
@@ -271,20 +140,10 @@ def JINJA_CONFIG():
'annoying',
'cronjobs',
- 'jingo_minify',
- 'product_details',
- 'tower',
- 'djcelery',
'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
-
- # For tests
- 'django_nose',
]
# Where to store product details
@@ -317,8 +176,6 @@ def JINJA_CONFIG():
SEARCH_PERPAGE = 20 # results per page
SEARCH_MAX_PAGES = SEARCH_MAX_RESULTS / SEARCH_PERPAGE
-TEST_RUNNER = 'test_utils.runner.RadicalTestSuiteRunner'
-
CLUSTER_SIM_THRESHOLD = 2
## Celery
@@ -327,9 +184,6 @@ def JINJA_CONFIG():
BROKER_VHOST = "input"
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
-BROKER_CONNECTION_TIMEOUT = 0.1
-CELERY_RESULT_BACKEND = 'amqp'
-CELERY_IGNORE_RESULT = True
CELERY_IMPORTS = ('django_arecibo.tasks',)
import djcelery

0 comments on commit b5811a3

Please sign in to comment.