jenkins-bot has submitted this change and it was merged.
Change subject: Always store cache in test/apicache for tests ......................................................................
Always store cache in test/apicache for tests
The API caching under the test framework stores data in tests/apicache when the Request was uncached in normal operation, and in <base_dir>/apicache if the request is processed by CachedRequest in normal operation.
This change forces all cached API requests to be stored in tests/apicache, irrespective of how the test was invoked, or whether the request would normally be cached or not.
CachedRequest methods _get_cache_dir and _make_dir converted to static methods for easy monkey-patching, and docstrings added.
Change-Id: Ibc7ec0a90a130fb1c834fd632af7872f1325b3e0 --- M pywikibot/data/api.py M tests/__init__.py 2 files changed, 28 insertions(+), 9 deletions(-)
Approvals: Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index bb5c1cb..865932c 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -441,17 +441,35 @@ self._data = None self._cachetime = None
- def _get_cache_dir(self): + @staticmethod + def _get_cache_dir(): + """The base directory path for cache entries. + + The directory will be created if it does not already exist. + + @return: basestring + """ path = os.path.join(pywikibot.config2.base_dir, 'apicache') - self._make_dir(path) + CachedRequest._make_dir(path) return path
- def _make_dir(self, dir): + @staticmethod + def _make_dir(dir): + """Create directory if it does not exist already. + + The directory name (dir) is returned unmodified. + + @param dir: directory path + @type dir: basestring + + @return: basestring + """ try: os.makedirs(dir) except OSError: # directory already exists pass + return dir
def _uniquedescriptionstr(self): login_status = self.site._loginstatus @@ -475,7 +493,8 @@ ).hexdigest()
def _cachefile_path(self): - return os.path.join(self._get_cache_dir(), self._create_file_name()) + return os.path.join(CachedRequest._get_cache_dir(), + self._create_file_name())
def _expired(self, dt): return dt + self.expiry < datetime.datetime.now() diff --git a/tests/__init__.py b/tests/__init__.py index 3ac2361..e725492 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -11,6 +11,11 @@ from pywikibot.data.api import Request as _original_Request from pywikibot.data.api import CachedRequest
+_cache_dir = os.path.join(os.path.split(__file__)[0], 'apicache') + +CachedRequest._get_cache_dir = staticmethod( + lambda *args: CachedRequest._make_dir(_cache_dir)) +
class TestRequest(CachedRequest):
@@ -18,11 +23,6 @@
def __init__(self, *args, **kwargs): super(TestRequest, self).__init__(0, *args, **kwargs) - - def _get_cache_dir(self): - path = os.path.join(os.path.split(__file__)[0], 'apicache') - self._make_dir(path) - return path
def _expired(self, dt): """Never invalidate cached data."""
pywikibot-commits@lists.wikimedia.org