Add disk cache options to Chrome

Add `Chrome` options `disk_cache` and `disk_cache_size` which add chromium
options `--disk-cache-dir=<DIR>` and `--disk-cache-size=N` (bytes).
The default is to use `--disable-cache` (no disk caching).

There are two ways to use the new vars, if you just use
`Chrome(disk_cache=True)` the chromium cli option `--disable-cache` is
NOT used and chromium writes disk cache inside profile dir.

If you use `Chrome(disk_cache='/tmp/custom_dir', disk_cache_size=10000)`
chromium will use `--disk-cache-dir=/tmp/custom_dir
--disk-cache-size=10000`.
This commit is contained in:
Vangelis Banos 2019-02-06 16:22:10 +00:00
parent 809ea3885f
commit c288c9ae98

View File

@ -62,7 +62,8 @@ def check_version(chrome_exe):
class Chrome: 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):
''' '''
Initializes instance of this class. Initializes instance of this class.
@ -79,6 +80,8 @@ 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_size = disk_cache_size
def __enter__(self): def __enter__(self):
''' '''
@ -134,7 +137,8 @@ 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): def start(self, proxy=None, cookie_db=None, disk_cache=None,
disk_cache_size=None):
''' '''
Starts chrome/chromium process. Starts chrome/chromium process.
@ -144,7 +148,12 @@ 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
`self._home_tmpdir`. If its a string, try to use that path for
disk cache (default None)
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.
(default None)
Returns: Returns:
websocket url to chrome window with about:blank loaded websocket url to chrome window with about:blank loaded
''' '''
@ -154,6 +163,10 @@ 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:
self.disk_cache = disk_cache
if disk_cache_size:
self.disk_cache_size = disk_cache_size
self._shutdown.clear() self._shutdown.clear()
new_env = os.environ.copy() new_env = os.environ.copy()
@ -166,12 +179,22 @@ class Chrome:
'--disable-background-networking', '--disable-background-networking',
'--disable-renderer-backgrounding', '--disable-hang-monitor', '--disable-renderer-backgrounding', '--disable-hang-monitor',
'--disable-background-timer-throttling', '--mute-audio', '--disable-background-timer-throttling', '--mute-audio',
'--disable-web-sockets', '--disable-cache', '--disable-web-sockets',
'--window-size=1100,900', '--no-default-browser-check', '--window-size=1100,900', '--no-default-browser-check',
'--disable-first-run-ui', '--no-first-run', '--disable-first-run-ui', '--no-first-run',
'--homepage=about:blank', '--disable-direct-npapi-requests', '--homepage=about:blank', '--disable-direct-npapi-requests',
'--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 isinstance(self.disk_cache, str):
chrome_args.append('--disk-cache-dir=%s' % self.disk_cache)
if self.disk_cache_size:
chrome_args.append('--disk-cache-size=%s' %
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: