Mercurial > farol > farol
view farol/cache.py @ 12:4219d6fb4c38
Implement three kind of caches
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Tue, 30 Sep 2014 12:18:52 +0200 |
parents | farol/main.py@a32f9b86edb4 |
children | eedf9606ab33 |
line wrap: on
line source
# -*- encoding: utf-8 -*- # Description: # Web stuff related to the cache. # # Authors: # BenoƮt Allard <benoit.allard@greenbone.net> # # Copyright: # Copyright (C) 2014 Greenbone Networks GmbH # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. """\ The caching stuff ... """ import os from flask import (Blueprint, current_app, session, flash, redirect, url_for, render_template, request) from werkzeug import secure_filename from farolluz.parsers.cvrf import parse from farolluz.renderer import render from .session import get_current, set_current, del_current, has_current, document_required mod = Blueprint('cache', __name__) def caching_type(): """\ Returns the type of caching we are using: None, 'session' or 'global' """ c_type = current_app.config.get('CACHING', 'global') return {'disabled': None}.get(c_type, c_type) def _caching_dir(): """\ Returns the current caching directory """ c_type = caching_type() if c_type is None: return None root_dir = current_app.config.get('CACHE_DIRECTORY', os.path.join(current_app.instance_path, '_cache')) if c_type == 'global': return root_dir uid = session.get('id') # No uid yet, so no cache ... if uid is None: return None return os.path.join(root_dir, uid.hex) def cache_content(): """\ The content of the cache """ dirname = _caching_dir() if dirname is None: return [] if not os.path.exists(dirname): os.makedirs(dirname) l = [] for path in os.listdir(dirname): name, ext = os.path.splitext(path) if ext == '.xml': l.append(name) return l def cleanup_cache(): """\ Remove old documents ... maybe ... from time to time ... """ pass @mod.route('/save', methods=['GET', 'POST']) @document_required def save(): if request.method != 'POST': return render_template('cache/save.j2', id_=get_current()._tracking._identification._id) # Get some kind of filename, and save the cvrf on cache (disk) path = secure_filename(request.form['fname']) path, _ = os.path.splitext(path) dirname = _caching_dir() with open(os.path.join(dirname, path + '.xml'), 'wt') as f: f.write(render(get_current(), 'cvrf.j2').encode('utf-8')) flash('File saved as %s' % path) del_current() return redirect(url_for('new')) @mod.route('/load/<element>', methods=['GET', 'POST']) def load(element): if request.method != 'POST': if has_current(): # Suggest to save first return render_template('cache/load.j2', element=element) # Ouch, GET request changing state of the server ... dirname = _caching_dir() fpath = os.path.join(dirname, element+'.xml') with open(fpath, 'rt') as f: set_current(parse(f)) os.remove(fpath) flash('"%s" has been removed from cache' % element) # Get some kind of id, and load the file. return redirect(url_for('document.view'))