Skip to content

Commit

Permalink
This is a major overhaul to the save function, how new experiments sa…
Browse files Browse the repository at this point in the history
…ve, and overall QOL improvements
  • Loading branch information
moshernoah committed Feb 3, 2025
1 parent 3436f48 commit d6882a4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
3 changes: 1 addition & 2 deletions experiment_pages/create_experiment/new_experiment_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def __init__(self, parent: CTk, menu_page: CTkFrame = None):
self.species = CTkEntry(self.main_frame, width=140)

self.measure_items = CTkEntry(self.main_frame, width=140)
self.measure_items.insert(0, 'Weight')
self.measure_items.configure(state='disabled')
self.measure_items.configure(state='normal')

self.animal_num = CTkEntry(self.main_frame, width=110)
self.group_num = CTkEntry(self.main_frame, width=110)
Expand Down
22 changes: 17 additions & 5 deletions experiment_pages/create_experiment/summary_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create_experiment(self):
self.experiment.save_to_database(directory)
if self.experiment.get_password():
password = self.experiment.get_password()
file = directory + '/' + self.experiment.get_name() + '.pmouser'
file = os.path.normpath(os.path.join(directory, self.experiment.get_name() + '.pmouser'))
manager = PasswordManager(password)
decrypted_data = manager.decrypt_file(file)
temp_folder_name = "Mouser"
Expand All @@ -40,14 +40,26 @@ def create_experiment(self):
with open(temp_file_path, "wb") as temp_file:
temp_file.write(decrypted_data)
temp_file.seek(0)
root= self.winfo_toplevel() #pylint: disable= redefined-outer-name
root = self.winfo_toplevel() #pylint: disable= redefined-outer-name
# Set the global variables for file paths
import sys
module = sys.modules['__main__']
setattr(module, 'CURRENT_FILE_PATH', file)
setattr(module, 'TEMP_FILE_PATH', temp_file.name)
page = ExperimentMenuUI(root, temp_file.name)
raise_frame(page)

else:
file = directory + '/' + self.experiment.get_name() + '.mouser'
root= self.winfo_toplevel()
file = os.path.normpath(os.path.join(directory, self.experiment.get_name() + '.mouser'))
print("FULL FILE NAME + DIRECTORY: " + file)
root = self.winfo_toplevel()
# Set the global variables for file paths
import sys
module = sys.modules['__main__']
setattr(module, 'CURRENT_FILE_PATH', file)
setattr(module, 'TEMP_FILE_PATH', file)
page = ExperimentMenuUI(root, file)
raise_frame(page) #pylint: disable=undefined-variable
raise_frame(page) #pylint: disable=undefined-variable

class SummaryUI(MouserPage):# pylint: disable=undefined-variable
'''Summary User Interface.'''
Expand Down
3 changes: 2 additions & 1 deletion experiment_pages/experiment/experiment_menu_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ExperimentMenuUI(MouserPage): #pylint: disable= undefined-variable
def __init__(self, parent: CTk, name: str, prev_page: ChangeableFrame = None, controller: SerialPortController = None): #pylint: disable= undefined-variable

#Get name of file from file path
self.file_path = name # Store the full file path
experiment_name = os.path.basename(name)
experiment_name = os.path.splitext(experiment_name)[0]
self.experiment = ExperimentDatabase(name)
Expand Down Expand Up @@ -99,7 +100,7 @@ def disconnect_database(self):
self.data_page.close_connection()
self.cage_page.close_connection()
self.rfid_page.close_connection()

def delete_experiment(self, page: CTkFrame, name: str):
'''Delete Experiment.'''

Expand Down
17 changes: 17 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ def open_serial_port_setting():
SerialPortSetting("serial_port_preference.csv", rfid_serial_port_controller) # pylint: disable=unused-variable
def save_file():
'''Command for the 'save file' option in menu bar.'''
global CURRENT_FILE_PATH, TEMP_FILE_PATH

if CURRENT_FILE_PATH is None:
# If no file path is set (new experiment), prompt for save location
file_path = asksaveasfilename(
defaultextension=".mouser",
filetypes=[("Database files", ".mouser .pmouser")]
)
if file_path: # Only proceed if user didn't cancel
CURRENT_FILE_PATH = file_path
else:
return

# If TEMP_FILE_PATH is None, use CURRENT_FILE_PATH
if TEMP_FILE_PATH is None:
TEMP_FILE_PATH = CURRENT_FILE_PATH

print("Current", CURRENT_FILE_PATH)
print("Temp", TEMP_FILE_PATH)

Expand Down
Binary file modified requirements.txt
Binary file not shown.
22 changes: 21 additions & 1 deletion shared/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tempfile
import os
from shared.password_utils import PasswordManager
from datetime import datetime


TEMP_FOLDER_NAME = "Mouser"
Expand Down Expand Up @@ -47,11 +48,22 @@ def create_temp_from_encrypted(filepath:str, password:str):


def save_temp_to_file(temp_file_path: str, permanent_file_path: str):
'''Save data from temporary file to a permanent file'''
'''
Save data from temporary file to a permanent file.
Automatically appends a timestamp to the filename before the extension.
'''
# Ensure paths are absolute and properly resolved
temp_file_path = os.path.abspath(temp_file_path)
permanent_file_path = os.path.abspath(permanent_file_path)

# Split the path into base and extension
base, ext = os.path.splitext(permanent_file_path)
# Take only the part before the first underscore if it exists
base = base.split('_')[0]
# Add timestamp before the extension
timestamp_str = datetime.now().strftime("_%Y%m%d_%H%M%S")
permanent_file_path = f"{base}{timestamp_str}{ext}"

with open(temp_file_path, 'rb') as temp_file:
data = temp_file.read()

Expand All @@ -64,6 +76,14 @@ def save_temp_to_encrypted(temp_file_path: str, permanent_file_path: str, passwo
temp_file_path = os.path.abspath(temp_file_path)
permanent_file_path = os.path.abspath(permanent_file_path)

# Split the path into base and extension
base, ext = os.path.splitext(permanent_file_path)
# Take only the part before the first underscore if it exists
base = base.split('_')[0]
# Add timestamp before the extension
timestamp_str = datetime.now().strftime("_%Y%m%d_%H%M%S")
permanent_file_path = f"{base}{timestamp_str}{ext}"

manager = PasswordManager(password)

manager.encrypt_file(temp_file_path)
Expand Down

0 comments on commit d6882a4

Please sign in to comment.