#!/usr/bin/env python
"""
This file contains the decorator templating code I demoed at the April 2008
Portland Python User Group meeting.  To just see the code, use your text
editor to search for the string CONTENT and you'll jump right there.  But do
come back for the commentary below.

You can run this file with Python and you'll be dropped into an interactive
shell where you can play around with the code bit by bit, like I did on the
big screen.  The command I used to get pydoc output on functions and their
signature was "help": help(splat)

I mentioned at the meeting that some things were missing for a totally
general, "library quality" implementation of the technique.

One piece is resolving name conflicts between the inspected function arguments
and the implementation of the decorator itself.  E.g. given the final
decorator in the slides, this application of it will fail:

  >>> @deprecated
  ... def somefunc(fn, warnings=False):
  ...    do_something_with(fn, warnings)

'inspect' plus control over the environment used by `exec` are sufficient
tools to detect and work around name conflicts in the template code.

The other missing piece is fairly subtle.  formatargspec as used in the
decorator will build a positional calling argument list:

  >>> def foo(x, y=123): pass
  ...
  >>> spec = inspect.getargspec(foo)
  >>> inspect.formatargspec(*spec[:3])
  '(x, y)'

  >>> c = _
  >>> "receiver%s" % c
  'receiver(x, y)'

But sometimes you want to act on the arguments as keywords::

  'receiver(x=x, y=y)

or a mix of positional and keyword arguments::

  'receiver(x, y=y)'

For example::

  >>> def receiver(*args, **kw): return args, kw
  ...
  >>> receiver(1, 123)
  ((1, 123), {})
  >>> receiver(x=1, y=123)
  ((), {'y': 123, 'x': 1})
  >>> receiver(1, y=123)
  ((1,), {'y': 123})

formatargspec provides tools to construct all of these calling options.  Dig
into the docstring for details, or just take a look at the actual
implementation in the standard library.

jason kirtland <jek@discorporate.us>
April 9th. 2008

Copyright jason kirtland
Text above: Creative Commons Attribution-Share Alike License
Code slides: the Artistic License
"""


##############################################################################
#
# sliderepl - 0.12 (hacked)
#   Copyright (c) Jason Kirtland <jek@discorporate.us>
#   sliderepl lives at http://discorporate.us/projects/sliderepl
#   sliderepl is licensed under the MIT License:
#   http://www.opensource.org/licenses/mit-license.php
#
# sliderepl may be textually included in a file that also contains its input
# data.  The input data may be under a different license. sliderepl's MIT
# License covers only this block of sliderepl code, extensions and other
# derivative works.
#
# Looking for the sample code?
#                        _ _       _
#     ___  ___ _ __ ___ | | |   __| | _____      ___ __
#    / __|/ __| '__/ _ \| | |  / _` |/ _ \ \ /\ / / '_ \
#    \__ \ (__| | | (_) | | | | (_| | (_) \ V  V /| | | |
#    |___/\___|_|  \___/|_|_|  \__,_|\___/ \_/\_/ |_| |_|
#
# This bootstrapping code loads the sample code below into an interactive
# Python session.

environ = globals().copy()
import code, inspect, itertools, logging, re, sys, traceback
try:
    import rlcompleter, readline
except ImportError:
    readline = None


class Deck(object):
    expose = ('commands', 'next', 'goto', 'show', 'info', 'quick', 'fancy')

    def __init__(self, path=None):
        self.path = path or '<no file>'
        self.slides = []
        self.current = 0
        self.enter_advances = False
        self._activate_fancy(True)

    def start(self):
        pass

    def next(self):
        """Advance to the next slide."""
        if self.current >= len(self.slides):
            self.display_meta("The slideshow is over.")
            return
        slide = self.slides[self.current]
        self.current += 1
        self.display_meta("\n", ("Running slide %s" % self.current), "\n")
        if (slide.name and
            not (slide.name.isdigit() and int(slide.name) == self.current)):
            self.display_meta(slide.name)
        slide.run()

    def slide_actor(fn):
        def decorated(self, slide_number):
            if isinstance(slide_number, str) and not slide_number.isdigit():
                self.display_meta("Usage: %s slide_number" % fn.__name__)
                return
            num = int(slide_number)
            if num < 1 or num > len(self.slides):
                self.display_meta("Slide #%s is out of range (1 - %s)." % (
                    num, len(self.slides)))
            else:
                return fn(self, num)
        decorated.__doc__ = fn.__doc__
        return decorated

    def show(self, slide_number):
        """show NUM, display a slide without executing it."""
        print str(self.slides[slide_number - 1]).strip()
    show = slide_actor(show)

    def goto(self, slide_number):
        """goto NUM, skip forward to another slide."""
        if slide_number <= self.current:
            self.display_meta("Cowardly refusing to re-run slides.")
        else:
            for _ in range(slide_number - self.current):
                self.next()
    goto = slide_actor(goto)

    def info(self):
        """Display information about this slide deck."""
        self.display_meta("Now at slide %s of %s from deck %s" % (
            self.current, len(self.slides), self.path))

    def commands(self):
        """Display this help message."""
        for cmd in self.expose:
            self.display_meta(cmd, "\t" + getattr(self, cmd).__doc__)

    def quick(self, toggle):
        """quick on|off, type enter to advance to the next slide."""
        if toggle not in ('on', 'off'):
            self.display_meta("Usage: quick on|off")
        else:
            self.enter_advances = (toggle == 'on')
            self.display_meta(
                "Quick mode %s (enter will advance to the next slide)" % (
                toggle))

    def fancy(self, toggle):
        """fancy on|off, make sliderepl output visually distinctive."""
        if toggle not in ('on', 'off'):
            self.display_meta("Usage: fancy on|off")
        else:
            self._fancy = (toggle == 'on')
            self._activate_fancy(self._fancy)
    def _activate_fancy(self, fancy):
        format = '%% %(line)s'
        if fancy:
            if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
                try:
                    import curses
                    curses.setupterm()
                except (KeyboardInterupt, SystemExit):
                    raise
                except:
                    pass
                else:
                    bold, normal = [curses.tigetstr(x)
                                    for x in ('bold', 'sgr0')]
                    if bold and normal:
                        format = bold + format + normal
        self.display_format = format
    del slide_actor

    def display_meta(self, *lines):
        for line in lines:
            print self.display_format % dict(line=line.rstrip())

    class Slide(object):
        def __init__(self, name=None):
            self.name = name
            self.codeblocks = []
            self.lines = []
            self._stack = []
            self._level = None
        def run(self):
            for display, co in self.codeblocks:
                if not getattr(self, 'no_echo', False):
                    shown = [getattr(sys, 'ps1', '>>> ') + display[0]]
                    shown.extend([getattr(sys, 'ps2', '... ') + l
                                          for l in display[1:]])
                    Deck._add_history(''.join(display).rstrip())
                    print ''.join(shown).rstrip()
                try:
                    exec co in environ
                except:
                    traceback.print_exc()
        def __str__(self):
            return ''.join(self.lines)
        def _append(self, line):
            self.lines.append(line)
            if not self._stack and line.isspace():
                return
            indent = len(line) - len(line.lstrip())
            if not self._stack:
                self._level = indent
            elif indent <= self._level:
                try:
                    co = self._compile()
                    if co:
                        self.codeblocks.append((self._pop(), co))
                except SyntaxError:
                    pass
            self._stack.append(line)
        def _close(self):
            if self._stack:
                co = self._compile()
                assert co
                self.codeblocks.append((self._pop(), co))
        def _compile(self):
            style = getattr(self, 'no_return', False) and 'exec' or 'single'
            return code.compile_command(''.join(self._stack), '<input>', style)
        def _pop(self):
            self._stack.reverse()
            lines = list(itertools.dropwhile(str.isspace, self._stack))
            lines.reverse()
            self._stack = []
            return lines

    def run(cls, path=None):
        """Run an interactive session for this Deck and exit when complete.

        If '--run-all' is first on the command line, all slides are executed
        immediately and the script will exit.  Useful for sanity testing.
        """
        if path is None:
            path = sys.argv[0]
        deck = cls.from_path(path)
        if not deck:
            sys.stderr.write("Aborting: no slides!\n")
            sys.exit(-1)

        deck.start()

        if sys.argv[1:] and sys.argv[1] == '--run-all':
            deck.goto(len(deck.slides))
            sys.exit(0)

        console = code.InteractiveConsole()
        global environ
        environ = console.locals
        console.raw_input = deck.readfunc
        if readline:
            readline.parse_and_bind('tab: complete')
            readline.set_completer(rlcompleter.Completer(environ).complete)
        deck.display_meta(*deck.banner.splitlines())
        console.interact('')
        sys.exit(0)
    run = classmethod(run)

    def from_path(cls, path):
        """Create a Deck from slides embedded in a file at path."""
        sl_re = re.compile(r'### +slide::\s*')
        a_re = re.compile(r',\s*')

        fh, deck, slide = open(path), cls(path), None
        for line in fh:
            if not sl_re.match(line):
                if slide:
                    slide._append(line)
                continue
            if slide:
                slide._close()
                deck.slides.append(slide)
            metadata = sl_re.split(line, 2)[1].split('-*-', 2)
            name = metadata[0].strip()
            if name == 'end':
                break
            slide = cls.Slide(name=name or None)
            if len(metadata) >= 2:
                for option in (metadata[1] and a_re.split(metadata[1]) or []):
                    setattr(slide, option.strip(), True)
        fh.close()
        return deck.slides and deck or None
    from_path = classmethod(from_path)

    def banner(self):
        return """\
This is an interactive Python prompt.
Extra commands: %s
Type "next" to start the presentation.""" % ', '.join(self.expose)
    banner = property(banner)

    def readfunc(self, prompt=''):
        line = raw_input(prompt)
        if prompt == getattr(sys, 'ps1', '>>> '):
            tokens = line.split()
            if line == '' and self.enter_advances:
                tokens = ('next',)
            if tokens and tokens[0] in self.expose:
                fn = getattr(self, tokens[0])
                if len(tokens) != len(inspect.getargspec(fn)[0]):
                    self.display_meta("Usage: %s %s" % (
                        tokens[0], ' '.join(inspect.getargspec(fn)[0][1:])))
                else:
                    self._add_history(line)
                    fn(*tokens[1:])
                return ''
        return line

    def _add_history(cls, line):
        if readline and line:
            readline.add_history(line)
    _add_history = classmethod(_add_history)

#  end of sliderepl
#
##############################################################################

Deck.run()

#        ____ ___  _   _ _____ _____ _   _ _____ _
#       / ___/ _ \| \ | |_   _| ____| \ | |_   _| |
#      | |  | | | |  \| | | | |  _| |  \| | | | | |
#      | |__| |_| | |\  | | | | |___| |\  | | | |_|
#       \____\___/|_| \_| |_| |_____|_| \_| |_| (_)
#
# Slide CONTENT starts here.

### slide::

def splat(times=10):
    """Return a number of splats."""
    return '*' * times

### slide::

import warnings

def deprecated(fn):
    """Decorate a function and issue a deprecation warning on execution."""
    def decorated(*args, **kwargs):
        warnings.warn(DeprecationWarning(fn.func_name))
        return fn(*args, **kwargs)
    return decorated

### slide::

import inspect

### slide:: -*- no_echo -*-

print '''\

something like:

code = """
def %(func_name)s%(func_args)s:
    warnings.warn(DeprecationWarning(fn.func_name))
    return fn(*args, **kw) <-- ???
"""
'''

### slide::

def as_template(fn):
    """Return a printable string of code."""
    spec = inspect.getargspec(fn)
    func_name = fn.func_name
    argspec = inspect.formatargspec(*spec)
    callspec = inspect.formatargspec(*spec[:3])

    text = """\
def %(func_name)s%(argspec)s:
    warnings.warn(DeprecationWarning('%(func_name)s'))
    return fn%(callspec)s
"""
    text %= dict(func_name=func_name, argspec=argspec,
                 callspec=callspec)
    return text

### slide::

def deprecated(fn):
    """Decorate a function and issue a deprecation warning on execution."""
    spec = inspect.getargspec(fn)
    func_name = fn.func_name
    argspec = inspect.formatargspec(*spec)
    callspec = inspect.formatargspec(*spec[:3])

    text = """\
def %(func_name)s%(argspec)s:
    warnings.warn(DeprecationWarning('%(func_name)s'))
    return fn%(callspec)s
"""
    text %= locals()

    env = locals().copy()
    env['warnings'] = warnings
    exec text in env
    env[func_name].func_doc = fn.func_doc
    return env[func_name]

### slide:: end


