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).
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user