]> git.phdru.name Git - m_lib.git/commitdiff
Import m_lib/pbar
authorOleg Broytman <phd@phdru.name>
Sat, 30 Nov 2013 22:14:18 +0000 (02:14 +0400)
committerOleg Broytman <phd@phdru.name>
Sat, 30 Nov 2013 22:14:18 +0000 (02:14 +0400)
m_lib/pbar/__init__.py [new file with mode: 0644]
m_lib/pbar/test/__init__.py [new file with mode: 0644]
m_lib/pbar/test/ind_test.py [new file with mode: 0755]
m_lib/pbar/test/test1.py [new file with mode: 0755]
m_lib/pbar/test/test2.py [new file with mode: 0755]
m_lib/pbar/test/test3.py [new file with mode: 0755]
m_lib/pbar/test/test4.py [new file with mode: 0755]
m_lib/pbar/tty_pbar.py [new file with mode: 0644]
setup.py

diff --git a/m_lib/pbar/__init__.py b/m_lib/pbar/__init__.py
new file mode 100644 (file)
index 0000000..89dcb8f
--- /dev/null
@@ -0,0 +1 @@
+"Broytman Flat ASCII Database for Python, Copyright (C) 1996-2001 PhiloSoft Design"
diff --git a/m_lib/pbar/test/__init__.py b/m_lib/pbar/test/__init__.py
new file mode 100644 (file)
index 0000000..6f91d98
--- /dev/null
@@ -0,0 +1 @@
+"Broytman Flat ASCII Database for Python - Test, Copyright (C) 1996-2001 PhiloSoft Design"
diff --git a/m_lib/pbar/test/ind_test.py b/m_lib/pbar/test/ind_test.py
new file mode 100755 (executable)
index 0000000..727633f
--- /dev/null
@@ -0,0 +1,25 @@
+#! /usr/bin/env python
+
+
+import sys
+from time import sleep
+from m_lib.pbar.tty_pbar import ttyProgressBar
+
+def test():
+   print "Test: ",
+   sys.stdout.flush()
+
+   x1 = 217
+   x2 = 837
+
+   pbar = ttyProgressBar(x1, x2)
+   for i in range(x1, x2+1):
+      pbar.display(i)
+      sleep(0.05)
+
+   pbar.erase()
+   print "Ok"
+
+
+if __name__ == "__main__":
+   test()
diff --git a/m_lib/pbar/test/test1.py b/m_lib/pbar/test/test1.py
new file mode 100755 (executable)
index 0000000..ed15ce6
--- /dev/null
@@ -0,0 +1,23 @@
+#! /usr/bin/env python
+"""
+   Test N1 to time file reading
+"""
+
+import sys
+from clock import clock
+
+
+def test():
+   print "Test: ",
+   sys.stdout.flush()
+
+   infile = open(sys.argv[1])
+   lines = infile.readlines()
+   infile.close()
+
+   print "Ok"
+
+
+if __name__ == "__main__":
+   test()
+   print "Overall time:", clock()
diff --git a/m_lib/pbar/test/test2.py b/m_lib/pbar/test/test2.py
new file mode 100755 (executable)
index 0000000..80dea23
--- /dev/null
@@ -0,0 +1,27 @@
+#! /usr/bin/env python
+"""
+   Test N2 to time file reading
+"""
+
+import sys
+from clock import clock
+
+
+def test():
+   print "Test: ",
+   sys.stdout.flush()
+
+   infile = open(sys.argv[1])
+   lines = []
+   line = '\n'
+   while line:
+      line = infile.readline()
+      lines.append(line)
+   infile.close()
+
+   print "Ok"
+
+
+if __name__ == "__main__":
+   test()
+   print "Overall time:", clock()
diff --git a/m_lib/pbar/test/test3.py b/m_lib/pbar/test/test3.py
new file mode 100755 (executable)
index 0000000..84a56c3
--- /dev/null
@@ -0,0 +1,44 @@
+#! /usr/bin/env python
+"""
+   Test N3 to time file reading
+"""
+
+import sys, os
+from clock import clock
+from m_lib.pbar.tty_pbar import ttyProgressBar
+
+
+def test():
+   print "Test: ",
+   sys.stdout.flush()
+
+   size = os.path.getsize(sys.argv[1])
+   infile = open(sys.argv[1])
+   pbar = ttyProgressBar(0, size)
+
+   lines = []
+   line = '\n'
+   lng = 0
+
+   # This is for DOS - it counts CRLF, which len() counts as 1 char!
+   if os.name == 'dos' or os.name == 'nt' :
+      dos_add = 1
+   else:
+      dos_add = 0 # UNIX' and Mac's len() counts CR or LF correct
+
+   while line:
+      line = infile.readline()
+      lines.append(line)
+
+      lng = lng + len(line) + dos_add
+      pbar.display(lng)
+
+   infile.close()
+   pbar.erase()
+
+   print "Ok"
+
+
+if __name__ == "__main__":
+   test()
+   print "Overall time:", clock()
diff --git a/m_lib/pbar/test/test4.py b/m_lib/pbar/test/test4.py
new file mode 100755 (executable)
index 0000000..1ce821c
--- /dev/null
@@ -0,0 +1,34 @@
+#! /usr/bin/env python
+"""
+   Test N4: earse()/redraw()
+"""
+
+import sys
+from time import sleep
+from m_lib.pbar.tty_pbar import ttyProgressBar
+
+
+def test():
+   sys.stdout.write("Displaying... ")
+   sys.stdout.flush()
+
+   pbar = ttyProgressBar(0, 100)
+   pbar.display(42)
+   sleep(2)
+   pbar.erase()
+
+   sys.stdout.write("erasing... ")
+   sys.stdout.flush()
+   sleep(2)
+
+   sys.stdout.write("redisplaying... ")
+   sys.stdout.flush()
+   pbar.redraw()
+   sleep(2)
+
+   del pbar
+   print "Ok"
+
+
+if __name__ == "__main__":
+   test()
diff --git a/m_lib/pbar/tty_pbar.py b/m_lib/pbar/tty_pbar.py
new file mode 100644 (file)
index 0000000..3903d1f
--- /dev/null
@@ -0,0 +1,113 @@
+"""
+   Progress bar (TTY version)
+
+   Written by Broytman, Jan 1997 - Sep 2003. Copyright (C) 1997-2003 PhiloSoft Design
+"""
+
+
+import sys, os
+
+
+class ttyProgressBar:
+   """
+      Simple progress indicator - displays bar "graph" using standard tty
+      commands - space, backspace, etc. This method is compatible with
+      (almost) all UNIX consoles and DOS boxes.
+
+      Example:
+
+         ====------  42%
+
+      This is a "bar" (width 10 chars) for 42%
+
+      Certainly, to use it nicely, do not write anything on screen
+      (to stdout or stderr), while using it (or use erase()/redraw() methods).
+      Erase or delete it after using.
+   """
+
+   left_c = '#'     # Chars for "graphics"
+   right_c = '_'
+   space_c = ' '    # a space
+   back_c = chr(8)  # a backspace
+
+   if os.name == 'dos' or os.name == 'nt' : # Use nice chars on DOS screen
+      left_c = chr(178)
+      right_c = chr(176)
+
+
+   def __init__(self, min, max, out = sys.stderr, width1 = 10,
+         width2 = 1+3+1): # 1 space + 3 chars for "100" + 1 for "%"
+      self.min = min
+      self.current = min
+      self.max = max - min
+
+      self.width1 = width1
+      self.width2 = width2
+      self.out = out
+
+      self.redraw()
+
+
+   def __del__(self):
+      self.erase()
+
+
+   def display(self, current):
+      """
+         Draw current value on indicator.
+         Optimized to draw as little as possible.
+      """
+
+      self.current = current
+      current -= self.min
+      lng = (current * self.width1) / self.max
+
+      if current >= self.max:
+         percent = 100
+      else:
+         percent = (current * 100) / self.max
+
+      flush = False
+
+      if self.lng <> lng:
+         self.lng =  lng
+         self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
+         self.out.write(ttyProgressBar.left_c*lng)
+         self.out.write(ttyProgressBar.right_c*(self.width1-lng) + ttyProgressBar.space_c)
+         self.percent = -1 # force to redraw percentage; the bug was fixed by William McVey
+         flush = True
+
+      elif self.percent <> percent:
+         self.out.write(ttyProgressBar.back_c * (self.width2-1))
+         flush = True
+
+
+      if self.percent <> percent:
+         self.percent =  percent
+         self.out.write(str(percent).rjust(3) + '%')
+         flush = True
+
+
+      if flush:
+         self.out.flush()
+
+      self.visible = True
+
+
+   def erase(self):
+      if self.visible: # Prevent erase() to be called twice - explicitly and from __del__()
+         self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
+         self.out.write(ttyProgressBar.space_c*(self.width1+self.width2))
+         self.out.write(ttyProgressBar.back_c*(self.width1+self.width2))
+         self.out.flush()
+         self.visible = False
+
+
+   def redraw(self):
+      self.lng = -1 # force to draw bar on the first call
+      self.percent = -1
+
+      # Clear space - just in case there was some cruft left
+      self.out.write(ttyProgressBar.space_c*(self.width1+self.width2))
+      # redraw everything
+      self.display(self.current)
index c76dc8bba9cd747b5dc56749d7ccd38a11a8d605..d9f45a7a79f7c45cb0621447e1e75ade7e33aa50 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -13,8 +13,9 @@ setup(name = "m_lib",
     platforms = "All",
     packages = ["m_lib", "m_lib.clock", "m_lib.flad", "m_lib.flad.test",
         "m_lib.hash", "m_lib.hash.test", "m_lib.lazy",
-        "m_lib.net", "m_lib.net.ftp", "m_lib.net.www", "m_lib.rus",
-        ],
+        "m_lib.net", "m_lib.net.ftp", "m_lib.net.www",
+        "m_lib.pbar", "m_lib.pbar.test", "m_lib.rus",
+    ],
     data_files = [("%s/m_lib/flad/test" % python_lib, [
         "m_lib/flad/test/test.cfg",
         "m_lib/flad/test/test.txt",