borg-qt/borg_qt/helper.py

96 lines
2.4 KiB
Python
Raw Permalink Normal View History

2019-02-18 13:00:11 +01:00
import argparse
2019-02-02 14:54:37 +01:00
import os
2019-02-02 11:27:38 +01:00
import math
2019-02-03 21:34:36 +01:00
import shutil
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QMessageBox
2019-02-02 14:54:37 +01:00
from PyQt5.QtGui import QDesktopServices
2019-01-19 16:24:18 +01:00
class BorgException(Exception):
pass
2019-02-16 11:33:48 +01:00
# This was taken from here: https://stackoverflow.com/a/1745965/7723859
def debug_trace():
'''Set a tracepoint in the Python debugger that works with Qt'''
from PyQt5.QtCore import pyqtRemoveInputHook
from pdb import set_trace
pyqtRemoveInputHook()
set_trace()
def show_error(e):
2019-01-27 14:49:31 +01:00
"""Helper function to show an error dialog."""
message = QMessageBox()
message.setIcon(QMessageBox.Warning)
message.setText("Error")
message.setWindowTitle("Borg-Qt Error")
message.setInformativeText(e.args[0])
message.exec_()
2019-02-02 11:27:38 +01:00
# taken from here: https://stackoverflow.com/a/14822210/7723859
def convert_size(size_bytes):
"""A function to display file sizes in human readable form."""
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1000, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
2019-02-02 14:54:37 +01:00
def open_path(target_path):
2019-02-10 17:21:41 +01:00
"""Opens the file manager at the given location.
Args:
target_path (str) The path to open in the file manager."""
2019-02-02 14:54:37 +01:00
if os.path.exists(target_path):
QDesktopServices.openUrl(QUrl.fromLocalFile(
os.path.abspath(target_path)))
2019-02-03 21:34:36 +01:00
def create_path(path):
2019-02-10 17:21:41 +01:00
"""Creates the given path.
Args:
path (str) The path to create."""
2019-02-03 21:34:36 +01:00
if not os.path.exists(path):
os.makedirs(path)
def remove_path(path):
2019-02-10 17:21:41 +01:00
"""Removes the given path recursively.
Args:
path (str) The path to delete."""
2019-02-03 21:34:36 +01:00
if os.path.exists(path):
shutil.rmtree(path)
def check_path(path):
2019-02-10 17:21:41 +01:00
"""Checks if the given path is writeable.
Args:
path (str) The path to check."""
if os.access(path, os.W_OK):
return True
exception = Exception("The selected path isn't writeable!")
show_error(exception)
2019-02-18 13:00:11 +01:00
def get_parser():
""" The argument parser of the command-line version """
parser = argparse.ArgumentParser(
description=('Create a backup in the background.'))
parser.add_argument(
'--background',
'-B',
help='Runs the application without showing the graphical interface.',
action='store_true')
return parser