mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
Make BaseFileCache into an abstractbaseclass so it's a proper interface
Push the opening and closing of files into the _load and _dump methods so that we don't invoke the slow codec machinery without reason.
This commit is contained in:
parent
c033e5111f
commit
45251f910c
5 changed files with 66 additions and 48 deletions
14
lib/ansible/plugins/cache/jsonfile.py
vendored
14
lib/ansible/plugins/cache/jsonfile.py
vendored
|
@ -19,6 +19,8 @@
|
|||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import codecs
|
||||
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
|
@ -31,10 +33,12 @@ class CacheModule(BaseFileCacheModule):
|
|||
"""
|
||||
A caching module backed by json files.
|
||||
"""
|
||||
plugin_name = 'jsonfile'
|
||||
|
||||
def _load(self, f):
|
||||
return json.load(f)
|
||||
def _load(self, filepath):
|
||||
# Valid JSON is always UTF-8 encoded.
|
||||
with codecs.open(filepath, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
def _dump(self, value, f):
|
||||
f.write(jsonify(value, format=True))
|
||||
def _dump(self, value, filepath):
|
||||
with codecs.open(filepath, 'w', encoding='utf-8') as f:
|
||||
f.write(jsonify(value, format=True))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue