Permalink
Browse files

First Import

  • Loading branch information...
0 parents commit ac44adf69ed118891726ee55df18e1e92dac6a07 @amol- amol- committed Sep 18, 2012
@@ -0,0 +1,14 @@
+.coverage
+*.pyc
+*.bak
+*.db
+*.sw?
+*.orig
+*.mak.py
+build/*
+docs/.build/*
+dist/*
+.svn
+*.egg-info/*
+.noseids
+.idea
@@ -0,0 +1,2 @@
+global-exclude *.pyc
+
@@ -0,0 +1,20 @@
+About backlash
+-------------------------
+
+backlash is a standalone version of the Werkzeug Debugger based on WebOb
+adapted to support for Python3.
+
+backlash has born as a future replacement for WebError in upcoming TurboGears2 versions.
+
+Installing
+-------------------------------
+
+backlash can be installed from pypi::
+
+ easy_install backlash
+
+or::
+
+ pip install backlash
+
+should just work for most of the users
@@ -0,0 +1 @@
+from backlash.debug import DebuggedApplication
@@ -0,0 +1,72 @@
+import sys
+import types
+
+# True if we are running on Python 3.
+PY3 = sys.version_info[0] == 3
+
+if PY3: # pragma: no cover
+ string_types = str,
+ integer_types = int,
+ class_types = type,
+ text_type = str
+ binary_type = bytes
+ long = int
+else:
+ string_types = basestring,
+ integer_types = (int, long)
+ class_types = (type, types.ClassType)
+ text_type = unicode
+ binary_type = str
+ long = long
+
+def text_(s, encoding='utf-8', errors='strict'):
+ if isinstance(s, binary_type):
+ return s.decode(encoding, errors)
+ return s # pragma: no cover
+
+if PY3: # pragma: no cover
+ def native_(s, encoding='latin-1', errors='strict'):
+ if isinstance(s, text_type):
+ return s
+ return str(s, encoding, errors)
+else:
+ def native_(s, encoding='latin-1', errors='strict'):
+ if isinstance(s, text_type): # pragma: no cover
+ return s.encode(encoding, errors)
+ return str(s)
+
+if PY3: # pragma: no cover
+ def iteritems_(d):
+ return d.items()
+else:
+ def iteritems_(d):
+ return d.iteritems()
+
+if PY3: # pragma: no cover
+ import builtins
+ exec_ = getattr(builtins, "exec")
+ def reraise(exc_info):
+ etype, exc, tb = exc_info
+ if exc.__traceback__ is not tb:
+ raise exc.with_traceback(tb)
+ raise exc
+else: # pragma: no cover
+ def exec_(code, globs=None, locs=None):
+ """Execute code in a namespace."""
+ if globs is None:
+ frame = sys._getframe(1)
+ globs = frame.f_globals
+ if locs is None:
+ locs = frame.f_locals
+ del frame
+ elif locs is None:
+ locs = globs
+ exec("""exec code in globs, locs""")
+ exec_("""def reraise(exc_info):
+ raise exc_info[0], exc_info[1], exc_info[2]
+""")
+
+try:
+ from xmlrpclib import ServerProxy as XmlRpcServerProxy
+except ImportError:
+ from xmlrpc.client import ServerProxy as XmlRpcServerProxy
@@ -0,0 +1,212 @@
+# -*- coding: utf-8 -*-
+"""
+ werkzeug.debug.console
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Interactive console support.
+
+ :copyright: (c) 2011 by the Werkzeug Team, see AUTHORS for more details.
+ :license: BSD.
+"""
+import sys
+import code
+from types import CodeType
+import threading
+
+from backlash._compat import exec_, text_, binary_type
+from backlash.utils import escape
+from backlash.repr import debug_repr, dump, helper
+
+_local = threading.local()
+
+class HTMLStringO(object):
+ """A StringO version that HTML escapes on write."""
+
+ def __init__(self):
+ self._buffer = []
+
+ def isatty(self):
+ return False
+
+ def close(self):
+ pass
+
+ def flush(self):
+ pass
+
+ def seek(self, n, mode=0):
+ pass
+
+ def readline(self):
+ if len(self._buffer) == 0:
+ return ''
+ ret = self._buffer[0]
+ del self._buffer[0]
+ return ret
+
+ def reset(self):
+ val = ''.join(self._buffer)
+ del self._buffer[:]
+ return val
+
+ def _write(self, x):
+ if isinstance(x, binary_type):
+ x = text_(x, 'utf-8', 'replace')
+ self._buffer.append(x)
+
+ def write(self, x):
+ self._write(escape(x))
+
+ def writelines(self, x):
+ self._write(escape(''.join(x)))
+
+
+class ThreadedStream(object):
+ """Thread-local wrapper for sys.stdout for the interactive console."""
+
+ def push():
+ if not isinstance(sys.stdout, ThreadedStream):
+ sys.stdout = ThreadedStream()
+ _local.stream = HTMLStringO()
+ push = staticmethod(push)
+
+ def fetch():
+ try:
+ stream = _local.stream
+ except AttributeError:
+ return ''
+ return stream.reset()
+ fetch = staticmethod(fetch)
+
+ def displayhook(obj):
+ try:
+ stream = _local.stream
+ except AttributeError:
+ return _displayhook(obj)
+ # stream._write bypasses escaping as debug_repr is
+ # already generating HTML for us.
+ if obj is not None:
+ stream._write(debug_repr(obj))
+ displayhook = staticmethod(displayhook)
+
+ def __setattr__(self, name, value):
+ raise AttributeError('read only attribute %s' % name)
+
+ def __dir__(self):
+ return dir(sys.__stdout__)
+
+ def __getattribute__(self, name):
+ if name == '__members__':
+ return dir(sys.__stdout__)
+ try:
+ stream = _local.stream
+ except AttributeError:
+ stream = sys.__stdout__
+ return getattr(stream, name)
+
+ def __repr__(self):
+ return repr(sys.__stdout__)
+
+
+# add the threaded stream as display hook
+_displayhook = sys.displayhook
+sys.displayhook = ThreadedStream.displayhook
+
+
+class _ConsoleLoader(object):
+
+ def __init__(self):
+ self._storage = {}
+
+ def register(self, code, source):
+ self._storage[id(code)] = source
+ # register code objects of wrapped functions too.
+ for var in code.co_consts:
+ if isinstance(var, CodeType):
+ self._storage[id(var)] = source
+
+ def get_source_by_code(self, code):
+ try:
+ return self._storage[id(code)]
+ except KeyError:
+ pass
+
+
+def _wrap_compiler(console):
+ compile = console.compile
+ def func(source, filename, symbol):
+ code = compile(source, filename, symbol)
+ console.loader.register(code, source)
+ return code
+ console.compile = func
+
+
+class _InteractiveConsole(code.InteractiveInterpreter):
+
+ def __init__(self, globals, locals):
+ code.InteractiveInterpreter.__init__(self, locals)
+ self.globals = dict(globals)
+ self.globals['dump'] = dump
+ self.globals['help'] = helper
+ self.globals['__loader__'] = self.loader = _ConsoleLoader()
+ self.more = False
+ self.buffer = []
+ _wrap_compiler(self)
+
+ def runsource(self, source):
+ source = source.rstrip() + '\n'
+ ThreadedStream.push()
+ prompt = self.more and '... ' or '>>> '
+ try:
+ source_to_eval = ''.join(self.buffer + [source])
+ if code.InteractiveInterpreter.runsource(self,
+ source_to_eval, '<debugger>', 'single'):
+ self.more = True
+ self.buffer.append(source)
+ else:
+ self.more = False
+ del self.buffer[:]
+ finally:
+ output = ThreadedStream.fetch()
+ return prompt + source + output
+
+ def runcode(self, code):
+ try:
+ exec_(code, self.globals, self.locals)
+ except Exception:
+ self.showtraceback()
+
+ def showtraceback(self):
+ from backlash.tbtools import get_current_traceback
+ tb = get_current_traceback(skip=1)
+ sys.stdout._write(tb.render_summary())
+
+ def showsyntaxerror(self, filename=None):
+ from backlash.tbtools import get_current_traceback
+ tb = get_current_traceback(skip=4)
+ sys.stdout._write(tb.render_summary())
+
+ def write(self, data):
+ sys.stdout.write(data)
+
+
+class Console(object):
+ """An interactive console."""
+
+ def __init__(self, globals=None, locals=None):
+ if locals is None:
+ locals = {}
+ if globals is None:
+ globals = {}
+ self._ipy = _InteractiveConsole(globals, locals)
+
+ def eval(self, code):
+ return self._ipy.runsource(code)
+
+class _ConsoleFrame(object):
+ """Helper class so that we can reuse the frame console code for the
+ standalone console.
+ """
+ def __init__(self, namespace):
+ self.console = Console(namespace)
+ self.id = 0
Oops, something went wrong.

0 comments on commit ac44adf

Please sign in to comment.