Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/lib/purk/scripts/console.py
blob: bef8e0e623f87d9e2d4496da47067b950e582a28 (plain)
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
import sys
import traceback
import windows
from conf import conf

class ConsoleWriter:
    __slots__ = ['window']
    def __init__(self, window):
        self.window = window
    def write(self, text):
        try:
            self.window.write(text, line_ending='')
        except:
            self.window.write(traceback.format_exc())

class ConsoleWindow(windows.SimpleWindow):
    def __init__(self, network, id):
        windows.SimpleWindow.__init__(self, network, id)
    
        writer = ConsoleWriter(self)
        
        sys.stdout = writer
        sys.stderr = writer
        
        self.globals = {'window': self}
        self.locals = {}

#this prevents problems (and updates an open console window) on reload
#window = None
#for window in manager:
#    if type(window).__name__ == "ConsoleWindow":
#        window.mutate(ConsoleWindow, window.network, window.id)
#del window

def onClose(e):
    if isinstance(e.window, ConsoleWindow):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

def onCommandConsole(e):
    windows.new(ConsoleWindow, None, "console").activate() 

def onCommandSay(e):
    if isinstance(e.window, ConsoleWindow):
        import pydoc #fix nonresponsive help() command
        old_pager, pydoc.pager = pydoc.pager, pydoc.plainpager 
        e.window.globals.update(sys.modules)
        text = ' '.join(e.args)
        try:
            e.window.write(">>> %s" % text) 
            result = eval(text, e.window.globals, e.window.locals)
            if result is not None:
                e.window.write(repr(result))
            e.window.globals['_'] = result
        except SyntaxError:
            try:
                exec text in e.window.globals, e.window.locals
            except:
                traceback.print_exc()
        except:
            traceback.print_exc()
        pydoc.pager = old_pager
    else:
        raise core.events.CommandError("There's no one here to speak to.")

def onStart(e):
    if conf.get('start-console'):
        windows.new(ConsoleWindow, None, "console")