def cache_image_data(cache_dir, cache_key, uploader, *args, **kwargs):
use_cache = True<EOL>if "<STR_LIT>" in kwargs:<EOL><INDENT>use_cache = kwargs["<STR_LIT>"]<EOL>del kwargs["<STR_LIT>"]<EOL><DEDENT>json_path = None<EOL>if cache_dir:<EOL><INDENT>json_path = os.path.join(cache_dir, "<STR_LIT>" % cache_key)<EOL>if use_cache and os.path.exists(json_path):<EOL><INDENT>LOG.info("<STR_LIT>" % (args,))<EOL>try:<EOL><INDENT>with closing(open(json_path, "<STR_LIT:r>")) as handle:<EOL><INDENT>img_data = json.load(handle)<EOL><DEDENT>return parts.Bunch([(key, parts.Bunch(val))<EOL>for key, val in img_data.items() <EOL>])<EOL><DEDENT>except (EnvironmentError, TypeError, ValueError) as exc:<EOL><INDENT>LOG.warn("<STR_LIT>" % (json_path, exc))<EOL><DEDENT><DEDENT><DEDENT>LOG.info("<STR_LIT>" % (args,))<EOL>img_data = uploader(*args, **kwargs)<EOL>if json_path:<EOL><INDENT>with closing(open(json_path, "<STR_LIT:w>")) as handle:<EOL><INDENT>json.dump(img_data, handle)<EOL><DEDENT><DEDENT>return img_data<EOL>
Call uploader and cache its results.