add crypt.py and project restructured

This commit is contained in:
Bullet64 2022-01-12 18:13:54 +01:00
parent 9c8429a639
commit 95ef455814
13 changed files with 181 additions and 146 deletions

5
.gitignore vendored
View file

@ -1,7 +1,2 @@
# test
backup-list.json
/.vscode/
/venv/
.pylintrc
/__pycache__
.gitignore

6
requirements.txt Normal file
View file

@ -0,0 +1,6 @@
cffi==1.15.0
cryptography==36.0.1
pycparser==2.21
pyqt5-qt5==5.15.2
pyqt5-sip==12.9.0
pyqt5==5.15.6

Binary file not shown.

Binary file not shown.

129
restic_ui/crypt.py Normal file
View file

@ -0,0 +1,129 @@
from PyQt5.QtCore import QSettings
# ------ import for file check ------ #
from pathlib import Path
# ------ import for crypt ------ #
from cryptography.fernet import Fernet
# set Current working directory(CWD)
# CWD = os.path.dirname(os.path.abspath(__file__))
CWD = str(Path.cwd())
# Get home directory from user
USERHOME = str(Path.home())
# set filename
FILE = 'settings'
# set path for user directory
PATH = (USERHOME + '/' '.restic_ui' + '/' + FILE)
FILE_BACKUP_LIST = 'backup_list.json' # set filename
# set path for user directory
USERPATH = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', '/', FILE_BACKUP_LIST))
settings = QSettings(PATH, QSettings.IniFormat)
#####################
# Class Crypt
#####################
class Crypt():
"Class to crypt/encrypt settings file"
def __init__(self):
pass
def write_key(path):
"""
Generates a key and save it into a file
"""
# crypt_value = settings.value("Crypt", True)
# set path for user directory
USERPATH = ('%s%s%s' % (path, '/', 'crypt.key'))
# create backup_list.json when not exists
if Path(USERPATH).exists():
msg_box_error(mainWin.w2, "Key file exists!?")
return False
else:
key = Fernet.generate_key()
with open(USERPATH, "wb") as key_file:
key_file.write(key)
def load_key():
"""
Loads the key from the current directory named `key.key`
"""
crypt_key_path = settings.value("Key_Path", True)
CRYPT_KEY_PATH2 = ('%s%s%s' % (crypt_key_path, '/', 'crypt.key'))
return open(CRYPT_KEY_PATH2, "rb").read()
# File
def encrypt(filename, key):
"""
Given a filename (str) and key (bytes), it encrypts the file and write it
"""
f = Fernet(key)
# check for 'backup_list.enc'
CRYPT_FILE = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.enc'))
if not Path(CRYPT_FILE).exists():
with open(USERPATH, "rb") as file:
# read all file data
file_data = file.read()
# encrypt data
encrypted_data = f.encrypt(file_data)
# write the encrypted settings file
CRYPT_PATH = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.enc'))
with open(CRYPT_PATH, "wb") as file:
file.write(encrypted_data)
# delete 'backup_list.json'!
Path2 = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.json'))
rem_file = Path(Path2)
rem_file.unlink()
else:
print("Error")
def decrypt(filename, key):
"""
Given a filename (str) and key (bytes), it decrypts the file and write it
"""
f = Fernet(key)
# check for 'backup_list.enc'
CRYPT_FILE = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.enc'))
if Path(CRYPT_FILE).exists():
with open(filename, "rb") as file:
# read the encrypted data
encrypted_data = file.read()
# decrypt data
decrypted_data = f.decrypt(encrypted_data)
# write the original file
ENCRYPT_PATH = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.json'))
with open(ENCRYPT_PATH, "wb") as file:
file.write(decrypted_data)
# delete 'backup_list.enc'
rem_file = Path(CRYPT_FILE)
rem_file.unlink()
else:
print("ERROR in decrypt")

View file

