Browse Source

Make it possible to choose between multiples tipes of progress bar

pull/52/head
Zykino 3 years ago
parent
commit
93f1205ab8
2 changed files with 31 additions and 8 deletions
  1. +20
    -8
      prismedia/pt_upload.py
  2. +11
    -0
      prismedia/upload.py

+ 20
- 8
prismedia/pt_upload.py View File

@ -305,7 +305,7 @@ def upload_video(oauth, secret, options):
logger_stdout = logging.getLogger('stdoutlogs')
encoder = MultipartEncoder(fields)
progress_callback = create_callback(encoder)
progress_callback = create_callback(encoder, options.get('--progress'))
multipart_data = MultipartEncoderMonitor(encoder, progress_callback)
headers = {
@ -338,20 +338,32 @@ def upload_video(oauth, secret, options):
'%s') % response)
exit(1)
upload_finished = False
def create_callback(encoder):
encoder_len = encoder.len
upload_size_MB = encoder_len * (1 / (1024 * 1024))
def create_callback(encoder, progress_type):
upload_size_MB = encoder.len * (1 / (1024 * 1024))
if progress_type is None or "percentage" in progress_type.lower():
progress_lambda = lambda x: int((x / encoder.len) * 100) # Default to percentage
elif "bigfile" in progress_type.lower():
progress_lambda = lambda x: x * (1 / (1024 * 1024)) # MB
elif "accurate" in progress_type.lower():
progress_lambda = lambda x: x * (1 / (1024)) # kB
else:
# Should not happen outside of development when adding partly a progress type
logger.critical("Peertube: Unknown progress type `" + progress_type + "`")
exit(1)
bar = ProgressBar(expected_size=100, label=f"Peertube upload progress ({upload_size_MB:.2f}MB) ", filled_char='=')
bar = ProgressBar(expected_size=progress_lambda(encoder.len), label=f"Peertube upload progress ({upload_size_MB:.2f}MB) ", filled_char='=')
def callback(monitor):
# We want the condition to capture the varible from the parent scope, not a local variable that is created after
global upload_finished
percentage = int((monitor.bytes_read / encoder_len) * 100)
bar.show(percentage)
progress = progress_lambda(monitor.bytes_read)
bar.show(progress)
if monitor.bytes_read == encoder_len:
if monitor.bytes_read == encoder.len:
if not upload_finished:
# We get two time in the callback with both bytes equals, skip the first
upload_finished = True

+ 11
- 0
prismedia/upload.py View File

@ -49,6 +49,7 @@ Options:
If the playlist is not found, spawn an error except if --playlistCreate is set.
--playlistCreate Create the playlist if not exists. (default do not create)
Only relevant if --playlist is set.
--progress=STRING Set the progress bar view, one of percentage, bigFile, accurate.
-h --help Show this help.
--version Show version.
@ -150,6 +151,7 @@ VALID_LANGUAGES = ('arabic', 'english', 'french',
'german', 'hindi', 'italian',
'japanese', 'korean', 'mandarin',
'portuguese', 'punjabi', 'russian', 'spanish')
VALID_PROGRESS = ('percentage', 'bigfile', 'accurate')
def validateVideo(path):
@ -235,6 +237,14 @@ def validateLogLevel(loglevel):
return True
def validateProgress(progress):
for prgs in progress.split(','):
if prgs.lower().replace(" ", "") not in VALID_PROGRESS:
return False
return True
def _optionnalOrStrict(key, scope, error):
option = key.replace('-', '')
option = option[0].upper() + option[1:]
@ -385,6 +395,7 @@ def main():
Optional('--channelCreate'): bool,
Optional('--playlist'): Or(None, str),
Optional('--playlistCreate'): bool,
Optional('--progress'): Or(None, And(str, validateProgress, error="Sorry, progress visualisation not supported")),
'--help': bool,
'--version': bool,
# This allow to return all other options for further use: https://github.com/keleshev/schema#extra-keys

Loading…
Cancel
Save