__init__.py 1.44 KB
Newer Older
Pierre Dittgen's avatar
Pierre Dittgen committed
1
import json
Christophe Benz's avatar
Christophe Benz committed
2
import logging
3
import os
4
from pathlib import Path
5
from urllib.parse import quote_plus
6

7 8
import flask
import jinja2
9 10 11 12
import requests
import tableschema
from cachetools.func import ttl_cache

13
import opendataschema
14

15
from . import config
16

Christophe Benz's avatar
Christophe Benz committed
17 18
log = logging.getLogger(__name__)

19

20
@ttl_cache(maxsize=50, ttl=5*60)
21 22 23 24
def download_with_cache(url):
    return requests.get(url)


25
@ttl_cache(maxsize=24, ttl=5*60)
26 27 28 29
def schema_from_url(url):
    return tableschema.Schema(url)


Pierre Dittgen's avatar
Pierre Dittgen committed
30 31
# And load schema catalogs which urls are found in config.json
schema_catalog_map = {}
32
if config.HOMEPAGE_CONFIG:
Christophe Benz's avatar
Christophe Benz committed
33
    log.info("Initializing homepage sections...")
34 35 36 37 38
    for section in config.HOMEPAGE_CONFIG['sections']:
        if isinstance(section['catalog'], str) and section['catalog'].startswith('http'):
            code = section['code']
            url = section['catalog']
            schema_catalog_map[code] = opendataschema.SchemaCatalog(url, download_func=download_with_cache)
39 40

# Flask things
41
app = flask.Flask(__name__)
42 43
app.secret_key = config.SECRET_KEY

44

45 46 47 48
# Jinja2 url_quote_plus custom filter
# https://stackoverflow.com/questions/12288454/how-to-import-custom-jinja2-filters-from-another-file-and-using-flask
blueprint = flask.Blueprint('filters', __name__)

49

50 51 52 53 54 55
@jinja2.contextfilter
@blueprint.app_template_filter()
def urlencode(context, value):
    return quote_plus(value)


56
# Keep this import after app initialisation (to avoid cyclic imports)
57
from . import views  # noqa isort:skip