Commit 99610d20 authored by Christophe Benz's avatar Christophe Benz

Allow loading catalog from a file too

parent 7fc79f53
...@@ -31,26 +31,22 @@ def generate_schema_from_url_func(session): ...@@ -31,26 +31,22 @@ def generate_schema_from_url_func(session):
return tableschema_from_url return tableschema_from_url
def is_http_url(ref) -> bool:
return isinstance(ref, str) and re.match("https?://", ref)
class SchemaCatalogRegistry: class SchemaCatalogRegistry:
"""Retain section_name -> catalog url matching """Retain section_name -> catalog url matching
and creates SchemaCatalog instance on demand""" and creates SchemaCatalog instance on demand"""
def __init__(self, session): def __init__(self, session):
self.session = session self.session = session
self.url_map = {} self.ref_map = {}
def add_ref(self, name, url): def add_ref(self, name, ref):
self.url_map[name] = url self.ref_map[name] = ref
def build_schema_catalog(self, name): def build_schema_catalog(self, name):
if name in self.url_map: if name in self.ref_map:
catalog_url = self.url_map[name] ref = self.ref_map[name]
try: try:
catalog = opendataschema.SchemaCatalog(catalog_url, session=self.session) catalog = opendataschema.SchemaCatalog(ref, session=self.session)
except requests.exceptions.RequestException as exc: except requests.exceptions.RequestException as exc:
log.exception(exc) log.exception(exc)
return None return None
...@@ -69,7 +65,7 @@ if config.HOMEPAGE_CONFIG: ...@@ -69,7 +65,7 @@ if config.HOMEPAGE_CONFIG:
name = section['name'] name = section['name']
log.info('Initializing homepage section "{}"...'.format(name)) log.info('Initializing homepage section "{}"...'.format(name))
catalog_ref = section.get('catalog') catalog_ref = section.get('catalog')
if is_http_url(catalog_ref): if catalog_ref:
schema_catalog_registry.add_ref(name, catalog_ref) schema_catalog_registry.add_ref(name, catalog_ref)
log.info("...done") log.info("...done")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment