Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper cache and config locations #135

Closed
hmenke opened this issue Feb 25, 2023 · 1 comment · Fixed by #137
Closed

Use proper cache and config locations #135

hmenke opened this issue Feb 25, 2023 · 1 comment · Fixed by #137

Comments

@hmenke
Copy link
Member

hmenke commented Feb 25, 2023

Currently we dump all of the state of the pairinteraction GUI into ~/.pairinteraction. This goes against all desktop specifications on all operating systems and should be fixed. Unfortunately this is platform dependent, so we need to distinguish:

  • Config Directory:

    Linux: $XDG_CONFIG_HOME/pairinteraction or ~/.config/pairinteraction
    Windows: %APPDATA%\pairinteraction
    MacOS: ~/Library/Preferences/pairinteraction

  • Cache directory:

    Linux: $XDG_CACHE_HOME/pairinteraction or ~/.cache/pairinteraction
    Windows: %LOCALAPPDATA%\pairinteraction
    MacOS: ~/Library/Caches/pairinteraction

@hmenke
Copy link
Member Author

hmenke commented Feb 25, 2023

Here is a first approximation, but it has to be tested on all platforms.

diff --git a/pairinteraction_gui/pairinteraction/app.py b/pairinteraction_gui/pairinteraction/app.py
index a7459045..e99b239c 100644
--- a/pairinteraction_gui/pairinteraction/app.py
+++ b/pairinteraction_gui/pairinteraction/app.py
@@ -483,21 +483,34 @@ class MainWindow(QtWidgets.QMainWindow):
         self.path_cpp_complex = os.path.join(self.path_base, self.path_workingdir, "pairinteraction-complex")
         self.path_quantumdefects = os.path.join(self.path_base, self.path_workingdir, "databases/quantum_defects.db")
 
-        if os.name == "nt":
-            self.path_out = os.path.join(self.userpath, "pairinteraction/")
+        if sys.platform == "win32":
+            self.path_config = os.path.expandvars(r'%APPDATA%\pairinteraction')
+            if r'%APPDATA%' in self.path_config
+                self.path_config = os.path.expanduser(r'~\AppData\Local\pairinteraction')
+            self.path_cache = os.path.expandvars(r'%LOCALAPPDATA%\pairinteraction')
+            if r'%LOCALAPPDATA%' in self.path_config
+                self.path_cache = os.path.expanduser(r'~\AppData\Local\pairinteraction')
+        elif sys.platform == "darwin":
+            self.path_config = os.path.expanduser(r'~/Library/Preferences/pairinteraction')
+            self.path_cache = os.path.expanduser(r'~/Library/Caches/pairinteraction')
         else:
-            self.path_out = os.path.join(self.userpath, ".pairinteraction/")
-        self.path_cache = os.path.join(self.path_out, "cache/")
+            self.path_config = os.path.expandvars(r'$XDG_CONFIG_HOME/pairinteraction')
+            if r'$XDG_CONFIG_HOME' in self.path_config:
+                self.path_config = os.path.expanduser(r'~/.config/pairinteraction')
+            self.path_cache = os.path.expandvars(r'$XDG_CACHE_HOME/pairinteraction')
+            if r'$XDG_CACHE_HOME' in self.path_cache:
+                self.path_cache = os.path.expanduser(r'~/.cache/pairinteraction')
+
         self.path_cache_wignerd = os.path.join(self.path_cache, "wignerd/")
-        self.path_lastsettings = os.path.join(self.path_out, "lastsettings/")
+        self.path_lastsettings = os.path.join(self.path_config, "lastsettings/")
 
         self.path_system_last = os.path.join(self.path_lastsettings, "lastsettings.sconf")
         self.path_plot_last = os.path.join(self.path_lastsettings, "lastsettings.pconf")
         self.path_view_last = os.path.join(self.path_lastsettings, "lastview.json")
         self.path_cache_last = os.path.join(self.path_lastsettings, "lastcache.json")
 
-        self.path_config = os.path.join(self.path_out, "conf.json")
-        self.path_version = os.path.join(self.path_out, "version.json")
+        self.path_config = os.path.join(self.path_config, "conf.json")
+        self.path_version = os.path.join(self.path_config, "version.json")
 
         self.proc = None
 
@@ -834,15 +847,8 @@ class MainWindow(QtWidgets.QMainWindow):
                 sys.exit()
 
         # Create directories
-        if not os.path.exists(self.path_out):
-            os.makedirs(self.path_out)
-            if os.name == "nt":
-                import ctypes
-
-                FILE_ATTRIBUTE_HIDDEN = 0x02
-                ret = ctypes.windll.kernel32.SetFileAttributesW(self.path_out, FILE_ATTRIBUTE_HIDDEN)
-                if not ret:
-                    raise ctypes.WinError()
+        os.makedirs(self.path_config, exist_ok=True)
+        os.makedirs(self.path_cache, exist_ok=True)
 
         if not os.path.isfile(self.path_version):
             with open(self.path_version, "w") as f:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant