Skip to content

Commit 948d156

Browse files
kill unused c imports and pep8 cleanup of reader
1 parent 640d111 commit 948d156

File tree

1 file changed

+56
-58
lines changed

1 file changed

+56
-58
lines changed

pyrepl/reader.py

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
unichr = chr
3131
basestring = bytes, str
3232

33+
3334
def _make_unctrl_map():
3435
uc_map = {}
3536
for c in map(unichr, range(256)):
@@ -38,54 +39,47 @@ def _make_unctrl_map():
3839
for i in range(32):
3940
c = unichr(i)
4041
uc_map[c] = '^' + unichr(ord('A') + i - 1)
41-
uc_map[b'\t'] = ' ' # display TABs as 4 characters
42+
uc_map[b'\t'] = ' ' # display TABs as 4 characters
4243
uc_map[b'\177'] = unicode('^?')
4344
for i in range(256):
4445
c = unichr(i)
4546
if c not in uc_map:
46-
uc_map[c] = unicode('\\%03o')%i
47+
uc_map[c] = unicode('\\%03o') % i
4748
return uc_map
4849

49-
# disp_str proved to be a bottleneck for large inputs, so it's been
50-
# rewritten in C; it's not required though.
51-
try:
52-
raise ImportError # currently it's borked by the unicode support
53-
54-
from _pyrepl_utils import disp_str, init_unctrl_map
5550

56-
init_unctrl_map(_make_unctrl_map())
57-
58-
del init_unctrl_map
59-
except ImportError:
60-
def _my_unctrl(c, u=_make_unctrl_map()):
61-
if c in u:
62-
return u[c]
51+
def _my_unctrl(c, u=_make_unctrl_map()):
52+
if c in u:
53+
return u[c]
54+
else:
55+
if unicodedata.category(c).startswith('C'):
56+
return b'\u%04x' % ord(c)
6357
else:
64-
if unicodedata.category(c).startswith('C'):
65-
return b'\u%04x'%(ord(c))
66-
else:
67-
return c
58+
return c
6859

69-
def disp_str(buffer, join=''.join, uc=_my_unctrl):
70-
""" disp_str(buffer:string) -> (string, [int])
7160

72-
Return the string that should be the printed represenation of
73-
|buffer| and a list detailing where the characters of |buffer|
74-
get used up. E.g.:
61+
def disp_str(buffer, join=''.join, uc=_my_unctrl):
62+
""" disp_str(buffer:string) -> (string, [int])
7563
76-
>>> disp_str(chr(3))
77-
('^C', [1, 0])
64+
Return the string that should be the printed represenation of
65+
|buffer| and a list detailing where the characters of |buffer|
66+
get used up. E.g.:
7867
79-
the list always contains 0s or 1s at present; it could conceivably
80-
go higher as and when unicode support happens."""
81-
s = [uc(x) for x in buffer]
82-
b = [] #XXX: bytearray
83-
for x in s:
84-
b.append(1)
85-
b.extend([0]*(len(x)-1))
86-
return join(s), b
68+
>>> disp_str(chr(3))
69+
('^C', [1, 0])
8770
88-
del _my_unctrl
71+
the list always contains 0s or 1s at present; it could conceivably
72+
go higher as and when unicode support happens."""
73+
# disp_str proved to be a bottleneck for large inputs,
74+
# so it needs to be rewritten in C; it's not required though.
75+
s = [uc(x) for x in buffer]
76+
b = [] # XXX: bytearray
77+
for x in s:
78+
b.append(1)
79+
b.extend([0] * (len(x) - 1))
80+
return join(s), b
81+
82+
del _my_unctrl
8983

9084
del _make_unctrl_map
9185

@@ -95,6 +89,7 @@ def disp_str(buffer, join=''.join, uc=_my_unctrl):
9589
SYNTAX_WORD,
9690
SYNTAX_SYMBOL] = range(3)
9791

92+
9893
def make_default_syntax_table():
9994
# XXX perhaps should use some unicodedata here?
10095
st = {}
@@ -164,13 +159,14 @@ def make_default_syntax_table():
164159
(r'\<end>', 'end-of-line'), # was 'end'
165160
(r'\<home>', 'beginning-of-line'), # was 'home'
166161
(r'\<f1>', 'help'),
167-
(r'\EOF', 'end'), # the entries in the terminfo database for xterms
168-
(r'\EOH', 'home'), # seem to be wrong. this is a less than ideal
169-
# workaround
162+
(r'\EOF', 'end'), # the entries in the terminfo database for xterms
163+
(r'\EOH', 'home'), # seem to be wrong. this is a less than ideal
164+
# workaround
170165
])
171166

