Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
j4321 committed Aug 14, 2018
2 parents e654ee6 + 0b910bc commit 9df8828
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Changelog

- tkfilebrowser 2.2.3
* Fix FileNotFoundError if initialdir does not exist
* Add Desktop in shortcuts (if found)
* Improve filetype filtering

- tkfilebrowser 2.2.2
* Fix ValueError in after_cancel with Python 3.6.5
Expand Down
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Changelog

- tkfilebrowser 2.2.3
* Fix FileNotFoundError if initialdir does not exist
* Add Desktop in shortcuts (if found)
* Improve filetype filtering

- tkfilebrowser 2.2.2
* Fix ValueError in after_cancel with Python 3.6.5
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
keywords=['tkinter', 'filedialog', 'filebrowser'],
packages=["tkfilebrowser"],
package_data={"tkfilebrowser": ["images/*"]},
requires=["os", "locale", "time", "psutil", "tkinter", "math", "urllib", "babel"])
requires=["os", "locale", "time", "psutil", "tkinter", "math", "urllib", "babel",
"re", "subprocess"])
19 changes: 14 additions & 5 deletions tkfilebrowser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@

def c_open_file_old():
rep = filedialog.askopenfilenames(parent=root, initialdir='/', initialfile='tmp',
filetypes=[("PNG", "*.png"), ("JPEG", "*.jpg"), ("All files", "*")])
filetypes=[("PNG", "*.png"),
("JPEG", "*.jpg"),
("All files", "*")])
print(rep)


Expand All @@ -48,14 +50,19 @@ def c_open_dir_old():


def c_save_old():
rep = filedialog.asksaveasfilename(parent=root, defaultextension=".png", initialdir='/tmp', initialfile='image.png',
filetypes=[("PNG", "*.png"), ("JPEG", "*.jpg"), ("All files", "*")])
rep = filedialog.asksaveasfilename(parent=root, defaultextension=".png",
initialdir='/tmp', initialfile='image.png',
filetypes=[("PNG", "*.png"),
("JPEG", "*.jpg"),
("Text files", "*.txt"),
("All files", "*")])
print(rep)


def c_open_file():
rep = askopenfilenames(parent=root, initialdir='/', initialfile='tmp',
filetypes=[("Pictures", "*.png|*.jpg|*.JPG"), ("All files", "*")])
filetypes=[("Pictures", "*.png|*.jpg|*.JPG"),
("All files", "*")])
print(rep)


Expand All @@ -66,7 +73,9 @@ def c_open_dir():

def c_save():
rep = asksaveasfilename(parent=root, defaultext=".png", initialdir='/tmp', initialfile='image.png',
filetypes=[("Pictures", "*.png|*.jpg|*.JPG"), ("All files", "*")])
filetypes=[("Pictures", "*.png|*.jpg|*.JPG"),
("Text files", "*.txt"),
("All files", "*")])
print(rep)


