Skip to content

Commit 961e0b5

Browse files
authored
Migrate from appdirs to platformdirs (#1617)
Fixes #1610. Replace deprecated `appdirs` with fork `platformdirs`. Use the new `*_path` api of set fork. This changes the type of the constants defined in `vorta.config` holding locations to `pathlib.Path`. * setup.cfg : Replace dep `appdirs` with `platformdirs`. * src/vorta/config.py : Migrate. Simplify code for ensuring that the directories exist. * src/vorta/log.py * src/vorta/autostart.py * src/vorta/application.py * src/vorta/__main__.py
1 parent b01fa10 commit 961e0b5

6 files changed

Lines changed: 20 additions & 27 deletions

File tree

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ package_dir =
3737
include_package_data = true
3838
python_requires = >=3.7
3939
install_requires =
40-
appdirs
40+
platformdirs >=3.0.0, <4.0.0; sys_platform == 'darwin' # for macOS: breaking changes in 3.0.0,
41+
platformdirs >=2.6.0, <4.0.0; sys_platform != 'darwin' # for others: 2.6+ works consistently.
4142
paramiko
4243
pyqt5
4344
peewee

src/vorta/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def exception_handler(type, value, tb):
5858

5959
# Init database
6060
sqlite_db = SqliteDatabase(
61-
os.path.join(SETTINGS_DIR, 'settings.db'),
61+
SETTINGS_DIR / 'settings.db',
6262
pragmas={
6363
'journal_mode': 'wal',
6464
},

src/vorta/application.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
import sys
4+
from pathlib import Path
45
from typing import Any, Dict, List, Tuple
56
from PyQt5 import QtCore
67
from PyQt5.QtWidgets import QMessageBox
@@ -22,7 +23,7 @@
2223

2324
logger = logging.getLogger(__name__)
2425

25-
APP_ID = os.path.join(TEMP_DIR, "socket")
26+
APP_ID = TEMP_DIR / "socket"
2627

2728

2829
class VortaApp(QtSingleApplication):
@@ -41,7 +42,7 @@ class VortaApp(QtSingleApplication):
4142
check_failed_event = QtCore.pyqtSignal(dict)
4243

4344
def __init__(self, args_raw, single_app=False):
44-
super().__init__(APP_ID, args_raw)
45+
super().__init__(str(APP_ID), args_raw)
4546
args = parse_args()
4647
if self.isRunning():
4748
if single_app:
@@ -193,8 +194,8 @@ def check_darwin_permissions(self):
193194
This function tries reading a file that is known to be restricted and warn the user about
194195
incomplete backups.
195196
"""
196-
test_path = os.path.expanduser('~/Library/Cookies')
197-
if os.path.exists(test_path) and not os.access(test_path, os.R_OK):
197+
test_path = Path('~/Library/Cookies').expanduser()
198+
if test_path.exists() and not os.access(test_path, os.R_OK):
198199
msg = QMessageBox()
199200
msg.setIcon(QMessageBox.Warning)
200201
msg.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse)

src/vorta/autostart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def open_app_at_startup(enabled=True):
4747

4848
elif sys.platform.startswith('linux'):
4949
from pathlib import Path
50-
from appdirs import user_config_dir
50+
from platformdirs import user_config_path
5151

5252
is_flatpak = Path('/.flatpak-info').exists()
5353

@@ -58,7 +58,7 @@ def open_app_at_startup(enabled=True):
5858
if is_flatpak:
5959
autostart_path = Path.home() / '.config' / 'autostart'
6060
else:
61-
autostart_path = Path(user_config_dir("autostart"))
61+
autostart_path = user_config_path("autostart")
6262

6363
if not autostart_path.exists():
6464
autostart_path.mkdir(parents=True, exist_ok=True)

src/vorta/config.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
import os
21
from pathlib import Path
3-
import appdirs
2+
import platformdirs
43

54
APP_NAME = 'Vorta'
65
APP_AUTHOR = 'BorgBase'
76
APP_ID_DARWIN = 'com.borgbase.client.macos'
8-
dirs = appdirs.AppDirs(APP_NAME, APP_AUTHOR)
9-
SETTINGS_DIR = dirs.user_data_dir
10-
LOG_DIR = dirs.user_log_dir
11-
CACHE_DIR = dirs.user_cache_dir
12-
TEMP_DIR = os.path.join(CACHE_DIR, "tmp")
7+
dirs = platformdirs.PlatformDirs(APP_NAME, APP_AUTHOR)
8+
SETTINGS_DIR = dirs.user_data_path
9+
LOG_DIR = dirs.user_log_path
10+
CACHE_DIR = dirs.user_cache_path
11+
TEMP_DIR = CACHE_DIR / "tmp"
1312
PROFILE_BOOTSTRAP_FILE = Path.home() / '.vorta-init.json'
1413

15-
if not os.path.exists(SETTINGS_DIR):
16-
os.makedirs(SETTINGS_DIR)
1714

18-
if not os.path.exists(LOG_DIR):
19-
os.makedirs(LOG_DIR)
20-
21-
if not os.path.exists(CACHE_DIR):
22-
os.makedirs(CACHE_DIR)
23-
24-
if not os.path.exists(TEMP_DIR):
25-
os.makedirs(TEMP_DIR)
15+
# ensure directories exist
16+
for dir in (SETTINGS_DIR, LOG_DIR, CACHE_DIR, TEMP_DIR):
17+
dir.mkdir(parents=True, exist_ok=True)

src/vorta/log.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88

99
import logging
10-
import os
1110
from logging.handlers import TimedRotatingFileHandler
1211
from .config import LOG_DIR
1312

@@ -23,7 +22,7 @@ def init_logger(background=False):
2322
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
2423

2524
# create handlers
26-
fh = TimedRotatingFileHandler(os.path.join(LOG_DIR, 'vorta.log'), when='d', interval=1, backupCount=5)
25+
fh = TimedRotatingFileHandler(LOG_DIR / 'vorta.log', when='d', interval=1, backupCount=5)
2726
fh.setLevel(logging.DEBUG)
2827
fh.setFormatter(formatter)
2928
logger.addHandler(fh)

0 commit comments

Comments
 (0)