EOX GitLab Instance

Commit 1be1be8a authored by Fabian Schindler's avatar Fabian Schindler
Browse files

Allowing to declare postprocessor by python path

parent 0e73be9b
Pipeline #19535 passed with stage
in 43 seconds
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Dict, Type from typing import Dict, Type
from .utils import import_by_path
class Postprocessor(ABC): class Postprocessor(ABC):
def __init__(self, **kwargs): def __init__(self, **kwargs):
...@@ -17,5 +19,9 @@ POSTPROCESSORS: Dict[str, Type[Postprocessor]] = { ...@@ -17,5 +19,9 @@ POSTPROCESSORS: Dict[str, Type[Postprocessor]] = {
def get_postprocessor(config: dict) -> Postprocessor: def get_postprocessor(config: dict) -> Postprocessor:
cls = POSTPROCESSORS[config.pop("type")] type_ = config.pop("type")
try:
cls = POSTPROCESSORS[type_]
except KeyError:
cls = import_by_path(type_)
return cls(**config) return cls(**config)
from typing import Iterator import importlib
from typing import Any, Iterator
from pygeofilter.backends.native.evaluate import NativeEvaluator from pygeofilter.backends.native.evaluate import NativeEvaluator
from pygeofilter.parsers.cql_json import parse as json_parse from pygeofilter.parsers.cql_json import parse as json_parse
...@@ -13,3 +14,14 @@ def cql_filter(_filter: dict, data: Iterator[dict]) -> Iterator[dict]: ...@@ -13,3 +14,14 @@ def cql_filter(_filter: dict, data: Iterator[dict]) -> Iterator[dict]:
nat_eval = NativeEvaluator(attribute_map=attr_map, use_getattr=False) nat_eval = NativeEvaluator(attribute_map=attr_map, use_getattr=False)
evaluator = nat_eval.evaluate(_filter) evaluator = nat_eval.evaluate(_filter)
yield from filter(evaluator, data) yield from filter(evaluator, data)
def import_by_path(path: str) -> Any:
"""Imports the object from the referenced module.
Args:
path (str): the dotted Python path, where the last element is the
object in the referenced module.
"""
module_path, _, object_name = path.rpartition(".")
return getattr(importlib.import_module(module_path), object_name)
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