Skip to content

Commit

Permalink
Migrate from appdirs to platformdirs (#1617)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ganeshrevadi authored Mar 10, 2023
1 parent b01fa10 commit 961e0b5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 27 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ package_dir =
include_package_data = true
python_requires = >=3.7
install_requires =
appdirs
platformdirs >=3.0.0, <4.0.0; sys_platform == 'darwin' # for macOS: breaking changes in 3.0.0,
platformdirs >=2.6.0, <4.0.0; sys_platform != 'darwin' # for others: 2.6+ works consistently.
paramiko
pyqt5
peewee
Expand Down
2 changes: 1 addition & 1 deletion src/vorta/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def exception_handler(type, value, tb):

# Init database
sqlite_db = SqliteDatabase(
os.path.join(SETTINGS_DIR, 'settings.db'),
SETTINGS_DIR / 'settings.db',
pragmas={
'journal_mode': 'wal',
},
Expand Down
9 changes: 5 additions & 4 deletions src/vorta/application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import sys
from pathlib import Path
from typing import Any, Dict, List, Tuple
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMessageBox
Expand All @@ -22,7 +23,7 @@

logger = logging.getLogger(__name__)

APP_ID = os.path.join(TEMP_DIR, "socket")
APP_ID = TEMP_DIR / "socket"


class VortaApp(QtSingleApplication):
Expand All @@ -41,7 +42,7 @@ class VortaApp(QtSingleApplication):
check_failed_event = QtCore.pyqtSignal(dict)

def __init__(self, args_raw, single_app=False):
super().__init__(APP_ID, args_raw)
super().__init__(str(APP_ID), args_raw)
args = parse_args()
if self.isRunning():
if single_app:
Expand Down Expand Up @@ -193,8 +194,8 @@ def check_darwin_permissions(self):
This function tries reading a file that is known to be restricted and warn the user about
incomplete backups.
"""
test_path = os.path.expanduser('~/Library/Cookies')
if os.path.exists(test_path) and not os.access(test_path, os.R_OK):
test_path = Path('~/Library/Cookies').expanduser()
if test_path.exists() and not os.access(test_path, os.R_OK):
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse)
Expand Down
4 changes: 2 additions & 2 deletions src/vorta/autostart.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def open_app_at_startup(enabled=True):

elif sys.platform.startswith('linux'):
from pathlib import Path
from appdirs import user_config_dir
from platformdirs import user_config_path

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

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

if not autostart_path.exists():
autostart_path.mkdir(parents=True, exist_ok=True)
Expand Down
26 changes: 9 additions & 17 deletions src/vorta/config.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import os
from pathlib import Path
import appdirs
import platformdirs

APP_NAME = 'Vorta'
APP_AUTHOR = 'BorgBase'
APP_ID_DARWIN = 'com.borgbase.client.macos'
dirs = appdirs.AppDirs(APP_NAME, APP_AUTHOR)
SETTINGS_DIR = dirs.user_data_dir
LOG_DIR = dirs.user_log_dir
CACHE_DIR = dirs.user_cache_dir
TEMP_DIR = os.path.join(CACHE_DIR, "tmp")
dirs = platformdirs.PlatformDirs(APP_NAME, APP_AUTHOR)
SETTINGS_DIR = dirs.user_data_path
LOG_DIR = dirs.user_log_path
CACHE_DIR = dirs.user_cache_path
TEMP_DIR = CACHE_DIR / "tmp"
PROFILE_BOOTSTRAP_FILE = Path.home() / '.vorta-init.json'

if not os.path.exists(SETTINGS_DIR):
os.makedirs(SETTINGS_DIR)

if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)

if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)

if not os.path.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
# ensure directories exist
for dir in (SETTINGS_DIR, LOG_DIR, CACHE_DIR, TEMP_DIR):
dir.mkdir(parents=True, exist_ok=True)
3 changes: 1 addition & 2 deletions src/vorta/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import logging
import os
from logging.handlers import TimedRotatingFileHandler
from .config import LOG_DIR

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

# create handlers
fh = TimedRotatingFileHandler(os.path.join(LOG_DIR, 'vorta.log'), when='d', interval=1, backupCount=5)
fh = TimedRotatingFileHandler(LOG_DIR / 'vorta.log', when='d', interval=1, backupCount=5)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
Expand Down

0 comments on commit 961e0b5

Please sign in to comment.