@ -106,6 +106,8 @@ from functions import (
from waitingspinnerwidget import QtWaitingSpinner
from crypt import Crypt
print(sys.version)
print(sys.path)
@ -148,6 +150,7 @@ except OSError as e:
CWD = str(Path.cwd())
USERHOME = str(Path.home()) # Get home directory from user
FILE_BACKUP_LIST = 'backup_list.json' # set filename
# Create ~/.restic_ui
@ -158,112 +161,6 @@ path_restic_ui.mkdir(parents=True, exist_ok=True)
# set path for user directory
USERPATH = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', '/', FILE_BACKUP_LIST))
#####################
# Class Crypt
#####################
class Crypt():
"Class to crypt/encrypt settings file"
def __init__(self):
pass
def write_key(path):
"""
Generates a key and save it into a file
"""
# crypt_value = settings.value("Crypt", True)
# set path for user directory
USERPATH = ('%s%s%s' % (path, '/', 'crypt.key'))
# create backup_list.json when not exists
if Path(USERPATH).exists():
msg_box_error(mainWin.w2, "Key file exists!?")
return False
else:
key = Fernet.generate_key()
with open(USERPATH, "wb") as key_file:
key_file.write(key)
def load_key():
"""
Loads the key from the current directory named `key.key`
"""
crypt_key_path = settings.value("Key_Path", True)
CRYPT_KEY_PATH2 = ('%s%s%s' % (crypt_key_path, '/', 'crypt.key'))
return open(CRYPT_KEY_PATH2, "rb").read()
# File
def encrypt(filename, key):
"""
Given a filename (str) and key (bytes), it encrypts the file and write it
"""
f = Fernet(key)
# check for 'backup_list.enc'
CRYPT_FILE = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.enc'))
if not Path(CRYPT_FILE).exists():
with open(USERPATH, "rb") as file:
# read all file data
file_data = file.read()
# encrypt data
encrypted_data = f.encrypt(file_data)
# write the encrypted settings file
CRYPT_PATH = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.enc'))
with open(CRYPT_PATH, "wb") as file:
file.write(encrypted_data)
# delete 'backup_list.json'!
Path2 = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.json'))
rem_file = Path(Path2)
rem_file.unlink()
else:
print("Error")
def decrypt(filename, key):
"""
Given a filename (str) and key (bytes), it decrypts the file and write it
"""
f = Fernet(key)
# check for 'backup_list.enc'
CRYPT_FILE = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.enc'))
if Path(CRYPT_FILE).exists():
with open(filename, "rb") as file:
# read the encrypted data
encrypted_data = file.read()
# decrypt data
decrypted_data = f.decrypt(encrypted_data)
# write the original file
ENCRYPT_PATH = ('%s%s%s%s%s' % (USERHOME, '/', '.restic_ui', "/", 'backup_list.json'))
with open(ENCRYPT_PATH, "wb") as file:
file.write(decrypted_data)
# delete 'backup_list.enc'
rem_file = Path(CRYPT_FILE)
rem_file.unlink()
else:
print("ERROR in decrypt")
# Get home directory from user
USERHOME = str(Path.home())
# set filename
FILE = 'settings'
@ -272,7 +169,6 @@ PATH = (USERHOME + '/' '.restic_ui' + '/' + FILE)
settings = QSettings(PATH, QSettings.IniFormat)
#####################
# Class Spinner
#####################
@ -2195,46 +2091,46 @@ class AddBackupWindow(QWidget):
# Add Backup
self.button2 = QPushButton("", self)
self.button2.setIcon(QIcon.fromTheme(
'document-save', QIcon(CWD + "/icons/document-save")))
'document-save', QIcon(CWD + "./icons/document-save")))
self.button2.clicked.connect(self.close)
self.button2.clicked.connect(add_backup)
# Get Repository Path
self.button3 = QPushButton("", self)
self.button3.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button3.clicked.connect(get_repository_path)
# Get Source Path
self.button4 = QPushButton("", self)
self.button4.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button4.clicked.connect(get_source_path)
# Exclude List
self.button5 = QPushButton("", self)
self.button5.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button5.clicked.connect(get_exclude_list)
# Quit Button Backup Tab
self.actionquit = QPushButton("", self)
self.actionquit.setIcon(QIcon.fromTheme(
'exit', QIcon(CWD + "/icons/exit")))
'exit', QIcon(CWD + "./icons/exit")))
self.actionquit.setToolTip("Quit without saving")
self.actionquit.clicked.connect(close)
# Save Backup
self.button6 = QPushButton("", self)
self.button6.setIcon(QIcon.fromTheme(
'document-save', QIcon(CWD + "/icons/document-save")))
'document-save', QIcon(CWD + "./icons/document-save")))
self.button6.clicked.connect(self.close)
self.button6.clicked.connect(add_rest_backup)
# Quit Button REST Tab
self.actionquit2 = QPushButton("", self)
self.actionquit2.setIcon(QIcon.fromTheme(
'exit', QIcon(CWD + "/icons/exit")))
'exit', QIcon(CWD + "./icons/exit")))
self.actionquit2.setToolTip("Quit without saving")
self.actionquit2.clicked.connect(close)
@ -2599,7 +2495,7 @@ class EditBackupWindow(QWidget):
# Save (Backup Tab)
self.button1 = QPushButton("", self)
self.button1.setIcon(QIcon.fromTheme(
'document-save', QIcon(CWD + "/icons/document-save")))
'document-save', QIcon(CWD + "./icons/document-save")))
self.button1.setToolTip("Save data")
self.button1.clicked.connect(save_backup)
self.button1.clicked.connect(close)
@ -2607,32 +2503,32 @@ class EditBackupWindow(QWidget):
# Get Repository Path
self.button3 = QPushButton("", self)
self.button3.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button3.clicked.connect(get_repository_path)
# Get Source Path
self.button4 = QPushButton("", self)
self.button4.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button4.clicked.connect(get_source_path)
# Exclude List
self.button5 = QPushButton("", self)
self.button5.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button5.clicked.connect(get_exclude_list)
# Quit (Backup Tab)
self.actionquit = QPushButton("", self)
self.actionquit.setIcon(QIcon.fromTheme(
'exit', QIcon(CWD + "/icons/exit")))
'exit', QIcon(CWD + "./icons/exit")))
self.actionquit.setToolTip("Quit without saving")
self.actionquit.clicked.connect(close)
# Save Backup (REST Tab)
self.button6 = QPushButton("", self)
self.button6.setIcon(QIcon.fromTheme(
'document-save', QIcon(CWD + "/icons/document-save")))
'document-save', QIcon(CWD + "./icons/document-save")))
self.button6.setToolTip("Save data")
self.button6.clicked.connect(save_backup)
self.button6.clicked.connect(close)
@ -2640,7 +2536,7 @@ class EditBackupWindow(QWidget):
# Quit (REST tab)
self.actionquit2 = QPushButton("", self)
self.actionquit2.setIcon(QIcon.fromTheme(
'exit', QIcon(CWD + "/icons/exit")))
'exit', QIcon(CWD + "./icons/exit")))
self.actionquit2.setToolTip("Quit without saving")
self.actionquit2.clicked.connect(close)
@ -3018,12 +2914,12 @@ class MainWindow(QMainWindow):
self.addToolBar(toolbar)
toolBar_1 = QAction(QIcon.fromTheme("media-playback-start", QIcon(
CWD + "/icons/media-playback-start")), "Start BACKUP", self)
CWD + "./icons/media-playback-start")), "Start BACKUP", self)
toolBar_1.triggered.connect(self.restic_backup)
toolbar.addAction(toolBar_1)
toolBar_2 = QAction(QIcon.fromTheme('reload', QIcon(
CWD + "/icons/reload")), "Reload JSON", self)
CWD + "./icons/reload")), "Reload JSON", self)
toolBar_2.triggered.connect(BackupList.load_json)
toolbar.addAction(toolBar_2)
@ -3040,32 +2936,32 @@ class MainWindow(QMainWindow):
# ----Help Menu ---- #
aboutButton = QAction(QIcon.fromTheme(
'help-info', QIcon(CWD + "/icons/help-info")), 'About Restic UI', self)
'help-info', QIcon(CWD + "./icons/help-info")), 'About Restic UI', self)
aboutButton.setShortcut('Ctrl+A')
aboutButton.triggered.connect(self.button_about)
helpMenu.addAction(aboutButton)
# ----JSON Menu ---- #
loadJSON = QAction(QIcon.fromTheme('reload', QIcon(
CWD + "/icons/reload")), 'Load File', self)
CWD + "./icons/reload")), 'Load File', self)
loadJSON.triggered.connect(BackupList.load_json)
loadJSON.setShortcut('Ctrl+L')
fileMenu.addAction(loadJSON)
addJSON = QAction(QIcon.fromTheme('add', QIcon(
CWD + "/icons/add")), 'Add Backup', self)
CWD + "./icons/add")), 'Add Backup', self)
addJSON.triggered.connect(self.show_new_window)
addJSON.setShortcut('Ctrl+A')
fileMenu.addAction(addJSON)
self.editJSON = QAction(QIcon.fromTheme('edit', QIcon(
CWD + "/icons/edit")), 'Edit Backup', self)
CWD + "./icons/edit")), 'Edit Backup', self)
self.editJSON.triggered.connect(self.fill_triggered)
self.editJSON.setShortcut('Ctrl+E')
fileMenu.addAction(self.editJSON)
delJSON = QAction(QIcon.fromTheme(
'edit-delete', QIcon(CWD + "/icons/delete")), 'Delete Backup', self)
'edit-delete', QIcon(CWD + "./icons/delete")), 'Delete Backup', self)
delJSON.triggered.connect(self.del_entry)
delJSON.setShortcut('Ctrl+D')
fileMenu.addAction(delJSON)
@ -3073,7 +2969,8 @@ class MainWindow(QMainWindow):
fileMenu.addSeparator()
exit = QAction(QIcon.fromTheme('exit', QIcon(
CWD + "/icons/exit.svg")), 'Exit', self)
CWD + "./icons/exit.svg")), 'Exit', self)
#exit = QAction(QIcon("./icons/exit.svg"), "Exit", self)
exit.triggered.connect(self.close_event)
exit.setShortcut('Ctrl+X')
fileMenu.addAction(exit)
@ -3148,7 +3045,7 @@ class MainWindow(QMainWindow):
toolsMenu.addAction(restic_version)
self.settings = QAction(QIcon.fromTheme('settings', QIcon(
CWD + "/icons/settings")), 'Settings', self)
CWD + "./icons/settings")), 'Settings', self)
self.settings.setShortcut('Ctrl+S')
# self.settings.triggered.connect(self.fill_triggered_settings)
self.settings.triggered.connect(self.show_settings_window)

