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:
Toshio Kuratomi 2017-02-16 14:59:14 -08:00 committed by Brian Coca
parent c033e5111f
commit 45251f910c
5 changed files with 66 additions and 48 deletions

View file

@ -19,6 +19,9 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import codecs
import yaml
from ansible.parsing.yaml.loader import AnsibleLoader
@ -29,10 +32,11 @@ class CacheModule(BaseFileCacheModule):
"""
A caching module backed by yaml files.
"""
plugin_name = 'yaml'
def _load(self, f):
return AnsibleLoader(f).get_single_data()
def _load(self, filepath):
with codecs.open(filepath, 'r', encoding='utf-8') as f:
return AnsibleLoader(f).get_single_data()
def _dump(self, value, f):
yaml.dump(value, f, Dumper=AnsibleDumper, default_flow_style=False)
def _dump(self, value, filepath):
with codecs.open(filepath, 'w', encoding='utf-8') as f:
yaml.dump(value, f, Dumper=AnsibleDumper, default_flow_style=False)