Source

sliderepl / sliderepl / core.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# sliderepl
#   Copyright (c) Jason Kirtland <jek@discorporate.us>
#   sliderepl is released under the MIT License:
#   http://www.opensource.org/licenses/mit-license.php
#   Modified by Jason Kirtland and Mike Bayer for PyCon 2009

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

if sys.platform == 'win32':
    clearcmd = "cls"
else:
    clearcmd = "clear"

class Deck(object):
    expose = ('next', 'goto', 'show', 'info', 'prev','quick',
              'rerun', 'xecute', 'presentation', 'timer')

    def __init__(self, path=None, **options):
        self.path = path or '<no file>'
        self.slides = []
        self.current = 0
        self.color = options.get('color', None)
        self._set_presentation(options.get('presentation', False))
        self._set_quick(options.get('quick', True))
        self._set_show_timer(options.get('timer', False))
        self.pending_exec = False
        self._letter_commands = {}
        self._expose_map = dict((name, getattr(self, name))
                                for name in self.expose)

        for name in self.expose:
            c = name[0]
            if c in self._expose_map:
                c = name[0:2]
            self._expose_map[c] = getattr(self, name)
            self._letter_commands[name] = c
        self._expose_map['?'] = self.commands

    def start(self):
        pass

    def _set_show_timer(self, mode):
        self._show_timer = mode
        print "%% show timer is now %s" % (self._show_timer and "ON" or "OFF")

    def _set_presentation(self, mode):
        self._presentation = mode
        print "%% presentation mode is now %s" % (self._presentation and "ON" or "OFF")

    def presentation(self):
        """Toggle presentation mode"""
        self._set_presentation(not self._presentation)

    def quick(self):
         """quick on|off, type enter to advance to the next slide."""

         self._set_quick(not self._quick)

    def timer(self):
        """Show current time in slide header"""

        self._set_show_timer(not self._show_timer)

    def _set_quick(self, mode):
        self._quick = mode
        print "%% quick mode (enter advances) is now %s"  % (self._quick and "ON" or "OFF")

    def xecute(self):
        """Execute the code for the current slide."""

        self._do_slide(self.current, run=True, echo=False)
        self.pending_exec = False

    def rerun(self):
        """Re-run the current slide."""

        self.pending_exec = False
        self._do_slide(self.current)

    def prev(self):
        """Advance to the previous slide."""

        self.pending_exec = False
        self.current -= 1

        self._do_slide(self.current)

    def next(self):
        """Advance to the next slide."""
        self._next()

    def _next(self, run=True):
        """Advance to the next slide."""

        if self.pending_exec:
            self.xecute()
            return

        if self.current >= len(self.slides):
            print "% The slideshow is over."
            return
        self.current += 1

        self._do_slide(self.current, run=run)

    def _do_slide(self, num, run=True, echo=True):
        slide = self.slides[num - 1]
        if self._show_timer:
            timer = " " + datetime.now().strftime("%H:%M:%S")
        else:
            timer = ""
        if echo:
            if self._presentation:
                if not getattr(slide, 'no_clear', False):
                    os.system(clearcmd)
                    print slide._banner(timer)
                else:
                    print ""
            else:
                print slide._banner(timer)

        slide.run(run=run, echo=echo)
        if run != 'force' and getattr(slide, 'no_exec', False):
            self.pending_exec = True

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

    @slide_actor
    def show(self, slide_number):
        """show NUM, display a slide without executing it."""

        self._do_slide(slide_number, False)

    @slide_actor
    def goto(self, slide_number):
        """goto NUM, skip forward to another slide or go back to a previous slide."""

        self.pending_exec = False
        if slide_number <= self.current:
            self.current = slide_number
            self._do_slide(self.current, run='force')
        else:
            for _ in range(slide_number - self.current):
                self._next(run='force')

    def info(self):
        """Display information about this slide deck."""
        print "%% 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:
            print "% " + cmd + \
                (cmd in self._letter_commands and " / " + self._letter_commands[cmd] or "")
            print "%\t" + self._expose_map[cmd].__doc__

    del slide_actor

    class Slide(object):
        def __init__(self, deck, file, index):
            self.deck = deck
            self.codeblocks = []
            self.lines = []
            self.intro = []
            self._stack = []
            self._level = None
            self.file = file
            self.index = index
            self.title = None

        def _banner(self, timer):
            banner = ""

            box_size = 63

            header = "%s / %d" % (self.file, self.index)
            if timer:
                header += " / " + timer

            if header:
                box_size = max(box_size, len(header))

            title = None

            if self.title:
                title = "%%% " + self.title

                box_size = max(box_size, len(title))
            if self.intro:
                box_size = max(*[box_size] + [len(l) for l in self.intro])

            box_size += 4

            if not self.deck._presentation:
                banner += "\n"

            banner += "+%s+\n" % ("-" * (box_size - 1))

            if title or self.intro:

                if title:
                    banner += "| %s%s|\n" % (title, (" " * (box_size - len(title) - 2)))

                if self.intro:
                    banner += "\n".join(
                                        "| %s%s|" % (l, (" " * (box_size - len(l) - 2)))
                                        for l in self.intro) + "\n"

            banner += "|%s%s |\n" % (" " * (box_size - len(header) - 2), header)

            banner += "+%s+\n" % ("-" * (box_size - 1))

            return banner

        def run(self, run=True, echo=True):
            if run is True and \
                echo and getattr(self, 'no_exec', False):
                run = False

            for i, (display, co) in enumerate(self.codeblocks):
                if echo and not getattr(self, 'no_echo', False):
                    shown = [getattr(sys, 'ps1', '>>> ') + display[0]]

                    space = False
                    for l in display[1:]:
                        if l.startswith(' '):
                            shown.append(getattr(sys, 'ps2', '... ') + l)
                        elif not l.isspace():
                            shown.append(getattr(sys, 'ps1', '>>> ') + l)
                        else:
                            shown.append(l)

                    #shown.extend([getattr(sys, 'ps2', '... ') + l
                    #                      for l in display[1:] if not ])


                    Deck._add_history(''.join(display).rstrip())
                    shown = ''.join(shown).rstrip()

                    print shown

                    if len(display) > 1:
                        if not re.match(r'#[\s\n]', display[0]) or \
                            (i + 1< len(self.codeblocks) and 
                                not re.match(r'#[\s\n]', self.codeblocks[i+1][0][0])):
                            print ""

                if run:
                    try:
                        exec co in environ
                    except:
                        traceback.print_exc()
            if not run:
                print "%%% next to execute"
                #print getattr(sys, 'ps1', '>>> ') + ("# next to execute")

        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'
            try:
                return code.compile_command(''.join(self._stack), '<input>', style)
            except:
                print "code:", ''.join(self._stack)
                raise

        def _pop(self):
            self._stack.reverse()
            lines = list(self._stack) #itertools.dropwhile(str.isspace, self._stack))
            lines.reverse()
            self._stack = []
            return lines

    @classmethod
    def run(cls, path=None, **options):
        """Run an interactive session for a Deck and exit when complete."""
        if path is None:
            path = sys.argv[0]
        deck = cls.from_path(path, **options)
        if not deck:
            sys.stderr.write("Aborting: no slides!\n")
            sys.exit(-1)

        deck.start()

        if options.get('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)
        console.interact(deck.banner)
        sys.exit(0)

    @classmethod
    def from_path(cls, path, **options):
        """Create a Deck from slides embedded in a file at path."""

        deck = cls(path, **options)
        cls._slides_from_file(path, deck)
        return deck.slides and deck or None

    @classmethod
    def _slides_from_file(cls, path, deck):
        s_re = re.compile(r'### +slide::(?:\s*(\d+|end))?'
                          r'(?:\s+-\*-\s*(.*?)-\*-)?')
        f_re = re.compile(r'### +file::(.+)$')
        t_re = re.compile(r'### +title::(.+)$')
        t_re_2 = re.compile(r'^#####* (.+) #####*$')

        e_re = re.compile(r'### +encoded::')
        c_re = re.compile(r'#($| .*$)')
        a_re = re.compile(r',\s*')

        slide  = None
        with open(path) as fh:
            lines = list(fh)
        while lines:
            line = lines.pop(0)
            m = f_re.match(line)
            if m:
                f_path = os.path.normpath(
                            os.path.join(
                                os.path.dirname(path), 
                                m.group(1).strip()))
                cls._slides_from_file(f_path, deck)
                if lines and not f_re.match(lines[0]):
                    lines.pop(0) # suppress next line
                continue

            m = t_re.match(line)
            if not m:
                m = t_re_2.match(line)
            if m:
                if slide:
                    slide.title = m.group(1).strip()
                    slide.intro = []
                    slide.lines = []
                continue

            if slide and not has_body:
                m = c_re.match(line)
                if m:
                    slide.intro.append(m.group(1).strip())
                    continue
                elif not line.isspace():
                    has_body = True
                    slide.lines = []
                elif slide.intro:
                    slide.intro.append("")

            m = e_re.match(line)
            if m:
                encoded = ""
                while lines:
                    line = lines.pop(0)
                    if not line.startswith("###"):
                        break
                    else:
                        encoded += line[4:]
                for line in encoded.decode("base64").decode("zlib").split("\n"):
                    slide._append(line + "\n")

            m = s_re.match(line)
            if not m:
                if slide:
                    if not line.isspace() or slide.lines:
                        slide._append(line)
                continue

            if slide:
                slide._close()
                deck.slides.append(slide)

            number, opts = m.groups()
            if number == 'end':
                break
            slide = cls.Slide(deck, file=fh.name, index=len(deck.slides) + 1)
            for option in opts and a_re.split(opts) or []:
                setattr(slide, option.strip(), True)
            has_body = getattr(slide, 'no_clear', False)

    def show_banner(self):
        print self.banner

    @property
    def banner(self):

        return """\
%% 
%% Slide Runner
%%
%% This is an interactive Python prompt.
%% Enter "?" for help.
%% Advance slides by pressing "enter" (quick mode), 
%% or entering the "n" or "next" command."""

    def readfunc(self, prompt=''):
        line = raw_input(prompt)
        if prompt == getattr(sys, 'ps1', '>>> '):
            tokens = line.split()
            if line == '' and self._quick:
                tokens = ('next',)

            if tokens and tokens[0] in self._expose_map:
                fn = self._expose_map[tokens[0]]
                if len(tokens) != len(inspect.getargspec(fn)[0]):
                    print "usage: %s %s" % (
                        tokens[0], ' '.join(inspect.getargspec(fn)[0][1:]))
                else:
                    self._add_history(line)
                    fn(*tokens[1:])
                return ''
        return line

    @classmethod
    def _add_history(cls, line):
        if readline and line:
            readline.add_history(line)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.