[Zope3-checkins] CVS: Zope3/lib/python/Zope/App - _app.py:1.4 __init__.py:1.3
Jim Fulton
jim@zope.com
Thu, 24 Oct 2002 11:37:33 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App
In directory cvs.zope.org:/tmp/cvs-serv28684/lib/python/Zope/App
Modified Files:
__init__.py
Added Files:
_app.py
Log Message:
Added a facility to make it easy to get to Zope 3 from a Python
prompt. Also added rudimentary debugging support.
>From lib/python (or with lib/python in your Python path), start Python
and:
from Zope.App import Application
# Create an Application object
app = Application('../../Data.fs', '../../site.zcml')
to get the applicatiuon root object on an open database connection,
call the application object:
root = app()
to execute a request for a path:
app.debug(path)
You can pass various extra parameters to debug to specify environment
variables and so on. (For now, see lib/python/Zope/App/_app.py.)
To prevent exceptions from being caught, provide the pm keyword
argument:
app.debug(path, pm=1)
This will let you do pdb post-mortem debugging of errors.
Added a little bit of framework for site initialization, so that the
debugging environment and z3.py use the same initialization code.
=== Zope3/lib/python/Zope/App/_app.py 1.3 => 1.4 ===
--- /dev/null Thu Oct 24 11:37:33 2002
+++ Zope3/lib/python/Zope/App/_app.py Thu Oct 24 11:37:02 2002
@@ -0,0 +1,122 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Code to initialize the application server
+
+$Id$
+"""
+__metaclass__ = type
+
+import base64
+from cStringIO import StringIO
+from Zope.Publisher.Publish import publish as _publish
+
+_configured = 0
+def config(file):
+ "Configure site globals"
+ global _configured
+
+ if _configured:
+ return
+
+ from Zope.Configuration.xmlconfig import XMLConfig
+
+ # Set user to system_user, so we can do anything we want
+ from Zope.Security.SecurityManagement import system_user
+ from Zope.Security.SecurityManagement import newSecurityManager
+ newSecurityManager(system_user)
+
+ # Load server-independent site config
+ XMLConfig(file)()
+
+ # Reset user
+ from Zope.Security.SecurityManagement import noSecurityManager
+ noSecurityManager()
+
+ _configured = 1
+
+def database(db):
+ if type(db) is str:
+ # Database name
+ if db.endswith('.py'):
+ # Python source, exec it
+ globals = {}
+ execfile(db, globals)
+ if 'DB' in globals:
+ db = globals['DB']
+ else:
+ storage = globals['Storage']
+ from ZODB.DB import DB
+ db = DB(storage)
+ elif db.endswith(".fs"):
+ from ZODB.FileStorage import FileStorage
+ from ZODB.DB import DB
+ storage = FileStorage(db)
+ db = DB(storage)
+
+ # Make sure we have an application object
+ connection = db.open()
+ if 'Application' not in connection.root():
+ from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+ import Transaction
+ connection.root()['Application'] = RootFolder()
+ Transaction.get_transaction().commit()
+
+ connection.close()
+
+ return db
+
+class Application:
+
+ def __init__(self, db, config_file=None):
+ if config_file is not None:
+ config(config_file)
+ self.db = database(db)
+
+ def __call__(self):
+ return self.db.open().root()['Application']
+
+ __browser_pub = None
+ __TestRequest = None
+ def debug(self, path='/', stdin='', stdout=None, basic=None, pm=0,
+ environment = None, **kw):
+
+ if stdout is None:
+ stdout = StringIO()
+
+ if type(stdin) is str:
+ stdin = StringIO(stdin)
+
+ env = {'PATH_INFO': path}
+ if environment is not None:
+ env.update(environment)
+ env.update(kw)
+
+ if basic:
+ env['HTTP_AUTHORIZATION']="Basic %s" % base64.encodestring(basic)
+
+ if self.__TestRequest is None:
+ from Zope.Publisher.Browser.BrowserRequest import TestRequest
+ from Zope.App.ZopePublication.Browser.Publication \
+ import BrowserPublication
+ self.__TestRequest = TestRequest
+ self.__browser_pub = BrowserPublication(self.db)
+
+ request = self.__TestRequest(stdin, stdout, env)
+ request.setPublication(self.__browser_pub)
+
+ _publish(request, handle_errors = not pm)
+
+ stdout.seek(0)
+ print stdout.read()
+
=== Zope3/lib/python/Zope/App/__init__.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/__init__.py:1.2 Mon Jun 10 19:27:45 2002
+++ Zope3/lib/python/Zope/App/__init__.py Thu Oct 24 11:37:02 2002
@@ -15,3 +15,6 @@
Zope application.
"""
+from _app import config, Application
+
+