ported to Python 3

This commit is contained in:
Harald Goettlicher 2022-09-02 23:11:56 +02:00
parent 06b74d5a90
commit 8a3c442f50

72
tkdu.py
View File

@ -3,6 +3,8 @@
# This is tkdu.py, an interactive program to display disk usage # This is tkdu.py, an interactive program to display disk usage
# Copyright 2004 Jeff Epler <jepler@unpythonic.net> # Copyright 2004 Jeff Epler <jepler@unpythonic.net>
# #
# This is the version ported to Python 3
#
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
@ -17,8 +19,8 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import math, Tkinter, sys, os, stat, string, time, gzip, FileDialog import math, tkinter, sys, os, stat, string, time, gzip, tkinter.filedialog
from tkFileDialog import askdirectory from tkinter.filedialog import askdirectory
MIN_PSZ = 1000 MIN_PSZ = 1000
MIN_IPSZ = 240 MIN_IPSZ = 240
@ -42,10 +44,10 @@ def allocate(path, files, canvas, x, y, w, h, first, depth):
if psz < MIN_PSZ: return if psz < MIN_PSZ: return
if path and path[-1] == "/": if path and path[-1] == "/":
basename_idx = len(path) basename_idx = len(path)
nslashes = string.count(path, os.sep) - 1 nslashes = path.count(os.sep) - 1
else: else:
basename_idx = len(path) + 1 basename_idx = len(path) + 1
nslashes = string.count(path, os.sep) nslashes = path.count(os.sep)
dircolor = dircolors[nslashes % len(dircolors)] dircolor = dircolors[nslashes % len(dircolors)]
leafcolor = leafcolors[nslashes % len(dircolors)] leafcolor = leafcolors[nslashes % len(dircolors)]
colors = (leafcolor, dircolor) colors = (leafcolor, dircolor)
@ -193,17 +195,17 @@ def run_queue(c):
if time.time() > end: break if time.time() > end: break
item = queue[0] item = queue[0]
del queue[0] del queue[0]
apply(item[0], item[1:]) item[0](*item[1:])
if queue: if queue:
c.aid = c.after_idle(run_queue, c) c.aid = c.after_idle(run_queue, c)
def chroot(e, r): def chroot(e, r):
c = e.widget c = e.widget
if r is None:
return
if not getkids(c.files, r): if not getkids(c.files, r):
r = os.path.dirname(r) r = os.path.dirname(r)
if r == c.cur: return if r == c.cur: return
if r is None:
return
if not r.startswith(c.root): if not r.startswith(c.root):
c.bell() c.bell()
return return
@ -292,7 +294,7 @@ def reconfigure(e):
if c.cur == "/": if c.cur == "/":
nslashes = -1 nslashes = -1
else: else:
nslashes = string.count(c.cur, os.sep) - 1 nslashes = c.cur.count(os.sep) - 1
parent = os.path.dirname(c.cur) parent = os.path.dirname(c.cur)
color = dircolors[nslashes % len(dircolors)] color = dircolors[nslashes % len(dircolors)]
c.configure(background=color) c.configure(background=color)
@ -322,16 +324,16 @@ def getkids(dict, path):
def doit(dir, files): def doit(dir, files):
sorted_files = {} sorted_files = {}
for k, v in files.items(): for k, v in files.items():
sv = map(lambda (k, v): [v, k, None], v.items()) t = [k, v]
sv.sort() sv = sorted(map((lambda t: [t[1], t[0], None]), v.items()))
sv.reverse() sv.reverse()
sorted_files[k] = (v, sv) sorted_files[k] = (v, sv)
t = Tkinter.Tk() t = tkinter.Tk()
c = Tkinter.Canvas(t, width=1024, height=768) c = tkinter.Canvas(t, width=1024, height=768)
c.tip = Tkinter.Toplevel(t) c.tip = tkinter.Toplevel(t)
c.tip.wm_overrideredirect(1) c.tip.wm_overrideredirect(1)
c.tipl = Tkinter.Label(c.tip) c.tipl = tkinter.Label(c.tip)
c.tipl.pack() c.tipl.pack()
c.pack(expand="yes", fill="both") c.pack(expand="yes", fill="both")
c.files = sorted_files c.files = sorted_files
@ -369,13 +371,13 @@ def main(f = sys.stdin):
files = {} files = {}
firstfile = None firstfile = None
for line in f.readlines(): for line in f.readlines():
sz, name = string.split(line[:-1], None, 1) sz, name = line[:-1].split(None, 1)
# name = name.split("/") # name = name.split("/")
sz = long(sz)*1024 sz = int(sz)*1024
putname(files, name, sz) putname(files, name, sz)
doit(name, files) doit(name, files)
def du(dir, files, fs=0, ST_MODE=stat.ST_MODE, ST_SIZE = stat.ST_SIZE, S_IFMT = 0170000, S_IFDIR = 0040000, lstat = os.lstat, putname_base = putname_base, fmt="%%s%s%%s" % os.sep): def du(dir, files, fs=0, ST_MODE=stat.ST_MODE, ST_SIZE = stat.ST_SIZE, S_IFMT = 0o170000, S_IFDIR = 0o040000, lstat = os.lstat, putname_base = putname_base, fmt="%%s%s%%s" % os.sep):
tsz = 0 tsz = 0
try: fns = os.listdir(dir) try: fns = os.listdir(dir)
@ -393,7 +395,7 @@ def du(dir, files, fs=0, ST_MODE=stat.ST_MODE, ST_SIZE = stat.ST_SIZE, S_IFMT =
continue continue
if info[ST_MODE] & S_IFMT == S_IFDIR: if info[ST_MODE] & S_IFMT == S_IFDIR:
sz = du(fn, files) + long(info[ST_SIZE]) sz = du(fn, files) + int(info[ST_SIZE])
else: else:
sz = info[ST_SIZE] sz = info[ST_SIZE]
d[fn] = sz d[fn] = sz
@ -403,9 +405,9 @@ def du(dir, files, fs=0, ST_MODE=stat.ST_MODE, ST_SIZE = stat.ST_SIZE, S_IFMT =
def abspath(p): def abspath(p):
return os.path.normpath(os.path.join(os.getcwd(), p)) return os.path.normpath(os.path.join(os.getcwd(), p))
class DirDialog(FileDialog.LoadFileDialog): class DirDialog(tkinter.filedialog.LoadFileDialog):
def __init__(self, master, title=None): def __init__(self, master, title=None):
FileDialog.LoadFileDialog.__init__(self, master, title) tkinter.filedialog.LoadFileDialog.__init__(self, master, title)
self.files.destroy() self.files.destroy()
self.filesbar.destroy() self.filesbar.destroy()
@ -445,11 +447,11 @@ def main_builtin_du(args):
if len(args) > 1: if len(args) > 1:
p = args[1] p = args[1]
else: else:
t = Tkinter.Tk() t = tkinter.Tk()
t.wm_withdraw() t.wm_withdraw()
p = askdirectory() p = askdirectory()
if Tkinter._default_root is t: if tkinter._default_root is t:
Tkinter._default_root = None tkinter._default_root = None
t.destroy() t.destroy()
if p is None: if p is None:
return return
@ -457,19 +459,19 @@ def main_builtin_du(args):
if p == '-h' or p == '--help' or p == '-?': if p == '-h' or p == '--help' or p == '-?':
base = os.path.basename(args[0]) base = os.path.basename(args[0])
print 'Usage:' print ('Usage:')
print ' ', base, '<file.gz> interpret file as gzipped du -ak output and visualize it' print (' ', base, '<file.gz> interpret file as gzipped du -ak output and visualize it')
print ' ', base, '<file> interpret file as du -ak output and visualize it' print (' ', base, '<file> interpret file as du -ak output and visualize it')
print ' ', base, '<folder> analyze disk usage in that folder' print (' ', base, '<folder> analyze disk usage in that folder')
print ' ', base, '- interpret stdin input as du -ak output and visualize it' print (' ', base, '- interpret stdin input as du -ak output and visualize it')
print ' ', base, ' ask for folder to analyze' print (' ', base, ' ask for folder to analyze')
print print
print 'Controls:' print ('Controls:')
print ' * Press `q` to quit' print (' * Press `q` to quit')
print ' * LMB: zoom in to item' print (' * LMB: zoom in to item')
print ' * RMB: zoom out one level' print (' * RMB: zoom out one level')
print ' * Press `1`..`9`: Show that many nested levels' print (' * Press `1`..`9`: Show that many nested levels')
print ' * Press `0`: Show man nested levels' print (' * Press `0`: Show man nested levels')
return return
if p == "-": if p == "-":