Commit d66166e9 authored by Ronny Eichler's avatar Ronny Eichler
Browse files

folder stats table with path support

parent f5541bbb
......@@ -27,12 +27,11 @@ class DataMan(cmd.Cmd):
print "hi there!"
def do_stats(self, path):
if not path:
path = "."
tools.stats(path)
table_hdr = "{0:^25}{sep}{1}{sep}{2}{sep}{3}{sep}{4}{sep}{5}{sep}{6}{sep}".format(
"Folder name", "size", "#files", "#vid", "#img", "#snd", "format", sep="|")
print table_hdr
if not len(path):
path = '.'
import folderstats as fs
fs.print_table(fs.gather(path))
def do_EOF(self, line):
"Exit"
return True
......
......@@ -3,6 +3,7 @@
from __future__ import print_function
import tools
import os
import sys
from termcolor import colored
EXT_VIDEO = ['.avi', '.mp4', '.mkv', '.wmv']
......@@ -11,7 +12,7 @@ EXT_IMAGE = ['.png', '.bmp', '.jpg', '.jpeg', '.pgm']
EXT_DOC = ['.md', '.toml', '.xml', '.tsv', '.csv', '.txt', '.doc', '.rst']
table_hdr = "{0:^28}{sep}{1:^6}{sep}{2:>3}{sep}{3:>3}{sep}{4:>3}{sep}{5:>3}{sep}{6:^10}{sep}".format(
"Folder name", "size", "#fil", "#vid", "#img", "#snd", "format", sep="|")
"Folder name", "size", "#fil", "#vid", "#img", "#snd", "format", sep=" ")
_row = "{0:<28}{1}{2:>4}{3:>4}{4:>4}{5:>4}{6:>10}"
......@@ -54,6 +55,7 @@ def dir_details(path):
data_fmt=data_fmt)
def gather(path):
#print("Gathering: ", path)
root, dirs, files = next(os.walk(path))
details = [dir_details(root)]
......@@ -61,7 +63,7 @@ def gather(path):
return details
else:
for d in dirs:
details.append(dir_details(d))
details.append(dir_details(os.path.join(root, d)))
return details
def prettify(element, color=None, align='>', width=0, sepl='', sepr=''):
......@@ -113,8 +115,8 @@ def mk_row(row, colorized=True, cols=['fname', 'size', 'num_files',
elif row[c] == 'Kwik':
color = 'green'
else:
color = 'red'
row_str += prettify(row[c],
color = None
row_str += prettify(row[c] if row[c] is not None else '',
color=color if colored else None,
sepr=sepr, align='>', width=10)
else:
......@@ -122,8 +124,33 @@ def mk_row(row, colorized=True, cols=['fname', 'size', 'num_files',
return row_str
getch = tools._find_getch()
def print_table(rows, color=True, page_size=-1):
termh, termw = tools.terminal_size()
if page_size is not None and page_size < 1:
page_size = termh - 5
line_length = None
for i, row in enumerate(rows):
row_string = mk_row(row)
if line_length is None:
line_length = len(tools.strip_ansi(row_string))
# pause after printing full page of rows
if page_size is not None and page_size > 1 and i%(page_size+1) == 0:
if i > 1:
print("[MORE]")
c = getch()
sys.stdout.write("\033[F")
if c == 'q':
print('\n ...{} not shown.'.format(len(rows)-i))
break
print(table_hdr)
print('_'*line_length)
# print current line
print(row_string)
if __name__ == "__main__":
color = True
print(table_hdr)
for row in gather(".")[:-9]:
print(mk_row(row))
print_table(gather('.'))
......@@ -3,6 +3,7 @@
import os
from os.path import join, getsize
from termcolor import colored
import re
def fmt_size(num, unit='B', si=True, sep=' ', col=False, pad=0):
colors = {"k": "blue", "M": "green", "G": "red", "T": "cyan",
......@@ -15,9 +16,11 @@ def fmt_size(num, unit='B', si=True, sep=' ', col=False, pad=0):
divisor = 1000 if si else 1024
for prefix in prefixes:
if abs(num) < divisor:
if col:
prefix = colored(prefix, colors[prefix]) if prefix else ' '
return "{:5.1f}{}{}{}".format(num, sep, prefix, unit, pad=pad-6)
if prefix:
prefix = colored(prefix, colors[prefix]) if col else prefix
return "{:5.1f}{}{}{}".format(num, sep, prefix, unit, pad=pad-6)
else:
return "{:5.0f}{}{}{} ".format(num, sep, prefix, unit, pad=pad-6)
num /= divisor
def directory_content(path):
......@@ -28,7 +31,10 @@ def dir_size(path):
for root, dirs, files in os.walk(path):
for f in files:
fp = os.path.join(root, f)
total_size += os.path.getsize(fp)
try:
total_size += os.path.getsize(fp)
except OSError:
pass
return total_size
def stats(path):
......@@ -42,6 +48,38 @@ def stats(path):
print d, fmt_size(dir_size(d))
print "Files:\n", files
def terminal_size():
"""Returns tuple of height, width of terminal window.
In many cases this is inaccruate."""
return map(int, os.popen('stty size', 'r').read().split())
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
ansi_escape = re.compile(r'\x1b[^m]*m')
def strip_ansi(string):
"""Remove the ANSI codes from a string"""
return ansi_escape.sub('', string)
if __name__ == "__main__":
stats('.')
print fmt_size(dir_size("."))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment