_utils.pyx 4.16 KB
Newer Older
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
# cython: profile=True

from _utils cimport *

import sys

cdef class FakeFile:

    def __init__(self,li):
        self._li = li
        self.__buffer = []
        self.__bufsize=0
        
    cpdef str read(self,int size=-1):
        
        cdef int csize=self.__bufsize
        cdef str line
        cdef str buffer
         
        try:
            while(csize < size or size < 0):
                    line = self._li.next()
                    csize+=len(line)
                    self.__buffer.append(line)
        except StopIteration:
            if csize==0:
                raise EOFError
        
        buffer = ''.join(self.__buffer)
        
        if size >= 0:
            self.__buffer=[buffer[size:]]
            self.__bufsize=len(self.__buffer[0])
            buffer=buffer[0:size]
        else:
            self.__buffer=[]
            self.__bufsize=0
            
        return buffer
        
    cpdef str readline(self):
     
43
            cdef str line  # @DuplicatedSignature
44 45 46 47 48 49 50 51 52 53 54 55 56 57
            
            try:
                if self.__buffer:
                    line = self.__buffer[0]
                    self.__buffer=[]
                    self.__bufsize=0
                else:
                    line=self._li.next()
            except StopIteration:
                raise EOFError
            
            return line
        
cpdef object progressBar(object pos,
58
                  off_t maxi,
59 60 61 62 63
                  bint reset=False,
                  bytes head=b'',
                  list delta=[],
                  list step=[1,0,0]):
                  
64
    cdef off_t    ipos
65 66 67
    cdef double percent 
    cdef int days,hour,minu,sec
    cdef bytes bar
68
    cdef off_t fraction
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    cdef int freq,cycle,arrow
    cdef tm remain

    cdef clock_t d
    cdef clock_t elapsed
    cdef clock_t newtime 
    cdef clock_t more
    
    #                   0123456789
    cdef char* wheel=  '|/-\\'
    cdef char*  spaces='          ' \
                       '          ' \
                       '          ' \
                       '          ' \
                       '          '
                
    cdef char*  diese ='##########' \
                       '##########' \
                       '##########' \
                       '##########' \
                       '##########' 
                  
    if reset:
        del delta[:]
93
        step[:]=[1,0,0]
94 95 96 97
    if not delta:
        delta.append(clock())
        delta.append(clock())
        
98 99
    if ( maxi<=0):
        maxi=1
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
    
    freq,cycle,arrow = step

    cycle+=1
    
    if cycle % freq == 0:
        cycle=1
        newtime = clock()
        d = newtime-delta[1]
        
        if d < 0.2 * CLOCKS_PER_SEC :
            freq*=2
        elif d > 0.4 * CLOCKS_PER_SEC and freq>1:
            freq/=2
            
        delta[1]=newtime
        elapsed = newtime-delta[0]
        
        if callable(pos):
            ipos=pos()
        else:
            ipos=pos
            
        percent = <double>ipos/<double>maxi
        more = <time_t>((<double>elapsed / percent * (1. - percent))/CLOCKS_PER_SEC)
        <void>gmtime_r(&more, &remain)
        days = remain.tm_yday 
        hour = remain.tm_hour
        minu  = remain.tm_min
        sec  = remain.tm_sec

        fraction=<int>(percent * 50.)
132 133 134 135
        if fraction < 0:
            fraction=0
        if fraction > 50:
            fraction=50
136 137 138
        arrow=(arrow+1) % 4
        
        if days:
139
            <void>fprintf(stderr,b'\r%s %5.1f %% |%.*s%c%.*s] remain : %d days %02d:%02d:%02d',
140 141
                            <char*>head,
                            percent*100,
142 143 144
                            fraction,diese,
                            wheel[arrow],
                            50-fraction,spaces,
145 146
                            days,hour,minu,sec)
        else:
147
            <void>fprintf(stderr,b'\r%s %5.1f %% |%.*s%c%.*s] remain : %02d:%02d:%02d',
148 149
                            <char*>head,
                            percent*100.,
150 151 152
                            fraction,diese,
                            wheel[arrow],
                            50-fraction,spaces,
153
                            hour,minu,sec)
154
        
155 156 157 158 159 160

    else:
        cycle+=1

    step[0:3] = freq,cycle,arrow