Switch from appdirs to platformdirs

Additionally, this removes our hack to support XDG directories on
anything but UNIXy platforms. It was pretty brittle to begin with and
probably won't work on Windows at all with platformdirs...
This commit is contained in:
Tobias Gruetzmacher 2022-05-30 01:04:10 +02:00
parent 4a597ff48f
commit 62ce301ce9
3 changed files with 14 additions and 48 deletions

View file

@ -3,12 +3,10 @@
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2022 Tobias Gruetzmacher
import argparse
import contextlib
import os
import platform
from pathlib import Path
import appdirs
from platformdirs import PlatformDirs
from . import events, configuration, singleton, director
from . import AppName, __version__
@ -26,7 +24,12 @@ class ArgumentParser(argparse.ArgumentParser):
out.info(self.format_help())
Examples = """\
# Making our config roaming seems sensible
platformdirs = PlatformDirs(appname=AppName, appauthor=False, roaming=True, opinion=True)
user_plugin_path = platformdirs.user_data_path / 'plugins'
ExtraHelp = f"""\
EXAMPLES
List available comics:
dosage -l
@ -38,13 +41,11 @@ directory:
If you already have downloaded several comics and want to get the latest
strips of all of them:
dosage --continue @
User plugin directory: {user_plugin_path}
"""
# Making our config roaming seems sensible
userdirs = appdirs.AppDirs(appname=AppName, appauthor=False, roaming=True)
def setup_options():
"""Construct option parser.
@return: new option parser
@ -52,7 +53,7 @@ def setup_options():
"""
parser = ArgumentParser(
description="A comic downloader and archiver.",
epilog=Examples,
epilog=ExtraHelp,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-v', '--verbose', action='count', default=0,
@ -227,6 +228,7 @@ def vote_comic(scraperobj):
def run(options):
"""Execute comic commands."""
set_output_info(options)
allscrapers.adddir(user_plugin_path)
# ensure only one instance of dosage is running
if not options.allow_multiple:
singleton.SingleInstance()
@ -241,7 +243,6 @@ def run(options):
if not options.comic:
out.warn(u'No comics specified, bailing out!')
return 1
add_user_scrapers()
if options.modulehelp:
return display_help(options)
if options.vote:
@ -249,37 +250,8 @@ def run(options):
return director.getComics(options)
def add_user_scrapers():
"""Add extra comic modules from the user data directory. This uses two
different locations: The "system-native" location and paths matching the
XDG basedir spec. While XDG isn't a thing on macOS and Windows, some users
(and developers) like to use these paths cross-plattform, therefore we
support both."""
dirs = set()
dirs.add(userdirs.user_data_dir)
with xdg_system():
dirs.add(userdirs.user_data_dir)
dirs = (Path(x) / 'plugins' for x in dirs)
for d in dirs:
allscrapers.adddir(d)
@contextlib.contextmanager
def xdg_system():
"""context manager to do something with appdirs while forcing the system to
be "linux2", which implements the XDG base dir spec.
"""
oldsys = appdirs.system
appdirs.system = 'linux2'
try:
yield
finally:
appdirs.system = oldsys
def do_list(column_list=True, verbose=False, listall=False):
"""List available comics."""
add_user_scrapers()
with out.pager():
out.info(u'Available comic scrapers:')
out.info(u'Comics tagged with [{}] require age confirmation'

View file

@ -32,10 +32,10 @@ project_urls =
[options]
packages = find:
install_requires =
appdirs
colorama
imagesize
lxml>=4.0.0
platformdirs
requests>=2.0
cached_property;python_version<'3.8'
importlib_metadata;python_version<'3.8'

View file

@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# Copyright (C) 2019-2020 Tobias Gruetzmacher
# Copyright (C) 2019-2022 Tobias Gruetzmacher
import time
from pathlib import Path
@ -15,12 +15,6 @@ def _nosleep(monkeypatch):
monkeypatch.setattr(time, 'sleep', sleep)
class FakeAppdirs:
@property
def user_data_dir(self):
return str(Path(__file__).parent / 'mocks')
@pytest.fixture()
def _noappdirs(monkeypatch):
monkeypatch.setattr('dosagelib.cmd.userdirs', FakeAppdirs())
monkeypatch.setattr('dosagelib.cmd.user_plugin_path', Path(__file__).parent / 'mocks' / 'plugins')