From 746dc6599d8c0ff6437f6c47142daefef1924097 Mon Sep 17 00:00:00 2001 From: JoeLametta Date: Sun, 9 Oct 2016 17:26:06 +0200 Subject: [PATCH] Issue24 (#42) * Address issue #24 * Remove getReadCaches() & replace it with getCache() Now whipper always follows XDG specifications. All changes were documented into the README file. The changes introduced here aren't backward compatible so, after updating whipper, you may need to configure it again (old config file are retained, though). --- README.md | 35 ++++++++++--- morituri/common/accurip.py | 5 +- morituri/common/cache.py | 31 +++++------- morituri/common/directory.py | 68 ++++++++++---------------- morituri/rip/main.py | 6 +-- morituri/test/test_common_directory.py | 3 -- 6 files changed, 71 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 83f0b21..17f1f1d 100755 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ FORK INFORMATIONS --------- The name of this fork is still to be decided: right now I'll be using whipper. -This branch is very close to morituri's master one (the internal 'morituri' references are still unchanged). As a starting point, I've just merged the following commits: +This branch is very close to morituri's master one (internal morituri references are still unchanged). As a starting point, I've just merged the following commits: - [#79](https://github.com/thomasvs/morituri/issues/79) - [#92](https://github.com/thomasvs/morituri/issues/92) - [#109](https://github.com/thomasvs/morituri/issues/109) @@ -12,9 +12,29 @@ This branch is very close to morituri's master one (the internal 'morituri' refe - [#140](https://github.com/thomasvs/morituri/issues/140) - [#141](https://github.com/thomasvs/morituri/issues/141) -And changed the default logger to morituri-whatlogger's one. +And changed the default logger to [morituri-yamllogger](https://github.com/JoeLametta/morituri-yamllogger)'s one. -In order to track whipper's development it's better to check its commit history (readme needs to be updated). +In order to track whipper's current development it's better to check its commit history (README *needs* to be updated). + +**WARNING:** As whipper is still under heavy development sometimes I will force push (`--force-with-lease`) to the non master branches. + +BACKWARD INCOMPATIBLE CHANGES +--------- +* Whipper has adopted new config/cache/state file paths + * Now always follows XDG specifications + * Paths used when XDG environment variables are available: + * `$XDG_CONFIG_HOME/whipper` + * `$XDG_CACHE_HOME/whipper` + * `$XDG_DATA_HOME/whipper` + * Paths used when XDG environment variables are **NOT** available: + * `$HOME/.config/whipper` + * `$HOME/.cache/whipper` + * `$HOME/.local/share/whipper` + * Configuration file information: + * `.moriturirc`, `morituri.conf` aren't used anymore + * `$XDG_CONFIG_HOME/whipper/whipper.conf` (OR `$HOME/.config/whipper/whipper.conf`) + * Plugins folder path: + * `$XDG_DATA_HOME/whipper/plugins` (OR `$HOME/.local/share/whipper/plugins`) WHIPPER [![Build Status](https://travis-ci.org/JoeLametta/whipper.svg?branch=master)](https://travis-ci.org/JoeLametta/whipper) --------- @@ -22,9 +42,7 @@ whipper is a fork of the morituri project (CDDA ripper for *nix systems aiming f It improves morituri which development seems to have halted/slowed down merging old pull requests and improving it with bugfixes and new functions. -If possible, I'll try to mainline the useful commits of this fork but, in the future, this may not be possible because of different project choices. - -The project home page is still TBD. +If possible, I'll try to upstream the progress done here but, in the future, this may not be possible because of different project choices. RATIONALE --------- @@ -150,7 +168,7 @@ The simplest way to get started making accurate rips is: FILING BUGS ----------- -whipper's bug tracker is still TBD. +whipper's bugs are tracked using the repository issue section provided by GitHub. morituri's bug tracker is at [http://thomas.apestaart.org/morituri/trac/]( http://thomas.apestaart.org/morituri/trac/). @@ -184,7 +202,7 @@ The configuration file is stored according to [XDG Base Directory Specification] http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) when possible. -It lives in `$XDG_CONFIG_HOME/morituri/morituri.conf` +It lives in `$XDG_CONFIG_HOME/whipper/whipper.conf` (or `$HOME/.config/whipper/whipper.conf`) The configuration file follows python's ConfigParser syntax. @@ -217,3 +235,4 @@ Note: to get a literal '%' character it must be doubled. CONTRIBUTING ------------ - Please send pull requests through GitHub. + diff --git a/morituri/common/accurip.py b/morituri/common/accurip.py index 4f5c701..a6c2309 100644 --- a/morituri/common/accurip.py +++ b/morituri/common/accurip.py @@ -26,9 +26,10 @@ import struct import urlparse import urllib2 -from morituri.common import log +from morituri.common import log, directory -_CACHE_DIR = os.path.join(os.path.expanduser('~'), '.morituri', 'cache') +d = directory.Directory() +_CACHE_DIR = d.getCache() class AccuCache(log.Loggable): diff --git a/morituri/common/cache.py b/morituri/common/cache.py index d2ff9fe..f9c701a 100644 --- a/morituri/common/cache.py +++ b/morituri/common/cache.py @@ -162,8 +162,8 @@ class ResultCache(log.Loggable): self._pcache = PersistedCache(self._path) def _getResultCachePath(self): - path = os.path.join(os.path.expanduser('~'), '.morituri', 'cache', - 'result') + d = directory.Directory() + path = d.getCache('result') return path def getRipResult(self, cddbdiscid, create=True): @@ -210,33 +210,26 @@ class TableCache(log.Loggable): if not path: d = directory.Directory() self._path = d.getCache('table') - self._readPaths = d.getReadCaches('table') else: self._path = path - self._readPaths = [path, ] self._pcache = PersistedCache(self._path) - self._readPCaches = [PersistedCache(p) for p in self._readPaths] def get(self, cddbdiscid, mbdiscid): # Before 0.2.1, we only saved by cddbdiscid, and had collisions # mbdiscid collisions are a lot less likely - for pcache in self._readPCaches: - ptable = pcache.get('mbdiscid.' + mbdiscid) - if ptable.object: - break + ptable = self._pcache.get('mbdiscid.' + mbdiscid) if not ptable.object: - for pcache in self._readPCaches: - ptable = pcache.get(cddbdiscid) - if ptable.object: - if ptable.object.getMusicBrainzDiscId() != mbdiscid: - self.debug('cached table is for different mb id %r' % ( - ptable.object.getMusicBrainzDiscId())) - ptable.object = None - else: - self.debug('no valid cached table found for %r' % - cddbdiscid) + ptable = self._pcache.get(cddbdiscid) + if ptable.object: + if ptable.object.getMusicBrainzDiscId() != mbdiscid: + self.debug('cached table is for different mb id %r' % ( + ptable.object.getMusicBrainzDiscId())) + ptable.object = None + else: + self.debug('no valid cached table found for %r' % + cddbdiscid) if not ptable.object: # get an empty persistable from the writable location diff --git a/morituri/common/directory.py b/morituri/common/directory.py index 47aac11..326e8ac 100644 --- a/morituri/common/directory.py +++ b/morituri/common/directory.py @@ -28,56 +28,40 @@ from morituri.common import log class Directory(log.Loggable): def getConfig(self): - try: - from xdg import BaseDirectory - directory = BaseDirectory.save_config_path('morituri') - path = os.path.join(directory, 'morituri.conf') - self.info('Using XDG, configuration file is %s' % path) - except ImportError: - path = os.path.join(os.path.expanduser('~'), '.moriturirc') - self.info('Not using XDG, configuration file is %s' % path) + config_directory = os.getenv('XDG_CONFIG_HOME') + if not config_directory: + config_directory = os.path.join(os.path.expanduser('~'), + u'.config') + path = os.path.join(config_directory, u'whipper/whipper.conf') + self.info('Configuration file path: %s' % path) return path - def getCache(self, name=None): - try: - from xdg import BaseDirectory - path = BaseDirectory.save_cache_path('morituri') - self.info('Using XDG, cache directory is %s' % path) - except (ImportError, AttributeError): - # save_cache_path was added in pyxdg 0.25 - path = os.path.join(os.path.expanduser('~'), '.morituri', 'cache') - if not os.path.exists(path): - os.makedirs(path) - self.info('Not using XDG, cache directory is %s' % path) - + cache_directory = os.getenv('XDG_CACHE_HOME') + if not cache_directory: + cache_directory = os.path.join(os.path.expanduser('~'), u'.cache') + path = os.path.join(cache_directory, u'whipper') + self.info('Cache directory path: %s' % path) + if not os.path.exists(path): + os.makedirs(path) if name: path = os.path.join(path, name) if not os.path.exists(path): os.makedirs(path) - return path - def getReadCaches(self, name=None): - paths = [] - - try: - from xdg import BaseDirectory - path = BaseDirectory.save_cache_path('morituri') - self.info('For XDG, read cache directory is %s' % path) - paths.append(path) - except (ImportError, AttributeError): - # save_cache_path was added in pyxdg 0.21 - pass - - path = os.path.join(os.path.expanduser('~'), '.morituri', 'cache') - if os.path.exists(path): - self.info('From before XDG, read cache directory is %s' % path) - paths.append(path) - + def getData(self, name=None): + data_directory = os.getenv('XDG_DATA_HOME') + if not data_directory: + data_directory = os.path.join(os.path.expanduser('~'), + u'.local/share') + path = os.path.join(data_directory, u'whipper') + self.info('Data directory path: %s' % path) + if not os.path.exists(path): + os.makedirs(path) if name: - paths = [os.path.join(p, name) for p in paths] - - return paths - + path = os.path.join(path, name) + if not os.path.exists(path): + os.makedirs(path) + return path diff --git a/morituri/rip/main.py b/morituri/rip/main.py index 31d0ece..a56658e 100644 --- a/morituri/rip/main.py +++ b/morituri/rip/main.py @@ -5,7 +5,7 @@ import os import sys import pkg_resources -from morituri.common import log, logcommand, common, config +from morituri.common import log, logcommand, common, config, directory from morituri.configure import configure from morituri.rip import cd, offset, drive, image, accurip, debug @@ -19,8 +19,8 @@ def main(argv): from morituri.configure import configure pluginsdir = configure.pluginsdir - homepluginsdir = os.path.join(os.path.expanduser('~'), - '.morituri', 'plugins') + d = directory.Directory() + homepluginsdir = d.getData('plugins') distributions, errors = pkg_resources.working_set.find_plugins( pkg_resources.Environment([pluginsdir, homepluginsdir])) diff --git a/morituri/test/test_common_directory.py b/morituri/test/test_common_directory.py index c7b2766..1c7b6c1 100644 --- a/morituri/test/test_common_directory.py +++ b/morituri/test/test_common_directory.py @@ -16,6 +16,3 @@ class DirectoryTestCase(common.TestCase): path = d.getCache() self.failUnless(path.startswith('/home')) - - paths = d.getReadCaches() - self.failUnless(paths[0].startswith('/home'))