EOX GitLab Instance

Skip to content
Snippets Groups Projects
Commit f0771a9d authored by Fabian Schindler's avatar Fabian Schindler
Browse files

Adding pre/post handler support

parent 02fc7e64
No related branches found
No related tags found
2 merge requests!36Staging to master to prepare 1.0.0 release,!32Registrar modularization
......@@ -62,6 +62,35 @@ properties:
kwargs:
description: Constructor keyword arguments
type: object
# TODO: describe type specific args/kwargs
pre_handlers:
description: List of handlers to be run prior the registration of an item.
type: array
items:
description: A single pre-registration handler
type: object
properties:
path:
description: Python module path to the registration handler
type: string
args:
description: arguments for the initialization of the handler
type: array
kwargs:
description: keyword arguments for the initialization of the handler
type: object
post_handlers:
description: List of handlers to be run prior the registration of an item.
type: array
items:
description: A single pre-registration handler
type: object
properties:
path:
description: Python module path to the registration handler
type: string
args:
description: arguments for the initialization of the handler
type: array
kwargs:
description: keyword arguments for the initialization of the handler
type: object
import os.path
import textwrap
from datetime import datetime
from .context import Context
from .utils import isoformat
from .xml import escape
class ReportingPostHandler:
def __init__(self, service_url: str, reporting_dir: str):
self.service_url = service_url
self.reporting_dir = reporting_dir
def __call__(self, config: dict, path: str, context: Context):
inserted = datetime.now()
timestamp = inserted.strftime("%Y%m%dT%H%M%S")
with open(os.path.join(self.reporting_dir, 'item_%s_%s.xml' % (timestamp, context.identifier)), 'w') as f:
f.write(textwrap.dedent("""
<?xml version="1.0" encoding="UTF-8"?>
<DataAccessItemt
xsi:schemaLocation="http://www.telespazio.com/CSCDA/CDD/PDAS PDAS_interfaces%2020190924_1916.xsd"
xmlns="http://www.telespazio.com/CSCDA/CDD/PDAS"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<identifier>{identifier}</identifier>
<BROWSE_AVAILABILITY_DATETIME>{availability_time}</BROWSE_AVAILABILITY_DATETIME>
<URL>
<Service>WCS</Service>
<URL>{wms_capabilities_url}</URL>
</URL>
<URL>
<Service>WMS</Service>
<URL>{wcs_capabilities_url}</URL>
</URL>
</DataAccessItem>
""".format(
identifier=escape(context.identifier),
availability_time=escape(isoformat(inserted)),
wcs_capabilities_url=escape(
'%s/ows?service=wcs&request=GetCapabilities&cql=identifier="%s"'
% (self.service_url, context.identifier)
),
wms_capabilities_url=escape(
'%s/ows?service=wms&request=GetCapabilities&cql=identifier="%s"'
% (self.service_url, context.identifier)
),
)))
import re
import logging
import importlib
from .source import get_source
from .scheme import get_scheme
......@@ -39,8 +40,24 @@ def register_file(config: dict, path: str, replace: bool=False):
return context
def _get_handlers(config, name):
handlers = []
for handler_def in config.get(name, []):
module_path, handler_name = handler_def['path'].rpartition('.')
handler_cls = getattr(importlib.import_module(module_path), handler_name)
handlers.append(
handler_cls(
*handler_def.get('args', []),
**handler_def.get('kwargs', []),
)
)
return handlers
def get_pre_handlers(config):
return []
return _get_handlers(config, 'pre_handlers')
def get_post_handlers(config):
return []
return _get_handlers(config, 'post_handlers')
def isoformat(dt):
""" Formats a datetime object to an ISO string. Timezone naive datetimes are
are treated as UTC Zulu. UTC Zulu is expressed with the proper "Z"
ending and not with the "+00:00" offset declaration.
:param dt: the :class:`datetime.datetime` to encode
:returns: an encoded string
"""
if not dt.utcoffset():
dt = dt.replace(tzinfo=None)
return dt.isoformat("T") + "Z"
return dt.isoformat("T")
......@@ -4,6 +4,7 @@ from tempfile import gettempdir, gettempprefix
from dataclasses import dataclass, field
from typing import Union, Type, Optional, List, Callable, Any
import logging
from xml.sax.saxutils import escape
import lxml.etree
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment