Files
@ a2fd5fca23a0
Branch filter:
Location: rattail-project/rattail/rattail/progress.py
a2fd5fca23a0
3.9 KiB
text/x-python
Stop creating separate 'batch' folder for `rattail make-appdir`
that needs to exist underneath 'data' folder instead
that needs to exist underneath 'data' folder instead
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | # -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2019 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Console Stuff
"""
from __future__ import unicode_literals, absolute_import
import os
import sys
import json
import socket
import six
from progress.bar import Bar
class ProgressBase(object):
"""
Base class for all progress implementations.
"""
def __init__(self, message, maximum):
self.message = message
self.maximum = maximum
def update(self, value):
return True
def finish(self):
pass
def destroy(self):
self.finish()
class ConsoleProgress(ProgressBase):
"""
Provides a console-based progress bar.
"""
def __init__(self, message, maximum, stdout=None):
self.stdout = stdout or sys.stderr
self.stdout.write("\n{}...\n".format(message))
self.bar = Bar(message='', max=maximum, width=70,
suffix='%(index)d/%(max)d %(percent)d%% ETA %(eta)ds')
def update(self, value):
self.bar.next()
return True
def finish(self):
self.bar.finish()
class FileProgress(ProgressBase):
"""
Progress indicator which writes progress info to the given file object.
"""
def __init__(self, path):
self.path = path
def __call__(self, message, maximum):
self.message = message
self.maximum = maximum
self.value = 0
self.write_info()
return self
def write_info(self):
info = {
'message': self.message,
'maximum': self.maximum,
'value': self.value,
}
with open(self.path, 'wt') as f:
f.write(json.dumps(info))
f.flush()
def update(self, value):
self.value = value
self.write_info()
return True
def finish(self):
if os.path.exists(self.path):
os.remove(self.path)
class SocketProgress(ProgressBase):
"""
Progress indicator which writes progress info to the given socket object.
"""
def __init__(self, host, port, suffix=None):
self.host = host
self.port = port
if suffix:
self.suffix = suffix
else:
self.suffix = "\n\n.".encode('utf_8')
def __call__(self, message, maximum):
self.connection = socket.create_connection((self.host, self.port))
self.message = message
self.maximum = maximum
self.value = 0
self.write_progress()
return self
def write_data(self, data):
data = json.dumps(data)
if six.PY3:
data = data.encode('utf_8')
self.connection.send(data)
self.connection.send(self.suffix)
def write_progress(self):
self.write_data({
'message': self.message,
'maximum': self.maximum,
'value': self.value,
})
def update(self, value):
self.value = value
self.write_progress()
return True
def finish(self):
self.write_data({
'process_complete': True,
})
self.connection.close()
|