Skip to content

Commit 6415cc8

Browse files
committed
Slightly improve performance in python2
1 parent 8a709a3 commit 6415cc8

2 files changed

Lines changed: 40 additions & 38 deletions

File tree

tkfilebrowser/constants.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,6 @@ def get_modification_date(file):
164164
return date
165165

166166

167-
def get_size(file):
168-
"""Return the size of file."""
169-
try:
170-
size_o = os.path.getsize(file)
171-
except OSError:
172-
size_o = 0
173-
if size_o > 0:
174-
m = int(floor(log(size_o) / log(1024)))
175-
if m < len(SIZES):
176-
unit = SIZES[m]
177-
s = size_o / (1024 ** m)
178-
else:
179-
unit = SIZES[-1]
180-
s = size_o / (1024**(len(SIZES) - 1))
181-
size = "%s %s" % (locale_number("%.1f" % s), unit)
182-
else:
183-
size = "0 " + _("B")
184-
return size
185-
186-
187167
def display_modification_date(mtime):
188168
"""Return the modDification date of file."""
189169
tps = fromtimestamp(mtime)

tkfilebrowser/filebrowser.py

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
import psutil
25-
from os import walk, mkdir
25+
from os import walk, mkdir, stat
2626
from os.path import exists, join, getmtime, realpath, split, expanduser, \
2727
abspath, isabs, splitext, dirname, getsize, isdir, isfile, islink
2828
try:
@@ -33,7 +33,7 @@
3333
import traceback
3434
import tkfilebrowser.constants as cst
3535
from tkfilebrowser.constants import unquote, tk, ttk, key_sort_files, \
36-
get_modification_date, get_size
36+
get_modification_date, display_modification_date, display_size
3737
from tkfilebrowser.autoscrollbar import AutoScrollbar
3838
from tkfilebrowser.path_button import PathButton
3939
from tkfilebrowser.tooltip import TooltipTreeWrapper
@@ -74,9 +74,9 @@ def __init__(self, parent, initialdir="", initialfile="", mode="openfile",
7474

7575
# python version compatibility
7676
if SCANDIR:
77-
self.display_folder = self.display_folder_scandir
77+
self.display_folder = self._display_folder_scandir
7878
else:
79-
self.display_folder = self.display_folder_walk
79+
self.display_folder = self._display_folder_walk
8080

8181
# keep track of folders to be able to move backward/foreward in history
8282
if initialdir:
@@ -699,15 +699,19 @@ def _display_recents(self):
699699
ext = splitext(f)[-1]
700700
if extension == [""] or ext in extension:
701701
tags.append("file_link")
702-
vals = (p, get_size(p), get_modification_date(p))
702+
stats = stat(p)
703+
vals = (p, display_size(stats.st_size),
704+
display_modification_date(stats.st_mtime))
703705
elif isdir(p):
704706
tags.append("folder_link")
705707
vals = (p, "", get_modification_date(p))
706708
elif isfile(p):
707709
ext = splitext(f)[-1]
708710
if extension == [""] or ext in extension:
709711
tags.append("file")
710-
vals = (p, get_size(p), get_modification_date(p))
712+
stats = stat(p)
713+
vals = (p, display_size(stats.st_size),
714+
display_modification_date(stats.st_mtime))
711715
elif isdir(p):
712716
tags.append("folder")
713717
vals = (p, "", get_modification_date(p))
@@ -898,7 +902,7 @@ def _update_path_bar(self, path):
898902
self.path_bar_buttons.append(b)
899903
b.grid(row=0, column=i + 2, sticky="ns")
900904

901-
def display_folder_walk(self, folder, reset=True, update_bar=True):
905+
def _display_folder_walk(self, folder, reset=True, update_bar=True):
902906
"""
903907
Display the content of folder in self.right_tree.
904908
Arguments:
@@ -969,9 +973,18 @@ def display_folder_walk(self, folder, reset=True, update_bar=True):
969973
tags = tags + (str(i % 2),)
970974
i += 1
971975

972-
self.right_tree.insert("", "end", p, text=f, tags=tags,
973-
values=("", get_size(p),
974-
get_modification_date(p)))
976+
try:
977+
stats = stat(p)
978+
except OSError:
979+
self.right_tree.insert("", "end", p, text=f, tags=tags,
980+
values=("",
981+
display_size(0),
982+
display_modification_date(cst.TODAY)))
983+
else:
984+
self.right_tree.insert("", "end", p, text=f, tags=tags,
985+
values=("",
986+
display_size(stats.st_size),
987+
display_modification_date(stats.st_mtime)))
975988
else:
976989
for f in files:
977990
ext = splitext(f)[-1]
@@ -990,9 +1003,18 @@ def display_folder_walk(self, folder, reset=True, update_bar=True):
9901003
tags = tags + (str(i % 2),)
9911004
i += 1
9921005

993-
self.right_tree.insert("", "end", p, text=f, tags=tags,
994-
values=("", get_size(p),
995-
get_modification_date(p)))
1006+
try:
1007+
stats = stat(p)
1008+
except OSError:
1009+
self.right_tree.insert("", "end", p, text=f, tags=tags,
1010+
values=("",
1011+
display_size(0),
1012+
display_modification_date(cst.TODAY)))
1013+
else:
1014+
self.right_tree.insert("", "end", p, text=f, tags=tags,
1015+
values=("",
1016+
display_size(stats.st_size),
1017+
display_modification_date(stats.st_mtime)))
9961018
items = self.right_tree.get_children("")
9971019
if items:
9981020
self.right_tree.focus_set()
@@ -1003,7 +1025,7 @@ def display_folder_walk(self, folder, reset=True, update_bar=True):
10031025
except StopIteration:
10041026
print("err")
10051027

1006-
def display_folder_scandir(self, folder, reset=True, update_bar=True):
1028+
def _display_folder_scandir(self, folder, reset=True, update_bar=True):
10071029
"""
10081030
Display the content of folder in self.right_tree.
10091031
@@ -1052,17 +1074,17 @@ def display_folder_scandir(self, folder, reset=True, update_bar=True):
10521074
else:
10531075
tags = tags + (str(i % 2),)
10541076
i += 1
1055-
stat = f.stat()
1077+
stats = f.stat()
10561078
if b_file:
10571079
if (extension == [""] or splitext(name)[-1] in extension):
10581080
self.right_tree.insert("", "end", f.path, text=name, tags=tags,
10591081
values=("",
1060-
cst.display_size(stat.st_size),
1061-
cst.display_modification_date(stat.st_mtime)))
1082+
display_size(stats.st_size),
1083+
display_modification_date(stats.st_mtime)))
10621084
else:
10631085
self.right_tree.insert("", "end", f.path, text=name, tags=tags,
10641086
values=("", "",
1065-
cst.display_modification_date(stat.st_mtime)))
1087+
display_modification_date(stats.st_mtime)))
10661088
items = self.right_tree.get_children("")
10671089
if items:
10681090
self.right_tree.focus_set()

0 commit comments

Comments
 (0)