View file

@ -28,8 +28,8 @@ from functions import (
get_path,
get_file_path)
from restic_ui import Crypt
#from restic_ui import Crypt
from crypt import Crypt
# set Current working directory(CWD)
# CWD = os.path.dirname(os.path.abspath(__file__))
@ -270,55 +270,55 @@ class SettingsWindow(QWidget):
self.button2 = QPushButton("", self)
self.button2.setIcon(QIcon.fromTheme(
'document-save', QIcon(CWD + "/icons/save")))
'document-save', QIcon(CWD + "./icons/save")))
self.button2.clicked.connect(set_settings)
# Get Home Path
self.button3 = QPushButton("", self)
self.button3.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button3.clicked.connect(get_home_path)
# Get Source Path
self.button4 = QPushButton("", self)
self.button4.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button4.clicked.connect(get_source_path)
# Get Exclude List
self.button5 = QPushButton("", self)
self.button5.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.button5.clicked.connect(get_exclude_list)
# Clear
self.button6 = QPushButton("", self)
self.button6.setIcon(QIcon.fromTheme(
'edit-clear', QIcon(CWD + "/icons/edit-clear")))
'edit-clear', QIcon(CWD + "./icons/edit-clear")))
self.button6.clicked.connect(del_source)
# Clear
self.button7 = QPushButton("", self)
self.button7.setIcon(QIcon.fromTheme(
'edit-clear', QIcon(CWD + "/icons/edit-clear")))
'edit-clear', QIcon(CWD + "./icons/edit-clear")))
self.button7.clicked.connect(del_home)
# Clear
self.button8 = QPushButton("", self)
self.button8.setIcon(QIcon.fromTheme(
'edit-clear', QIcon(CWD + "/icons/edit-clear")))
'edit-clear', QIcon(CWD + "./icons/edit-clear")))
self.button8.clicked.connect(del_exclude)
# Get Home Path
self.get_key_path = QPushButton("", self)
self.get_key_path.setIcon(QIcon.fromTheme(
'folder-open', QIcon(CWD + "/icons/folder-open")))
'folder-open', QIcon(CWD + "./icons/folder-open")))
self.get_key_path.clicked.connect(get_key_path)
# Clear
self.button10 = QPushButton("", self)
self.button10.setIcon(QIcon.fromTheme(
'edit-clear', QIcon(CWD + "/icons/edit-clear")))
'edit-clear', QIcon(CWD + "./icons/edit-clear")))
self.button10.clicked.connect(del_key)
#####################

8
setup.py Normal file
View file

@ -0,0 +1,8 @@
import setuptools
with open('requirements.txt', 'r') as f:
install_requires = f.read().splitlines()
setuptools.setup(name='restic_ui',
packages=['restic_ui'],
install_requires=install_requires)