Steve Schmechel wrote:
I caught that from advice you gave on another post and that was exactly the command from the second example in my original post.


Unfortunately, that does not work either.

(Ref: http://www.mail-archive.com/repoze-dev@lists.repoze.org/msg01422.html It may have worked for George Hu, but he never replied back. He appears to be using some sort of SQL database.)

I am new to BFG, but I am trying to do my research before I ask a question.

Sorry, I didn't see Steve's second example.

This was actually a BFG bug. I'm attaching a patch that will compile against 1.1b2 to this mail; 1.1b3 (or 1.1 final, if we don't manage another beta) will have this patch in it.

Thanks!

- C

Index: repoze/bfg/scripting.py
===================================================================
--- repoze/bfg/scripting.py     (revision 6933)
+++ repoze/bfg/scripting.py     (working copy)
@@ -1,4 +1,6 @@
-def get_root(app, environ=None):
+from repoze.bfg.request import FakeRequest
+
+def get_root(app, request=None):
     """ Return a tuple composed of ``(root, closer)`` when provided a
     ``repoze.bfg.router.Router`` instance as the ``app`` argument.
     The ``root`` returned is the application root object.  The
@@ -8,13 +10,13 @@
     environment passed to the BFG application root factory.  An empty
     environ is constructed and passed to the root factory if
     ``environ`` is None."""
+    if request is None:
+        request = FakeRequest({})
     registry = app.registry
-    threadlocals = {'registry':registry, 'request':None}
+    threadlocals = {'registry':registry, 'request':request}
     app.threadlocal_manager.push(threadlocals)
-    if environ is None:
-        environ = {}
-    def closer(environ=environ): # keep environ alive via this function default
+    def closer(request=request): # keep request alive via this function default
         app.threadlocal_manager.pop()
-    root = app.root_factory(environ)
+    root = app.root_factory(request)
     return root, closer
 
Index: repoze/bfg/traversal.py
===================================================================
--- repoze/bfg/traversal.py     (revision 6933)
+++ repoze/bfg/traversal.py     (working copy)
@@ -14,6 +14,7 @@
 
 from repoze.bfg.location import lineage
 from repoze.bfg.encode import url_quote
