Skip to content

Commit

Permalink
Rename attribute buffer to _buffer to avoid possible side effect.
Browse files Browse the repository at this point in the history
Some external io related function may look for a buffer attribute for
additional processing (e.g. autopep8).
  • Loading branch information
ywangd committed Jan 22, 2016
1 parent ccaa749 commit cf939bb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
18 changes: 9 additions & 9 deletions system/shio.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def __init__(self, stash, debug=False):
self.logger = logging.getLogger('StaSh.IO')
self.tell_pos = 0
# The input buffer, push from the Left end, read from the right end
self.buffer = deque()
self._buffer = deque()
self.chunk_size = 4096
# When buffer is empty, hold back for certain before read again
# This is to lower the cpu usage of the reading thread so it does
# not affect the UI thread by noticeable amount
self.holdback = 0.2

def push(self, s):
self.buffer.extendleft(s)
self._buffer.extendleft(s)

# Following methods to provide file like object interface
@property
Expand Down Expand Up @@ -61,13 +61,13 @@ def read(self, size=-1):
size = size if size != 0 else 1

if size == -1:
return ''.join(self.buffer.pop() for _ in len(self.buffer))
return ''.join(self._buffer.pop() for _ in len(self._buffer))

else:
ret = []
while len(ret) < size:
try:
ret.append(self.buffer.pop())
ret.append(self._buffer.pop())
except IndexError:
# Wait briefly when the buffer is empty to avoid taxing the CPU
time.sleep(self.holdback)
Expand All @@ -78,7 +78,7 @@ def readline(self, size=-1):
ret = []
while True:
try:
ret.append(self.buffer.pop())
ret.append(self._buffer.pop())
if ret[-1] == '\n':
break
except IndexError:
Expand All @@ -95,7 +95,7 @@ def readlines(self, size=-1):
ret = []
while True:
try:
ret.append(self.buffer.pop())
ret.append(self._buffer.pop())
if ret[-1] == '\0':
break
except IndexError:
Expand All @@ -118,7 +118,7 @@ def read1(self):
self.stash.mini_buffer.cbreak = True
while True:
try:
yield self.buffer.pop()
yield self._buffer.pop()
except IndexError:
time.sleep(self.holdback)

Expand All @@ -137,12 +137,12 @@ def readline_no_block(self):
ret = []
while True:
try:
ret.append(self.buffer.pop())
ret.append(self._buffer.pop())
if ret[-1] == '\n':
yield ''.join(ret)
ret = []
except IndexError:
self.buffer.extend(ret)
self._buffer.extend(ret)
break

def write(self, s, no_wait=False):
Expand Down
54 changes: 27 additions & 27 deletions system/shscreens.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, stash, nlines_max=100, debug=False):
self.debug = debug
self.logger = logging.getLogger('StaSh.Screen')

self.buffer = deque() # buffer to hold chars
self._buffer = deque() # buffer to hold chars
self.lock = threading.Lock()

self.attrs = ShChar(' ')
Expand All @@ -104,7 +104,7 @@ def reset(self, *args): # *args is a necessary placeholder
with at least one parameter (even it is a dummy 0).
"""
# empty the buffer
self.buffer.clear()
self._buffer.clear()

# The cursor position
self.cursor_x = 0
Expand All @@ -127,7 +127,7 @@ def text(self):
"""
:rtype: str
"""
return ''.join(char.data for char in self.buffer)
return ''.join(char.data for char in self._buffer)

@property
def renderable_chars(self):
Expand All @@ -137,7 +137,7 @@ def renderable_chars(self):
:rtype: [ShChar]
"""
_, rbound = self.get_bounds()
return [self.buffer[x] for x in range(rbound, len(self.buffer))]
return [self._buffer[x] for x in range(rbound, len(self._buffer))]

@property
def x_modifiable(self):
Expand All @@ -148,8 +148,8 @@ def x_modifiable(self):
"""
# The position is either the x_drawend or last LF location plus one,
# whichever is larger.
for idx in reversed(range(self.x_drawend, len(self.buffer))):
if self.buffer[idx].data == '\n':
for idx in reversed(range(self.x_drawend, len(self._buffer))):
if self._buffer[idx].data == '\n':
return idx + 1
else:
return self.x_drawend
Expand All @@ -159,16 +159,16 @@ def modifiable_chars(self):
"""
:rtype: str
"""
return ''.join(self.buffer[idx].data
for idx in range(self.x_modifiable, len(self.buffer)))
return ''.join(self._buffer[idx].data
for idx in range(self.x_modifiable, len(self._buffer)))

@modifiable_chars.setter
def modifiable_chars(self, s):
"""
Set the modifiable_chars to the given string.
:param str s: A new value for modifiable_chars.
"""
self.replace_in_range((self.x_modifiable, len(self.buffer)), s)
self.replace_in_range((self.x_modifiable, len(self._buffer)), s)

@contextmanager
def acquire_lock(self):
Expand Down Expand Up @@ -200,7 +200,7 @@ def clean(self):
Mark everything is rendered.
"""
self.intact_left_bound = 0
self.intact_right_bound = len(self.buffer)
self.intact_right_bound = len(self._buffer)

