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