summaryrefslogtreecommitdiff
path: root/modules/profiles/dev/pystartup.py
blob: adde66c91152ed694f866cc68771b1a3bfe794f9 (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
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
import atexit
import os
import readline
import rlcompleter
import sys
from code import InteractiveConsole
from tempfile import mkstemp

readline.parse_and_bind("tab: complete")


class TermColors(dict):
    color_templates = (
        ("Normal", "0"),
        ("Black", "0;30"),
        ("Red", "0;31"),
        ("Green", "0;32"),
        ("Brown", "0;33"),
        ("Blue", "0;34"),
        ("Purple", "0;35"),
        ("Cyan", "0;36"),
        ("LightGray", "0;37"),
        ("DarkGray", "1;30"),
        ("LightRed", "1;31"),
        ("LightGreen", "1;32"),
        ("Yellow", "1;33"),
        ("LightBlue", "1;34"),
        ("LightPurple", "1;35"),
        ("LightCyan", "1;36"),
        ("White", "1;37"),
    )
    color_base = "\001\033[%sm\002"

    def __init__(self):
        self.update(dict([(k, self.color_base % v) for k, v in self.color_templates]))


class Completer(object):
    def save_history(self):
        import readline

        readline.write_history_file(self.python_histfile)

    def __init__(self):
        self.python_dir = os.path.expanduser("%s/python" % os.environ["XDG_DATA_HOME"])

        if not os.path.exists(self.python_dir):
            os.mkdir(self.python_dir)

        self.python_histfile = os.path.expanduser("%s/history" % self.python_dir)

        if os.path.exists(self.python_histfile):
            readline.read_history_file(self.python_histfile)

        readline.set_history_length(1000)
        atexit.register(self.save_history)


def DisplayHook(value):
    if value is not None:
        try:
            import __builtin__

            __builtin__._ = value
        except ImportError:
            __builtins__._ = value

        import pprint

        pprint.pprint(value)
        del pprint


class EditableBufferInteractiveConsole(InteractiveConsole):
    def __init__(self, *args, **kwargs):
        self.last_buffer = []
        InteractiveConsole.__init__(self, *args, **kwargs)

    def runsource(self, source, *args):
        self.last_buffer = [source.encode("utf-8")]
        return InteractiveConsole.runsource(self, source, *args)

    def raw_input(self, *args):
        line = InteractiveConsole.raw_input(self, *args)

        if line == EDIT_CMD:
            tmp_fd, tmp_file = mkstemp(".py")

            os.write(tmp_fd, b"\n".join(self.last_buffer))
            os.close(tmp_fd)

            os.system("%s %s" % (EDITOR, tmp_file))

            line = open(tmp_file).read()

            os.unlink(tmp_file)
            tmp_file = ""

            lines = line.split("\n")

            for i in range(len(lines) - 1):
                self.push(lines[i])

            line = lines[-1]
        return line


TC = TermColors()
ps1 = "%sλ%s %s>%s "
sys.ps1 = ps1 % (TC["Blue"], TC["Normal"], TC["White"], TC["Normal"])
ps2 = "    %s%s %s>%s "
sys.ps2 = ps2 % (TC["Blue"], TC["Normal"], TC["White"], TC["Normal"])
sys.displayhook = DisplayHook

C = Completer()
EDITOR = os.environ.get("EDITOR", "vim")
EDIT_CMD = ":e"
C = EditableBufferInteractiveConsole(locals=locals())
C.interact(banner="")

sys.exit()