Author: Ronny Pfannschmidt <[email protected]>
Branch:
Changeset: r177:92c4f16f275e
Date: 2012-05-03 10:45 +0200
http://bitbucket.org/pypy/pyrepl/changeset/92c4f16f275e/
Log: kill unused c imports and pep8 cleanup of reader
diff --git a/pyrepl/reader.py b/pyrepl/reader.py
--- a/pyrepl/reader.py
+++ b/pyrepl/reader.py
@@ -30,6 +30,7 @@
unichr = chr
basestring = bytes, str
+
def _make_unctrl_map():
uc_map = {}
for c in map(unichr, range(256)):
@@ -38,54 +39,47 @@
for i in range(32):
c = unichr(i)
uc_map[c] = '^' + unichr(ord('A') + i - 1)
- uc_map[b'\t'] = ' ' # display TABs as 4 characters
+ uc_map[b'\t'] = ' ' # display TABs as 4 characters
uc_map[b'\177'] = unicode('^?')
for i in range(256):
c = unichr(i)
if c not in uc_map:
- uc_map[c] = unicode('\\%03o')%i
+ uc_map[c] = unicode('\\%03o') % i
return uc_map
-# disp_str proved to be a bottleneck for large inputs, so it's been
-# rewritten in C; it's not required though.
-try:
- raise ImportError # currently it's borked by the unicode support
- from _pyrepl_utils import disp_str, init_unctrl_map
+def _my_unctrl(c, u=_make_unctrl_map()):
+ if c in u:
+ return u[c]
+ else:
+ if unicodedata.category(c).startswith('C'):
+ return b'\u%04x' % ord(c)
+ else:
+ return c
- init_unctrl_map(_make_unctrl_map())
- del init_unctrl_map
-except ImportError:
- def _my_unctrl(c, u=_make_unctrl_map()):
- if c in u:
- return u[c]
- else:
- if unicodedata.category(c).startswith('C'):
- return b'\u%04x'%(ord(c))
- else:
- return c
+def disp_str(buffer, join=''.join, uc=_my_unctrl):
+ """ disp_str(buffer:string) -> (string, [int])
- def disp_str(buffer, join=''.join, uc=_my_unctrl):
- """ disp_str(buffer:string) -> (string, [int])
+ Return the string that should be the printed represenation of
+ |buffer| and a list detailing where the characters of |buffer|
+ get used up. E.g.:
- Return the string that should be the printed represenation of
- |buffer| and a list detailing where the characters of |buffer|
- get used up. E.g.:
+ >>> disp_str(chr(3))
+ ('^C', [1, 0])
- >>> disp_str(chr(3))
- ('^C', [1, 0])
+ the list always contains 0s or 1s at present; it could conceivably
+ go higher as and when unicode support happens."""
+ # disp_str proved to be a bottleneck for large inputs,
+ # so it needs to be rewritten in C; it's not required though.
+ s = [uc(x) for x in buffer]
+ b = [] # XXX: bytearray
+ for x in s:
+ b.append(1)
+ b.extend([0] * (len(x) - 1))
+ return join(s), b
- the list always contains 0s or 1s at present; it could conceivably
- go higher as and when unicode support happens."""
- s = [uc(x) for x in buffer]
- b = [] #XXX: bytearray
- for x in s:
- b.append(1)
- b.extend([0]*(len(x)-1))
- return join(s), b
-
- del _my_unctrl
+del _my_unctrl
del _make_unctrl_map
@@ -95,6 +89,7 @@
SYNTAX_WORD,
SYNTAX_SYMBOL] = range(3)
+
def make_default_syntax_table():
# XXX perhaps should use some unicodedata here?
st = {}
@@ -164,13 +159,14 @@
(r'\<end>', 'end-of-line'), # was 'end'
(r'\<home>', 'beginning-of-line'), # was 'home'
(r'\<f1>', 'help'),
- (r'\EOF', 'end'), # the entries in the terminfo database for xterms
- (r'\EOH', 'home'), # seem to be wrong. this is a less than ideal
- # workaround
+ (r'\EOF', 'end'), # the entries in the terminfo database for xterms
+ (r'\EOH', 'home'), # seem to be wrong. this is a less than ideal
+ # workaround
])
if 'c' in globals(): # only on python 2.x
- del c # from the listcomps
+ del c # from the listcomps
+
class Reader(object):
"""The Reader class implements the bare bones of a command reader,
@@ -248,9 +244,9 @@
self.commands = {}
self.msg = ''
for v in vars(commands).values():
- if ( isinstance(v, type)
- and issubclass(v, commands.Command)
- and v.__name__[0].islower() ):
+ if (isinstance(v, type)
+ and issubclass(v, commands.Command)
+ and v.__name__[0].islower()):
self.commands[v.__name__] = v
self.commands[v.__name__.replace('_', '-')] = v
self.syntax_table = make_default_syntax_table()
@@ -294,15 +290,15 @@
wrapcount = (len(l) + lp) // w
if wrapcount == 0:
screen.append(prompt + l)
- screeninfo.append((lp, l2+[1]))
+ screeninfo.append((lp, l2 + [1]))
else:
- screen.append(prompt + l[:w-lp] + "\\")
- screeninfo.append((lp, l2[:w-lp]))
- for i in range(-lp + w, -lp + wrapcount*w, w):
- screen.append(l[i:i+w] + "\\")
+ screen.append(prompt + l[:w - lp] + "\\")
+ screeninfo.append((lp, l2[:w - lp]))
+ for i in range(-lp + w, -lp + wrapcount * w, w):
+ screen.append(l[i:i + w] + "\\")
screeninfo.append((0, l2[i:i + w]))
- screen.append(l[wrapcount*w - lp:])
- screeninfo.append((0, l2[wrapcount*w - lp:]+[1]))
+ screen.append(l[wrapcount * w - lp:])
+ screeninfo.append((0, l2[wrapcount * w - lp:] + [1]))
self.screeninfo = screeninfo
self.cxy = self.pos2xy(self.pos)
if self.msg and self.msg_at_bottom:
@@ -330,9 +326,9 @@
if e == -1:
break
# Found start and end brackets, subtract from string length
- l = l - (e-s+1)
- out_prompt += prompt[pos:s] + prompt[s+1:e]
- pos = e+1
+ l = l - (e - s + 1)
+ out_prompt += prompt[pos:s] + prompt[s + 1:e]
+ pos = e + 1
out_prompt += prompt[pos:]
return out_prompt, l
@@ -408,7 +404,7 @@
"""Return what should be in the left-hand margin for line
`lineno'."""
if self.arg is not None and cursor_on_line:
- return "(arg: %s) "%self.arg
+ return "(arg: %s) " % self.arg
if "\n" in self.buffer:
if lineno == 0:
res = self.ps2
@@ -521,17 +517,18 @@
# this call sets up self.cxy, so call it first.
screen = self.calc_screen()
self.console.refresh(screen, self.cxy)
- self.dirty = 0 # forgot this for a while (blush)
+ self.dirty = 0 # forgot this for a while (blush)
def do_cmd(self, cmd):
#print cmd
- if isinstance(cmd[0], basestring): #XXX: unify to text
+ if isinstance(cmd[0], basestring):
+ #XXX: unify to text
cmd = self.commands.get(cmd[0],
commands.invalid_command)(self, *cmd)
elif isinstance(cmd[0], type):
cmd = cmd[0](self, cmd)
else:
- return # nothing to do
+ return # nothing to do
cmd.do()
@@ -561,7 +558,7 @@
while 1:
event = self.console.get_event(block)
- if not event: # can only happen if we're not blocking
+ if not event: # can only happen if we're not blocking
return None
translate = True
@@ -626,6 +623,7 @@
"""Return the current buffer as a unicode string."""
return unicode('').join(self.buffer)
+
def test():
from pyrepl.unix_console import UnixConsole
reader = Reader(UnixConsole())
@@ -636,5 +634,5 @@
while reader.readline():
pass
-if __name__=='__main__':
+if __name__ == '__main__':
test()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit