1 __version__ = '030415'
2 __author__ = 'spex66@gmx.net'
3
4
5
6 from BaseService import BaseService
7 import threading, sys
8
9 from code import InteractiveConsole
10 try:
11 import readline
12 except:
13 pass
14
15 class ShellService(BaseService, InteractiveConsole):
16 """
17 A subclass of Thread to emulate a shell.
18 """
19
20 def __init__(self, engine, name=None):
21 BaseService.__init__(self, name or 'ShellService')
22 InteractiveConsole.__init__(self, locals={'_': engine})
23
24
25 # target that is executed by thread ########################################
26
27 def run(self):
28 """Closely emulate the interactive Python console."""
29
30 # INIT
31
32 try:
33 sys.ps1
34 except AttributeError:
35 sys.ps1 = ">>> "
36 try:
37 sys.ps2
38 except AttributeError:
39 sys.ps2 = "... "
40
41 cprt = 'Type "copyright", "credits" or "license" for more information.'
42
43 self.write("Python %s on %s\n%s\n(%s)\n" %
44 (sys.version, sys.platform, cprt,
45 self.__class__.__name__))
46
47 more = 0
48
49 # SERVE Python prompt
50
51 print "%s starts" % (self.getName(),)
52 while self.isNotStopped():
53 if more:
54 prompt = sys.ps2
55 else:
56 prompt = sys.ps1
57
58 try:
59 line = raw_input(prompt)
60 except EOFError:
61 print '...ShSrv EOF...'
62 continue
63
64 if line.lower() in ('bye', 'quit', 'exit'):
65 # get exit commands
66 self.loop = 0
67 self.locals['_'].post_event( ('quit', ))
68 else:
69 more = self.push(line)
70
71
72 print "%s ends" % (self.getName(),)
73
74
75 def stop(self):
76 print 'Press any key to leave the shell...'
77 BaseService.stop(self)
syntax highlighted by Code2HTML, v. 0.9.1