172167
if 'c' in globals(): # only on python 2.x
173-
del c # from the listcomps
168+
del c # from the listcomps
169+
174170

175171
class Reader(object):
176172
"""The Reader class implements the bare bones of a command reader,
@@ -248,9 +244,9 @@ def __init__(self, console):
248244
self.commands = {}
249245
self.msg = ''
250246
for v in vars(commands).values():
251-
if ( isinstance(v, type)
252-
and issubclass(v, commands.Command)
253-
and v.__name__[0].islower() ):
247+
if (isinstance(v, type)
248+
and issubclass(v, commands.Command)
249+
and v.__name__[0].islower()):
254250
self.commands[v.__name__] = v
255251
self.commands[v.__name__.replace('_', '-')] = v
256252
self.syntax_table = make_default_syntax_table()
@@ -294,15 +290,15 @@ def calc_screen(self):
294290
wrapcount = (len(l) + lp) // w
295291
if wrapcount == 0:
296292
screen.append(prompt + l)
297-
screeninfo.append((lp, l2+[1]))
293+
screeninfo.append((lp, l2 + [1]))
298294
else:
299-
screen.append(prompt + l[:w-lp] + "\\")
300-
screeninfo.append((lp, l2[:w-lp]))
301-
for i in range(-lp + w, -lp + wrapcount*w, w):
302-
screen.append(l[i:i+w] + "\\")
295+
screen.append(prompt + l[:w - lp] + "\\")
296+
screeninfo.append((lp, l2[:w - lp]))
297+
for i in range(-lp + w, -lp + wrapcount * w, w):
298+
screen.append(l[i:i + w] + "\\")
303299
screeninfo.append((0, l2[i:i + w]))
304-
screen.append(l[wrapcount*w - lp:])
305-
screeninfo.append((0, l2[wrapcount*w - lp:]+[1]))
300+
screen.append(l[wrapcount * w - lp:])
301+
screeninfo.append((0, l2[wrapcount * w - lp:] + [1]))
306302
self.screeninfo = screeninfo
307303
self.cxy = self.pos2xy(self.pos)
308304
if self.msg and self.msg_at_bottom:
@@ -330,9 +326,9 @@ def process_prompt(self, prompt):
330326
if e == -1:
331327
break
332328
# Found start and end brackets, subtract from string length
333-
l = l - (e-s+1)
334-
out_prompt += prompt[pos:s] + prompt[s+1:e]
335-
pos = e+1
329+
l = l - (e - s + 1)
330+
out_prompt += prompt[pos:s] + prompt[s + 1:e]
331+
pos = e + 1
336332
out_prompt += prompt[pos:]
337333
return out_prompt, l
338334

@@ -408,7 +404,7 @@ def get_prompt(self, lineno, cursor_on_line):
408404
"""Return what should be in the left-hand margin for line
409405
`lineno'."""
410406
if self.arg is not None and cursor_on_line:
411-
return "(arg: %s) "%self.arg
407+
return "(arg: %s) " % self.arg
412408
if "\n" in self.buffer:
413409
if lineno == 0:
414410
res = self.ps2
@@ -521,17 +517,18 @@ def refresh(self):
521517
# this call sets up self.cxy, so call it first.
522518
screen = self.calc_screen()
523519
self.console.refresh(screen, self.cxy)
524-
self.dirty = 0 # forgot this for a while (blush)
520+
self.dirty = 0 # forgot this for a while (blush)
525521

526522
def do_cmd(self, cmd):
527523
#print cmd
528-
if isinstance(cmd[0], basestring): #XXX: unify to text
524+
if isinstance(cmd[0], basestring):
525+
#XXX: unify to text
529526
cmd = self.commands.get(cmd[0],
530527
commands.invalid_command)(self, *cmd)
531528
elif isinstance(cmd[0], type):
532529
cmd = cmd[0](self, cmd)
533530
else:
534-
return # nothing to do
531+
return # nothing to do
535532

536533
cmd.do()
537534

@@ -561,7 +558,7 @@ def handle1(self, block=1):
561558

562559
while 1:
563560
event = self.console.get_event(block)
564-
if not event: # can only happen if we're not blocking
561+
if not event: # can only happen if we're not blocking
565562
return None
566563

567564
translate = True
@@ -626,6 +623,7 @@ def get_unicode(self):
626623
"""Return the current buffer as a unicode string."""
627624
return unicode('').join(self.buffer)
628625

626+
629627
def test():
630628
from pyrepl.unix_console import UnixConsole
631629
reader = Reader(UnixConsole())
@@ -636,5 +634,5 @@ def test():
636634
while reader.readline():
637635
pass
638636

639-
if __name__=='__main__':
637+
if __name__ == '__main__':
640638
test()

0 commit comments

Comments
 (0)