Skip to content

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
TechGeek01 committed Dec 11, 2021
1 parent 4f03205 commit 12d009c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.

### Changed
* Converted backup details and overview to tabbed layout, and made window smaller
* Cleaned up code

## 3.2.0 - 2021-11-28
### Added
Expand Down
80 changes: 22 additions & 58 deletions backdrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,11 @@ def update_backup_eta_timer():
else:
print(f"{bcolor.OKGREEN}Backup completed successfully in {str(datetime.now() - backup_start_time).split('.')[0]}{bcolor.ENDC}")

def display_backup_command_info(display_command_list: dict):
def display_backup_command_info(display_command_list: list):
"""Enumerate the display widget with command info after a backup analysis.
Args:
display_command_list (dict): The command list to pull data from.
display_command_list (list): The command list to pull data from.
"""

if not CLI_MODE:
Expand All @@ -358,16 +358,15 @@ def display_backup_command_info(display_command_list: dict):
for i, item in enumerate(display_command_list):
if item['type'] == Backup.COMMAND_TYPE_FILE_LIST:
if item['mode'] == Backup.COMMAND_MODE_DELETE:
cmd_header_text = f"Delete {len(item[Backup.COMMAND_TYPE_FILE_LIST])} files from {item['drive']}"
cmd_header_text = f"Delete {len(item['list'])} files from {item['drive']}"
elif item['mode'] == Backup.COMMAND_MODE_REPLACE:
cmd_header_text = f"Update {len(item[Backup.COMMAND_TYPE_FILE_LIST])} files on {item['drive']}"
cmd_header_text = f"Update {len(item['list'])} files on {item['drive']}"
elif item['mode'] == Backup.COMMAND_MODE_COPY:
cmd_header_text = f"Copy {len(item[Backup.COMMAND_TYPE_FILE_LIST])} new files to {item['drive']}"
cmd_header_text = f"Copy {len(item['list'])} new files to {item['drive']}"
else:
cmd_header_text = f"Work with {len(item[Backup.COMMAND_TYPE_FILE_LIST])} files on {item['drive']}"
cmd_header_text = f"Work with {len(item['list'])} files on {item['drive']}"

if not CLI_MODE:

backup_summary_block = BackupDetailBlock(
parent=content_tab_frame.tab['details']['content'].frame,
title=cmd_header_text,
Expand All @@ -390,7 +389,7 @@ def display_backup_command_info(display_command_list: dict):
trimmed_file_list = trimmed_file_list + '...'

backup_summary_block.add_line('file_size', 'Total size', human_filesize(item['size']))
backup_summary_block.add_copy_line('file_list', 'File list', trimmed_file_list, '\n'.join(item[Backup.COMMAND_TYPE_FILE_LIST]))
backup_summary_block.add_copy_line('file_list', 'File list', trimmed_file_list, '\n'.join(item['list']))
backup_summary_block.add_line('current_file', 'Current file', 'Pending' if item['enabled'] else 'Skipped', fg=root_window.uicolor.PENDING if item['enabled'] else root_window.uicolor.FADED)
backup_summary_block.add_line('progress', 'Progress', 'Pending' if item['enabled'] else 'Skipped', fg=root_window.uicolor.PENDING if item['enabled'] else root_window.uicolor.FADED)

Expand Down Expand Up @@ -442,7 +441,6 @@ def start_backup_analysis():
"""Start the backup analysis in a separate thread."""

global backup
global TREE_SELECTION_LOCKED # URGENT: Move this lock from variable to Backup class var?

# FIXME: If backup @analysis @thread is already running, it needs to be killed before it's rerun
# CAVEAT: This requires some way to have the @analysis @thread itself check for the kill flag and break if it's set.
Expand Down Expand Up @@ -625,7 +623,7 @@ def load_source():
def load_source_in_background():
"""Start a source refresh in a new thread."""

if not TREE_SELECTION_LOCKED and not thread_manager.is_alive('Refresh Source'):
if (not backup or not backup.is_running()) and not thread_manager.is_alive('Refresh Source'):
thread_manager.start(thread_manager.SINGLE, is_progress_thread=True, target=load_source, name='Refresh Source', daemon=True)

def change_source_drive(selection):
Expand All @@ -636,19 +634,15 @@ def change_source_drive(selection):
"""

global PREV_SOURCE_DRIVE
global TREE_SELECTION_LOCKED
global TREE_SELECTION_LOCKED
global config

# If backup is running, ignore request to change
if backup and backup.is_running():
source_select_menu.set_menu(config['source_drive'], *tuple(source_avail_drive_list))
return

# Iff analysis is valid, invalidate it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
# Invalidate analysis validation
reset_analysis_output()

config['source_drive'] = selection
PREV_SOURCE_DRIVE = selection
Expand Down Expand Up @@ -682,7 +676,6 @@ def select_source():

global prev_source_selection
global source_select_bind
global TREE_SELECTION_LOCKED
global backup

def update_share_size(item):
Expand Down Expand Up @@ -766,13 +759,11 @@ def update_share_size(item):

progress.stop_indeterminate()

if not TREE_SELECTION_LOCKED or not backup or not backup.is_running():
if not backup or not backup.is_running():
progress.start_indeterminate()

# If analysis was run, and selection locked, unlock it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
# If analysis was run, invalidate it
reset_analysis_output()

selected = tree_source.selection()

Expand Down Expand Up @@ -983,7 +974,7 @@ def load_dest_in_background():

# URGENT: Make load_dest and load_source replaceable, and in their own class
# URGENT: Invalidate load_source or load_dest if tree gets refreshed via some class def call
if not TREE_SELECTION_LOCKED and not thread_manager.is_alive('Refresh Destination'):
if not (backup and backup.is_running()) and not thread_manager.is_alive('Refresh Destination'):
thread_manager.start(thread_manager.SINGLE, target=load_dest, is_progress_thread=True, name='Refresh Destination', daemon=True)

def gui_select_from_config():
Expand Down Expand Up @@ -1139,15 +1130,12 @@ def select_dest():
global prev_selection
global prev_dest_selection
global dest_select_bind
global TREE_SELECTION_LOCKED

if not TREE_SELECTION_LOCKED or not backup or not backup.is_running():
if not backup or not backup.is_running():
progress.start_indeterminate()

# If analysis was run, and selection locked, unlock it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
# If analysis was run, invalidate it
reset_analysis_output()

dest_selection = tree_dest.selection()

Expand Down Expand Up @@ -1236,8 +1224,6 @@ def select_dest_in_background(event):
def start_backup():
"""Start the backup in a new thread."""

global TREE_SELECTION_LOCKED

if backup and not verification_running:
statusbar_counter_btn.configure(text='0 failed', state='disabled')
statusbar_details.configure(text='')
Expand Down Expand Up @@ -1672,7 +1658,7 @@ def check_for_updates(info: dict):
exit()

# Set meta info
APP_VERSION = '3.2.1-alpha.1'
APP_VERSION = '3.3.0-rc.1'

# Set constants
DRIVE_TYPE_LOCAL = 3
Expand Down Expand Up @@ -2219,8 +2205,6 @@ def update_ui_component(status: int, data=None):
data (*): The data to update (optional).
"""

global TREE_SELECTION_LOCKED

if status == Status.UPDATEUI_ANALYSIS_BTN:
start_analysis_btn.configure(**data)
elif status == Status.UPDATEUI_BACKUP_BTN:
Expand All @@ -2243,10 +2227,6 @@ def update_ui_component(status: int, data=None):
update_status_bar_action(data)
elif status == Status.RESET_ANALYSIS_OUTPUT:
reset_analysis_output()
elif status == Status.UNLOCK_TREE_SELECTION:
TREE_SELECTION_LOCKED = False
elif status == Status.LOCK_TREE_SELECTION:
TREE_SELECTION_LOCKED = True

def open_config_file():
"""Open a config file and load it."""
Expand Down Expand Up @@ -2876,17 +2856,14 @@ def change_source_mode():
global source_right_click_bind
global WINDOW_MIN_WIDTH
global PREV_SOURCE_MODE
global TREE_SELECTION_LOCKED

# If backup is running, ignore request to change
if backup and backup.is_running():
settings_sourceMode.set(PREV_SOURCE_MODE)
return

# If analysis is valid, invalidate it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
reset_analysis_output()

prefs.set('selection', 'source_mode', settings_sourceMode.get())
root_geom = root_window.geometry().split('+')
Expand Down Expand Up @@ -2941,17 +2918,14 @@ def change_dest_mode():

global dest_right_click_bind
global PREV_DEST_MODE
global TREE_SELECTION_LOCKED

# If backup is running, ignore request to change
if backup and backup.is_running():
settings_destMode.set(PREV_DEST_MODE)
return

# If analysis is valid, invalidate it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
reset_analysis_output()

prefs.set('selection', 'dest_mode', settings_destMode.get())

Expand Down Expand Up @@ -2988,7 +2962,6 @@ def change_source_type(toggle_type: int):

global PREV_LOCAL_SOURCE_DRIVE
global PREV_NETWORK_SOURCE_DRIVE
global TREE_SELECTION_LOCKED

# If backup is running, ignore request to change
if backup and backup.is_running():
Expand All @@ -2997,9 +2970,7 @@ def change_source_type(toggle_type: int):
return

# If analysis is valid, invalidate it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
reset_analysis_output()

selected_local = settings_showDrives_source_local.get()
selected_network = settings_showDrives_source_network.get()
Expand Down Expand Up @@ -3028,7 +2999,6 @@ def change_destination_type(toggle_type: int):

global PREV_LOCAL_DEST_DRIVE
global PREV_NETWORK_DEST_DRIVE
global TREE_SELECTION_LOCKED

# If backup is running, ignore request to change
if backup and backup.is_running():
Expand All @@ -3037,9 +3007,7 @@ def change_destination_type(toggle_type: int):
return

# If analysis is valid, invalidate it
if TREE_SELECTION_LOCKED:
TREE_SELECTION_LOCKED = False
reset_analysis_output()
reset_analysis_output()

selected_local = settings_showDrives_dest_local.get()
selected_network = settings_showDrives_dest_network.get()
Expand Down Expand Up @@ -3079,8 +3047,6 @@ def start_verify_data_from_hash_list():
FileUtils.LIST_FAIL: []
}

