1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
# -*- coding: utf-8 -*-
"""
tkfilebrowser - Alternative to filedialog for Tkinter
Copyright 2017-2018 Juliette Monsel <j_4321@protonmail.com>
tkfilebrowser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
tkfilebrowser is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
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.
Constants and functions
"""
# Ensure babel is installed so that it can be imported
import sirilpy
sirilpy.ensure_installed("babel")
import locale
from babel.numbers import format_number
from babel.dates import format_date, format_datetime
from datetime import datetime
import os
from math import log, floor
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesnocancel, showerror
from urllib.parse import unquote
PATH = os.path.dirname(__file__)
LOCAL_PATH = os.path.join(os.path.expanduser('~'), '.config', 'tkfilebrowser')
if not os.path.exists(LOCAL_PATH):
try:
if not os.path.exists(os.path.join(os.path.expanduser('~'), '.config')):
os.mkdir(os.path.join(os.path.expanduser('~'), '.config'))
os.mkdir(LOCAL_PATH)
except Exception:
# avoid raising error if the path is not writtable
pass
RECENT_FILES = os.path.join(LOCAL_PATH, 'recent_files')
# --- images
if tk.TkVersion < 8.6:
from PIL.ImageTk import PhotoImage
else:
PhotoImage = tk.PhotoImage
IM_HOME = os.path.join(PATH, "images", "home.png")
IM_DESKTOP = os.path.join(PATH, "images", "desktop.png")
IM_FOLDER = os.path.join(PATH, "images", "folder.png")
IM_FOLDER_LINK = os.path.join(PATH, "images", "folder_link.png")
IM_NEW = os.path.join(PATH, "images", "new_folder.png")
IM_FILE = os.path.join(PATH, "images", "file.png")
IM_FILE_LINK = os.path.join(PATH, "images", "file_link.png")
IM_LINK_BROKEN = os.path.join(PATH, "images", "link_broken.png")
IM_DRIVE = os.path.join(PATH, "images", "drive.png")
IM_RECENT = os.path.join(PATH, "images", "recent.png")
IM_RECENT_24 = os.path.join(PATH, "images", "recent_24.png")
# --- translation
try:
lang_tuple = locale.getlocale()
LANG = lang_tuple[0] if lang_tuple and lang_tuple[0] else 'en'
# Normalize to lowercase and handle cases like 'en_US'
if not (LANG[:2].lower() in ('en', 'fr')):
LANG = 'en'
except Exception:
LANG = 'en'
EN = {}
FR = {"B": "octets", "MB": "Mo", "kB": "ko", "GB": "Go", "TB": "To",
"Name: ": "Nom : ", "Folder: ": "Dossier : ", "Size": "Taille",
"Name": "Nom", "Modified": "Modifié", "Save": "Enregistrer",
"Open": "Ouvrir", "Cancel": "Annuler", "Location": "Emplacement",
"Today": "Aujourd'hui", "Confirmation": "Confirmation",
"Error": "Erreur",
"The file {file} already exists, do you want to replace it?": "Le fichier {file} existe déjà, voulez-vous le remplacer ?",
"Shortcuts": "Raccourcis", "Save As": "Enregistrer sous",
"Recent": "Récents", "Recently used": "Récemment utilisés"}
LANGUAGES = {"fr": FR, "en": EN}
if LANG[:2] == "fr":
TR = LANGUAGES["fr"]
else:
TR = LANGUAGES["en"]
def _(text):
""" translation function """
return TR.get(text, text)
fromtimestamp = datetime.fromtimestamp
def locale_date(date=None):
return format_date(date, 'short', locale=LANG)
def locale_datetime(date=None):
return format_datetime(date, 'EEEE HH:mm', locale=LANG)
def locale_number(nb):
return format_number(nb, locale=LANG)
SIZES = [_("B"), _("kB"), _("MB"), _("GB"), _("TB")]
# --- locale settings for dates
TODAY = locale_date()
YEAR = datetime.now().year
DAY = int(format_date(None, 'D', locale=LANG))
# --- functions
def add_trace(variable, mode, callback):
"""
Add trace to variable.
Ensure compatibility with old and new trace method.
mode: "read", "write", "unset" (new syntax)
"""
try:
return variable.trace_add(mode, callback)
except AttributeError:
# fallback to old method
return variable.trace(mode[0], callback)
def remove_trace(variable, mode, cbname):
"""
Remove trace from variable.
Ensure compatibility with old and new trace method.
mode: "read", "write", "unset" (new syntax)
"""
try:
variable.trace_remove(mode, cbname)
except AttributeError:
# fallback to old method
variable.trace_vdelete(mode[0], cbname)
def get_modification_date(file):
"""Return the modification date of file."""
try:
tps = fromtimestamp(os.path.getmtime(file))
except OSError:
tps = TODAY
date = locale_date(tps)
if date == TODAY:
date = _("Today") + tps.strftime(" %H:%M")
elif tps.year == YEAR and (DAY - int(tps.strftime("%j"))) < 7:
date = locale_datetime(tps)
return date
def display_modification_date(mtime):
"""Return the modDification date of file."""
if isinstance(mtime, str):
return mtime
tps = fromtimestamp(mtime)
date = locale_date(tps)
if date == TODAY:
date = _("Today") + tps.strftime(" %H:%M")
elif tps.year == YEAR and (DAY - int(tps.strftime("%j"))) < 7:
date = locale_datetime(tps)
return date
def display_size(size_o):
"""Return the size of file."""
if isinstance(size_o, str):
return size_o
if size_o > 0:
m = int(floor(log(size_o) / log(1024)))
if m < len(SIZES):
unit = SIZES[m]
s = size_o / (1024 ** m)
else:
unit = SIZES[-1]
s = size_o / (1024**(len(SIZES) - 1))
size = "%s %s" % (locale_number("%.1f" % s), unit)
else:
size = "0 " + _("B")
return size
def key_sort_files(file):
return file.is_file(), file.name.lower()
|