Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Jul 27, 2020
1 parent 490a84a commit 278dba4
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 102 deletions.
4 changes: 1 addition & 3 deletions doc/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RPM

RPMs are available in the Fedora_ and EPEL_ repositories

EL6 and EL7 (RHEL/CentOS/Scientific)
EL6, EL7, and EL8 (RHEL/CentOS/Scientific)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

(EPEL_ repositories must be configured_)
Expand All @@ -44,5 +44,3 @@ Fedora
.. _EPEL: https://fedoraproject.org/wiki/EPEL
.. _Fedora: https://fedoraproject.org/
.. _configured: https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F


28 changes: 15 additions & 13 deletions enlighten/_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,25 @@ def update_from(self, source, incr=1, force=False):
"""

# Make sure source is a parent or peer
if source is self.parent or getattr(source, 'parent', None) is self.parent:

if self.count + incr < 0 or source.count - incr < 0:
raise ValueError('Invalid increment: %s' % incr)

if source is self.parent:
if self.parent.count - self.parent.subcount - incr < 0:
raise ValueError('Invalid increment: %s' % incr)
if source is not self.parent and getattr(source, 'parent', None) is not self.parent:
raise ValueError('source must be parent or peer')

else:
source.count -= incr
# Make sure counts won't go negative
if self.count + incr < 0 or source.count - incr < 0:
raise ValueError('Invalid increment: %s' % incr)

self.count += incr
self.parent.update(0, force)
# Make sure parent count won't go negative
if source is self.parent:
if self.parent.count - self.parent.subcount - incr < 0:
raise ValueError('Invalid increment: %s' % incr)

# Deduct from peer count
else:
raise ValueError('source must be parent or peer')
source.count -= incr

# Increment self and update parent
self.count += incr
self.parent.update(0, force)


class Counter(PrintableCounter):
Expand Down
130 changes: 67 additions & 63 deletions enlighten/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _add_counter(self, counter_class, *args, **kwargs):

# Get pinned counters
# pylint: disable=protected-access
pinned = dict((pos, ctr) for ctr, pos in self.counters.items() if ctr._pinned)
pinned = {pos: ctr for ctr, pos in self.counters.items() if ctr._pinned}

# Check position
if position is not None:
Expand Down Expand Up @@ -291,8 +291,8 @@ def _resize_handler(self, *args, **kwarg): # pylint: disable=unused-argument
self.width = newWidth
self._set_scroll_area(force=True)

for cter in self.counters:
cter.refresh(flush=False)
for counter in self.counters:
counter.refresh(flush=False)
self.stream.flush()

self.resize_lock = False
Expand All @@ -310,6 +310,9 @@ def _set_scroll_area(self, force=False):
newOffset = max(self.counters.values()) + 1
if newOffset > oldOffset:
self.scroll_offset = newOffset
use_new = True
else:
use_new = False

if not self.enabled:
return
Expand All @@ -327,11 +330,11 @@ def _set_scroll_area(self, force=False):
newHeight = term.height
scrollPosition = max(0, newHeight - self.scroll_offset)

if force or newOffset > oldOffset or newHeight != self.height:
if force or use_new or newHeight != self.height:
self.height = newHeight

# Add line feeds so we don't overwrite existing output
if newOffset - oldOffset > 0:
if use_new:
term.move_to(0, max(0, newHeight - oldOffset))
self.stream.write(u'\n' * (newOffset - oldOffset))

Expand All @@ -348,25 +351,25 @@ def _at_exit(self):
Resets terminal to normal configuration
"""

if self.process_exit:

try:
if not self.process_exit:
return

term = self.term
try:
term = self.term

if self.set_scroll:
term.reset()
else:
term.move_to(0, term.height)
if self.set_scroll:
term.reset()
else:
term.move_to(0, term.height)

self.term.feed()
self.term.feed()

self.stream.flush()
if self.companion_stream is not None:
self.companion_stream.flush()
self.stream.flush()
if self.companion_stream is not None:
self.companion_stream.flush()

