Skip to content

Commit

Permalink
Clean up a bit and use better default locations for data and configur…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
teekuningas committed Apr 8, 2024
1 parent 25233a6 commit 6270c91
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
6 changes: 3 additions & 3 deletions meggie/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
],
"pipelines": [
{"id": "spectrum_sensor",
"name": "Sensor-level continuous data analysis",
"name": "Continuous data analysis",
"include_tabs": ["preprocessing", "spectrum"]},
{"id": "evoked_sensor",
"name": "Sensor-level evoked response analysis",
"name": "Evoked response analysis",
"include_tabs": ["preprocessing", "epochs", "evoked"]},
{"id": "induced_sensor",
"name": "Sensor-level induced response analysis",
"name": "Induced response analysis",
"include_tabs": ["preprocessing", "epochs", "tfr"]}
]
}
35 changes: 23 additions & 12 deletions meggie/mainwindow/preferences.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
"""Contains a class for handling reading and storing of
global preferences such as the previous experiment and
the tab settings. Normally saved to ~/.meggieprefs.
the tab settings.
"""

import os
import configparser
import logging

from meggie.utilities.filemanager import homepath
from meggie.utilities.filemanager import configpath
from meggie.utilities.filemanager import datapath


class PreferencesHandler(object):
"""Class for storing and setting preferences."""

def __init__(self):
self.prefs_path = ""
self.workspace = ""
def __init__(self, prefs_path=""):

if prefs_path:
self.prefs_path = prefs_path
else:
self.prefs_path = os.path.join(configpath(), "preferences.cfg")

self.workspace = datapath()

self.previous_experiment_name = ""
self.auto_load_last_open_experiment = False
self.active_plugins = []
Expand All @@ -24,10 +31,9 @@ def __init__(self):

def read_config(self):
"""Reads the config file from file system"""
filename = os.path.join(homepath(), ".meggieprefs")
config = configparser.RawConfigParser()
if os.path.isfile(filename):
config.read(filename)
if os.path.isfile(self.prefs_path):
config.read(self.prefs_path)
return config

def write_preferences_to_disk(self):
Expand Down Expand Up @@ -66,11 +72,16 @@ def write_preferences_to_disk(self):
"Active plugins: " + str(self.active_plugins)
)

path = self.prefs_path
if not path:
path = os.path.join(homepath(), ".meggieprefs")
try:
os.makedirs(os.path.dirname(self.prefs_path))
except PermissionError:
logging.getLogger("ui_logger").exception(
"Could not save the configuration file."
)
except FileExistsError:
pass

with open(path, "w") as configfile:
with open(self.prefs_path, "w") as configfile:
config.write(configfile)

def read_preferences_from_disk(self):
Expand Down
3 changes: 1 addition & 2 deletions meggie/tests/test_experiment_and_subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def test_experiment_and_subject():

# Create preferences object to store working directory
# Also set prefs_path inside tempdir
prefs = PreferencesHandler()
prefs = PreferencesHandler(prefs_path=os.path.join(dirpath, ".meggieprefs"))
prefs.workspace = dirpath
prefs.prefs_path = os.path.join(dirpath, ".meggieprefs")

# create experiment (creates experiment directory inside working directory,
# also saves prefs (previous_experiment is set) and saves .exp file)
Expand Down
28 changes: 26 additions & 2 deletions meggie/utilities/filemanager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Contains functions for tasks related to file system.
"""

import appdirs
import os
import shutil
import re
Expand Down Expand Up @@ -260,16 +261,39 @@ def homepath():
Path to home directory.
"""
from os.path import expanduser

home = expanduser("~")
home = os.path.expanduser("~")

if not home:
return "."

return home


def configpath():
"""Find a path for configuration files.
Returns
-------
str
Path to config directory
"""
return appdirs.user_config_dir("meggie")


def datapath():
"""Find a path for data files.
Returns
-------
str
Path to data directory
"""
return os.path.join(appdirs.user_data_dir("meggie"), "experiments")


def get_supported_formats():
mne_supported = mne_get_supported()

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ python-json-logger
h5io
mne-qt-browser
pyqt5
appdirs

0 comments on commit 6270c91

Please sign in to comment.