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

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

13

14 15
# Let this import after app initialisation
from . import config
16

17 18 19 20 21 22 23 24 25 26 27 28

@ttl_cache(maxsize=20*1024, ttl=5*60)
def download_with_cache(url):
    return requests.get(url)


@ttl_cache(maxsize=20*1024, ttl=5*60)
def schema_from_url(url):
    return tableschema.Schema(url)


# load config.json and catalog_schema_toml
Pierre Dittgen's avatar
Pierre Dittgen committed
29
ui_config = json.load(config.UI_CONFIG_FILE.open('rt', encoding='utf-8')) if config.UI_CONFIG_FILE else []
30
# super ugly way to access catalog_toml url
Pierre Dittgen's avatar
Pierre Dittgen committed
31
# TODO: improve it
32
schema_catalog_url = ui_config['sections'][0]['catalog']
33
table_schema_catalog = opendataschema.SchemaCatalog(schema_catalog_url, download_func=download_with_cache)
34

35 36

# Flask things
37
app = flask.Flask(__name__)
38 39
app.secret_key = config.SECRET_KEY

40

41 42 43 44
# 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__)

45

46 47 48 49 50 51
@jinja2.contextfilter
@blueprint.app_template_filter()
def urlencode(context, value):
    return quote_plus(value)


52 53
# Keep this import after app initialisation (to avoid cyclic imports)
from . import views  # isort:skip