Repository navigation
Expand file tree
/
Copy pathpython-csp
More file actions
134 lines (101 loc) · 3.98 KB
/
Copy pathpython-csp
File metadata and controls
134 lines (101 loc) · 3.98 KB
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
#!/usr/bin/env python
"""Interactive interpreter for python-csp, with online help.
Features:
* CSP primitives are imported automatically.
* History is saved between sessions in C{~/.csp-console-history}.
* Tab-completion can be used to complete keywords or variables.
Copyright (C) Sarah Mount, 2009.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
import atexit
import code
import os
import sys
__date__ = 'December 2008'
class _Printer(object):
"""Print documentation in twenty-line chunks.
Based on a class of the same name from the site.py module.
"""
MAXLINES = 20
def __init__(self, documentation):
self.__lines = documentation.split('\n')
self.__linecnt = len(self.__lines)
def __call__(self):
prompt = '\n*** Hit Return for more, or q (and Return) to quit: '
lineno = 0
while True:
try:
for i in range(lineno, lineno + self.MAXLINES):
print(self.__lines[i])
except IndexError:
break
else:
lineno += self.MAXLINES
key = None
while key not in ('', 'q', 'Q'):
key = input(prompt)
if key == 'q':
break
print()
class TabSafeCompleter(rlcompleter.Completer):
"""Enable safe use of Tab for either tab completion or nested scope.
"""
def complete(self, text, state):
if text == '':
return ['\t', None][state]
else:
return rlcompleter.Completer.complete(self, text, state)
class CSPConsole(code.InteractiveConsole):
"""python-csp interactive console with REPL.
Features:
* CSP channel server is started automatically.
* CSP primitives are imported automatically.
* History is saved between sessions in C{~/.csp-console-history}.
* Tab-completion can be used to complete keywords or variables.
"""
# From the docs of the readline module.
def __init__(self, locals=None, filename="<console>",
histfile=os.path.expanduser("~/.csp-console-history")):
code.InteractiveConsole.__init__(self)
self.init_history(histfile)
def init_history(self, histfile):
readline.parse_and_bind("tab: complete")
delims = ' \t\n`!@#$%^&*()-=+[{]}\\|;:,<>?'
readline.set_completer_delims(delims)
readline.set_completer(TabSafeCompleter().complete)
if hasattr(readline, "read_history_file"):
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(self.save_history, histfile)
def save_history(self, histfile):
readline.write_history_file(histfile)
def raw_input(self, *args):
return code.InteractiveConsole.raw_input(self, *args)
_ban = "\npython-csp (c) 2008. Licensed under the GPL(v2).\n\n"
if __name__ == '__main__':
c = CSPConsole(locals=locals())
# Don't expect the csp types to be available in locals()
c.push('from csp.csp import *')
c.push('from csp.builtins import *')
c.push('from csp.guards import *')
c.interact(banner=_ban)