vorta/src/vorta/utils.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

533 lines
18 KiB
Python
Raw Permalink Normal View History

import argparse
import errno
import fnmatch
import getpass
import math
import os
import re
import socket
import sys
import unicodedata
from datetime import datetime as dt
from functools import reduce
from typing import Any, Callable, Iterable, List, Optional, Tuple, TypeVar
import psutil
from PyQt6 import QtCore
from PyQt6.QtCore import QFileInfo, QThread, pyqtSignal
from PyQt6.QtWidgets import QApplication, QFileDialog, QSystemTrayIcon
from vorta.borg._compatibility import BorgCompatibility
from vorta.log import logger
from vorta.network_status.abc import NetworkStatusMonitor
# Used to store whether a user wanted to override the
# default directory for the --development flag
DEFAULT_DIR_FLAG = object()
METRIC_UNITS = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
NONMETRIC_UNITS = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi']
borg_compat = BorgCompatibility()
_network_status_monitor = None
class FilePathInfoAsync(QThread):
signal = pyqtSignal(str, str, str)
def __init__(self, path, exclude_patterns_str):
self.path = path
QThread.__init__(self)
self.exiting = False
self.exclude_patterns = []
for _line in (exclude_patterns_str or '').splitlines():
line = _line.strip()
if line != '':
self.exclude_patterns.append(line)
def run(self):
# logger.info("running thread to get path=%s...", self.path)
self.size, self.files_count = get_path_datasize(self.path, self.exclude_patterns)
self.signal.emit(self.path, str(self.size), str(self.files_count))
def normalize_path(path):
"""normalize paths for MacOS (but do nothing on other platforms)"""
# HFS+ converts paths to a canonical form, so users shouldn't be required to enter an exact match.
# Windows and Unix filesystems allow different forms, so users always have to enter an exact match.
return unicodedata.normalize('NFD', path) if sys.platform == 'darwin' else path
# prepare patterns as borg does
# see `FnmatchPattern._prepare` at
# https://github.com/borgbackup/borg/blob/master//src/borg/patterns.py
def prepare_pattern(pattern):
"""Prepare and process fnmatch patterns as borg does"""
if pattern.endswith(os.path.sep):
# trailing sep indicates that the contents should be excluded
# but not the directory it self.
pattern = os.path.normpath(pattern).rstrip(os.path.sep)
pattern += os.path.sep + '*' + os.path.sep
else:
pattern = os.path.normpath(pattern) + os.path.sep + '*'
pattern = pattern.lstrip(os.path.sep) # sep at beginning is removed
return re.compile(fnmatch.translate(pattern))
def match(pattern: re.Pattern, path: str):
"""Check whether a path matches the given pattern."""
path = path.lstrip(os.path.sep) + os.path.sep
return pattern.match(path) is not None
def get_directory_size(dir_path, exclude_patterns):
'''Get number of files only and total size in bytes from a path.
Based off https://stackoverflow.com/a/17936789'''
exclude_patterns = [prepare_pattern(p) for p in exclude_patterns]
data_size_filtered = 0
seen = set()
seen_filtered = set()
for dir_path, subdirectories, file_names in os.walk(dir_path, topdown=True):
is_excluded = False
for pattern in exclude_patterns:
if match(pattern, dir_path):
is_excluded = True
break
if is_excluded:
subdirectories.clear() # so that os.walk won't walk them
continue
for file_name in file_names:
file_path = os.path.join(dir_path, file_name)
# Ignore symbolic links, since borg doesn't follow them
if os.path.islink(file_path):
continue
is_excluded = False
for pattern in exclude_patterns:
if match(pattern, file_path):
is_excluded = True
break
try:
stat = os.stat(file_path)
if stat.st_ino not in seen: # Visit each file only once
# this won't add the size of a hardlinked file
seen.add(stat.st_ino)
if not is_excluded:
data_size_filtered += stat.st_size
seen_filtered.add(stat.st_ino)
except (FileNotFoundError, PermissionError):
continue
files_count_filtered = len(seen_filtered)
return data_size_filtered, files_count_filtered
def get_network_status_monitor():
global _network_status_monitor
if _network_status_monitor is None:
_network_status_monitor = NetworkStatusMonitor.get_network_status_monitor()
logger.info(
'Using %s NetworkStatusMonitor implementation.',
_network_status_monitor.__class__.__name__,
)
return _network_status_monitor
def get_path_datasize(path, exclude_patterns):
file_info = QFileInfo(path)
if file_info.isDir():
data_size, files_count = get_directory_size(file_info.absoluteFilePath(), exclude_patterns)
else:
data_size = file_info.size()
files_count = 1
return data_size, files_count
def nested_dict():
"""
Combination of two idioms to quickly build dicts from lists of keys:
- https://stackoverflow.com/a/16724937/3983708
- https://stackoverflow.com/a/14692747/3983708
"""
return dict()
def get_dict_from_list(dataDict, mapList):
return reduce(lambda d, k: d.setdefault(k, {}), mapList, dataDict)
def choose_file_dialog(parent, title, want_folder=True):
2021-01-17 20:58:32 +00:00
dialog = QFileDialog(parent, title, os.path.expanduser('~'))
dialog.setFileMode(QFileDialog.FileMode.Directory if want_folder else QFileDialog.FileMode.ExistingFiles)
dialog.setParent(parent, QtCore.Qt.WindowType.Sheet)
2021-01-17 20:58:32 +00:00
if want_folder:
dialog.setOption(QFileDialog.Option.ShowDirsOnly)
return dialog
2018-11-20 04:19:00 +00:00
def is_ssh_private_key_file(filepath: str) -> bool:
"""Check if the file is a SSH key."""
try:
with open(filepath, 'r') as f:
first_line = f.readline()
pattern = r'^-----BEGIN(\s\w+)? PRIVATE KEY-----'
return re.match(pattern, first_line) is not None
except UnicodeDecodeError:
return False
def get_private_keys() -> List[str]:
2018-10-27 17:24:34 +00:00
"""Find SSH keys in standard folder."""
ssh_folder = os.path.expanduser('~/.ssh')
available_private_keys = []
if os.path.isdir(ssh_folder):
for key in os.listdir(ssh_folder):
key_file = os.path.join(ssh_folder, key)
if not os.path.isfile(key_file):
continue
# ignore config, known_hosts*, *.pub, etc.
if key.endswith('.pub') or key.startswith('known_hosts') or key == 'config':
continue
try:
if is_ssh_private_key_file(key_file):
if os.stat(key_file).st_mode & 0o077 == 0:
available_private_keys.append(key)
else:
logger.warning(f'Permissions for {key_file} are too open.')
else:
logger.debug(f'Not a private SSH key file: {key}')
except PermissionError:
logger.warning(f'Permission error while opening file: {key_file}', exc_info=True)
continue
except OSError as e:
if e.errno == errno.ENXIO:
# when key_file is a (ControlPath) socket
continue
raise
2018-10-27 17:24:34 +00:00
return available_private_keys
def sort_sizes(size_list):
"""Sorts sizes with extensions. Assumes that size is already in largest unit possible"""
final_list = []
for suffix in [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]:
sub_list = [
float(size[: -len(suffix)])
for size in size_list
if size.endswith(suffix) and size[: -len(suffix)][-1].isnumeric()
]
sub_list.sort()
final_list += [(str(size) + suffix) for size in sub_list]
# Skip additional loops
if len(final_list) == len(size_list):
break
return final_list
Number = TypeVar("Number", int, float)
def clamp(n: Number, min_: Number, max_: Number) -> Number:
"""Restrict the number n inside a range"""
return min(max_, max(n, min_))
def find_best_unit_for_sizes(sizes: Iterable[int], metric: bool = True, precision: int = 1) -> int:
"""
Selects the index of the biggest unit (see the lists in the pretty_bytes function) capable of
representing the smallest size in the sizes iterable.
"""
min_size = min((s for s in sizes if isinstance(s, int)), default=None)
return find_best_unit_for_size(min_size, metric=metric, precision=precision)
def find_best_unit_for_size(size: Optional[int], metric: bool = True, precision: int = 1) -> int:
"""
Selects the index of the biggest unit (see the lists in the pretty_bytes function) capable of
representing the passed size.
"""
if not isinstance(size, int) or size == 0: # this will also take care of the None case
return 0
power = 10**3 if metric else 2**10
n = math.floor(math.log(abs(size) * 10**precision, power))
return n
def pretty_bytes(
size: int, metric: bool = True, sign: bool = False, precision: int = 1, fixed_unit: Optional[int] = None
) -> str:
"""
Formats the size with the requested unit and precision. The find_best_size_unit function
can be used to find the correct unit for a list of sizes. If no fixed_unit is passed it will
find the biggest unit to represent the size
"""
if not isinstance(size, int):
return ''
prefix = '+' if sign and size > 0 else ''
power, units = (10**3, METRIC_UNITS) if metric else (2**10, NONMETRIC_UNITS)
if fixed_unit is None:
n = find_best_unit_for_size(size, metric=metric, precision=precision)
else:
n = fixed_unit
n = clamp(n, 0, len(units) - 1)
size /= power**n
try:
unit = units[n]
digits = f'%.{precision}f' % (round(size, precision))
return f'{prefix}{digits} {unit}B'
except KeyError as error:
logger.error(error)
return "NaN"
2018-10-28 04:10:15 +00:00
def get_asset(path):
2018-10-28 04:10:15 +00:00
if getattr(sys, 'frozen', False):
# we are running in a bundle
bundle_dir = os.path.join(sys._MEIPASS, 'assets')
2018-10-28 04:10:15 +00:00
else:
# we are running in a normal Python environment
bundle_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets')
2018-10-28 04:10:15 +00:00
return os.path.join(bundle_dir, path)
2018-10-29 06:59:24 +00:00
def get_sorted_wifis(profile):
"""
Get Wifi networks known to the OS (only current one on macOS) and
merge with networks from other profiles. Update last connected time.
"""
2018-10-29 06:59:24 +00:00
from vorta.store.models import WifiSettingModel
# Pull networks known to OS and all other backup profiles
system_wifis = get_network_status_monitor().get_known_wifis()
from_other_profiles = WifiSettingModel.select().where(WifiSettingModel.profile != profile.id).execute()
for wifi in list(from_other_profiles) + system_wifis:
db_wifi, created = WifiSettingModel.get_or_create(
ssid=wifi.ssid,
profile=profile.id,
defaults={'last_connected': wifi.last_connected, 'allowed': True},
)
# Update last connected time
if not created and db_wifi.last_connected != wifi.last_connected:
db_wifi.last_connected = wifi.last_connected
db_wifi.save()
# Finally return list of networks and settings for that profile
2018-11-22 02:18:03 +00:00
return (
WifiSettingModel.select()
.where(WifiSettingModel.profile == profile.id)
.order_by(-WifiSettingModel.last_connected)
2022-08-15 05:21:14 +00:00
)
def parse_args():
parser = argparse.ArgumentParser(description='Vorta Backup GUI for Borg.')
2019-05-30 18:35:57 +00:00
parser.add_argument('--version', '-V', action='store_true', help="Show version and exit.")
parser.add_argument(
'--daemonize',
'-d',
action='store_true',
help="Fork to background and don't open window on startup.",
)
parser.add_argument(
'--create',
dest='profile',
help='Create a backup in the background using the given profile. '
'Vorta must already be running for this to work.',
)
# the "development" attribute will be None if the flag is not called
# if the flag is called without an extra argument, the "development" attribute
# will be set to the value of DEFAULT_DIR_FLAG.
# if the flag is called with an extra argument, the "development" attribute
# will be set to that argument
parser.add_argument(
'--development',
'-D',
nargs='?',
const=DEFAULT_DIR_FLAG,
metavar="CONFIG_DIRECTORY",
help='Start vorta in a local development environment. '
'All log, config, cache, and temp files will be stored within the project tree. '
'You can follow this flag with an optional path and it will store the files in the provided location.',
)
return parser.parse_known_args()[0]
def slugify(value):
"""
Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts spaces to hyphens. Also strips leading and
trailing whitespace.
Copied from Django.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[-\s]+', '-', value)
def uses_dark_mode():
"""
This function detects if we are running in dark mode (e.g. macOS dark mode).
"""
palette = QApplication.instance().palette()
return palette.windowText().color().lightness() > palette.window().color().lightness()
# patched socket.getfqdn() - see https://bugs.python.org/issue5004
# Reused with permission from https://github.com/borgbackup/borg/blob/master/src/borg/platform/base.py (BSD-3-Clause)
def _getfqdn(name=""):
"""Get fully qualified domain name from name.
An empty argument is interpreted as meaning the local host.
"""
name = name.strip()
if not name or name == "0.0.0.0":
name = socket.gethostname()
try:
addrs = socket.getaddrinfo(name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME)
except OSError:
pass
else:
for addr in addrs:
if addr[3]:
name = addr[3]
break
return name
def format_archive_name(profile, archive_name_tpl):
"""
2021-02-18 01:52:59 +00:00
Generate an archive name. Default set in models.BackupProfileModel
"""
hostname = socket.gethostname()
hostname = hostname.split(".")[0]
available_vars = {
'hostname': hostname,
'fqdn': _getfqdn(hostname),
'profile_id': profile.id,
'profile_slug': profile.slug(),
'now': dt.now(),
'utc_now': dt.utcnow(),
'user': getpass.getuser(),
}
return archive_name_tpl.format(**available_vars)
SHELL_PATTERN_ELEMENT = re.compile(r'([?\[\]*])')
def get_mount_points(repo_url):
mount_points = {}
repo_mounts = []
for proc in psutil.process_iter():
try:
name = proc.name()
if name == 'borg' or name.startswith('python'):
if 'mount' not in proc.cmdline():
continue
if borg_compat.check('V2'):
# command line syntax:
# `borg mount -r <repo> <mountpoint> <path> (-a <archive_pattern>)`
cmd = proc.cmdline()
if repo_url in cmd:
i = cmd.index(repo_url)
if len(cmd) > i + 1:
mount_point = cmd[i + 1]
# Archive mount?
ao = '-a' in cmd
if ao or '--match-archives' in cmd:
i = cmd.index('-a' if ao else '--match-archives')
if len(cmd) >= i + 1 and not SHELL_PATTERN_ELEMENT.search(cmd[i + 1]):
mount_points[mount_point] = cmd[i + 1]
else:
repo_mounts.append(mount_point)
else:
for idx, parameter in enumerate(proc.cmdline()):
if parameter.startswith(repo_url):
# mount from this repo
# The borg mount command specifies that the mount_point
# parameter comes after the archive name
if len(proc.cmdline()) > idx + 1:
mount_point = proc.cmdline()[idx + 1]
# archive or full mount?
if parameter[len(repo_url) :].startswith('::'):
archive_name = parameter[len(repo_url) + 2 :]
mount_points[archive_name] = mount_point
break
else:
# repo mount point
repo_mounts.append(mount_point)
except (psutil.ZombieProcess, psutil.AccessDenied, psutil.NoSuchProcess):
# Getting process details may fail (e.g. zombie process on macOS)
# or because the process is owned by another user.
# Also see https://github.com/giampaolo/psutil/issues/783
continue
return mount_points, repo_mounts
def is_system_tray_available():
app = QApplication.instance()
if app is None:
app = QApplication([])
tray = QSystemTrayIcon()
is_available = tray.isSystemTrayAvailable()
app.quit()
else:
tray = QSystemTrayIcon()
is_available = tray.isSystemTrayAvailable()
return is_available
Improve UX and HIG-conformity. By @real-yfprojects (#1176) * Remove fullstops in the labels of the settings. * src/vorta/store/settings.py (get_misc_setting): Remove fullstops in the `label` fields of each setting. * Fix *Add Profile*-Dialog. * src/vorta/assets/UI/profileadd.ui (label_2): Rename to `profileExplainer`. * src/vorta/assets/UI/profileadd.ui (profileExplainer): Remove unnecessary text and rephrase it in simpler terms. * src/vorta/assets/UI/profileadd.ui (formLayout): Move into frame for better layout in dialog. * src/vorta/assets/UI/profileadd.ui (Dialog): Make dialog modal. * src/vorta/assets/UI/profileadd.ui : Modify spacing a bit and change all widgets to `expanding` mode. * src/vorta/assets/UI/profileadd.ui (Dialog): Set title to `Add Profile`. * src/vorta/assets/UI/profileadd.ui : Make `label_3` a buddy of `profileNameField`. * Add colon after entry label in `Add Profile`-Dialog. * src/vorta/assets/UI/profileadd.ui (label_3): Add colon at the end of label. * Fix capitalization in schedule tab. * src/vorta/assets/UI/scheduletab.ui (pruneCheckBox): Replace `Archive` by `archive`. * Fix tooltips. Ensure sentence capitalization. Rephrase some tooltips that do not mention the action they provide a tip for. (There are still many to go.) Remove fullstop from tooltips that aren't a sentence. Add fullstop to tooltips that are a sentence. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repoadd.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * src/vorta/views/export_window.py * src/vorta/views/import_window.py * src/vorta/views/source_tab.py * Replace `n't` by `not` in GUI strings. * src/vorta/application.py * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/scheduletab.ui * src/vorta/assets/UI/sshadd.ui * src/vorta/notifications.py * src/vorta/views/main_window.py * src/vorta/views/main_window.py * src/vorta/views/repo_tab.py * Shorten unlink button tooltip. * src/vorta/assets/UI/repotab.ui (repoRemoveToolbutton): Shorten tooltip by only keeping the most valuable information needed to understand the feature. * Fix entry being embedded in label text. Sentences should not be constructed from text in several controls. Sentences that run from one control to another will often not make sense when translated into other languages. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/scheduletab.ui * Rewrite tooltips to make them HIG conform. The KDE HIG was used. * src/vorta/assets/UI/exportwindow.ui * src/vorta/assets/UI/importwindow.ui * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repoadd.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * src/vorta/views/export_window.py * src/vorta/views/import_window.py (ImportWindow.init_overwrite_profile_checkbox): Make tooltip static as the HIG suggests. * src/vorta/views/main_window.py * Replace `...` with unicode ellipses `…` (U+2028) in GUI text. * src/vorta/borg/break_lock.py * src/vorta/borg/check.py * src/vorta/borg/create.py * src/vorta/borg/delete.py * src/vorta/borg/diff.py * src/vorta/borg/extract.py * src/vorta/borg/info_archive.py * src/vorta/borg/info_repo.py * src/vorta/borg/init.py * src/vorta/borg/list_archive.py * src/vorta/borg/list_archive.py * src/vorta/borg/list_repo.py * Clean prune tab in `archivetab.ui`. * src/vorta/assets/UI/archivetab.ui * Prettify `repotab`. * src/vorta/assets/UI/repotab.ui * Fix tooltips for pruning in `archivetab.ui`. * src/vorta/assets/UI/archivetab.ui * Use affirmative phrase in checkbox for metered networks. * src/vorta/assets/UI/scheduletab.ui (dontRunOnMeteredNetworksCheckbox): Rename to `meteredNetworksCheckBox`. * src/vorta/assets/UI/scheduletab.ui (meteredNetworksCheckBox): Change text into affirmative phrase. * src/vorta/views/schedule_tab.py : Invert values for `meteredNetworksCheckBox`. * Add label in shell commands pane in `scheduletab` and fix placeholders. Placeholders shouldn't be a replacement for a label. * src/vorta/assets/UI/scheduletab.ui * Group settings widgets in `misctab`. * src/vorta/assets/UI/misctab.ui (checkboxLayout): Replace with frame `frameSettings` containing a form layout. * src/vorta/utils.py (search): Added. Searches for a key inside an iterable applying a given function before comparison. * src/vorta/store/models.py (SettingsModel): Add field `group` to assign settings to a group. * src/vorta/store/migrations.py (run_migrations): Add new schema version `19` and implement migration adding the `group` field. * src/vorta/store/connection.py (SCHEMA_VERSION): Update to `19`. * src/vorta/store/settings.py (get_misc_settings): Add and assign settings groups. * src/vorta/store/connection.py (init_db): Update group and type of settings if needed. * src/vorta/views/misc_tab.py (Imports): Import `search` from `..utils`. * src/vorta/views/misc_tab.py : Instanciate logger. * src/vorta/views/misc_tab.py (MiscTab.__init__): Create a checkboxLayout for `frameSettings`. * src/vorta/views/misc_tab.py (populate): Add settings widgets in groups with labels in spacer in between. * Fix tests for `misctab`. * tests/test_misc.py * Fix margins. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/misctab.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/scheduletab.ui * src/vorta/assets/UI/sourcetab.ui * Morph buttons in `sourcetab` into toolbuttons with icons. Adds gradient buttons along the way. Fixes #933. * src/vorta/assets/UI/sourcetab.ui : Change layout and buttons. Merge buttons with adding capabilities into one toolbutton (with menu). * src/vorta/assets/UI/sourcetab.ui : Rename `sourceRemove` to `removeButton`. Rename `sourceUpdate` to `updateButton`. Add `addButton`. * src/vorta/assets/icons/minus.svg : Add minus sign icon from *fontawesome* v6.0. * src/vorta/views/source_tab.py (SourceTab.set_icons): Added. * src/vorta/views/source_tab.py (SourceTab.__init__): Create Menu for the `addButton` with actions to add files and folders and to paste. * src/vorta/application.py (VortaApp.eventFilter): Call `set_icons` of `sourceTab` as well. * Fix tests for `sourcetab`. * tests/test_source.py * Add paste icon to paste action in `sourcetab`. * src/vorta/assets/icons/paste.svg : Added from fontawesome (paste-solid, v6.0) * src/vorta/views/source_tab.py (SourceTab.__init__): Save paste action in `pasteAction`. * src/vorta/views/source_tab.py (SourceTab.set_icons): Set icon for `pasteAction`. * Add icons to open actions in `sourcetab`. * src/vorta/assets/icons/file.svg : Added `file-solid` from fontawesome, v6.0 * src/vorta/assets/icons/folder.svg : Added `folder-solid` from fontawesome, v6.0 * src/vorta/views/source_tab.py (SourceTab.__init__): Save files and folders action to `addFilesAction` and `addFoldersAction`. * src/vorta/views/source_tab.py (SourceTab.set_icons): Set icons for `addFilesAction` and `addFolderAction`. * Fix icon size of `file.svg`. * src/vorta/assets/icons/file.svg * Set fill of added svgs to `#000000`. * src/vorta/assets/icons/file.svg * src/vorta/assets/icons/folder.svg * src/vorta/assets/icons/minus.svg * src/vorta/assets/icons/paste.svg * Improve UX and consistency within the app for `scheduletab`. * src/vorta/assets/UI/scheduletab.ui : Arrange schedule pane in a form layout. * src/vorta/views/schedule_tab.py (ScheduleTab.__init__): Connect enabled state of entries to radiobuttons and checkboxes. * Workaround scheduletab tests being broken in github actions. For some unknown reason clicking the `scheduleFixedRadio` won't work when running using github action on ubuntu. * tests/test_schedule.py * Fix labels and spacing in `scheduletab`. * src/vorta/assets/UI/scheduletab.ui * Make archive operations more accessible and rename actions of `ArchiveTab`. * src/vorta/views/archive_tab.py (ArchiveTab): Rename `list_action` to `refresh_archive_list`. Rename `refresh_archive_action` to `refresh_archive_info`. Rename `refresh_archive_result` to `info_result`. Rename `list_archive_action` to `extract_action`. Rename `list_archive_result` to `extract_list_result`. * src/vorta/views/main_window.py : Apply renaming. * src/vorta/assets/UI/archivetab.ui : Create own buttons for the archive actions. Remove `archiveActionButton`. And some other layout changes. * src/vorta/assets/UI/archivetab.ui : Rename `pruneButton` to `bPrune`. Rename `checkButton` to `bCheck`. Rename `diffButton` to `bDiff`. Rename `listButton` to `bList`. * tests/test_archives.py : Apply renaming. * src/vorta/views/archive_tab.py : Connect new action buttons. * src/vorta/views/archive_tab.py : Remove `archiveActionButton`. * tests/test_archives.py : Fix tests. * Enable and disable archive actions depending on selection. * src/vorta/assets/UI/archivetab.ui : Put archive actions into a frame. * src/vorta/views/archive_tab.py (ArchiveTab): Added `on_selection_change` that enables/disables the frame depending on the selection count. * src/vorta/views/archive_tab.py (ArchiveTab.populate_from_profile): Clear selection. * src/vorta/views/archive_tab.py (ArchiveTab.__toggle_all_buttons): Add `fArchiveActions`. Call `on_selection_change` at the end. * Fix tests for `archivetab`. * tests/test_archives.py * Replace line by spacer in repotab. * src/vorta/assets/UI/repotab.ui * Show labels for archive action buttons. * src/vorta/assets/UI/archivetab.ui * Add tooltips and ellipses to archivetab. * src/vorta/assets/UI/archivetab.ui * src/vorta/views/archive_tab.py * Fix tooltips. * src/vorta/assets/UI/sourcetab.ui : Add tooltips. * src/vorta/views/archive_tab.py (ArchiveTab.on_selection_change): Add reason for disabled state dynamically to archive action buttons. * Add context menu to source view. * src/vorta/views/source_tab.py (SourceTab): Implement `sourceitem_contextmenu` and `source_copy`. Set context menu policiy of `sourceFilesWidget` to `CustomContextMenu`. * src/vorta/assets/UI/sourcetab.ui: Change size hints. * Add context menu to archive view. * src/vorta/views/archive_tab.py (ArchiveTab): Set context menu policy of `archiveTable` to `CustomContextMenu`. Implement `archiveitem_contextmenu`. * Replace `Type` column in sources view by icon. * src/vorta/views/source_tab.py (SourceColumn): Remove `Type` column. * src/vorta/views/source_tab.py (SourceTab.set_icons): Set icon for each item in source view. * src/vorta/views/source_tab.py (SourceTab.set_path_info): Set icon instead of `Type` column. * src/vorta/assets/UI/sourcetab.ui : Remove `Type` column. * Fix initial sort indicator of source view. * src/vorta/views/source_tab.py * Fix adding items while sorting enabled. * src/vorta/views/source_tab.py * Remove status bar and remove fix size hint for log text label. * src/vorta/assets/UI/mainwindow.ui * src/vorta/views/main_window.py (MainWindow.__init__): Set minimum height of `logText` to two times the height of a line calculated by `QFontMetrics`. * Resize main window height to `670`. * src/vorta/assets/UI/mainwindow.ui * Replace `QToolbutton` by `QPushbutton`. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * src/vorta/views/source_tab.py * Fix flake8 * Improve label of entry for `borg create` extra arguments. * src/vorta/assets/UI/scheduletab.ui * Unify label font size in `repotab`. * src/vorta/assets/UI/repotab.ui * Morph `QPushButton`s into `QToolButton`s. Some exceptions were made, especially in the dialog windows. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * Add copy capabilities to archive view and a copy shortcut to it and to source tab. * src/vorta/views/source_tab.py (SourceTab): Add QShortcut for copying. * src/vorta/views/archive_tab.py (ArchiveTab.archiveitem_contextmenu): Add copy action. * src/vorta/views/archive_tab.py (ArchiveTab): Add QShortcut for copying. * Move actions in comboBoxes to buttons. * src/vorta/assets/UI/repotab.ui : Add `bAddSSHKey` and `bAddRepo`. * src/vorta/views/repo_tab.py : Move code out of `ssh_select_action` and `repo_select_action` into `add_existing_repo`, `new_repo` and `create_ssh_key`. * src/vorta/views/repo_tab.py * Make tooltip of `storePassword` checkbox more fluent. * src/vorta/assets/UI/exportwindow.ui * Introduce `QDialogButtonBox` to modal dialogs in vorta. * src/vorta/assets/UI/extractdialog.ui * src/vorta/views/extract_dialog.py * src/vorta/assets/UI/repoadd.ui * src/vorta/views/repo_add_dialog.py * src/vorta/assets/UI/sshadd.ui * src/vorta/views/ssh_dialog.py * Move some options in scheduletab to the side. This results in two columns of options and fixes vertical scrolling. * src/vorta/assets/UI/scheduletab.ui * Changes for macOS layout * Set `sizeAdjustPolicy` of comboBoxes to `AdustToContents`. * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repotab.ui * Fix some icons, translations strings and link * Lint Co-authored-by: real-yfprojects <real-yfprojects@users.noreply.github.com> Co-authored-by: Manu <3916435+m3nu@users.noreply.github.com> Co-authored-by: Manu <manu@snapdragon.cc>
2022-03-24 06:27:07 +00:00
def search(key, iterable: Iterable, func: Callable = None) -> Tuple[int, Any]:
"""
Search for a key in an iterable.
Before comparing an item with the key `func` is called on the item.
Parameters
----------
key : Any
The key to search for.
iterable : Iterable
The iterable to search in.
func : Callable, optional
The function to apply, by default None
Returns
-------
Tuple[int, Any] or None
The index and the item in case of a match else `None`.
"""
if not func:
2022-08-15 05:21:14 +00:00
Improve UX and HIG-conformity. By @real-yfprojects (#1176) * Remove fullstops in the labels of the settings. * src/vorta/store/settings.py (get_misc_setting): Remove fullstops in the `label` fields of each setting. * Fix *Add Profile*-Dialog. * src/vorta/assets/UI/profileadd.ui (label_2): Rename to `profileExplainer`. * src/vorta/assets/UI/profileadd.ui (profileExplainer): Remove unnecessary text and rephrase it in simpler terms. * src/vorta/assets/UI/profileadd.ui (formLayout): Move into frame for better layout in dialog. * src/vorta/assets/UI/profileadd.ui (Dialog): Make dialog modal. * src/vorta/assets/UI/profileadd.ui : Modify spacing a bit and change all widgets to `expanding` mode. * src/vorta/assets/UI/profileadd.ui (Dialog): Set title to `Add Profile`. * src/vorta/assets/UI/profileadd.ui : Make `label_3` a buddy of `profileNameField`. * Add colon after entry label in `Add Profile`-Dialog. * src/vorta/assets/UI/profileadd.ui (label_3): Add colon at the end of label. * Fix capitalization in schedule tab. * src/vorta/assets/UI/scheduletab.ui (pruneCheckBox): Replace `Archive` by `archive`. * Fix tooltips. Ensure sentence capitalization. Rephrase some tooltips that do not mention the action they provide a tip for. (There are still many to go.) Remove fullstop from tooltips that aren't a sentence. Add fullstop to tooltips that are a sentence. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repoadd.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * src/vorta/views/export_window.py * src/vorta/views/import_window.py * src/vorta/views/source_tab.py * Replace `n't` by `not` in GUI strings. * src/vorta/application.py * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/scheduletab.ui * src/vorta/assets/UI/sshadd.ui * src/vorta/notifications.py * src/vorta/views/main_window.py * src/vorta/views/main_window.py * src/vorta/views/repo_tab.py * Shorten unlink button tooltip. * src/vorta/assets/UI/repotab.ui (repoRemoveToolbutton): Shorten tooltip by only keeping the most valuable information needed to understand the feature. * Fix entry being embedded in label text. Sentences should not be constructed from text in several controls. Sentences that run from one control to another will often not make sense when translated into other languages. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/scheduletab.ui * Rewrite tooltips to make them HIG conform. The KDE HIG was used. * src/vorta/assets/UI/exportwindow.ui * src/vorta/assets/UI/importwindow.ui * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repoadd.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * src/vorta/views/export_window.py * src/vorta/views/import_window.py (ImportWindow.init_overwrite_profile_checkbox): Make tooltip static as the HIG suggests. * src/vorta/views/main_window.py * Replace `...` with unicode ellipses `…` (U+2028) in GUI text. * src/vorta/borg/break_lock.py * src/vorta/borg/check.py * src/vorta/borg/create.py * src/vorta/borg/delete.py * src/vorta/borg/diff.py * src/vorta/borg/extract.py * src/vorta/borg/info_archive.py * src/vorta/borg/info_repo.py * src/vorta/borg/init.py * src/vorta/borg/list_archive.py * src/vorta/borg/list_archive.py * src/vorta/borg/list_repo.py * Clean prune tab in `archivetab.ui`. * src/vorta/assets/UI/archivetab.ui * Prettify `repotab`. * src/vorta/assets/UI/repotab.ui * Fix tooltips for pruning in `archivetab.ui`. * src/vorta/assets/UI/archivetab.ui * Use affirmative phrase in checkbox for metered networks. * src/vorta/assets/UI/scheduletab.ui (dontRunOnMeteredNetworksCheckbox): Rename to `meteredNetworksCheckBox`. * src/vorta/assets/UI/scheduletab.ui (meteredNetworksCheckBox): Change text into affirmative phrase. * src/vorta/views/schedule_tab.py : Invert values for `meteredNetworksCheckBox`. * Add label in shell commands pane in `scheduletab` and fix placeholders. Placeholders shouldn't be a replacement for a label. * src/vorta/assets/UI/scheduletab.ui * Group settings widgets in `misctab`. * src/vorta/assets/UI/misctab.ui (checkboxLayout): Replace with frame `frameSettings` containing a form layout. * src/vorta/utils.py (search): Added. Searches for a key inside an iterable applying a given function before comparison. * src/vorta/store/models.py (SettingsModel): Add field `group` to assign settings to a group. * src/vorta/store/migrations.py (run_migrations): Add new schema version `19` and implement migration adding the `group` field. * src/vorta/store/connection.py (SCHEMA_VERSION): Update to `19`. * src/vorta/store/settings.py (get_misc_settings): Add and assign settings groups. * src/vorta/store/connection.py (init_db): Update group and type of settings if needed. * src/vorta/views/misc_tab.py (Imports): Import `search` from `..utils`. * src/vorta/views/misc_tab.py : Instanciate logger. * src/vorta/views/misc_tab.py (MiscTab.__init__): Create a checkboxLayout for `frameSettings`. * src/vorta/views/misc_tab.py (populate): Add settings widgets in groups with labels in spacer in between. * Fix tests for `misctab`. * tests/test_misc.py * Fix margins. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/misctab.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/scheduletab.ui * src/vorta/assets/UI/sourcetab.ui * Morph buttons in `sourcetab` into toolbuttons with icons. Adds gradient buttons along the way. Fixes #933. * src/vorta/assets/UI/sourcetab.ui : Change layout and buttons. Merge buttons with adding capabilities into one toolbutton (with menu). * src/vorta/assets/UI/sourcetab.ui : Rename `sourceRemove` to `removeButton`. Rename `sourceUpdate` to `updateButton`. Add `addButton`. * src/vorta/assets/icons/minus.svg : Add minus sign icon from *fontawesome* v6.0. * src/vorta/views/source_tab.py (SourceTab.set_icons): Added. * src/vorta/views/source_tab.py (SourceTab.__init__): Create Menu for the `addButton` with actions to add files and folders and to paste. * src/vorta/application.py (VortaApp.eventFilter): Call `set_icons` of `sourceTab` as well. * Fix tests for `sourcetab`. * tests/test_source.py * Add paste icon to paste action in `sourcetab`. * src/vorta/assets/icons/paste.svg : Added from fontawesome (paste-solid, v6.0) * src/vorta/views/source_tab.py (SourceTab.__init__): Save paste action in `pasteAction`. * src/vorta/views/source_tab.py (SourceTab.set_icons): Set icon for `pasteAction`. * Add icons to open actions in `sourcetab`. * src/vorta/assets/icons/file.svg : Added `file-solid` from fontawesome, v6.0 * src/vorta/assets/icons/folder.svg : Added `folder-solid` from fontawesome, v6.0 * src/vorta/views/source_tab.py (SourceTab.__init__): Save files and folders action to `addFilesAction` and `addFoldersAction`. * src/vorta/views/source_tab.py (SourceTab.set_icons): Set icons for `addFilesAction` and `addFolderAction`. * Fix icon size of `file.svg`. * src/vorta/assets/icons/file.svg * Set fill of added svgs to `#000000`. * src/vorta/assets/icons/file.svg * src/vorta/assets/icons/folder.svg * src/vorta/assets/icons/minus.svg * src/vorta/assets/icons/paste.svg * Improve UX and consistency within the app for `scheduletab`. * src/vorta/assets/UI/scheduletab.ui : Arrange schedule pane in a form layout. * src/vorta/views/schedule_tab.py (ScheduleTab.__init__): Connect enabled state of entries to radiobuttons and checkboxes. * Workaround scheduletab tests being broken in github actions. For some unknown reason clicking the `scheduleFixedRadio` won't work when running using github action on ubuntu. * tests/test_schedule.py * Fix labels and spacing in `scheduletab`. * src/vorta/assets/UI/scheduletab.ui * Make archive operations more accessible and rename actions of `ArchiveTab`. * src/vorta/views/archive_tab.py (ArchiveTab): Rename `list_action` to `refresh_archive_list`. Rename `refresh_archive_action` to `refresh_archive_info`. Rename `refresh_archive_result` to `info_result`. Rename `list_archive_action` to `extract_action`. Rename `list_archive_result` to `extract_list_result`. * src/vorta/views/main_window.py : Apply renaming. * src/vorta/assets/UI/archivetab.ui : Create own buttons for the archive actions. Remove `archiveActionButton`. And some other layout changes. * src/vorta/assets/UI/archivetab.ui : Rename `pruneButton` to `bPrune`. Rename `checkButton` to `bCheck`. Rename `diffButton` to `bDiff`. Rename `listButton` to `bList`. * tests/test_archives.py : Apply renaming. * src/vorta/views/archive_tab.py : Connect new action buttons. * src/vorta/views/archive_tab.py : Remove `archiveActionButton`. * tests/test_archives.py : Fix tests. * Enable and disable archive actions depending on selection. * src/vorta/assets/UI/archivetab.ui : Put archive actions into a frame. * src/vorta/views/archive_tab.py (ArchiveTab): Added `on_selection_change` that enables/disables the frame depending on the selection count. * src/vorta/views/archive_tab.py (ArchiveTab.populate_from_profile): Clear selection. * src/vorta/views/archive_tab.py (ArchiveTab.__toggle_all_buttons): Add `fArchiveActions`. Call `on_selection_change` at the end. * Fix tests for `archivetab`. * tests/test_archives.py * Replace line by spacer in repotab. * src/vorta/assets/UI/repotab.ui * Show labels for archive action buttons. * src/vorta/assets/UI/archivetab.ui * Add tooltips and ellipses to archivetab. * src/vorta/assets/UI/archivetab.ui * src/vorta/views/archive_tab.py * Fix tooltips. * src/vorta/assets/UI/sourcetab.ui : Add tooltips. * src/vorta/views/archive_tab.py (ArchiveTab.on_selection_change): Add reason for disabled state dynamically to archive action buttons. * Add context menu to source view. * src/vorta/views/source_tab.py (SourceTab): Implement `sourceitem_contextmenu` and `source_copy`. Set context menu policiy of `sourceFilesWidget` to `CustomContextMenu`. * src/vorta/assets/UI/sourcetab.ui: Change size hints. * Add context menu to archive view. * src/vorta/views/archive_tab.py (ArchiveTab): Set context menu policy of `archiveTable` to `CustomContextMenu`. Implement `archiveitem_contextmenu`. * Replace `Type` column in sources view by icon. * src/vorta/views/source_tab.py (SourceColumn): Remove `Type` column. * src/vorta/views/source_tab.py (SourceTab.set_icons): Set icon for each item in source view. * src/vorta/views/source_tab.py (SourceTab.set_path_info): Set icon instead of `Type` column. * src/vorta/assets/UI/sourcetab.ui : Remove `Type` column. * Fix initial sort indicator of source view. * src/vorta/views/source_tab.py * Fix adding items while sorting enabled. * src/vorta/views/source_tab.py * Remove status bar and remove fix size hint for log text label. * src/vorta/assets/UI/mainwindow.ui * src/vorta/views/main_window.py (MainWindow.__init__): Set minimum height of `logText` to two times the height of a line calculated by `QFontMetrics`. * Resize main window height to `670`. * src/vorta/assets/UI/mainwindow.ui * Replace `QToolbutton` by `QPushbutton`. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * src/vorta/views/source_tab.py * Fix flake8 * Improve label of entry for `borg create` extra arguments. * src/vorta/assets/UI/scheduletab.ui * Unify label font size in `repotab`. * src/vorta/assets/UI/repotab.ui * Morph `QPushButton`s into `QToolButton`s. Some exceptions were made, especially in the dialog windows. * src/vorta/assets/UI/archivetab.ui * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repotab.ui * src/vorta/assets/UI/sourcetab.ui * Add copy capabilities to archive view and a copy shortcut to it and to source tab. * src/vorta/views/source_tab.py (SourceTab): Add QShortcut for copying. * src/vorta/views/archive_tab.py (ArchiveTab.archiveitem_contextmenu): Add copy action. * src/vorta/views/archive_tab.py (ArchiveTab): Add QShortcut for copying. * Move actions in comboBoxes to buttons. * src/vorta/assets/UI/repotab.ui : Add `bAddSSHKey` and `bAddRepo`. * src/vorta/views/repo_tab.py : Move code out of `ssh_select_action` and `repo_select_action` into `add_existing_repo`, `new_repo` and `create_ssh_key`. * src/vorta/views/repo_tab.py * Make tooltip of `storePassword` checkbox more fluent. * src/vorta/assets/UI/exportwindow.ui * Introduce `QDialogButtonBox` to modal dialogs in vorta. * src/vorta/assets/UI/extractdialog.ui * src/vorta/views/extract_dialog.py * src/vorta/assets/UI/repoadd.ui * src/vorta/views/repo_add_dialog.py * src/vorta/assets/UI/sshadd.ui * src/vorta/views/ssh_dialog.py * Move some options in scheduletab to the side. This results in two columns of options and fixes vertical scrolling. * src/vorta/assets/UI/scheduletab.ui * Changes for macOS layout * Set `sizeAdjustPolicy` of comboBoxes to `AdustToContents`. * src/vorta/assets/UI/mainwindow.ui * src/vorta/assets/UI/repotab.ui * Fix some icons, translations strings and link * Lint Co-authored-by: real-yfprojects <real-yfprojects@users.noreply.github.com> Co-authored-by: Manu <3916435+m3nu@users.noreply.github.com> Co-authored-by: Manu <manu@snapdragon.cc>
2022-03-24 06:27:07 +00:00
def func(x):
return x
for i, item in enumerate(iterable):
if func(item) == key:
return i, item
return None