except ValueError: # Possibly closed file handles
pass
except ValueError: # Possibly closed file handles
pass

def remove(self, counter):
"""
Expand Down Expand Up @@ -401,43 +404,44 @@ def stop(self):
"""

if self.enabled:
if not self.enabled:
return

term = self.term
stream = self.stream
positions = self.counters.values()
term = self.term
stream = self.stream
positions = self.counters.values()

if not self.no_resize and RESIZE_SUPPORTED:
signal.signal(signal.SIGWINCH, self.sigwinch_orig)
if not self.no_resize and RESIZE_SUPPORTED:
signal.signal(signal.SIGWINCH, self.sigwinch_orig)

try:
for num in range(self.scroll_offset - 1, 0, -1):
if num not in positions:
term.move_to(0, term.height - num)
stream.write(term.clear_eol)
try:
for num in range(self.scroll_offset - 1, 0, -1):
if num not in positions:
term.move_to(0, term.height - num)
stream.write(term.clear_eol)

stream.flush()
stream.flush()

finally:
finally:

if self.set_scroll:
if self.set_scroll:

self.term.reset()
self.term.reset()

if self.companion_term:
self.companion_term.reset()
if self.companion_term:
self.companion_term.reset()

else:
term.move_to(0, term.height)
else:
term.move_to(0, term.height)

self.process_exit = False
self.enabled = False
for cter in self.counters:
cter.enabled = False
self.process_exit = False
self.enabled = False
for counter in self.counters:
counter.enabled = False

# Feed terminal if lowest position isn't cleared
if 1 in positions:
term.feed()
# Feed terminal if lowest position isn't cleared
if 1 in positions:
term.feed()

def write(self, output='', flush=True, counter=None):
"""
Expand All @@ -449,26 +453,26 @@ def write(self, output='', flush=True, counter=None):
Write to stream at a given position
"""

position = self.counters[counter] if counter else 0

if self.enabled:
if not self.enabled:
return

term = self.term
stream = self.stream
position = self.counters[counter] if counter else 0
stream = self.stream
term = self.term

try:
term.move_to(0, term.height - position)
# Include \r and term call to cover most conditions
stream.write(u'\r' + term.clear_eol + output)

finally:
# Reset position and scrolling
if not self.refresh_lock:
if self.autorefresh:
self._autorefresh(exclude=(counter,))
self._set_scroll_area()
if flush:
stream.flush()
try:
term.move_to(0, term.height - position)
# Include \r and term call to cover most conditions
stream.write(u'\r' + term.clear_eol + output)

finally:
# Reset position and scrolling
if not self.refresh_lock:
if self.autorefresh:
self._autorefresh(exclude=(counter,))
self._set_scroll_area()
if flush:
stream.flush()

def _autorefresh(self, exclude):
"""
Expand Down
8 changes: 4 additions & 4 deletions enlighten/_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def change_scroll(self, position):
self.stream.write(self.csr(0, position))
self.stream.write(self.move(position, 0))

def move_to(self, xpos, ypos):
def move_to(self, x_pos, y_pos):
"""
Move cursor to specified position
"""

self.stream.write(self.move(ypos, xpos))
self.stream.write(self.move(y_pos, x_pos))

def _height_and_width(self):
"""
Expand All @@ -70,8 +70,8 @@ def _height_and_width(self):
try:
return self._cache['height_and_width']
except KeyError:
handw = self._cache['height_and_width'] = super(Terminal, self)._height_and_width()
return handw
h_and_w = self._cache['height_and_width'] = super(Terminal, self)._height_and_width()
return h_and_w

