def test_all_together(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_folder("static") appcache.add_urls("/") appcache.add_urls("/test") @app.route("/") def main(): return "yay" @app.route("/test") def test(): return "yay!!" urls = appcache.urls self.assertEquals(4, len(urls)) self.assertTrue("/" in urls) self.assertTrue("/test" in urls) self.assertTrue("/static/static1.js" in urls) self.assertTrue("/static/static2.css" in urls) # so there is a hash.. hash, updated = appcache.hash() self.assertTrue(hash) self.assertTrue(updated)
def test_route(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_urls("/static/static1.js") client = app.test_client() response = client.get("/manifest.appcache", follow_redirects=True) self.assertEquals(200, response.status_code) data = response.data.split("\n") self.assertEquals(4, len(data)) self.assertEquals(hashlib.sha1("Test").hexdigest(), data[1]) self.assertEquals("/static/static1.js", data[2])
def test_correct_hash(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_urls("/") mode = 1 @app.route("/") def main(): if mode == 1: return "yay" else: return "yay1" hash, updated = appcache.hash() self.assertEquals(hashlib.sha1("yay").hexdigest(), hash) h, updated_again = appcache.hash() self.assertEquals(updated, updated_again) mode = 2 h, updated_again = appcache.hash() self.assertEquals(hashlib.sha1("yay1").hexdigest(), h) self.assertNotEquals(updated, updated_again)
def test_finalize(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_urls("/static/static1.js") appcache.finalize() with self.assertRaises(RuntimeError): appcache.add_urls("/") # these exists.. # should never be recomputed. hash, updated = appcache.hash() self.assertTrue(hash) self.assertTrue(updated)
def test_route_registered(self): app = Flask(__name__) Appcache(app) found = False for rule in app.url_map.iter_rules(): if app.config["APPCACHE_URL"] == rule.rule: found = True self.assertTrue(found) app = Flask(__name__) app.config["APPCACHE_URL"] = "/yay/manifest.appcache" Appcache(app) for rule in app.url_map.iter_rules(): if app.config["APPCACHE_URL"] == rule.rule: found = True self.assertTrue(found)
def test_init_app(self): app = Flask(__name__) Appcache(app) self.assertEquals("manifest.appcache", app.config["APPCACHE_TEMPLATE"]) self.assertEquals("/manifest.appcache", app.config["APPCACHE_URL"]) self.assertEquals("http://localhost", app.config["APPCACHE_URL_BASE"]) app = Flask(__name__) appcache = Appcache() appcache.init_app(app) self.assertEquals("manifest.appcache", app.config["APPCACHE_TEMPLATE"]) self.assertEquals("/manifest.appcache", app.config["APPCACHE_URL"]) self.assertEquals("http://localhost", app.config["APPCACHE_URL_BASE"]) app = Flask(__name__) app.config["APPCACHE_URL"] = "/yay/manifest.appcache" Appcache(app) self.assertEquals("/yay/manifest.appcache", app.config["APPCACHE_URL"])
def test_cache_control(self): app = Flask(__name__) app.config["DEBUG"] = True appcache = Appcache(app) appcache.add_urls("/static/static1.js") client = app.test_client() response = client.get("/static/static1.js", follow_redirects=True) self.assertTrue("no-cache" in response.headers["Cache-Control"]) self.assertTrue("Expires" not in response.headers) app = Flask(__name__) app.config["DEBUG"] = False appcache = Appcache(app) appcache.add_urls("/static/static1.js") client = app.test_client() response = client.get("/static/static1.js", follow_redirects=True) self.assertTrue("must-revalidate" in response.headers["Cache-Control"]) self.assertTrue("Expires" not in response.headers)
def test_add_folders(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_folder("static", base="/static") urls = appcache.urls self.assertEquals(2, len(urls)) self.assertTrue("/static/static1.js" in urls) self.assertTrue("/static/static2.css" in urls) app = Flask(__name__) appcache = Appcache(app) appcache.add_folder("static", base="/media") urls = appcache.urls self.assertEquals(2, len(urls)) self.assertTrue("/media/static1.js" in urls) self.assertTrue("/media/static2.css" in urls)
{content} </script> """) for root, subdir, fnames in os.walk('static/partials'): for filename in fnames: if filename.endswith('.html'): with open(os.path.join(root, filename)) as f: content = f.read().decode('utf-8') content = unicode(content) PARTIALS += inline_partial.format(path=('/static/partials/' + filename), content=content) # Sets up Appcache appcache = Appcache(app) if app.debug: appcache.add_excluded_urls('/static/js/app.min.js', '/static/css/app.min.css') else: appcache.add_excluded_urls('/static/js/develop') appcache.add_excluded_urls('/static/css/develop') appcache.add_excluded_urls('/static/partials') appcache.add_excluded_urls('/static/js/tests') appcache.add_excluded_urls('/static/.webassets-cache') appcache.add_folder('static') appcache.add_urls('/meta.js', '/') @app.before_request
def test_excluded_urls(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_excluded_urls("/static/develop") appcache.add_urls("/static/develop/js.js") self.assertEquals(0, len(appcache.urls)) appcache.add_urls("/static/js.js") self.assertEquals(1, len(appcache.urls)) app = Flask(__name__) appcache = Appcache(app) appcache.add_excluded_urls("/static/ignored") appcache.add_folder("test_ignore", base="/static") urls = list(appcache.urls) self.assertEquals(1, len(urls)) self.assertEquals("/static/not_ignored", urls[0])
# fileHandler.setLevel(logging.INFO) # streamHandler = logging.StreamHandler() # streamHandler.setLevel(logging.INFO) # logger.addHandler(fileHandler) logger.info("-----------------\n---------------------\n-------------------") if os.uname()[4].startswith("arm"): import RPi.GPIO as GPIO redis_url = os.getenv('REDISTOGO_URL', 'redis://*****:*****@app.route('/device/<device_name>/<command>') def device(device_name, command): command = command.lower() device_name = device_name.lower() status_command = "status" projector_device = "projector" tuner_device = "tuner_"
def test_add_cached_urls(self): app = Flask(__name__) appcache = Appcache(app) appcache.add_urls("/") self.assertTrue("/" in appcache.urls)
import re import urllib import os # BeautifulSoup for legacy reasons. This code is old! from BeautifulSoup import BeautifulSoup from flask import Flask, render_template, jsonify, send_file from flask.ext.appcache import Appcache VERSION = "2.0" app = Flask(__name__) app.config.from_pyfile("settings.py") app.config.from_pyfile("settings_local.py", silent=True) appcache = Appcache(app) appcache.add_urls("/") appcache.add_folder(os.path.join(os.path.dirname(os.path.abspath(__file__)), "static"), base="/static") class AppURLOpener(urllib.FancyURLopener): version = "BetterSudburyBusTracker/{}".format(VERSION) urllib._urlopener = AppURLOpener() # Ogod. This is code from 2011! With some mods and commentary. class StopNotFoundError(Exception): pass class BusStop(object): """A stop object, hides all the details of interacting with the city's site""" # URL to get details about the stop