ipython/iperl unification

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-05-28 15:42:40 +00:00
parent a2b8913d04
commit aa6347d4c6
4 changed files with 35 additions and 106 deletions

124
buffer.py
View File

@ -399,41 +399,16 @@ class ConsoleBuffer(Buffer):
def readonly(self): def readonly(self):
return True return True
iperl = None class InterpreterBuffer(Buffer):
class IperlBuffer(Buffer): _basename = 'Interpreter'
btype = 'iperl' def create_name(cls, parent):
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):
if hasattr(parent, 'path'): if hasattr(parent, 'path'):
return '*IPerl:%s*' % parent.name() return '*%s:%s*' % (cls._basename, parent.name())
else: else:
return '*IPerl*' return '*%s*' % cls._basename
get_iperl_name = classmethod(get_iperl_name) create_name = classmethod(create_name)
btype = 'iperl' btype = 'interpreter'
modename = 'iperl'
readre = re.compile('^([A-Z]+):(.*)\n$') readre = re.compile('^([A-Z]+):(.*)\n$')
_name = '*IPerl*'
def __init__(self, parent): def __init__(self, parent):
if hasattr(parent, 'path'): if hasattr(parent, 'path'):
self.parent = parent self.parent = parent
@ -445,14 +420,11 @@ class IperlBuffer2(Buffer):
self.prompt = '***' self.prompt = '***'
self.clear() self.clear()
self.pipe_read() self.pipe_read()
self._name = get_iperl_name(parent) self._name = self.create_name(parent)
def name(self): def name(self):
return self._name return self._name
def get_cmd(self): def get_cmd(self):
if self.parent: raise Exception, 'unimplemented'
return ('iperl', '-p', self.parent.path)
else:
return ('iperl',)
def pipe_readline(self): def pipe_readline(self):
line = self.pipe.stdout.readline() line = self.pipe.stdout.readline()
m = self.readre.match(line) m = self.readre.match(line)
@ -482,7 +454,6 @@ class IperlBuffer2(Buffer):
assert typ_ == 'COMPLETIONS', '%r %r' % (typ_, value) assert typ_ == 'COMPLETIONS', '%r %r' % (typ_, value)
candidates = [x for x in value.split('|') if x] candidates = [x for x in value.split('|') if x]
return candidates return candidates
def clear(self): def clear(self):
self.set_data('', force=True) self.set_data('', force=True)
def changed(self): def changed(self):
@ -490,72 +461,25 @@ class IperlBuffer2(Buffer):
def readonly(self): def readonly(self):
return True return True
class IpythonBuffer(Buffer): class IperlBuffer(InterpreterBuffer):
def get_ipython_name(parent): _basename = 'IPerl'
if hasattr(parent, 'path'): btype = 'iperl'
return '*IPython:%s*' % parent.name() modename = 'iperl'
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
def get_cmd(self): def get_cmd(self):
if self.parent: if self.parent:
return ('epython', '-p', self.parent.path) return ('iperl', '-p', '-r', self.parent.path)
else: else:
return ('epython',) return ('iperl', '-p')
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
def clear(self): class IpythonBuffer(InterpreterBuffer):
self.set_data('', force=True) _basename = 'IPython'
def changed(self): btype = 'ipython'
return False modename = 'ipython'
def readonly(self): def get_cmd(self):
return True if self.parent:
return ('epython', '-p', '-r', self.parent.path)
else:
return ('epython', '-p')
class BinaryDataException(Exception): class BinaryDataException(Exception):
pass pass

View File

@ -66,10 +66,11 @@ class IperlStart(method.Method):
def execute(self, w, **vargs): def execute(self, w, **vargs):
a = w.application a = w.application
if not a.has_buffer_name('*IPerl*'): if not a.has_buffer_name('*IPerl*'):
b = buffer.IperlBuffer() b = buffer.IperlBuffer(None)
a.add_buffer(b) a.add_buffer(b)
window.Window(b, a) 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: if a.window().buffer is not b:
a.switch_buffer(b) a.switch_buffer(b)
f = lambda x: None f = lambda x: None
@ -117,8 +118,11 @@ class IperlMini(mode.Fundamental):
def _get_iperl(self): def _get_iperl(self):
a = self.window.application a = self.window.application
if not a.has_buffer_name('*IPerl*'): if not a.has_buffer_name('*IPerl*'):
raise Exception, "No iperl found!" b = buffer.IperlBuffer(None)
b = a.bufferlist.get_buffer_by_name('*IPerl*') a.add_buffer(b)
window.Window(b, a)
else:
b = a.bufferlist.get_buffer_by_name('*IPerl*')
return b return b
def __init__(self, w): def __init__(self, w):

View File

@ -76,7 +76,7 @@ class IpythonPathStart(method.Method):
if w.buffer.btype == 'ipython': if w.buffer.btype == 'ipython':
b = w.buffer b = w.buffer
else: else:
name = buffer.IpythonBuffer.get_ipython_name(w.buffer) name = buffer.IpythonBuffer.create_name(w.buffer)
if not a.has_buffer_name(name): if not a.has_buffer_name(name):
b = buffer.IpythonBuffer(w.buffer) b = buffer.IpythonBuffer(w.buffer)
a.add_buffer(b) a.add_buffer(b)

View File

@ -98,7 +98,8 @@ if __name__ == "__main__":
stanzas.append(('path', value)) stanzas.append(('path', value))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option('-e', '--eval', type='string', action='callback', callback=add_eval) 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() parser.parse_args()
del parser, add_path, add_eval del parser, add_path, add_eval