Improve disk cache options

Remove `--disable-cache`, its not used any more.

Rename `disk_cache` to `disk_cache_dir` and use only path (str)
argument.

Decouple `--disk-cache-size` from `--disk-cache-dir` so it is possible
to use either or both.
This commit is contained in:
Vangelis Banos 2019-02-07 07:42:45 +00:00
parent c288c9ae98
commit 31e611771e

View file

@ -63,7 +63,7 @@ class Chrome:
logger = logging.getLogger(__module__ + '.' + __qualname__) logger = logging.getLogger(__module__ + '.' + __qualname__)
def __init__(self, chrome_exe, port=9222, ignore_cert_errors=False, def __init__(self, chrome_exe, port=9222, ignore_cert_errors=False,
disk_cache=None, disk_cache_size=None): disk_cache_dir=None, disk_cache_size=None):
''' '''
Initializes instance of this class. Initializes instance of this class.
@ -80,7 +80,7 @@ class Chrome:
self.ignore_cert_errors = ignore_cert_errors self.ignore_cert_errors = ignore_cert_errors
self._shutdown = threading.Event() self._shutdown = threading.Event()
self.chrome_process = None self.chrome_process = None
self.disk_cache = disk_cache self.disk_cache_dir = disk_cache_dir
self.disk_cache_size = disk_cache_size self.disk_cache_size = disk_cache_size
def __enter__(self): def __enter__(self):
@ -137,7 +137,7 @@ class Chrome:
cookie_location, exc_info=True) cookie_location, exc_info=True)
return cookie_db return cookie_db
def start(self, proxy=None, cookie_db=None, disk_cache=None, def start(self, proxy=None, cookie_db=None, disk_cache_dir=None,
disk_cache_size=None): disk_cache_size=None):
''' '''
Starts chrome/chromium process. Starts chrome/chromium process.
@ -148,9 +148,8 @@ class Chrome:
which, if supplied, will be written to which, if supplied, will be written to
{chrome_user_data_dir}/Default/Cookies before running the {chrome_user_data_dir}/Default/Cookies before running the
browser (default None) browser (default None)
disk_cache: use disk cache. If True, use default cache location inside disk_cache_dir: use directory for disk cache. The default location
`self._home_tmpdir`. If its a string, try to use that path for is inside `self._home_tmpdir` (default None).
disk cache (default None)
disk_cache_size: Forces the maximum disk space to be used by the disk disk_cache_size: Forces the maximum disk space to be used by the disk
cache, in bytes. Used only when `cache` is a disk path. cache, in bytes. Used only when `cache` is a disk path.
(default None) (default None)
@ -163,8 +162,8 @@ class Chrome:
self._home_tmpdir.name, 'chrome-user-data') self._home_tmpdir.name, 'chrome-user-data')
if cookie_db: if cookie_db:
self._init_cookie_db(cookie_db) self._init_cookie_db(cookie_db)
if disk_cache: if disk_cache_dir:
self.disk_cache = disk_cache self.disk_cache_dir = disk_cache_dir
if disk_cache_size: if disk_cache_size:
self.disk_cache_size = disk_cache_size self.disk_cache_size = disk_cache_size
self._shutdown.clear() self._shutdown.clear()
@ -186,15 +185,10 @@ class Chrome:
'--disable-web-security', '--disable-notifications', '--disable-web-security', '--disable-notifications',
'--disable-extensions', '--disable-save-password-bubble'] '--disable-extensions', '--disable-save-password-bubble']
if self.disk_cache: if self.disk_cache_dir:
if isinstance(self.disk_cache, str): chrome_args.append('--disk-cache-dir=%s' % self.disk_cache_dir)
chrome_args.append('--disk-cache-dir=%s' % self.disk_cache)
if self.disk_cache_size: if self.disk_cache_size:
chrome_args.append('--disk-cache-size=%s' % chrome_args.append('--disk-cache-size=%s' % self.disk_cache_size)
self.disk_cache_size)
else:
chrome_args.append('--disable-cache')
if self.ignore_cert_errors: if self.ignore_cert_errors:
chrome_args.append('--ignore-certificate-errors') chrome_args.append('--ignore-certificate-errors')
if proxy: if proxy: