Source code for shapelib.factories.registry

from collections.abc import Callable
from typing import Any

from shapelib.factories.geo_file_factory import GeoFileFactory


[docs] def register_handler(file_extension: str) -> Callable[[type[Any]], type[Any]]: """ A decorator to register a geospatial file handler class with the GeoFileFactory. This decorator function associates a file extension (e.g., ".shp", ".geojson") with a handler class in the `GeoFileFactory`. The handler class is registered to process files with the specified extension. Parameters ---------- file_extension : str The file extension that the handler class can process (e.g., ".shp"). Returns ------- Callable[[Type[Any]], Type[Any]] A decorator function that registers the handler class with `GeoFileFactory`. Examples -------- >>> @register_handler(".shp") >>> class ShapefileHandler(GeoFileHandler): >>> # Class implementation here >>> pass """ def decorator(handler_class: type[Any]) -> type[Any]: """ Registers the handler class with the GeoFileFactory. This function adds the handler class to the factory's internal registry for the given file extension, allowing the factory to retrieve the handler based on the file extension. Parameters ---------- handler_class : Type[Any] The handler class to be registered. Returns ------- Type[Any] The registered handler class. Examples -------- >>> @register_handler(".geojson") >>> class GeoJSONHandler(GeoFileHandler): >>> # Class implementation here >>> pass """ GeoFileFactory.register_handler(file_extension, handler_class) return handler_class return decorator