Skip to content

Commit 941b807

Browse files
made history persistence optional
- also included completer namespace initialization fix
1 parent 9718f79 commit 941b807

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

cocos/interpreter/history/_readline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def load(self, filename, size):
1212
readline.set_history_length(size)
1313

1414
def save(self):
15-
if self.filename:
15+
if self.persistent and self.filename:
1616
readline.write_history_file(self.filename)
1717

1818
def reset(self, size):

cocos/interpreter/history/history.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
class History(object):
66

7-
def __init__(self, filename=None, size=100):
7+
def __init__(self, filename=None, size=100, persistent=False):
88
self.filename = filename
99
self.size = size
10+
self.persistent = persistent
1011
if filename and os.path.exists(filename):
1112
self.load(filename, size)
1213
else:
@@ -17,13 +18,13 @@ def __len__(self):
1718
return len(self.data)
1819

1920
def __del__(self):
20-
self.save()
21+
self.persist()
2122

2223
def load(self, filename, size):
2324
self.data = pickle.load(open(filename, 'rb'))
2425

25-
def save(self):
26-
if self.filename:
26+
def persist(self):
27+
if self.persistent and self.filename:
2728
pickle.dump(self.data, open(self.filename, 'wb'))
2829

2930
def reset(self, size):

cocos/interpreter/interpreter.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
class Interpreter(InteractiveInterpreter, EventDispatcher):
2121
def __init__(self, ns_locals=None, ns_globals=None,
2222
stdin=None, stdout=None, stderr=None,
23-
history_file=None, history_size=100):
23+
history_file=None, history_size=100,
24+
history_persistent=False):
2425

2526
# redirect input and output
2627
if stdin is None:
@@ -39,16 +40,19 @@ def __init__(self, ns_locals=None, ns_globals=None,
3940
if ns_globals is None:
4041
ns_globals = {}
4142

42-
self.history = self.init_history(history_file, history_size)
43-
self.completer = self.init_completer(ns_locals)
44-
super(Interpreter, self).__init__(locals=ns_locals)
43+
self.history = self.init_history(history_file, history_size,
44+
history_persistent)
45+
namespace = ns_globals.copy()
46+
namespace.update(ns_locals)
47+
self.completer = self.init_completer(namespace)
48+
super(Interpreter, self).__init__(locals=namespace)
4549

46-
def init_history(self, filename, size):
50+
def init_history(self, filename, size, persistent):
4751
if has_readline:
4852
from history import ReadlineHistory
49-
history = ReadlineHistory(filename, size)
53+
history = ReadlineHistory(filename, size, persistent)
5054
else:
51-
history = History(filename, size)
55+
history = History(filename, size, persistent)
5256
return history
5357

5458
def init_completer(self, namespace=None):
@@ -81,14 +85,14 @@ def toggle_env(self):
8185
# Common Events
8286
#################
8387
def on_exit(self):
84-
self.history.save()
88+
self.history.persist()
8589

8690
#################
8791
# Command Events
8892
#################
8993
def on_command(self, command):
9094
self.execute(command)
91-
self.stdout.dispatch_event('on_command_done')
95+
self.dispatch_event('on_command_done')
9296

9397
def on_completion(self, command):
9498
if not command.strip():

cocos/layer/interpreter.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ class InterpreterLayer(Layer, EventDispatcher):
2121

2222
history_file = '.cocos_history'
2323
history_size = 100
24+
history_persistent = False
2425

2526
is_event_handler = True #: enable pyglet's events
2627

27-
def __init__(self, ns_locals=None):
28+
def __init__(self, ns_locals=None, ns_globals=None):
2829
super(InterpreterLayer, self).__init__()
2930
self.init_config()
3031
self.init_background()
31-
self.init_interpreter(ns_locals)
32+
self.init_interpreter(ns_locals, ns_globals)
3233
self.init_text()
3334

3435
def init_config(self):
@@ -52,14 +53,17 @@ def init_background(self):
5253
bg.y = bg_y
5354
self.add(bg, z=-1)
5455

55-
def init_interpreter(self, ns_locals=None):
56-
if ns_locals is None:
57-
ns_locals = director.interpreter_locals
58-
ns_locals['self'] = self
56+
def init_interpreter(self, ns_locals=None, ns_globals=None):
57+
if ns_globals is None:
58+
ns_globals = director.interpreter_locals
59+
ns_globals['self'] = self
5960
self.interpreter = Interpreter(stdin=self, stdout=self, stderr=self,
60-
ns_locals=ns_locals,
61+
ns_locals=ns_locals, ns_globals=ns_globals,
6162
history_file=self.history_file,
62-
history_size=self.history_size)
63+
history_size=self.history_size,
64+
history_persistent=self.history_persistent)
65+
# register event handlers
66+
self.interpreter.push_handlers(self)
6367

6468
def init_text(self):
6569
self.content = self.prompt

0 commit comments

Comments
 (0)