Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from fabiomcosta/master
Browse files Browse the repository at this point in the history
threading.local instances are not iterable, #fix
  • Loading branch information
heynemann committed Dec 21, 2011
2 parents 3f2ab07 + d2d8c7e commit 7f2718c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
6 changes: 2 additions & 4 deletions django_pyvows/context.py
Expand Up @@ -32,24 +32,21 @@ def start_environment(cls, settings_path):
os.environ['DJANGO_SETTINGS_MODULE'] = settings_path
settings_tracker.install()


def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
self.ignore('get_settings', 'template', 'request', 'model', 'url', 'find_in_parent',
'start_environment', 'port', 'host', 'get_url', 'get', 'post')


@property
def settings(self):
thread = current_thread()
if not hasattr(thread, "settings"):
if not hasattr(thread, 'settings'):
thread.settings = local()
return thread.settings

def setup(self):
DjangoContext.start_environment(self.get_settings())


def get_settings(self):
if 'DJANGO_SETTINGS_MODULE' in os.environ:
return os.environ['DJANGO_SETTINGS_MODULE']
Expand Down Expand Up @@ -88,6 +85,7 @@ def get_url(self, path):
except ValueError:
return path


class DjangoHTTPContext(DjangoContext):

def start_server(self, host=None, port=None):
Expand Down
6 changes: 2 additions & 4 deletions django_pyvows/server.py
Expand Up @@ -14,11 +14,8 @@
from threading import Thread, local, current_thread
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

from pyvows import Vows
from django.http import HttpRequest
from django.core.handlers.wsgi import WSGIHandler

from django_pyvows.assertions import Url, Model, Template

class WSGIRequestHandler(BaseHTTPRequestHandler):
"""A request handler that implements WSGI dispatching."""
Expand Down Expand Up @@ -128,10 +125,11 @@ def make_response():
thread = current_thread()
if not hasattr(thread, "settings"):
thread.settings = local()
for key, value in settings.items():
for key, value in getattr(settings, '__dict__', settings).items():
setattr(thread.settings, key, value)
while True:
self.handle_request()

self.thr = Thread(target=make_response)
self.thr.daemon = True
self.thr.start()
Expand Down
5 changes: 2 additions & 3 deletions django_pyvows/settings_manager.py
Expand Up @@ -8,7 +8,6 @@
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com

import sys
from threading import current_thread

class SettingsTracker(object):
Expand All @@ -24,7 +23,7 @@ def _import(self, name, globals=None, locals=None, fromlist=[], level=-1):
fromlist = (fromlist or [])
if name == 'django.conf' and 'settings' in fromlist:
result.settings = VowsSettings(result.settings)
elif name == "django" and 'conf' in fromlist:
elif name == 'django' and 'conf' in fromlist:
result.conf.settings = VowsSettings(result.conf.settings)
return result

Expand All @@ -35,7 +34,7 @@ def __init__(self, original_settings):

def __getattr__(self, attr_name):
thread = current_thread()
if hasattr(thread, "settings"):
if hasattr(thread, 'settings'):
if hasattr(thread.settings, attr_name):
return getattr(thread.settings, attr_name)
return getattr(self.original_settings, attr_name)
Expand Down

0 comments on commit 7f2718c

Please sign in to comment.