starcluster.spinner

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

# Source Code for Module starcluster.spinner

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