Package starcluster :: Module spinner
[hide private]
[frames] | no frames]

Source Code for Module starcluster.spinner

 1  import sys 
 2  import time 
 3  from threading import Thread 
 4   
 5   
6 -class Spinner(Thread):
7 #Set the screen position of the spinner (chars from the left). 8 spin_screen_pos = 1 9 #Set the current index position in the spinner character list. 10 char_index_pos = 0 11 #Set the time between character changes in the spinner. 12 sleep_time = 1 13 #Set the spinner type: 0-3 14 spin_type = 2 15
16 - def __init__(self, type=spin_type):
17 Thread.__init__(self) 18 self.setDaemon(True) 19 self.stop_spinner = False 20 self.stopped = False 21 if type == 0: 22 self.char = ['O', 'o', '-', 'o', '0'] 23 elif type == 1: 24 self.char = ['.', 'o', 'O', 'o', '.'] 25 elif type == 2: 26 self.char = ['|', '/', '-', '\\', '-'] 27 else: 28 self.char = ['*', '#', '@', '%', '+'] 29 self.len = len(self.char)
30
31 - def Print(self, crnt):
32 str, crnt = self.curr(crnt) 33 sys.stdout.write("\b \b%s" % str) 34 sys.stdout.flush() # Flush stdout to get output before sleeping! 35 time.sleep(self.sleep_time) 36 return crnt
37
38 - def curr(self, crnt):
39 """ 40 Iterator for the character list position 41 """ 42 if crnt == 4: 43 return self.char[4], 0 44 elif crnt == 0: 45 return self.char[0], 1 46 else: 47 test = crnt 48 crnt += 1 49 return self.char[test], crnt
50
51 - def done(self):
52 sys.stdout.write("\b \b\n")
53
54 - def stop(self):
55 self.stop_spinner = True 56 while not self.stopped: 57 time.sleep(0.5) # give time for run to get the message
58
59 - def run(self):
60 # the comma keeps print from ending with a newline. 61 print " " * self.spin_screen_pos, 62 while True: 63 if self.stop_spinner: 64 self.done() 65 self.stopped = True 66 return 67 self.char_index_pos = self.Print(self.char_index_pos)
68
69 - def test(self, sleep=3.4):
70 print 'Waiting for process...', 71 self.start() 72 time.sleep(sleep) 73 self.stop() 74 print 'Process is finished...'
75 76 if __name__ == "__main__": 77 for i in range(0, 10): 78 s = Spinner() 79 s.test(sleep=float('3.' + str(i))) 80