Expand Down
2 changes: 1 addition & 1 deletion tkfilebrowser/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
tkfilebrowser - Alternative to filedialog for Tkinter
Copyright 2017 Juliette Monsel <[email protected]>
Copyright 2017-2018 Juliette Monsel <[email protected]>
tkfilebrowser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
69 changes: 42 additions & 27 deletions tkfilebrowser/filebrowser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
tkfilebrowser - Alternative to filedialog for Tkinter
Copyright 2017 Juliette Monsel <[email protected]>
Copyright 2017-2018 Juliette Monsel <[email protected]>
tkfilebrowser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -19,11 +19,11 @@
Main class
"""
# TODO: fix filetype display for extensions like .tar.xz
# TODO: improve extension change
# TODO: show desktop in shortcuts


import psutil
from re import search
from subprocess import check_output
from os import walk, mkdir, stat, access, W_OK
from os.path import exists, join, getmtime, realpath, split, expanduser, \
abspath, isabs, splitext, dirname, getsize, isdir, isfile, islink
Expand Down Expand Up @@ -141,8 +141,6 @@ def __init__(self, parent, initialdir="", initialfile="", mode="openfile",
default='white')
fg = style.lookup('TLabel', 'foreground', default='black')
active_bg = style.lookup('TButton', 'background', ('active',))
# active_fg = style.lookup('TButton', 'foreground', ('active',))
# disabled_fg = style.lookup('TButton', 'foreground', ('disabled',))
sel_bg = style.lookup('Treeview', 'background', ('selected',))
sel_fg = style.lookup('Treeview', 'foreground', ('selected',))
self.option_add('*TCombobox*Listbox.selectBackground', sel_bg)
Expand Down Expand Up @@ -182,7 +180,7 @@ def __init__(self, parent, initialdir="", initialfile="", mode="openfile",
for name, exts in filetypes:
if name not in self.filetypes:
self.filetypes[name] = []
self.filetypes[name].extend([ext.split("*")[-1].strip() for ext in exts.split("|")])
self.filetypes[name] = r'%s$' % exts.strip().replace('.', '\.').replace('*', '.*')
values = list(self.filetypes.keys())
w = max([len(f) for f in values] + [5])
b_filetype = ttk.Combobox(self, textvariable=self.filetype,
Expand All @@ -196,10 +194,8 @@ def __init__(self, parent, initialdir="", initialfile="", mode="openfile",
self.filetype.trace_add('write', lambda *args: self._change_filetype())
except AttributeError:
self.filetype.trace('w', lambda *args: self._change_filetype())
# b_filetype.bind('<<ComboboxSelected>>',
# lambda e: self._change_filetype())
else:
self.filetypes[""] = [""]
self.filetypes[""] = r".*$"

# --- recent files
self._recent_files = RecentFiles(cst.RECENT_FILES, 30)
Expand Down Expand Up @@ -279,11 +275,13 @@ def __init__(self, parent, initialdir="", initialfile="", mode="openfile",
scroll_left.grid(row=0, column=1, sticky="ns")
self.left_tree.configure(yscrollcommand=scroll_left.set)

# --- list devices and bookmarked locations
# list devices and bookmarked locations
# -------- recent
self.left_tree.insert("", "end", iid="recent", text=_("Recent"),
image=self.im_recent)
wrapper.add_tooltip("recent", _("Recently used"))

# -------- devices
devices = psutil.disk_partitions()

for d in devices:
Expand All @@ -295,10 +293,26 @@ def __init__(self, parent, initialdir="", initialfile="", mode="openfile",
self.left_tree.insert("", "end", iid=m, text=txt,
image=self.im_drive)
wrapper.add_tooltip(m, m)

# -------- home
home = expanduser("~")
self.left_tree.insert("", "end", iid=home, image=self.im_home,
text=split(home)[-1])
wrapper.add_tooltip(home, home)

# -------- desktop
try:
desktop = check_output(['xdg-users-dir', 'DESKTOP']).decode().strip()
except Exception:
# FileNotFoundError in python3 if xdg-users-dir is not installed,
# but OSError in python2
desktop = join(home, 'Desktop')
if exists(desktop):
self.left_tree.insert("", "end", iid=desktop, image=self.im_folder,
text=split(desktop)[-1])
wrapper.add_tooltip(desktop, desktop)

# -------- bookmarks
path_bm = join(home, ".config", "gtk-3.0", "bookmarks")
path_bm2 = join(home, ".gtk-bookmarks") # old location
if exists(path_bm):
Expand Down Expand Up @@ -702,8 +716,7 @@ def _display_recents(self):
f = "/"
if islink(p):
if isfile(p):
ext = splitext(f)[-1]
if extension == [""] or ext in extension:
if extension == r".*$" or search(extension, f):
tags.append("file_link")
stats = stat(p)
vals = (p, display_size(stats.st_size),
Expand All @@ -712,8 +725,7 @@ def _display_recents(self):
tags.append("folder_link")
vals = (p, "", get_modification_date(p))
elif isfile(p):
ext = splitext(f)[-1]
if extension == [""] or ext in extension:
if extension == r".*$" or search(extension, f):
tags.append("file")
stats = stat(p)
vals = (p, display_size(stats.st_size),
Expand Down Expand Up @@ -756,11 +768,16 @@ def _change_filetype(self):
else:
self._display_recents()
if self.mode == 'save':
name, ext = splitext(self.entry.get())
new_ext = self.filetypes[self.filetype.get()][0]
if new_ext and ext not in self.filetypes[self.filetype.get()]:
self.entry.delete(len(name), 'end')
self.entry.insert('end', new_ext)
filename = self.entry.get()
new_ext = self.filetypes[self.filetype.get()]
if filename and not search(new_ext, filename):
old_ext = search(r'\..+$', filename).group()
exts = [e[2:].replace('\.', '.') for e in new_ext[:-1].split('|')]
exts = [e for e in exts if search(r'\.[^\*]+$', e)]
if exts:
filename = filename.replace(old_ext, exts[0])
self.entry.delete(0, 'end')
self.entry.insert(0, filename)

# --- path completion in entries: key bindings
def _down(self, event):
Expand Down Expand Up @@ -817,12 +834,11 @@ def _completion(self, action, modif, pos, prev_txt):
if self.mode is not "opendir":
files.sort(key=lambda n: n.lower())
extension = self.filetypes[self.filetype.get()]
if extension == [""]:
if extension == r".*$":
l2.extend([i.replace(" ", "\ ") for i in files if i[:len(f)] == f])
else:
for i in files:
ext = splitext(i)[-1]
if ext in extension and i[:len(f)] == f:
if search(extension, i) and i[:len(f)] == f:
l2.append(i.replace(" ", "\ "))
l2.extend([i.replace(" ", "\ ") + "/" for i in dirs if i[:len(f)] == f])

Expand Down Expand Up @@ -979,7 +995,7 @@ def _display_folder_walk(self, folder, reset=True, update_bar=True):
# display files
files.sort(key=lambda n: n.lower())
extension = self.filetypes[self.filetype.get()]
if extension == [""]:
if extension == ".*":
for f in files:
p = join(root, f)
if islink(p):
Expand Down Expand Up @@ -1009,8 +1025,7 @@ def _display_folder_walk(self, folder, reset=True, update_bar=True):
display_modification_date(stats.st_mtime)))
else:
for f in files:
ext = splitext(f)[-1]
if ext in extension:
if extension == r".*$" or search(extension, f):
p = join(root, f)
if islink(p):
tags = ("file_link",)
Expand Down Expand Up @@ -1108,7 +1123,7 @@ def _display_folder_scandir(self, folder, reset=True, update_bar=True):
i += 1
stats = f.stat()
if b_file:
if (extension == [""] or splitext(name)[-1] in extension):
if extension == r".*$" or search(extension, name):
self.right_tree.insert("", "end", f.path, text=name, tags=tags,
values=("",
display_size(stats.st_size),
Expand Down
9 changes: 2 additions & 7 deletions tkfilebrowser/functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
tkfilebrowser - Alternative to filedialog for Tkinter
Copyright 2017 Juliette Monsel <[email protected]>
Copyright 2017-2018 Juliette Monsel <[email protected]>
tkfilebrowser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -17,11 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
The icons are modified versions of icons from the elementary project
(the xfce fork to be precise https://github.com/shimmerproject/elementary-xfce)
Copyright 2007-2013 elementary LLC.
Functions
"""

Expand Down Expand Up @@ -127,7 +122,7 @@ def asksaveasfilename(parent=None, title=_("Save As"), **kwargs):
Options:
* initialdir: initial folder whose content is displayed
* initialfile: initial selected item (just the name, not the full path)
* defaultext (save mode only): extension added to filename if none is given
* defaultext: extension added to filename if none is given
* filetypes: [('name', '*.ext1|*.ext2|..'), ...]
show only files of given filetype ("*" for all files)
* okbuttontext: text displayed on the validate button, if None, the
Expand Down

0 comments on commit 9df8828

Please sign in to comment.