def clear_cache(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def process_files():
"""

with enlighten.Counter(total=100, desc='Simple', unit='ticks') as pbar:
for num in range(100): # pylint: disable=unused-variable
for _ in range(100):
time.sleep(0.05)
pbar.update()

Expand Down
4 changes: 2 additions & 2 deletions examples/context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def process_files():

with enlighten.Manager() as manager:
with manager.counter(total=SPLINES, desc='Reticulating:', unit='splines') as retic:
for num in range(SPLINES): # pylint: disable=unused-variable
for _ in range(SPLINES):
time.sleep(random.uniform(0.1, 0.5)) # Random processing time
retic.update()

with manager.counter(total=LLAMAS, desc='Herding:', unit='llamas') as herd:
for num in range(SPLINES): # pylint: disable=unused-variable
for _ in range(SPLINES):
time.sleep(random.uniform(0.1, 0.5)) # Random processing time
herd.update()

Expand Down
2 changes: 1 addition & 1 deletion examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(manager, initials=15):

# Simulated preparation
pbar = manager.counter(total=initials, desc='Initializing:', unit='initials')
for num in range(initials): # pylint: disable=unused-variable
for _ in range(initials):
time.sleep(random.uniform(0.05, 0.25)) # Random processing time
pbar.update()
pbar.close()
Expand Down
2 changes: 1 addition & 1 deletion examples/floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def process_files(count=None):
pbar = enlighten.Counter(total=count, desc='Simple', unit='ticks',
bar_format=BAR_FMT, counter_format=COUNTER_FMT)

for num in range(100): # pylint: disable=unused-variable
for _ in range(100):
time.sleep(0.05)
pbar.update(1.1)

Expand Down
2 changes: 1 addition & 1 deletion examples/multicolored.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def load(manager, units=80):
pb_loading.update_from(pb_connecting)

# Connect to up to 5 units at a time
for _ in range(0, min(units - count, 5 - len(connecting))):
for _ in range(min(units - count, 5 - len(connecting))):
node = Node(count)
node.connect()
connecting.append(node)
Expand Down
16 changes: 8 additions & 8 deletions examples/multiple_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ def process_files(manager):
enterprise = manager.counter(total=DATACENTERS, desc='Processing:', unit='datacenters')

# Iterate through data centers
for dnum in range(1, DATACENTERS + 1):
for d_num in range(1, DATACENTERS + 1):
systems = random.randint(*SYSTEMS) # Random number of systems
# Get a child progress bar. leave is False so it can be replaced
currCenter = manager.counter(total=systems, desc=' Datacenter %d:' % dnum,
datacenter = manager.counter(total=systems, desc=' Datacenter %d:' % d_num,
unit='systems', leave=False)

# Iterate through systems
for snum in range(1, systems + 1):
for s_num in range(1, systems + 1):

# Has no total, so will act as counter. Leave is False
system = manager.counter(desc=' System %d:' % snum, unit='files', leave=False)
system = manager.counter(desc=' System %d:' % s_num, unit='files', leave=False)
files = random.randint(*FILES) # Random file count

# Iterate through files
for fnum in range(files): # pylint: disable=unused-variable
for _ in range(files):
system.update() # Update count
time.sleep(random.uniform(0.001, 0.005)) # Random processing time

system.close() # Close counter so it gets removed
# Log status
LOGGER.info('Updated %d files on System %d in Datacenter %d', files, snum, dnum)
currCenter.update() # Update count
LOGGER.info('Updated %d files on System %d in Datacenter %d', files, s_num, d_num)
datacenter.update() # Update count

currCenter.close() # Close counter so it gets removed
datacenter.close() # Close counter so it gets removed

enterprise.update() # Update count

Expand Down
4 changes: 2 additions & 2 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def test_stop(self):

# Clear stream
self.tty.stdout.write(u'X\n')
for num in range(4 + 1): # pylint: disable=unused-variable
for _ in range(4 + 1):
self.tty.stdread.readline()

self.assertFalse(reset.called)
Expand Down Expand Up @@ -789,7 +789,7 @@ def test_get_manager_tty(self):
self.assertTrue(manager.defaults['enabled'])

@unittest.skipIf(STDOUT_NO_FD, 'No file descriptor for stdout')
def test_get_manager_notty(self):
def test_get_manager_no_tty(self):

# stdout is not attached to a tty
with redirect_output('stdout', OUTPUT):
Expand Down

0 comments on commit 278dba4

Please sign in to comment.