Skip to content

Commit

Permalink
refactor calling the command line interpreter. always use the "classi…
Browse files Browse the repository at this point in the history
…c" one for executing scripts.
  • Loading branch information
janwijbrand committed May 1, 2012
1 parent b0a1290 commit 464a94b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/grokcore/startup/README.txt
Expand Up @@ -260,7 +260,7 @@ API Documentation
>>> import shutil
>>> shutil.rmtree(temp_dir)

``get_debugger(zope_conf_path)``
``interactive_debug_prompt(zope_conf_path)``
--------------------------------------------

Get an interactive console with a debugging shell started.
Expand Down Expand Up @@ -289,10 +289,10 @@ API Documentation
in the ``[interactive_debugger]`` section of your ``buildout.cfg``.

>>> import zope.app.appsetup.appsetup
>>> # Ugh - allow a reconfiguration of an app.
>>> zope.app.appsetup.appsetup._configured = False

>>> temp_dir = tempfile.mkdtemp()

>>> sitezcml = os.path.join(temp_dir, 'site.zcml')
>>> open(sitezcml, 'w').write(
... """<configure xmlns="http://namespaces.zope.org/zope">
Expand Down Expand Up @@ -336,10 +336,10 @@ API Documentation
... pprint(__file__)
... pprint(__name__)""")
>>>
>>> sys.argv = ['get_debugger', script]
>>> sys.argv = ['interactive_debugger', script]
>>> from grokcore.startup import interactive_debug_prompt
>>> try:
... interactive_debug_prompt(zope_conf=zopeconf)
... interactive_debug_prompt(zopeconf)
... except SystemExit:
... # Catch the exit from the interactive prompt as it would
... # exit this test as well.
Expand Down
9 changes: 4 additions & 5 deletions src/grokcore/startup/debug.py
Expand Up @@ -30,9 +30,8 @@

class GrokDebug(object):

def __init__(self, zope_conf='parts/etc/zope.debug.conf'):
db = zope.app.wsgi.config(zope_conf)
debugger = zope.app.debug.Debugger.fromDatabase(db)
def __init__(self, debugger):
debugger = debugger
self.app = debugger
self.root = debugger.root()
self.context = self.root
Expand Down Expand Up @@ -179,8 +178,8 @@ def path_completer(self, event):
if obj.__name__.startswith(tail)]


def ipython_debug_prompt(zope_conf):
grokd = GrokDebug(zope_conf)
def ipython_debug_prompt(debugger):
grokd = GrokDebug(debugger)
banner = textwrap.dedent(
"""\
IPython shell for Grok.
Expand Down
44 changes: 22 additions & 22 deletions src/grokcore/startup/startup.py
Expand Up @@ -32,47 +32,47 @@ def do_not_reraise_exception(context):
# Return the created application
return app

def _classic_debug_prompt(zope_conf):
db = zope.app.wsgi.config(zope_conf)
debugger = zope.app.debug.Debugger.fromDatabase(db)
def _classic_debug_prompt(debugger):
globals_ = {
'debugger': debugger,
'app': debugger,
'root': debugger.root()}
# Invoke an interactive interpreter prompt
banner = (
"Welcome to the interactive debug prompt.\n"
"The 'root' variable contains the ZODB root folder.\n"
"The 'app' variable contains the Debugger, 'app.publish(path)' "
"simulates a request.")
code.interact(banner=banner, local=globals_)

def _ipython_debug_prompt(debugger):
from grokcore.startup.debug import ipython_debug_prompt
return ipython_debug_prompt(debugger)

def interactive_debug_prompt(zope_conf):
db = zope.app.wsgi.config(zope_conf)
debugger = zope.app.debug.Debugger.fromDatabase(db)
if len(sys.argv) > 1:
# There're arguments passed to the command. We replicate the
# "old" zopectl run command behaviour that would execfile()
# the second argument.

globals_ = {
'debugger': debugger,
'app': debugger,
'root': debugger.root()}
# The current first argument is the interactive_debugger command
# itself. Pop it from the args list and as a result, the script
# to run is the first argument.
del sys.argv[0]

globals_['__name__'] = '__main__'
globals_['__file__'] = sys.argv[0]
execfile(sys.argv[0], globals_)

# Housekeeping.
db.close()
sys.exit()

# Invoke an interactive interpreter prompt
banner = (
"Welcome to the interactive debug prompt.\n"
"The 'root' variable contains the ZODB root folder.\n"
"The 'app' variable contains the Debugger, 'app.publish(path)' "
"simulates a request.")
code.interact(banner=banner, local=globals_)

def _ipython_debug_prompt(zope_conf):
from grokcore.startup.debug import ipython_debug_prompt
return ipython_debug_prompt(zope_conf)

def interactive_debug_prompt(zope_conf):
# Start the interpreter.
try:
import IPython
except ImportError:
return _classic_debug_prompt(zope_conf)
return _ipython_debug_prompt(zope_conf)
return _classic_debug_prompt(debugger)
return _ipython_debug_prompt(debugger)

0 comments on commit 464a94b

Please sign in to comment.