Skip to content

Commit

Permalink
MIT
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Jul 17, 2010
1 parent eb1ad35 commit 889c28d
Show file tree
Hide file tree
Showing 22 changed files with 981 additions and 13 deletions.
19 changes: 19 additions & 0 deletions LISENCE
@@ -0,0 +1,19 @@
Copyright (c) 2010 Kenneth Reitz.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
10 changes: 10 additions & 0 deletions README.mkd
@@ -0,0 +1,10 @@
ShowMe -- Quick and easy debugging for Python
=============================================

Usage
-----

Hmmm, I wonder what's being passed in there?

@showme.args
def complex_function(a, b, c):…
8 changes: 8 additions & 0 deletions fabfile.py
@@ -0,0 +1,8 @@
from fabric.api import *


def scrub():
""" Death to the bytecode! """
local("rm -fr dist build")
local("find . -name \"*.pyc\" -exec rm '{}' ';'")

13 changes: 0 additions & 13 deletions show/core.py

This file was deleted.

File renamed without changes.
22 changes: 22 additions & 0 deletions showme/core.py
@@ -0,0 +1,22 @@
from showme.packages.decorator import decorator

# ______
# ___________ /_ ______ ___ _________ ___ _____
# __ ___/__ __ \_ __ \__ | /| / /__ __ `__ \_ _ \
# _(__ ) _ / / // /_/ /__ |/ |/ / _ / / / / // __/
# /____/ /_/ /_/ \____/ ____/|__/ /_/ /_/ /_/ \___/

__version__ = '0.0.2'
__author__ = 'Kenneth Reitz'
__license__ = 'MIT'
__copyright__ = 'Copyright 2010 Kenneth Reitz'

import pprint
pp = pprint.PrettyPrinter(indent=4)

@decorator
def args(f, *args, **kwargs):
# print('Arguments: \n %s\n %s' % (args, kwargs))
# pprint.PrettyPrinter(kwargs)
pp.pprint(kwargs)
return f(*args, **kwargs)
2 changes: 2 additions & 0 deletions showme/packages/__init__.py
@@ -0,0 +1,2 @@
import decorator
import colorama
6 changes: 6 additions & 0 deletions showme/packages/colorama/__init__.py
@@ -0,0 +1,6 @@
from .initialise import init
from .ansi import Fore, Back, Style
from .ansitowin32 import AnsiToWin32

__version__ = '0.1.14'

49 changes: 49 additions & 0 deletions showme/packages/colorama/ansi.py
@@ -0,0 +1,49 @@
'''
This module generates ANSI character codes to printing colors to terminals.
See: http://en.wikipedia.org/wiki/ANSI_escape_code
'''

CSI = '\033['

def code_to_chars(code):
return CSI + str(code) + 'm'

class AnsiCodes(object):
def __init__(self, codes):
for name in dir(codes):
if not name.startswith('_'):
value = getattr(codes, name)
setattr(self, name, code_to_chars(value))

class AnsiFore:
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
RESET = 39

class AnsiBack:
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47
RESET = 49

class AnsiStyle:
BRIGHT = 1
DIM = 2
NORMAL = 22
RESET_ALL = 0

Fore = AnsiCodes( AnsiFore )
Back = AnsiCodes( AnsiBack )
Style = AnsiCodes( AnsiStyle )

176 changes: 176 additions & 0 deletions showme/packages/colorama/ansitowin32.py
@@ -0,0 +1,176 @@

import re
import sys

from .ansi import AnsiFore, AnsiBack, AnsiStyle, Style
from .winterm import WinTerm, WinColor, WinStyle
from .win32 import windll


if windll is not None:
winterm = WinTerm()


def is_a_tty(stream):
return hasattr(stream, 'isatty') and stream.isatty()


class StreamWrapper(object):
'''
Wraps a stream (such as stdout), acting as a transparent proxy for all
attribute access apart from method 'write()', which is delegated to our
Converter instance.
'''
def __init__(self, wrapped, converter):
# double-underscore everything to prevent clashes with names of
# attributes on the wrapped stream object.
self.__wrapped = wrapped
self.__convertor = converter

def __getattr__(self, name):
return getattr(self.__wrapped, name)

def write(self, text):
self.__convertor.write(text)


class AnsiToWin32(object):
'''
Implements a 'write()' method which, on Windows, will strip ANSI character
sequences from the text, and if outputting to a tty, will convert them into
win32 function calls.
'''
ANSI_RE = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')

def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
# The wrapped stream (normally sys.stdout or sys.stderr)
self.wrapped = wrapped

# should we reset colors to defaults after every .write()
self.autoreset = autoreset

# create the proxy wrapping our output stream
self.stream = StreamWrapper(wrapped, self)

on_windows = sys.platform.startswith('win')

# should we strip ANSI sequences from our output?
if strip is None:
strip = on_windows
self.strip = strip