+from repoze.bfg.request import FakeRequest
 
 def find_root(model):
     """ Find the root node in the graph to which ``model``
@@ -631,8 +632,3 @@
 def _join_path_tuple(tuple):
     return tuple and '/'.join([quote_path_segment(x) for x in tuple]) or '/'
 
-class FakeRequest(dict):
-    def __init__(self, environ):
-        self.environ = environ
-        self.update(environ)
-        
Index: repoze/bfg/tests/test_scripting.py
===================================================================
--- repoze/bfg/tests/test_scripting.py  (revision 6933)
+++ repoze/bfg/tests/test_scripting.py  (working copy)
@@ -1,29 +1,29 @@
 import unittest
 
 class TestGetRoot(unittest.TestCase):
-    def _callFUT(self, app, environ=None):
+    def _callFUT(self, app, request=None):
         from repoze.bfg.paster import get_root
-        return get_root(app, environ)
+        return get_root(app, request)
 
-    def test_it_noenviron(self):
+    def test_it_norequest(self):
         app = DummyApp()
         root, closer = self._callFUT(app)
         self.assertEqual(len(app.threadlocal_manager.pushed), 1)
         pushed = app.threadlocal_manager.pushed[0]
         self.assertEqual(pushed['registry'], dummy_registry)
-        self.assertEqual(pushed['request'], None)
+        self.assertEqual(pushed['request'].environ, {})
         self.assertEqual(len(app.threadlocal_manager.popped), 0)
         closer()
         self.assertEqual(len(app.threadlocal_manager.popped), 1)
 
-    def test_it_withenviron(self):
+    def test_it_withrequest(self):
         app = DummyApp()
-        environ = {}
-        root, closer = self._callFUT(app, environ)
+        request = DummyRequest({})
+        root, closer = self._callFUT(app, request)
         self.assertEqual(len(app.threadlocal_manager.pushed), 1)
         pushed = app.threadlocal_manager.pushed[0]
         self.assertEqual(pushed['registry'], dummy_registry)
-        self.assertEqual(pushed['request'], None)
+        self.assertEqual(pushed['request'], request)
         self.assertEqual(len(app.threadlocal_manager.popped), 0)
         closer()
         self.assertEqual(len(app.threadlocal_manager.popped), 1)
@@ -54,3 +54,7 @@
     def pop(self):
         self.popped.append(True)
         
+class DummyRequest:
+    def __init__(self, environ):
+        self.environ = environ
+        
Index: repoze/bfg/tests/test_traversal.py
===================================================================
--- repoze/bfg/tests/test_traversal.py  (revision 6933)
+++ repoze/bfg/tests/test_traversal.py  (working copy)
@@ -970,23 +970,6 @@
         self.assertEqual(result['view_name'], '')
         self.assertEqual(result['context'], model)
 
-class TestFakeRequest(unittest.TestCase):
-    def _makeOne(self, environ):
-        from repoze.bfg.traversal import FakeRequest
-        return FakeRequest(environ)
-
-    def test_environ(self):
-        environ = {'a':1, 'b':2}
-        request = self._makeOne(environ)
-        self.assertEqual(request.environ['a'], 1)
-        self.assertEqual(request.environ['b'], 2)
-        
-    def test_asdict(self):
-        environ = {'a':1, 'b':2}
-        request = self._makeOne(environ)
-        self.assertEqual(request['a'], 1)
-        self.assertEqual(request['b'], 2)
-
 def make_traverser(result):
     class DummyTraverser(object):
         def __init__(self, context):
Index: repoze/bfg/tests/test_request.py
===================================================================
--- repoze/bfg/tests/test_request.py    (revision 6933)
+++ repoze/bfg/tests/test_request.py    (working copy)
@@ -168,6 +168,24 @@
         self._callFUT(request, [('c', 1)])
         self.assertEqual(request.global_response_headers, headers + [('c', 1)])
 
+class TestFakeRequest(unittest.TestCase):
+    def _makeOne(self, environ):
+        from repoze.bfg.request import FakeRequest
+        return FakeRequest(environ)
+
+    def test_environ(self):
+        environ = {'a':1, 'b':2}
+        request = self._makeOne(environ)
+        self.assertEqual(request.environ['a'], 1)
+        self.assertEqual(request.environ['b'], 2)
+        
+    def test_asdict(self):
+        environ = {'a':1, 'b':2}
+        request = self._makeOne(environ)
+        self.assertEqual(request['a'], 1)
+        self.assertEqual(request['b'], 2)
+
+
 class DummyRequest:
     def __init__(self, environ=None):
         if environ is None:
Index: repoze/bfg/tests/test_paster.py
===================================================================
--- repoze/bfg/tests/test_paster.py     (revision 6944)
+++ repoze/bfg/tests/test_paster.py     (working copy)
@@ -27,7 +27,7 @@
         self.assertEqual(len(app.threadlocal_manager.pushed), 1)
         pushed = app.threadlocal_manager.pushed[0]
         self.assertEqual(pushed['registry'], dummy_registry)
-        self.assertEqual(pushed['request'], None)
+        self.assertEqual(pushed['request'].environ, {})
         self.assertEqual(interact.local, {'root':dummy_root})
         self.failUnless(interact.banner)
         self.assertEqual(len(app.threadlocal_manager.popped), 1)
@@ -51,7 +51,7 @@
         self.assertEqual(len(app.threadlocal_manager.pushed), 1)
         pushed = app.threadlocal_manager.pushed[0]
         self.assertEqual(pushed['registry'], dummy_registry)
-        self.assertEqual(pushed['request'], None)
+        self.assertEqual(pushed['request'].environ, {})
         
self.assertEqual(dummy_shell_factory.shell.local_ns,{'root':dummy_root})
         self.assertEqual(dummy_shell_factory.shell.global_ns, {})
         self.failUnless('\n\n' in dummy_shell_factory.shell.IP.BANNER)
Index: repoze/bfg/request.py
===================================================================
--- repoze/bfg/request.py       (revision 6933)
+++ repoze/bfg/request.py       (working copy)
@@ -93,3 +93,9 @@
            '``repoze.bfg.request`` is '
            'deprecated.  Use ``from repoze.bfg.threadlocal import '
            'get_current_request instead.')
+
+class FakeRequest(dict):
+    def __init__(self, environ):
+        self.environ = environ
+        self.update(environ)
+        
Index: CHANGES.txt
===================================================================
--- CHANGES.txt (revision 6944)
+++ CHANGES.txt (working copy)
@@ -18,6 +18,10 @@
   output is now sent to the console.  Thanks to Daniel Holth for the
   patch.
 
+- The ``bfgshell`` command did not function properly; it was still
+  expecting to be able to call the root factory with a bare
+  ``environ`` rather than a request object.
+
 1.1b2 (2009-11-02)
 ==================
 
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to