src/ldt/ldt/management/utils.py
author ymh <ymh.work@gmail.com>
Wed, 22 Apr 2020 17:19:13 +0200
changeset 1497 c42847c334a0
parent 501 5b198be85d50
permissions -rw-r--r--
add option media creation date in createmediacontent
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
501
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Feb 1, 2012
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import sys
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import codecs #@UnresolvedImport
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import math
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
def show_progress(current_line, total_line, label, width, writer=None):
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    if writer is None:
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
        writer = sys.stdout
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
        if sys.stdout.encoding is not None:
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
            writer = codecs.getwriter(sys.stdout.encoding)(sys.stdout)
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    percent = (float(current_line) / float(total_line)) * 100.0
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    marks = math.floor(width * (percent / 100.0)) #@UndefinedVariable
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    spaces = math.floor(width - marks) #@UndefinedVariable
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    loader = u'[' + (u'=' * int(marks)) + (u' ' * int(spaces)) + u']'
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    s = u"%s %3d%% %*d/%d - %*s\r" % (loader, percent, len(str(total_line)), current_line, total_line, width, label[:width])
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    writer.write(s) #takes the header into account
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    if percent >= 100:
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        writer.write("\n")
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    writer.flush()
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    
5b198be85d50 small change after old platform migration
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    return writer