From aa6347d4c6083e9da8c6e2c66cc694ba2e9d2de1 Mon Sep 17 00:00:00 2001 From: moculus Date: Wed, 28 May 2008 15:42:40 +0000 Subject: [PATCH] ipython/iperl unification --HG-- branch : pmacs2 --- buffer.py | 124 +++++++++----------------------------------- mode/iperlmini.py | 12 +++-- mode/ipythonmini.py | 2 +- tools/epython | 3 +- 4 files changed, 35 insertions(+), 106 deletions(-) diff --git a/buffer.py b/buffer.py index 00c9446..4ea822e 100644 --- a/buffer.py +++ b/buffer.py @@ -399,41 +399,16 @@ class ConsoleBuffer(Buffer): def readonly(self): return True -iperl = None -class IperlBuffer(Buffer): - btype = 'iperl' - modename = 'iperl' - def __new__(cls, *args, **kwargs): - global iperl - if iperl is None: - iperl = object.__new__(IperlBuffer, *args, **kwargs) - return iperl - def __init__(self): - Buffer.__init__(self) - self.clear() - def clear(self): - iperl.set_data('', force=True) - def name(self): - return '*IPerl*' - def changed(self): - return False - def close(self): - global iperl - iperl = None - def readonly(self): - return True - -class IperlBuffer2(Buffer): - def get_iperl_name(parent): +class InterpreterBuffer(Buffer): + _basename = 'Interpreter' + def create_name(cls, parent): if hasattr(parent, 'path'): - return '*IPerl:%s*' % parent.name() + return '*%s:%s*' % (cls._basename, parent.name()) else: - return '*IPerl*' - get_iperl_name = classmethod(get_iperl_name) - btype = 'iperl' - modename = 'iperl' + return '*%s*' % cls._basename + create_name = classmethod(create_name) + btype = 'interpreter' readre = re.compile('^([A-Z]+):(.*)\n$') - _name = '*IPerl*' def __init__(self, parent): if hasattr(parent, 'path'): self.parent = parent @@ -445,14 +420,11 @@ class IperlBuffer2(Buffer): self.prompt = '***' self.clear() self.pipe_read() - self._name = get_iperl_name(parent) + self._name = self.create_name(parent) def name(self): return self._name def get_cmd(self): - if self.parent: - return ('iperl', '-p', self.parent.path) - else: - return ('iperl',) + raise Exception, 'unimplemented' def pipe_readline(self): line = self.pipe.stdout.readline() m = self.readre.match(line) @@ -482,7 +454,6 @@ class IperlBuffer2(Buffer): assert typ_ == 'COMPLETIONS', '%r %r' % (typ_, value) candidates = [x for x in value.split('|') if x] return candidates - def clear(self): self.set_data('', force=True) def changed(self): @@ -490,72 +461,25 @@ class IperlBuffer2(Buffer): def readonly(self): return True -class IpythonBuffer(Buffer): - def get_ipython_name(parent): - if hasattr(parent, 'path'): - return '*IPython:%s*' % parent.name() - else: - return '*IPython*' - get_ipython_name = classmethod(get_ipython_name) - btype = 'ipython' - modename = 'ipython' - readre = re.compile('^([A-Z]+):(.*)\n$') - _name = '*IPython*' - def __init__(self, parent): - if hasattr(parent, 'path'): - self.parent = parent - else: - self.parent = None - Buffer.__init__(self) - f = open('/dev/null', 'w') - self.pipe = Popen(self.get_cmd(), stdin=PIPE, stdout=PIPE, stderr=f) - self.prompt = '***' - self.clear() - self.pipe_read() - self._name = get_ipython_name(parent) - def name(self): - return self._name +class IperlBuffer(InterpreterBuffer): + _basename = 'IPerl' + btype = 'iperl' + modename = 'iperl' def get_cmd(self): if self.parent: - return ('epython', '-p', self.parent.path) + return ('iperl', '-p', '-r', self.parent.path) else: - return ('epython',) - def pipe_readline(self): - line = self.pipe.stdout.readline() - m = self.readre.match(line) - if m: - return (m.group(1), m.group(2)) - else: - return (None, line.rstrip()) - def pipe_read(self): - lines = [] - while True: - (type_, value) = self.pipe_readline() - if type_ == 'PROMPT': - self.prompt = value.strip() + ' ' - break - value.rstrip() - if value: - lines.append(value) - if lines: - output = '\n'.join(lines) + '\n' - p = self.get_buffer_end() - self.insert_string(p, output, force=True) - def pipe_write(self, s): - self.pipe.stdin.write("%s\n" % s) - self.pipe.stdin.flush() - def pipe_read_completions(self): - (typ_, value) = self.pipe_readline() - assert typ_ == 'COMPLETIONS', '%r %r' % (typ_, value) - candidates = [x for x in value.split('|') if x] - return candidates + return ('iperl', '-p') - def clear(self): - self.set_data('', force=True) - def changed(self): - return False - def readonly(self): - return True +class IpythonBuffer(InterpreterBuffer): + _basename = 'IPython' + btype = 'ipython' + modename = 'ipython' + def get_cmd(self): + if self.parent: + return ('epython', '-p', '-r', self.parent.path) + else: + return ('epython', '-p') class BinaryDataException(Exception): pass diff --git a/mode/iperlmini.py b/mode/iperlmini.py index 6d158e6..7378b85 100644 --- a/mode/iperlmini.py +++ b/mode/iperlmini.py @@ -66,10 +66,11 @@ class IperlStart(method.Method): def execute(self, w, **vargs): a = w.application if not a.has_buffer_name('*IPerl*'): - b = buffer.IperlBuffer() + b = buffer.IperlBuffer(None) a.add_buffer(b) window.Window(b, a) - b = a.bufferlist.get_buffer_by_name('*IPerl*') + else: + b = a.bufferlist.get_buffer_by_name('*IPerl*') if a.window().buffer is not b: a.switch_buffer(b) f = lambda x: None @@ -117,8 +118,11 @@ class IperlMini(mode.Fundamental): def _get_iperl(self): a = self.window.application if not a.has_buffer_name('*IPerl*'): - raise Exception, "No iperl found!" - b = a.bufferlist.get_buffer_by_name('*IPerl*') + b = buffer.IperlBuffer(None) + a.add_buffer(b) + window.Window(b, a) + else: + b = a.bufferlist.get_buffer_by_name('*IPerl*') return b def __init__(self, w): diff --git a/mode/ipythonmini.py b/mode/ipythonmini.py index 19c1e78..4b838bd 100644 --- a/mode/ipythonmini.py +++ b/mode/ipythonmini.py @@ -76,7 +76,7 @@ class IpythonPathStart(method.Method): if w.buffer.btype == 'ipython': b = w.buffer else: - name = buffer.IpythonBuffer.get_ipython_name(w.buffer) + name = buffer.IpythonBuffer.create_name(w.buffer) if not a.has_buffer_name(name): b = buffer.IpythonBuffer(w.buffer) a.add_buffer(b) diff --git a/tools/epython b/tools/epython index ece197e..35957a2 100755 --- a/tools/epython +++ b/tools/epython @@ -98,7 +98,8 @@ if __name__ == "__main__": stanzas.append(('path', value)) parser = optparse.OptionParser() parser.add_option('-e', '--eval', type='string', action='callback', callback=add_eval) - parser.add_option('-p', '--path', type='string', action='callback', callback=add_path) + parser.add_option('-r', '--run', type='string', action='callback', callback=add_path) + parser.add_option('-p', '--pipe', action='store_true') parser.parse_args() del parser, add_path, add_eval