# should we should convert ANSI sequences into win32 calls?
if convert is None:
convert = on_windows and is_a_tty(wrapped)
self.convert = convert

# dict of ansi codes to win32 functions and parameters
self.win32_calls = self.get_win32_calls()

# are we wrapping stderr?
self.on_stderr = self.wrapped is sys.stderr


def should_wrap(self):
'''
True if this class is actually needed. If false, then the output
stream will not be affected, nor will win32 calls be issued, so
wrapping stdout is not actually required. This will generally be
False on non-Windows platforms, unless optional functionality like
autoreset has been requested using kwargs to init()
'''
return self.convert or self.strip or self.autoreset


def get_win32_calls(self):
if self.convert and winterm:
return {
AnsiStyle.RESET_ALL: (winterm.reset_all, ),
AnsiStyle.BRIGHT: (winterm.style, WinStyle.BRIGHT),
AnsiStyle.DIM: (winterm.style, WinStyle.NORMAL),
AnsiStyle.NORMAL: (winterm.style, WinStyle.NORMAL),
AnsiFore.BLACK: (winterm.fore, WinColor.BLACK),
AnsiFore.RED: (winterm.fore, WinColor.RED),
AnsiFore.GREEN: (winterm.fore, WinColor.GREEN),
AnsiFore.YELLOW: (winterm.fore, WinColor.YELLOW),
AnsiFore.BLUE: (winterm.fore, WinColor.BLUE),
AnsiFore.MAGENTA: (winterm.fore, WinColor.MAGENTA),
AnsiFore.CYAN: (winterm.fore, WinColor.CYAN),
AnsiFore.WHITE: (winterm.fore, WinColor.GREY),
AnsiFore.RESET: (winterm.fore, ),
AnsiBack.BLACK: (winterm.back, WinColor.BLACK),
AnsiBack.RED: (winterm.back, WinColor.RED),
AnsiBack.GREEN: (winterm.back, WinColor.GREEN),
AnsiBack.YELLOW: (winterm.back, WinColor.YELLOW),
AnsiBack.BLUE: (winterm.back, WinColor.BLUE),
AnsiBack.MAGENTA: (winterm.back, WinColor.MAGENTA),
AnsiBack.CYAN: (winterm.back, WinColor.CYAN),
AnsiBack.WHITE: (winterm.back, WinColor.GREY),
AnsiBack.RESET: (winterm.back, ),
}


def write(self, text):
if self.strip or self.convert:
self.write_and_convert(text)
else:
self.wrapped.write(text)
self.wrapped.flush()
if self.autoreset:
self.reset_all()


def reset_all(self):
if self.convert:
self.call_win32('m', (0,))
else:
self.wrapped.write(Style.RESET_ALL)


def write_and_convert(self, text):
'''
Write the given text to our wrapped stream, stripping any ANSI
sequences from the text, and optionally converting them into win32
calls.
'''
cursor = 0
for match in self.ANSI_RE.finditer(text):
start, end = match.span()
self.write_plain_text(text, cursor, start)
self.convert_ansi(*match.groups())
cursor = end
self.write_plain_text(text, cursor, len(text))


def write_plain_text(self, text, start, end):
if start < end:
self.wrapped.write(text[start:end])
self.wrapped.flush()


def convert_ansi(self, paramstring, command):
if self.convert:
params = self.extract_params(paramstring)
self.call_win32(command, params)


def extract_params(self, paramstring):
def split(paramstring):
for p in paramstring.split(';'):
if p != '':
yield int(p)
return tuple(split(paramstring))


def call_win32(self, command, params):
if params == []:
params = [0]
if command == 'm':
for param in params:
if param in self.win32_calls:
func_args = self.win32_calls[param]
func = func_args[0]
args = func_args[1:]
kwargs = dict(on_stderr=self.on_stderr)
func(*args, **kwargs)

32 changes: 32 additions & 0 deletions showme/packages/colorama/initialise.py
@@ -0,0 +1,32 @@
import atexit
import sys

from .ansitowin32 import AnsiToWin32


orig_stdout = sys.stdout
orig_stderr = sys.stderr


@atexit.register
def reset_all():
AnsiToWin32(orig_stdout).reset_all()


def init(autoreset=False, convert=None, strip=None, wrap=True):

if wrap==False and (autoreset==True or convert==True or strip==True):
raise ValueError('wrap=False conflicts with any other arg=True')

sys.stdout = wrap_stream(orig_stdout, convert, strip, autoreset, wrap)
sys.stderr = wrap_stream(orig_stderr, convert, strip, autoreset, wrap)


def wrap_stream(stream, convert, strip, autoreset, wrap):
if wrap:
wrapper = AnsiToWin32(stream,
convert=convert, strip=strip, autoreset=autoreset)
if wrapper.should_wrap():
stream = wrapper.stream
return stream

File renamed without changes.

0 comments on commit 889c28d

Please sign in to comment.