diff options
| author | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-10 20:00:03 -0500 |
|---|---|---|
| committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-11 12:56:57 -0500 |
| commit | dde673e54520b6d8a88cde57a49aa342be8dcb03 (patch) | |
| tree | 210db25e8dc9d1946a0a30ab09b517e6cd9454b1 /requests_cache/backends/sqlite.py | |
| parent | 237796a184eeb83c6ea9accab989df3a7415e314 (diff) | |
| download | requests-cache-dde673e54520b6d8a88cde57a49aa342be8dcb03.tar.gz | |
Make parent dirs for new SQLite databases
Diffstat (limited to 'requests_cache/backends/sqlite.py')
| -rw-r--r-- | requests_cache/backends/sqlite.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/requests_cache/backends/sqlite.py b/requests_cache/backends/sqlite.py index 00cf9ef..45c3f79 100644 --- a/requests_cache/backends/sqlite.py +++ b/requests_cache/backends/sqlite.py @@ -2,7 +2,10 @@ import sqlite3 import threading from contextlib import contextmanager from logging import getLogger -from os.path import basename, expanduser +from os import makedirs +from os.path import abspath, basename, dirname, expanduser +from pathlib import Path +from typing import Union from .base import BaseCache, BaseStorage @@ -15,20 +18,16 @@ class DbCache(BaseCache): Reading is fast, saving is a bit slower. It can store big amount of data with low memory usage. Args: - db_path: Database file path + db_path: Database file path (expands user paths and creates parent dirs) fast_save: Speedup cache saving up to 50 times but with possibility of data loss. See :py:class:`.DbDict` for more info timeout: Timeout for acquiring a database lock """ - def __init__(self, db_path: str = 'http_cache', fast_save: bool = False, **kwargs): + def __init__(self, db_path: Union[Path, str] = 'http_cache', fast_save: bool = False, **kwargs): super().__init__(**kwargs) kwargs.setdefault('suppress_warnings', True) - # Allow paths with user directories (~/*), and add file extension if not specified - db_path = expanduser(str(db_path)) - if '.' not in basename(db_path): - db_path += '.sqlite' - + db_path = _get_db_path(db_path) self.responses = DbPickleDict(db_path, table_name='responses', fast_save=fast_save, **kwargs) self.redirects = DbDict(db_path, table_name='redirects', **kwargs) @@ -178,3 +177,14 @@ class DbPickleDict(DbDict): def __getitem__(self, key): return self.deserialize(super().__getitem__(key)) + + +def _get_db_path(db_path): + """Get resolved path for database file""" + # Allow paths with user directories (~/*), and add file extension if not specified + db_path = abspath(expanduser(str(db_path))) + if '.' not in basename(db_path): + db_path += '.sqlite' + # Make sure parent dirs exist + makedirs(dirname(db_path), exist_ok=True) + return db_path |
