refactor morituri.common.directory to shed Directory() and bulk

This commit is contained in:
Samantha Baldwin
2016-11-02 17:52:14 -04:00
parent 1e0176553b
commit bccba71b42
6 changed files with 32 additions and 70 deletions

View File

@@ -28,8 +28,7 @@ import urllib2
from morituri.common import log, directory from morituri.common import log, directory
d = directory.Directory() _CACHE_DIR = directory.cache_path()
_CACHE_DIR = d.getCache()
class AccuCache(log.Loggable): class AccuCache(log.Loggable):

View File

@@ -155,17 +155,9 @@ class PersistedCache(log.Loggable):
class ResultCache(log.Loggable): class ResultCache(log.Loggable):
def __init__(self, path=None): def __init__(self, path=None):
if not path: self._path = path or directory.cache_path('result')
path = self._getResultCachePath()
self._path = path
self._pcache = PersistedCache(self._path) self._pcache = PersistedCache(self._path)
def _getResultCachePath(self):
d = directory.Directory()
path = d.getCache('result')
return path
def getRipResult(self, cddbdiscid, create=True): def getRipResult(self, cddbdiscid, create=True):
""" """
Retrieve the persistable RipResult either from our cache (from a Retrieve the persistable RipResult either from our cache (from a
@@ -208,8 +200,7 @@ class TableCache(log.Loggable):
def __init__(self, path=None): def __init__(self, path=None):
if not path: if not path:
d = directory.Directory() self._path = directory.cache_path('table')
self._path = d.getCache('table')
else: else:
self._path = path self._path = path

View File

@@ -33,18 +33,12 @@ from morituri.common import directory, log
class Config(log.Loggable): class Config(log.Loggable):
def __init__(self, path=None): def __init__(self, path=None):
if not path: self._path = path or directory.config_path()
path = self.getDefaultPath()
self._path = path
self._parser = ConfigParser.SafeConfigParser() self._parser = ConfigParser.SafeConfigParser()
self.open() self.open()
def getDefaultPath(self):
return directory.Directory().getConfig()
def open(self): def open(self):
# Open the file with the correct encoding # Open the file with the correct encoding
if os.path.exists(self._path): if os.path.exists(self._path):

View File

@@ -20,51 +20,31 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with morituri. If not, see <http://www.gnu.org/licenses/>. # along with morituri. If not, see <http://www.gnu.org/licenses/>.
import os from os import getenv, makedirs
from os.path import join, expanduser, exists
from morituri.common import log def config_path():
path = join(getenv('XDG_CONFIG_HOME') or join(expanduser('~'), u'.config'),
u'whipper')
if not exists(path):
makedirs(path)
return join(path, u'whipper.conf')
def cache_path(name=None):
path = join(getenv('XDG_CACHE_HOME') or join(expanduser('~'), u'.cache'),
u'whipper')
if name:
path = join(path, name)
if not exists(path):
makedirs(path)
return path
class Directory(log.Loggable): def data_path(name=None):
path = join(getenv('XDG_DATA_HOME')
def getConfig(self): or join(expanduser('~'), u'.local/share'),
config_directory = os.getenv('XDG_CONFIG_HOME') u'whipper')
if not config_directory: if name:
config_directory = os.path.join(os.path.expanduser('~'), path = join(path, name)
u'.config') if not exists(path):
folder_path = os.path.join(config_directory, u'whipper') makedirs(path)
if not os.path.exists(folder_path): return path
os.makedirs(folder_path)
path = os.path.join(config_directory, u'whipper/whipper.conf')
self.info('Configuration file path: %s' % path)
return path
def getCache(self, name=None):
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 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:
path = os.path.join(path, name)
if not os.path.exists(path):
os.makedirs(path)
return path

View File

@@ -19,7 +19,7 @@ def main():
'https://thomas.apestaart.org/morituri/trac') 'https://thomas.apestaart.org/morituri/trac')
# register plugins with pkg_resources # register plugins with pkg_resources
distributions, _ = pkg_resources.working_set.find_plugins( distributions, _ = pkg_resources.working_set.find_plugins(
pkg_resources.Environment([directory.Directory().getData('plugins')]) pkg_resources.Environment([directory.data_path('plugins')])
) )
map(pkg_resources.working_set.add, distributions) map(pkg_resources.working_set.add, distributions)
c = Rip() c = Rip()

View File

@@ -9,10 +9,8 @@ from morituri.test import common
class DirectoryTestCase(common.TestCase): class DirectoryTestCase(common.TestCase):
def testAll(self): def testAll(self):
d = directory.Directory() path = directory.config_path()
path = d.getConfig()
self.failUnless(path.startswith('/home')) self.failUnless(path.startswith('/home'))
path = d.getCache() path = directory.cache_path()
self.failUnless(path.startswith('/home')) self.failUnless(path.startswith('/home'))