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) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2022 Tobias Gruetzmacher # Copyright (C) 2015-2022 Tobias Gruetzmacher
import argparse import argparse
import contextlib
import os import os
import platform import platform
from pathlib import Path
import appdirs from platformdirs import PlatformDirs
from . import events, configuration, singleton, director from . import events, configuration, singleton, director
from . import AppName, __version__ from . import AppName, __version__
@ -26,7 +24,12 @@ class ArgumentParser(argparse.ArgumentParser):
out.info(self.format_help()) 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 EXAMPLES
List available comics: List available comics:
dosage -l dosage -l
@ -38,13 +41,11 @@ directory:
If you already have downloaded several comics and want to get the latest If you already have downloaded several comics and want to get the latest
strips of all of them: strips of all of them:
dosage --continue @ 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(): def setup_options():
"""Construct option parser. """Construct option parser.
@return: new option parser @return: new option parser
@ -52,7 +53,7 @@ def setup_options():
""" """
parser = ArgumentParser( parser = ArgumentParser(
description="A comic downloader and archiver.", description="A comic downloader and archiver.",
epilog=Examples, epilog=ExtraHelp,
formatter_class=argparse.RawDescriptionHelpFormatter) formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-v', '--verbose', action='count', default=0, parser.add_argument('-v', '--verbose', action='count', default=0,
@ -227,6 +228,7 @@ def vote_comic(scraperobj):
def run(options): def run(options):
"""Execute comic commands.""" """Execute comic commands."""
set_output_info(options) set_output_info(options)
allscrapers.adddir(user_plugin_path)
# ensure only one instance of dosage is running # ensure only one instance of dosage is running
if not options.allow_multiple: if not options.allow_multiple:
singleton.SingleInstance() singleton.SingleInstance()
@ -241,7 +243,6 @@ def run(options):
if not options.comic: if not options.comic:
out.warn(u'No comics specified, bailing out!') out.warn(u'No comics specified, bailing out!')
return 1 return 1
add_user_scrapers()
if options.modulehelp: if options.modulehelp:
return display_help(options) return display_help(options)
if options.vote: if options.vote:
@ -249,37 +250,8 @@ def run(options):
return director.getComics(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): def do_list(column_list=True, verbose=False, listall=False):
"""List available comics.""" """List available comics."""
add_user_scrapers()
with out.pager(): with out.pager():
out.info(u'Available comic scrapers:') out.info(u'Available comic scrapers:')
out.info(u'Comics tagged with [{}] require age confirmation' out.info(u'Comics tagged with [{}] require age confirmation'

View file

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

View file

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