mirror of
https://github.com/KevinMidboe/Node-Com-Handler.git
synced 2025-10-29 17:50:27 +00:00
Major cleanup, added all old files (python2) to 'old_v0.1' folder
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from __future__ import division
|
||||
|
||||
from collections import deque
|
||||
from datetime import timedelta
|
||||
from math import ceil
|
||||
from sys import stderr
|
||||
from time import time
|
||||
|
||||
|
||||
__version__ = '1.2'
|
||||
|
||||
|
||||
class Infinite(object):
|
||||
file = stderr
|
||||
sma_window = 10
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.index = 0
|
||||
self.start_ts = time()
|
||||
self._ts = self.start_ts
|
||||
self._dt = deque(maxlen=self.sma_window)
|
||||
for key, val in kwargs.items():
|
||||
setattr(self, key, val)
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key.startswith('_'):
|
||||
return None
|
||||
return getattr(self, key, None)
|
||||
|
||||
@property
|
||||
def avg(self):
|
||||
return sum(self._dt) / len(self._dt) if self._dt else 0
|
||||
|
||||
@property
|
||||
def elapsed(self):
|
||||
return int(time() - self.start_ts)
|
||||
|
||||
@property
|
||||
def elapsed_td(self):
|
||||
return timedelta(seconds=self.elapsed)
|
||||
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
def finish(self):
|
||||
pass
|
||||
|
||||
def next(self, n=1):
|
||||
if n > 0:
|
||||
now = time()
|
||||
dt = (now - self._ts) / n
|
||||
self._dt.append(dt)
|
||||
self._ts = now
|
||||
|
||||
self.index = self.index + n
|
||||
self.update()
|
||||
|
||||
def iter(self, it):
|
||||
for x in it:
|
||||
yield x
|
||||
self.next()
|
||||
self.finish()
|
||||
|
||||
|
||||
class Progress(Infinite):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Progress, self).__init__(*args, **kwargs)
|
||||
self.max = kwargs.get('max', 100)
|
||||
|
||||
@property
|
||||
def eta(self):
|
||||
return int(ceil(self.avg * self.remaining))
|
||||
|
||||
@property
|
||||
def eta_td(self):
|
||||
return timedelta(seconds=self.eta)
|
||||
|
||||
@property
|
||||
def percent(self):
|
||||
return self.progress * 100
|
||||
|
||||
@property
|
||||
def progress(self):
|
||||
return min(1, self.index / self.max)
|
||||
|
||||
@property
|
||||
def remaining(self):
|
||||
return max(self.max - self.index, 0)
|
||||
|
||||
def start(self):
|
||||
self.update()
|
||||
|
||||
def goto(self, index):
|
||||
incr = index - self.index
|
||||
self.next(incr)
|
||||
|
||||
def iter(self, it):
|
||||
try:
|
||||
self.max = len(it)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
for x in it:
|
||||
yield x
|
||||
self.next()
|
||||
self.finish()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,83 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from . import Progress
|
||||
from .helpers import WritelnMixin
|
||||
|
||||
|
||||
class Bar(WritelnMixin, Progress):
|
||||
width = 32
|
||||
message = ''
|
||||
suffix = '%(index)d/%(max)d'
|
||||
bar_prefix = ' |'
|
||||
bar_suffix = '| '
|
||||
empty_fill = ' '
|
||||
fill = '#'
|
||||
hide_cursor = True
|
||||
|
||||
def update(self):
|
||||
filled_length = int(self.width * self.progress)
|
||||
empty_length = self.width - filled_length
|
||||
|
||||
message = self.message % self
|
||||
bar = self.fill * filled_length
|
||||
empty = self.empty_fill * empty_length
|
||||
suffix = self.suffix % self
|
||||
line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix,
|
||||
suffix])
|
||||
self.writeln(line)
|
||||
|
||||
|
||||
class ChargingBar(Bar):
|
||||
suffix = '%(percent)d%%'
|
||||
bar_prefix = ' '
|
||||
bar_suffix = ' '
|
||||
empty_fill = u'∙'
|
||||
fill = u'█'
|
||||
|
||||
|
||||
class FillingSquaresBar(ChargingBar):
|
||||
empty_fill = u'▢'
|
||||
fill = u'▣'
|
||||
|
||||
|
||||
class FillingCirclesBar(ChargingBar):
|
||||
empty_fill = u'◯'
|
||||
fill = u'◉'
|
||||
|
||||
|
||||
class IncrementalBar(Bar):
|
||||
phases = (u' ', u'▏', u'▎', u'▍', u'▌', u'▋', u'▊', u'▉', u'█')
|
||||
|
||||
def update(self):
|
||||
nphases = len(self.phases)
|
||||
expanded_length = int(nphases * self.width * self.progress)
|
||||
filled_length = int(self.width * self.progress)
|
||||
empty_length = self.width - filled_length
|
||||
phase = expanded_length - (filled_length * nphases)
|
||||
|
||||
message = self.message % self
|
||||
bar = self.phases[-1] * filled_length
|
||||
current = self.phases[phase] if phase > 0 else ''
|
||||
empty = self.empty_fill * max(0, empty_length - len(current))
|
||||
suffix = self.suffix % self
|
||||
line = ''.join([message, self.bar_prefix, bar, current, empty,
|
||||
self.bar_suffix, suffix])
|
||||
self.writeln(line)
|
||||
|
||||
|
||||
class ShadyBar(IncrementalBar):
|
||||
phases = (u' ', u'░', u'▒', u'▓', u'█')
|
||||
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from . import Infinite, Progress
|
||||
from .helpers import WriteMixin
|
||||
|
||||
|
||||
class Counter(WriteMixin, Infinite):
|
||||
message = ''
|
||||
hide_cursor = True
|
||||
|
||||
def update(self):
|
||||
self.write(str(self.index))
|
||||
|
||||
|
||||
class Countdown(WriteMixin, Progress):
|
||||
hide_cursor = True
|
||||
|
||||
def update(self):
|
||||
self.write(str(self.remaining))
|
||||
|
||||
|
||||
class Stack(WriteMixin, Progress):
|
||||
phases = (u' ', u'▁', u'▂', u'▃', u'▄', u'▅', u'▆', u'▇', u'█')
|
||||
hide_cursor = True
|
||||
|
||||
def update(self):
|
||||
nphases = len(self.phases)
|
||||
i = min(nphases - 1, int(self.progress * nphases))
|
||||
self.write(self.phases[i])
|
||||
|
||||
|
||||
class Pie(Stack):
|
||||
phases = (u'○', u'◔', u'◑', u'◕', u'●')
|
||||
@@ -0,0 +1,91 @@
|
||||
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
HIDE_CURSOR = '\x1b[?25l'
|
||||
SHOW_CURSOR = '\x1b[?25h'
|
||||
|
||||
|
||||
class WriteMixin(object):
|
||||
hide_cursor = False
|
||||
|
||||
def __init__(self, message=None, **kwargs):
|
||||
super(WriteMixin, self).__init__(**kwargs)
|
||||
self._width = 0
|
||||
if message:
|
||||
self.message = message
|
||||
|
||||
if self.file.isatty():
|
||||
if self.hide_cursor:
|
||||
print(HIDE_CURSOR, end='', file=self.file)
|
||||
print(self.message, end='', file=self.file)
|
||||
self.file.flush()
|
||||
|
||||
def write(self, s):
|
||||
if self.file.isatty():
|
||||
b = '\b' * self._width
|
||||
c = s.ljust(self._width)
|
||||
print(b + c, end='', file=self.file)
|
||||
self._width = max(self._width, len(s))
|
||||
self.file.flush()
|
||||
|
||||
def finish(self):
|
||||
if self.file.isatty() and self.hide_cursor:
|
||||
print(SHOW_CURSOR, end='', file=self.file)
|
||||
|
||||
|
||||
class WritelnMixin(object):
|
||||
hide_cursor = False
|
||||
|
||||
def __init__(self, message=None, **kwargs):
|
||||
super(WritelnMixin, self).__init__(**kwargs)
|
||||
if message:
|
||||
self.message = message
|
||||
|
||||
if self.file.isatty() and self.hide_cursor:
|
||||
print(HIDE_CURSOR, end='', file=self.file)
|
||||
|
||||
def clearln(self):
|
||||
if self.file.isatty():
|
||||
print('\r\x1b[K', end='', file=self.file)
|
||||
|
||||
def writeln(self, line):
|
||||
if self.file.isatty():
|
||||
self.clearln()
|
||||
print(line, end='', file=self.file)
|
||||
self.file.flush()
|
||||
|
||||
def finish(self):
|
||||
if self.file.isatty():
|
||||
print(file=self.file)
|
||||
if self.hide_cursor:
|
||||
print(SHOW_CURSOR, end='', file=self.file)
|
||||
|
||||
|
||||
from signal import signal, SIGINT
|
||||
from sys import exit
|
||||
|
||||
|
||||
class SigIntMixin(object):
|
||||
"""Registers a signal handler that calls finish on SIGINT"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SigIntMixin, self).__init__(*args, **kwargs)
|
||||
signal(SIGINT, self._sigint_handler)
|
||||
|
||||
def _sigint_handler(self, signum, frame):
|
||||
self.finish()
|
||||
exit(0)
|
||||
@@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from . import Infinite
|
||||
from .helpers import WriteMixin
|
||||
|
||||
|
||||
class Spinner(WriteMixin, Infinite):
|
||||
message = ''
|
||||
phases = ('-', '\\', '|', '/')
|
||||
hide_cursor = True
|
||||
|
||||
def update(self):
|
||||
i = self.index % len(self.phases)
|
||||
self.write(self.phases[i])
|
||||
|
||||
|
||||
class PieSpinner(Spinner):
|
||||
phases = [u'◷', u'◶', u'◵', u'◴']
|
||||
|
||||
|
||||
class MoonSpinner(Spinner):
|
||||
phases = [u'◑', u'◒', u'◐', u'◓']
|
||||
|
||||
|
||||
class LineSpinner(Spinner):
|
||||
phases = [u'⎺', u'⎻', u'⎼', u'⎽', u'⎼', u'⎻']
|
||||
Reference in New Issue
Block a user