# noinspection PyProtectedMember
def draw(self, c):
Expand All @@ -209,11 +209,11 @@ def draw(self, c):
location. This method should ONLY be called by ShStream.
:param str c: A new character to draw
"""
if len(self.buffer) < self.intact_right_bound:
self.intact_right_bound = len(self.buffer)
if len(self._buffer) < self.intact_right_bound:
self.intact_right_bound = len(self._buffer)

self.buffer.append(self.attrs._replace(data=c))
self.cursor_x = self.x_drawend = len(self.buffer)
self._buffer.append(self.attrs._replace(data=c))
self.cursor_x = self.x_drawend = len(self._buffer)

if c == '\n':
self.nlines += 1
Expand All @@ -232,7 +232,7 @@ def replace_in_range(self, rng, s, relative_to_x_modifiable=False, set_drawend=F
:return:
"""
if rng is None:
rng = (len(self.buffer), len(self.buffer))
rng = (len(self._buffer), len(self._buffer))

elif relative_to_x_modifiable: # Convert to absolute location if necessary
rng = rng[0] + self.x_modifiable, rng[1] + self.x_modifiable
Expand All @@ -241,16 +241,16 @@ def replace_in_range(self, rng, s, relative_to_x_modifiable=False, set_drawend=F
if rng[0] < self.intact_right_bound:
self.intact_right_bound = rng[0]

rotate_n = max(len(self.buffer) - rng[1], 0)
rotate_n = max(len(self._buffer) - rng[1], 0)
try:
self.buffer.rotate(rotate_n) # rotate buffer first so deletion is possible
self._buffer.rotate(rotate_n) # rotate buffer first so deletion is possible
if rng[0] != rng[1]: # delete chars if necessary
self._pop_chars(rng[1] - rng[0])
# The newly inserted chars are always of default properties
self.buffer.extend(DEFAULT_CHAR._replace(data=c) for c in s)
self._buffer.extend(DEFAULT_CHAR._replace(data=c) for c in s)

finally:
self.buffer.rotate(-rotate_n)
self._buffer.rotate(-rotate_n)

# Update cursor to the end of this replacement
self.cursor_x = rng[0] + len(s)
Expand All @@ -265,8 +265,8 @@ def replace_in_range(self, rng, s, relative_to_x_modifiable=False, set_drawend=F
self._ensure_nlines_max()

def ensure_cursor_in_modifiable_range(self):
if self.cursor_x > len(self.buffer):
self.cursor_x = len(self.buffer)
if self.cursor_x > len(self._buffer):
self.cursor_x = len(self._buffer)
elif self.cursor_x < self.x_modifiable:
self.cursor_x = self.x_modifiable

Expand All @@ -277,9 +277,9 @@ def _pop_chars(self, n=1):
:return:
"""
for _ in range(n):
self.buffer.pop()
if len(self.buffer) < self.intact_right_bound:
self.intact_right_bound = len(self.buffer)
self._buffer.pop()
if len(self._buffer) < self.intact_right_bound:
self.intact_right_bound = len(self._buffer)

def _ensure_nlines_max(self):
"""
Expand All @@ -288,9 +288,9 @@ def _ensure_nlines_max(self):
char_count = line_count = 0
for _ in range(self.nlines_max, self.nlines):
# Remove the top line
for idx in range(len(self.buffer)):
for idx in range(len(self._buffer)):
char_count += 1
if self.buffer.popleft().data == '\n':
if self._buffer.popleft().data == '\n':
line_count += 1
break

Expand Down Expand Up @@ -468,7 +468,7 @@ def _render(self):
# Lock screen to get atomic information
with self.screen.acquire_lock():
intact_left_bound, intact_right_bound = self.screen.get_bounds()
screen_buffer_length = len(self.screen.buffer)
screen_buffer_length = len(self.screen._buffer)
cursor_x = self.screen.cursor_x
renderable_chars = self.screen.renderable_chars
self.screen.clean()
Expand Down

0 comments on commit cf939bb

Please sign in to comment.