__init__.py 1.31 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 31 32 33
# super ugly way to access catalog_toml url
schema_catalog_url = ui_config['sections'][0]['catalog']
table_schema_catalog = opendataschema.TableSchemaCatalog(schema_catalog_url, download_func=download_with_cache)

34 35

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

39

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

44

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


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