TREE_SELECTION_LOCKED = False

if not CLI_MODE:
update_handler = UpdateHandler(
current_version=APP_VERSION,
Expand Down Expand Up @@ -3606,8 +3572,6 @@ def toggle_split_mode(event):

dest_select_bind = tree_dest.bind('<<TreeviewSelect>>', select_dest_in_background)

# backup_middle_control_frame.grid(row=4, column=1, columnspan=2, pady=(0, WINDOW_ELEMENT_PADDING / 2), sticky='ew')

# Add tab frame for main detail views
content_tab_frame = TabbedFrame(root_window.main_frame, tabs={
'summary': 'Backup summary',
Expand Down
12 changes: 6 additions & 6 deletions bin/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,15 @@ def scan_share_source_for_new_files(drive, share, path, exclusions: list, all_sh
'type': Backup.COMMAND_TYPE_FILE_LIST,
'drive': self.DRIVE_VID_INFO[drive]['name'],
'size': sum((size for drive, file, size in delete_items)),
Backup.COMMAND_TYPE_FILE_LIST: file_delete_list,
'list': file_delete_list,
'mode': Backup.COMMAND_MODE_DELETE
})

purge_command_list.append({
'displayIndex': len(display_purge_command_list) + 1,
'type': Backup.COMMAND_TYPE_FILE_LIST,
'drive': self.DRIVE_VID_INFO[drive]['name'],
Backup.COMMAND_TYPE_FILE_LIST: file_delete_list,
'list': file_delete_list,
'payload': delete_items,
'mode': Backup.COMMAND_MODE_DELETE
})
Expand All @@ -805,15 +805,15 @@ def scan_share_source_for_new_files(drive, share, path, exclusions: list, all_sh
'type': Backup.COMMAND_TYPE_FILE_LIST,
'drive': self.DRIVE_VID_INFO[drive]['name'],
'size': sum((source_size for drive, share, file, source_size, dest_size in replace_items)),
Backup.COMMAND_TYPE_FILE_LIST: file_replace_list,
'list': file_replace_list,
'mode': Backup.COMMAND_MODE_REPLACE
})

copy_command_list.append({
'displayIndex': len(display_purge_command_list) + 1,
'type': Backup.COMMAND_TYPE_FILE_LIST,
'drive': self.DRIVE_VID_INFO[drive]['name'],
Backup.COMMAND_TYPE_FILE_LIST: file_replace_list,
'list': file_replace_list,
'payload': replace_items,
'mode': Backup.COMMAND_MODE_REPLACE
})
Expand All @@ -829,15 +829,15 @@ def scan_share_source_for_new_files(drive, share, path, exclusions: list, all_sh
'type': Backup.COMMAND_TYPE_FILE_LIST,
'drive': self.DRIVE_VID_INFO[drive]['name'],
'size': sum((size for drive, share, file, size in new_items)),
Backup.COMMAND_TYPE_FILE_LIST: file_copy_list,
'list': file_copy_list,
'mode': Backup.COMMAND_MODE_COPY
})

copy_command_list.append({
'displayIndex': len(display_purge_command_list) + 1,
'type': Backup.COMMAND_TYPE_FILE_LIST,
'drive': self.DRIVE_VID_INFO[drive]['name'],
Backup.COMMAND_TYPE_FILE_LIST: file_copy_list,
'list': file_copy_list,
'payload': new_items,
'mode': Backup.COMMAND_MODE_COPY
})
Expand Down

0 comments on commit 12d009c

Please sign in to comment.