def assets_env(app): env = Environment(app) env.url = "/static" # App Engine doesn't support automatic rebuilding. env.auto_build = False # This file needs to be shipped with your code. env.manifest = ('file:' + os.path.join(root_dir, 'webassets.manifest')) env.versions = 'hash:32' # create static bundles env.register('new_js', 'js/hiro.js', filters='yui_js', output="javascript/hiro.%(version)s.js") env.register('hoc_js', 'js/hoc.js', filters='yui_js', output="javascript/hoc.%(version)s.js") env.register('new_css', 'css/hiro.css', filters='cssmin', output="stylesheets/hiro.%(version)s.css") env.register('hoc_css', 'css/hoc.css', filters='cssmin', output="stylesheets/hoc.%(version)s.css") env.debug = app.config['DEBUG'] return env
def init_app(app): here, f = os.path.split(os.path.abspath(__file__)) # configure assets assets = Environment(app) assets.versions = 'hash' assets.directory = '%s/src' % here if app.debug == False: assets.auto_build = False # i have no idea why, but an arbitrary # second path segment is required here assets.url = '/static/turnip' # load asset bundles bundles = YAMLLoader("%s/src/assets.yaml" % here).load_bundles() [assets.register(name, bundle) for name, bundle in bundles.iteritems()]
def configure_assets(app): assets = Environment(app) assets.versions = 'hash' assets.url_expire = True # ----------------------------------------- # # Bundle the assets, using "normal" cache busting non_hljs_css = Bundle('private/css/bootswatch/journal/bootstrap.css', 'private/css/custom.css', output='public/css/non-hljs.%(version)s.css') hljs_css = Bundle('private/css/highlightjs-style.css', output='public/css/hljs.%(version)s.css') #font_ttf = Bundle('private/fonts/glyphicons-halflings-regular.ttf', # output='public/fonts/glyphicons-halflings-regular.ttf') assets.register('non_hljs_css', non_hljs_css) assets.register('hljs_css', hljs_css) #assets.register('font_ttf', font_ttf) return assets
from flask import Flask from flask.ext.assets import Environment from flask.ext.flatpages import FlatPages from flask_frozen import Freezer app = Flask(__name__) pages = FlatPages(app) freezer = Freezer(app, with_static_files=False) assets = Environment(app) assets.versions = "hash" __ALL__ = ("app", "assets", "freezer", "pages")
# static asset versioning and packaging assets = Environment(app) js = Bundle('jquery-1.11.0.js', 'bootstrap-3.1.1/dist/js/bootstrap.js', 'bootstrap-selectpicker/bootstrap-select.js', 'typeahead.bundle.js', 'lodash.min.js', 'can.jquery.js', 'modal.js', filters='rjsmin', output='build/tuxtrax-%(version)s.js') css = Bundle('ptrax.css', output='build/tuxtrax-%(version)s.css') assets.debug = constants.DEBUG assets.versions = "hash" assets.register('js_base', js) assets.register('css_base', css) @app.route('/dev') def index(): tags = [tag.name for tag in Tag.query.all()] resp = make_response( render_template('index.html', user=g.user, showhidden=False, tags=tags)) resp.set_cookie('submission_ver', str(submission_dataset_ver())) return resp
from flask.ext.migrate import Migrate, MigrateCommand from flask.ext.script import Manager from flask.ext.sqlalchemy import SQLAlchemy from webassets.loaders import PythonLoader import project.asset_bundles app = Flask(__name__) app.config.from_object('project.config') db = SQLAlchemy(app) migrate = Migrate(app, db) import routes # from models import * assets = Environment(app) assets.versions = "hash:32" assets.auto_build = app.config["SERVER_ENV"] == 'development' assets.debug = False bundles = PythonLoader(project.asset_bundles).load_bundles() for name, bundle in bundles.iteritems(): assets.register(name, bundle) manager = Manager(app) manager.add_command('db', MigrateCommand) manager.add_command('assets', ManageAssets) if __name__ == '__main__': manager.run()
import sass from flask.ext.assets import Environment, Bundle, register_filter from webassets_libsass import LibSass from .app import app register_filter(LibSass) assets = Environment(app) assets.versions = 'hash' assets.url = app.static_url_path styles = Bundle('css/*.scss', filters=["libsass", "cssmin"], output='gen/styles.css') scripts = Bundle('js/*.js', output='gen/scripts.js') assets.register("styles", styles) assets.register("scripts", scripts)
import flask as f from flask import Flask from flask.ext.assets import Environment, Bundle from raven.contrib.flask import Sentry import config.conf as conf app = Flask(__name__) app.config.from_object("web.config.conf") sentry = Sentry(dsn=conf.SENTRY_DSN) if not app.config["DEBUG_MODE"]: sentry.init_app(app) assets = Environment(app) assets.versions = 'hash:32' main_js = Bundle("main_bundle.js", output='dist/main_bundle.%(version)s.js') assets.register('js_all', main_js) all_css = Bundle("style.css", output='dist/main.%(version)s.css') assets.register('css_all', all_css) @app.before_request def something_before_request(): pass import controllers
from os.path import dirname, join, realpath, normpath from flask.ext.assets import Environment, Bundle from app import app environment = Environment(app) environment.versions = 'hash' manifest_path = normpath(realpath(join(dirname(__file__), '.static-manifest'))) environment.manifest = 'file:%s' % manifest_path environment.cache = False environment.debug = False css_main = Bundle('css/main.css', Bundle('less/main.less', filters='less'), filters='cssmin', output='css/main.%(version)s.css') js_head = Bundle( 'https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js', output='js/head.%(version)s.js') js_foot = Bundle( 'https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js', Bundle('js/scripts.js', filters='jsmin'), output='js/main.%(version)s.js')
def create_app(config=None, debug=False): ''' Inicializa la aplicación Flask. Carga los siguientes módulos: - index: página de inicio - page: páginas estáticas - user: gestión del usuario - files: búsqueda y obtención de ficheros - status: servicio de monitorización de la aplicación Y además, inicializa los siguientes servicios: - Configuración: carga valores por defecto y modifica con el @param config - Web Assets: compilación y compresión de recursos estáticos - i18n: detección de idioma en la URL y catálogos de mensajes - Cache y auth: Declarados en el módulo services - Files: Clases para acceso a datos ''' app = Flask(__name__) app.config.from_object(defaults) app.debug = debug app.session_interface = NoSessionInterface() # Configuración if config: app.config.from_object(config) # Runtime config app.config["DOWNLOADER_FILES"] = { k: os.path.join(os.path.abspath(os.path.join(app.root_path,"../downloads")), v) for k, v in app.config["DOWNLOADER_FILES"].iteritems() } # Gestión centralizada de errores if app.config["SENTRY_DSN"]: sentry.init_app(app) logging.getLogger().setLevel(logging.DEBUG if debug else logging.INFO) # Configuración dependiente de la versión del código revision_filename_path = os.path.join(os.path.dirname(app.root_path), "revision") if os.path.exists(revision_filename_path): f = open(revision_filename_path, "r") data = f.read() f.close() revisions = tuple( tuple(i.strip() for i in line.split("#")[0].split()) for line in data.strip().split("\n") if line.strip() and not line.strip().startswith("#")) revision_hash = md5(data).hexdigest() app.config.update( CACHE_KEY_PREFIX = "%s%s/" % ( app.config["CACHE_KEY_PREFIX"] if "CACHE_KEY_PREFIX" in app.config else "", revision_hash ), REVISION_HASH = revision_hash, REVISION = revisions ) else: app.config.update( REVISION_HASH = None, REVISION = () ) # Registra valores/funciones para plantillas app.jinja_env.globals["u"] = u # Blueprints print "Registering blueprints." app.register_blueprint(index) register_files_converters(app) app.register_blueprint(news) app.register_blueprint(files) for blueprint in downloader_blueprints: app.register_blueprint(blueprint) # Registra filtros de plantillas register_filters(app) # Web Assets print "Web assets." if not os.path.isdir(app.static_folder+"/gen"): os.mkdir(app.static_folder+"/gen") assets = Environment(app) app.assets = assets assets.debug = app.debug assets.versions = "timestamp" register_filter(JsSlimmer) register_filter(CssSlimmer) assets.register('css_torrents', Bundle('main.css', 'jquery.treeview.css', filters='pyscss', output='gen/main.css', debug=False), '960_24_col.css', filters='css_slimmer', output='gen/torrents.css') assets.register('js_torrents', Bundle('modernizr.js', 'jquery.js', 'jquery.treeview.js', 'jquery.cookie.js', 'jquery.colorbox-min.js', 'torrents.js', 'cookies.js', filters='rjsmin', output='gen/torrents.js'), ) print "Initializing services." # CSRF protection csrf.init_app(app) # Traducciones babel.init_app(app) @babel.localeselector def get_locale(): return "en" # Cache cache.init_app(app) # Mail mail.init_app(app) print "Database accesses:" # Acceso a bases de datos filesdb.init_app(app) print "* files" torrentsdb.init_app(app, searchd) print "* torrents" pagesdb.init_app(app) print "* pages" #feedbackdb.init_app(app) #print "* feedback" configdb.init_app(app) print "* config" entitiesdb.init_app(app) print "* entities" for service_name, params in app.config["DATA_SOURCE_SHARING"].iteritems(): eval(service_name).share_connections(**{key:eval(value) for key, value in params.iteritems()}) configdb.register_action("flush_cache", cache.clear, _unique=True) print "Blacklists." # Blacklists if app.debug: blacklists.debug=True blacklist_data = torrentsdb.get_blacklists() blacklists.load_data(blacklist_data) blacklists.clone_into(blacklists_adult, lambda x:x!="misconduct") def refresh_blacklists(): ''' Refresh blacklists. ''' blacklists.load_data(torrentsdb.get_blacklists()) configdb.register_action("refresh_blacklists", refresh_blacklists) def update_downloader_properties(): ''' Downloader updated. ''' local_cache["downloader_properties"] = get_downloader_properties() configdb.register_action("update_downloader", update_downloader_properties) with app.app_context(): local_cache["downloader_properties"] = get_downloader_properties() # IPs españolas spanish_ips.load(os.path.join(os.path.dirname(app.root_path),app.config["SPANISH_IPS_FILENAME"])) @app.before_first_request def init_process(): if not eventmanager.is_alive(): # Fallback inicio del eventManager eventmanager.start() print "Event manager." # Profiler # profiler.init_app(app, feedbackdb) # Servicio de búsqueda eventmanager.once(searchd.init_app, hargs=(app, filesdb, entitiesdb, profiler)) # Refresco de conexiones eventmanager.once(filesdb.load_servers_conn) eventmanager.interval(app.config["FOOCONN_UPDATE_INTERVAL"], filesdb.load_servers_conn) # Refresco de configuración eventmanager.once(configdb.pull) eventmanager.interval(app.config["CONFIG_UPDATE_INTERVAL"], configdb.pull) # Categorias categories_cache.init_app(app.config["TORRENTS_CATEGORIES"], app.config["SUBCATEGORIES_MIN_OCCURS"]) categories_cache.update_subcategories(torrentsdb.get_subcategories()) def refresh_subcategories(): ''' Refresh subcategories. ''' categories_cache.update_subcategories(torrentsdb.get_subcategories()) configdb.register_action("refresh_subcategories", refresh_subcategories) eventmanager.interval(app.config["CATEGORIES_REFRESH_INTERVAL"], refresh_subcategories) @app.before_request def before_request(): g.cache_code = "" g.last_modified = None # No preprocesamos la peticiones a static if request.path.startswith("/static/"): g.must_cache = False return g.blacklisted_content = False init_g(current_app) if not g.domain: return multidomain.redirect_to_domain(g.domains_family[0], 301) # ignora peticiones sin blueprint if request.blueprint is None and request.path.endswith("/"): if "?" in request.url: root = request.url_root[:-1] path = request.path.rstrip("/") query = request.url if not isinstance(query, unicode): query = query.decode("utf-8") query = query[query.find(u"?"):] return redirect(root+path+query, 301) return redirect(request.url.rstrip("/"), 301) @app.after_request def after_request(response): if request.user_agent.browser == "msie": response.headers["X-UA-Compatible"] = "IE-edge" if g.must_cache: response.headers["X-Cache-Control"] = "max-age=%d"%g.must_cache if g.cache_code: response.headers["X-Cache-Code"] = g.cache_code if g.last_modified: response.headers["Last-Modified"] = utils.formatdate(time.mktime(max(g.last_modified, current_app.config["APP_LAST_MODIFIED"]).timetuple()), False, True) return response # Páginas de error errors = { 404: ("Page not found", "The requested address does not exists."), 410: ("File not available", "The file you are trying to access has been removed."), 500: ("An error happened", "We had some problems displaying this page. Maybe later we can show it to you."), 503: ("Service unavailable", "This page is temporarily unavailable. Please try again later.") } @allerrors(app, 400, 401, 403, 404, 405, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 500, 501, 502, 503) def all_errors(e): error = e.code if hasattr(e,"code") else 500 title, description = errors[error if error in errors else 500] init_g(current_app) if not g.domain: return multidomain.redirect_to_domain(g.domains_family[0], 301) g.title.append(title) return render_template('error.html', code=str(error), title=title, description=description), error print "App ready." return app
from flask.ext.babel import gettext, ngettext from flask_frozen import Freezer from htmlmin.main import minify import functools _basedir = os.path.abspath(os.path.dirname(__file__)) app = Flask(__name__) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 43200 * 20 # 20 days app.register_blueprint(API) db = SQLAlchemy(app) app.config.from_pyfile('apihoyodecrimen.cfg') cache.init_app(app) assets = Environment(app) assets.versions = 'hash' # use the last modified timestamp babel = Babel(app) app.config['FREEZER_STATIC_IGNORE'] = ['/api/v1/*'] freezer = Freezer(app) def uglify(route_function): @functools.wraps(route_function) def wrapped(*args, **kwargs): yielded_html = route_function(*args, **kwargs) minified_html = minify(yielded_html) return minified_html return wrapped
import spamsub.models app.config.from_pyfile('config/common.py') # actual CSRF secret key and github token go in here try: app.config.from_pyfile('config/sensitive.py') except IOError: pass if os.getenv('SPAMSUB_CONFIGURATION'): app.config.from_envvar('SPAMSUB_CONFIGURATION') # attach assets assets = Environment(app) assets.versions = 'hash' manifest_path = os.path.realpath( os.path.join(os.path.dirname(__file__), '.static-manifest')) assets.manifest = 'file://%s' % manifest_path bundles = YAMLLoader(os.path.realpath( os.path.join(os.path.dirname(__file__), 'assets.yml'))).load_bundles() for bundle_name, bundle in bundles.items(): assets.register(bundle_name, bundle) ## import our own blueprints here if necessary # from apps.foo.views import foo_app # attach any blueprints # app.register_blueprint(foo_app, url_prefix='/foo')
from os.path import dirname, join, realpath, normpath from flask.ext.assets import Environment, Bundle from app import app environment = Environment(app) environment.versions = 'hash' manifest_path = normpath(realpath(join(dirname(__file__), '.static-manifest'))) environment.manifest = 'file:%s' % manifest_path environment.cache = False environment.debug = False css_main = Bundle( 'css/main.css', Bundle('less/main.less', filters='less'), filters='cssmin', output='css/main.%(version)s.css' ) js_head = Bundle( 'https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js', output='js/head.%(version)s.js' ) js_foot = Bundle( 'https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js', Bundle( 'js/scripts.js',
application.jinja_env.add_extension('jinja2.ext.do') application.jinja_env.add_extension('jinja2.ext.loopcontrols') application.jinja_env.add_extension('jinja2.ext.with_') application.jinja_env.globals['url_for'] = url_for_ assets = Environment(application) assets.cache = False assets.debug = application.config['DEBUG'] assets.directory = application.static_folder assets.manifest = 'json:assets/versions.json' assets.url = application.static_url_path assets.url_expire = True assets.versions = 'timestamp' assets.register( 'stylesheets', Bundle( Bundle('stylesheets/all.less', filters='less', output='stylesheets/all.css'), filters=None if application.config['DEBUG'] else 'cssmin,cssrewrite', output='assets/compressed.css')) assets.register( 'javascripts', Bundle('vendors/lodash/dist/lodash.js', 'vendors/jquery/dist/jquery.js', 'vendors/angular/angular.js', 'vendors/angular-route/angular-route.js',
application.jinja_env.add_extension('jinja2.ext.do') application.jinja_env.add_extension('jinja2.ext.loopcontrols') application.jinja_env.add_extension('jinja2.ext.with_') application.jinja_env.globals['url_for'] = url_for_ assets = Environment(application) assets.cache = False assets.debug = application.config['DEBUG'] assets.directory = application.static_folder assets.manifest = 'json:assets/versions.json' assets.url = application.static_url_path assets.url_expire = True assets.versions = 'timestamp' assets.register('stylesheets', Bundle( Bundle( 'stylesheets/all.less', filters='less', output='stylesheets/all.css' ), filters=None if application.config['DEBUG'] else 'cssmin,cssrewrite', output='assets/compressed.css' )) assets.register('javascripts', Bundle( 'vendors/lodash/dist/lodash.js', 'vendors/jquery/dist/jquery.js', 'vendors/angular/angular.js', 'vendors/angular-route/angular-route.js',