From 0ce501fa0d059e6daaf97dab324183f6fc9abce8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Jan 2013 13:39:42 +0530 Subject: [PATCH] Store caches outside the config directory for non-portable calibre installs --- src/calibre/constants.py | 36 +++++++++++++++++++ src/calibre/devices/apple/driver.py | 6 ++-- .../library/catalogs/epub_mobi_builder.py | 5 ++- src/calibre/web/jsbrowser/browser.py | 7 ++-- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/calibre/constants.py b/src/calibre/constants.py index 40d39b0ad4..dff477bbc4 100644 --- a/src/calibre/constants.py +++ b/src/calibre/constants.py @@ -79,6 +79,42 @@ def debug(): global DEBUG DEBUG = True +_cache_dir = None + +def _get_cache_dir(): + confcache = os.path.join(config_dir, u'caches') + if isportable: + return confcache + if os.environ.has_key('CALIBRE_CACHE_DIRECTORY'): + return os.path.abspath(os.environ['CALIBRE_CACHE_DIRECTORY']) + + if iswindows: + w = plugins['winutil'][0] + candidate = os.path.join(w.special_folder_path(w.CSIDL_LOCAL_APPDATA), u'%s-cache'%__appname__) + elif isosx: + candidate = os.path.join(os.path.expanduser(u'~/Library/Caches'), __appname__) + else: + candidate = os.environ.get('XDG_CACHE_HOME', u'~/.cache') + candidate = os.path.join(os.path.expanduser(candidate), + __appname__) + if isinstance(candidate, bytes): + try: + candidate = candidate.decode(filesystem_encoding) + except ValueError: + candidate = confcache + if not os.path.exists(candidate): + try: + os.makedirs(candidate) + except: + candidate = confcache + return candidate + +def cache_dir(): + global _cache_dir + if _cache_dir is None: + _cache_dir = _get_cache_dir() + return _cache_dir + # plugins {{{ class Plugins(collections.Mapping): diff --git a/src/calibre/devices/apple/driver.py b/src/calibre/devices/apple/driver.py index eacb143790..95dedf546a 100644 --- a/src/calibre/devices/apple/driver.py +++ b/src/calibre/devices/apple/driver.py @@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en' import cStringIO, ctypes, datetime, os, platform, re, shutil, sys, tempfile, time -from calibre.constants import __appname__, __version__, DEBUG +from calibre.constants import __appname__, __version__, DEBUG, cache_dir from calibre import fit_image, confirm_config_name, strftime as _strftime from calibre.constants import isosx, iswindows from calibre.devices.errors import OpenFeedback, UserFeedback @@ -289,9 +289,7 @@ class ITUNES(DriverBase): # Properties cached_books = {} - cache_dir = os.path.join(config_dir, 'caches', 'itunes') calibre_library_path = prefs['library_path'] - archive_path = os.path.join(cache_dir, "thumbs.zip") description_prefix = "added by calibre" ejected = False iTunes = None @@ -884,6 +882,8 @@ class ITUNES(DriverBase): logger().info(" BCD: %s" % ['0x%x' % x for x in sorted(self.BCD)]) logger().info(" PRODUCT_ID: %s" % ['0x%x' % x for x in sorted(self.PRODUCT_ID)]) + self.cache_dir = os.path.join(cache_dir(), 'itunes') + self.archive_path = os.path.join(self.cache_dir, "thumbs.zip") # Confirm/create thumbs archive if not os.path.exists(self.cache_dir): if DEBUG: diff --git a/src/calibre/library/catalogs/epub_mobi_builder.py b/src/calibre/library/catalogs/epub_mobi_builder.py index 8f27db61be..9f946e2ee0 100644 --- a/src/calibre/library/catalogs/epub_mobi_builder.py +++ b/src/calibre/library/catalogs/epub_mobi_builder.py @@ -9,7 +9,7 @@ from xml.sax.saxutils import escape from calibre import (prepare_string_for_xml, strftime, force_unicode, isbytestring) -from calibre.constants import isosx +from calibre.constants import isosx, cache_dir from calibre.customize.conversion import DummyReporter from calibre.customize.ui import output_profiles from calibre.ebooks.BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, Tag, NavigableString @@ -18,7 +18,6 @@ from calibre.ebooks.metadata import author_to_author_sort from calibre.library.catalogs import AuthorSortMismatchException, EmptyCatalogException, \ InvalidGenresSourceFieldException from calibre.ptempfile import PersistentTemporaryDirectory -from calibre.utils.config import config_dir from calibre.utils.date import format_date, is_date_undefined, now as nowf from calibre.utils.filenames import ascii_text, shorten_components_to from calibre.utils.icu import capitalize, collation_order, sort_key @@ -109,7 +108,7 @@ class CatalogBuilder(object): self.plugin = plugin self.reporter = report_progress self.stylesheet = stylesheet - self.cache_dir = os.path.join(config_dir, 'caches', 'catalog') + self.cache_dir = os.path.join(cache_dir(), 'catalog') self.catalog_path = PersistentTemporaryDirectory("_epub_mobi_catalog", prefix='') self.content_dir = os.path.join(self.catalog_path, "content") self.excluded_tags = self.get_excluded_tags() diff --git a/src/calibre/web/jsbrowser/browser.py b/src/calibre/web/jsbrowser/browser.py index d8f0e79bc4..c22d912128 100644 --- a/src/calibre/web/jsbrowser/browser.py +++ b/src/calibre/web/jsbrowser/browser.py @@ -16,7 +16,7 @@ from PyQt4.Qt import (QObject, QNetworkAccessManager, QNetworkDiskCache, from PyQt4.QtWebKit import QWebPage, QWebSettings, QWebView, QWebElement from calibre import USER_AGENT, prints, get_proxies, get_proxy_info -from calibre.constants import ispy3, config_dir +from calibre.constants import ispy3, cache_dir from calibre.utils.logging import ThreadSafeLog from calibre.gui2 import must_use_qt from calibre.web.jsbrowser.forms import FormsMixin @@ -44,7 +44,7 @@ class WebPage(QWebPage): # {{{ settings = self.settings() if enable_developer_tools: settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True) - QWebSettings.enablePersistentStorage(os.path.join(config_dir, 'caches', + QWebSettings.enablePersistentStorage(os.path.join(cache_dir(), 'webkit-persistence')) QWebSettings.setMaximumPagesInCache(0) @@ -135,8 +135,7 @@ class NetworkAccessManager(QNetworkAccessManager): # {{{ self.log = log if use_disk_cache: self.cache = QNetworkDiskCache(self) - self.cache.setCacheDirectory(os.path.join(config_dir, 'caches', - 'jsbrowser')) + self.cache.setCacheDirectory(os.path.join(cache_dir(), 'jsbrowser')) self.setCache(self.cache) self.sslErrors.connect(self.on_ssl_errors) self.pf = ProxyFactory(log)