EOX GitLab Instance

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

Fixing linting and flake8 issues

parent e7d6fd16
No related branches found
No related tags found
1 merge request!1Adding path registration capabilities
Pipeline #19061 failed
...@@ -17,12 +17,12 @@ def get_backends(config: dict) -> List[ItemBackend]: ...@@ -17,12 +17,12 @@ def get_backends(config: dict) -> List[ItemBackend]:
backends: List[ItemBackend] = [] backends: List[ItemBackend] = []
for cfg_backend in config["backends"]: for cfg_backend in config["backends"]:
# construct backend # construct backend
backend_cls = import_by_path(cfg_backend['path']) backend_cls = import_by_path(cfg_backend["path"])
backends.append( backends.append(
backend_cls( backend_cls(
*cfg_backend.get('args', []), *cfg_backend.get("args", []),
**cfg_backend.get('kwargs', {}), **cfg_backend.get("kwargs", {}),
) )
) )
...@@ -40,9 +40,9 @@ def get_path_backends(config: dict) -> List[PathBackend]: ...@@ -40,9 +40,9 @@ def get_path_backends(config: dict) -> List[PathBackend]:
carried out carried out
""" """
backends = [] backends = []
for cfg_backend in config['path_backends']: for cfg_backend in config["path_backends"]:
# construct backend # construct backend
backend_cls = import_by_path(cfg_backend['path']) backend_cls = import_by_path(cfg_backend["path"])
backends.append( backends.append(
backend_cls( backend_cls(
......
...@@ -4,19 +4,17 @@ from typing import TYPE_CHECKING, Optional ...@@ -4,19 +4,17 @@ from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING: if TYPE_CHECKING:
from pystac import Item from pystac import Item
from ..source import Source
class ItemBackend(ABC): class ItemBackend(ABC):
def __repr__(self) -> str: def __repr__(self) -> str:
return f"<{self.__class__.__name__}>" return f"<{self.__class__.__name__}>"
@abstractmethod @abstractmethod
def item_exists(self, item: 'Item') -> bool: def item_exists(self, item: "Item") -> bool:
... ...
@abstractmethod @abstractmethod
def register_item(self, item: 'Item'): def register_item(self, item: "Item"):
... ...
@abstractmethod @abstractmethod
......
...@@ -15,7 +15,7 @@ from urllib.parse import urlparse ...@@ -15,7 +15,7 @@ from urllib.parse import urlparse
import django import django
from django.db import transaction from django.db import transaction
# from django.db.models import Q from django.db.models import Q
if TYPE_CHECKING: if TYPE_CHECKING:
from pystac import Item from pystac import Item
...@@ -313,12 +313,11 @@ class EOxServerBackend(Backend): ...@@ -313,12 +313,11 @@ class EOxServerBackend(Backend):
""" """
# ugly, ugly hack # ugly, ugly hack
from eoxserver.resources.coverages import models from eoxserver.resources.coverages import models
try: try:
logger.info(f"Deregistering product '{identifier}'") logger.info(f"Deregistering product '{identifier}'")
product = models.Product.objects.get(identifier=identifier) product = models.Product.objects.get(identifier=identifier)
grids = list(models.Grid.objects.filter( grids = list(models.Grid.objects.filter(coverage__parent_product=product))
coverage__parent_product=product
))
product.delete() product.delete()
# clean up grids # clean up grids
...@@ -331,15 +330,11 @@ class EOxServerBackend(Backend): ...@@ -331,15 +330,11 @@ class EOxServerBackend(Backend):
if grid and not grid.name and not grid_used: if grid and not grid.name and not grid_used:
grid.delete() grid.delete()
except models.Product.DoesNotExist: except models.Product.DoesNotExist:
logger.info( logger.info(f"No product with identifier '{identifier}' found")
f"No product with identifier '{identifier}' found"
)
# no product found with that id # no product found with that id
# return empty list # return empty list
return None return None
logger.info( logger.info(f"Deregistered product with identifier '{identifier}'")
f"Deregistered product with identifier '{identifier}'"
)
# return the deleted identifier # return the deleted identifier
return identifier return identifier
from os.path import join, dirname from os.path import join, dirname
import logging.config import logging.config
from typing import TextIO
import click import click
import yaml import yaml
import jsonschema import jsonschema
from .registrar import ( from .registrar import (
deregister_path, register_item, register_path, deregister_item, deregister_path,
deregister_identifier register_item,
register_path,
deregister_item,
deregister_identifier,
) )
from .daemon import run_daemon from .daemon import run_daemon
from .config import load_config from .config import load_config
...@@ -48,23 +50,33 @@ def cli(): ...@@ -48,23 +50,33 @@ def cli():
pass pass
@cli.command(help='Run the registrar daemon, attaching to a Redis queue') @cli.command(help="Run the registrar daemon, attaching to a Redis queue")
@click.option('--config-file', type=click.File('r')) @click.option("--config-file", type=click.File("r"))
@click.option('--validate', is_flag=True) @click.option("--validate", is_flag=True)
@click.option('--replace', is_flag=True) @click.option("--replace", is_flag=True)
@click.option('--host', type=str) @click.option("--host", type=str)
@click.option('--port', type=int) @click.option("--port", type=int)
@click.option('--listen-queue', type=str) @click.option("--listen-queue", type=str)
@click.option('--deregister-queue', type=str) @click.option("--deregister-queue", type=str)
@click.option('--progress-set', type=str) @click.option("--progress-set", type=str)
@click.option('--failure-set', type=str) @click.option("--failure-set", type=str)
@click.option('--success-set', type=str) @click.option("--success-set", type=str)
@click.option('--extra/-e', type=str, multiple=True, default=[]) @click.option("--extra/-e", type=str, multiple=True, default=[])
@click.option('--debug', is_flag=True) @click.option("--debug", is_flag=True)
def daemon(config_file=None, validate=False, replace=False, host=None, def daemon(
port=None, listen_queue=None, progress_set=None, config_file=None,
failure_set=None, success_set=None, validate=False,
deregister_queue=None, extra=None, debug=False): replace=False,
host=None,
port=None,
listen_queue=None,
progress_set=None,
failure_set=None,
success_set=None,
deregister_queue=None,
extra=None,
debug=False,
):
""" Run the registrar daemon to listen on the given queues """ Run the registrar daemon to listen on the given queues
and execute the (de-)registrations commands. and execute the (de-)registrations commands.
...@@ -96,25 +108,33 @@ def daemon(config_file=None, validate=False, replace=False, host=None, ...@@ -96,25 +108,33 @@ def daemon(config_file=None, validate=False, replace=False, host=None,
handlers[deregister_queue] = deregister_item handlers[deregister_queue] = deregister_item
for extra_handler in extra: for extra_handler in extra:
queue, _, path = extra_handler.partition('=') queue, _, path = extra_handler.partition("=")
handler = import_by_path(path.strip()) handler = import_by_path(path.strip())
handlers[queue.strip()] = handler handlers[queue.strip()] = handler
run_daemon( run_daemon(
config, replace, host, port, handlers, progress_set, config,
failure_set, success_set, deregister_queue replace,
host,
port,
handlers,
progress_set,
failure_set,
success_set,
deregister_queue,
) )
@cli.command(help='Run a single, one-off registration') @cli.command(help="Run a single, one-off registration")
@click.argument('item', type=str) @click.argument("item", type=str)
@click.option('--config-file', type=click.File('r')) @click.option("--config-file", type=click.File("r"))
@click.option('--is-path', is_flag=True) @click.option("--is-path", is_flag=True)
@click.option('--validate', is_flag=True) @click.option("--validate", is_flag=True)
@click.option('--replace', is_flag=True) @click.option("--replace", is_flag=True)
@click.option('--debug', is_flag=True) @click.option("--debug", is_flag=True)
def register(item, is_path=False, config_file=None, validate=False, def register(
replace=False, debug=False): item, is_path=False, config_file=None, validate=False, replace=False, debug=False
):
setup_logging(debug) setup_logging(debug)
config = load_config(config_file) config = load_config(config_file)
if validate: if validate:
...@@ -126,14 +146,15 @@ def register(item, is_path=False, config_file=None, validate=False, ...@@ -126,14 +146,15 @@ def register(item, is_path=False, config_file=None, validate=False,
register_path(config, item, replace) register_path(config, item, replace)
@cli.command(help='Run a single, one-off de-registration') @cli.command(help="Run a single, one-off de-registration")
@click.argument('--path', type=str) @click.argument("--path", type=str)
@click.argument('--identifier', type=str) @click.argument("--identifier", type=str)
@click.option('--config-file', type=click.File('r')) @click.option("--config-file", type=click.File("r"))
@click.option('--validate', is_flag=True) @click.option("--validate", is_flag=True)
@click.option('--debug', is_flag=True) @click.option("--debug", is_flag=True)
def deregister(file_path=None, identifier=None, config_file=None, def deregister(
validate=False, debug=False): file_path=None, identifier=None, config_file=None, validate=False, debug=False
):
setup_logging(debug) setup_logging(debug)
config = load_config(config_file) config = load_config(config_file)
if validate: if validate:
...@@ -145,5 +166,5 @@ def deregister(file_path=None, identifier=None, config_file=None, ...@@ -145,5 +166,5 @@ def deregister(file_path=None, identifier=None, config_file=None,
deregister_identifier(config, identifier) deregister_identifier(config, identifier)
if __name__ == '__main__': if __name__ == "__main__":
cli() cli()
...@@ -19,10 +19,17 @@ logger = logging.getLogger(__name__) ...@@ -19,10 +19,17 @@ logger = logging.getLogger(__name__)
QueueItemHandler = Callable[[dict, str, bool], None] QueueItemHandler = Callable[[dict, str, bool], None]
def run_daemon(config: dict, replace: bool, host: str, port: int, def run_daemon(
handlers: Dict[str, QueueItemHandler], config: dict,
progress_set: str, failure_set: str, success_set: str): replace: bool,
""" Run the registrar daemon, listening on a redis queue host: str,
port: int,
handlers: Dict[str, QueueItemHandler],
progress_set: str,
failure_set: str,
success_set: str,
):
"""Run the registrar daemon, listening on a redis queue
for items to be (de-)registered. for items to be (de-)registered.
Args: Args:
...@@ -41,9 +48,7 @@ def run_daemon(config: dict, replace: bool, host: str, port: int, ...@@ -41,9 +48,7 @@ def run_daemon(config: dict, replace: bool, host: str, port: int,
""" """
# initialize the queue client # initialize the queue client
client = redis.Redis( client = redis.Redis(host=host, port=port, charset="utf-8", decode_responses=True)
host=host, port=port, charset="utf-8", decode_responses=True
)
queue_names = list(handlers.keys()) queue_names = list(handlers.keys())
logger.debug( logger.debug(
f"waiting on items on redis " f"waiting on items on redis "
......
...@@ -34,9 +34,7 @@ class ReportingPostHandler: ...@@ -34,9 +34,7 @@ class ReportingPostHandler:
def __call__(self, config: dict, item: "Item"): def __call__(self, config: dict, item: "Item"):
inserted = datetime.now() inserted = datetime.now()
timestamp = inserted.strftime("%Y%m%dT%H%M%S") timestamp = inserted.strftime("%Y%m%dT%H%M%S")
sanitized_filename = self.sanitize_path( sanitized_filename = self.sanitize_path(f"item_{timestamp}_{item.id}.xml")
f'item_{timestamp}_{item.id}.xml'
)
filename = os.path.join(self.reporting_dir, sanitized_filename) filename = os.path.join(self.reporting_dir, sanitized_filename)
logger.info(f"Generating report for product {item.id} at {filename}") logger.info(f"Generating report for product {item.id} at {filename}")
...@@ -76,23 +74,6 @@ class ReportingPostHandler: ...@@ -76,23 +74,6 @@ class ReportingPostHandler:
<Service>WMS</Service> <Service>WMS</Service>
<URL>{wms_capabilities_url}</URL> <URL>{wms_capabilities_url}</URL>
</URL> </URL>
</DataAccessItem>\ </DataAccessItem>"""
""".format( )
identifier=escape(item.id), )
availability_time=escape(isoformat(inserted)),
wcs_capabilities_url=escape(
'%s/ows?service=wcs&request=GetCapabilities'
'&cql=identifier="%s_%s"'
% (
self.service_url, item.id, [
i
for i in item.assets.keys() if i != "gsc_metadata"
][0]
)
),
wms_capabilities_url=escape(
'%s/ows?service=wms&request=GetCapabilities'
'&cql=identifier="%s"'
% (self.service_url, item.id)
),
)))
...@@ -90,9 +90,7 @@ def register_path(config: dict, path: str, replace: bool = False): ...@@ -90,9 +90,7 @@ def register_path(config: dict, path: str, replace: bool = False):
logger.info(f"Replacing '{path!r}'.") logger.info(f"Replacing '{path!r}'.")
backend.register_path(source, path, replace=True) backend.register_path(source, path, replace=True)
else: else:
raise RegistrationError( raise RegistrationError(f"Path {path!r} is already registered")
f'Path {path!r} is already registered'
)
else: else:
logger.info(f"Registering '{path!r}'.") logger.info(f"Registering '{path!r}'.")
backend.register_path(source, path, replace=False) backend.register_path(source, path, replace=False)
...@@ -105,8 +103,7 @@ def register_path(config: dict, path: str, replace: bool = False): ...@@ -105,8 +103,7 @@ def register_path(config: dict, path: str, replace: bool = False):
post_handler(config, path) post_handler(config, path)
logger.info( logger.info(
f"Successfully {'replaced' if replace else 'registered'} " f"Successfully {'replaced' if replace else 'registered'} " f"'{path!r}'"
f"'{path!r}'"
) )
...@@ -121,8 +118,7 @@ def deregister_identifier(config, identifier: str, _: bool = False): ...@@ -121,8 +118,7 @@ def deregister_identifier(config, identifier: str, _: bool = False):
return _deregister_identifier(config, identifier) return _deregister_identifier(config, identifier)
def _deregister_identifier(config, identifier: str, def _deregister_identifier(config, identifier: str, item: Optional[dict] = None):
item: Optional[dict] = None):
for pre_handler in get_deregister_pre_handlers(config): for pre_handler in get_deregister_pre_handlers(config):
pre_handler(config, identifier, item) pre_handler(config, identifier, item)
...@@ -187,15 +183,15 @@ def get_error_handlers(config): ...@@ -187,15 +183,15 @@ def get_error_handlers(config):
def get_path_pre_handlers(config): def get_path_pre_handlers(config):
return _get_handlers(config, 'path_pre_handlers') return _get_handlers(config, "path_pre_handlers")
def get_path_post_handlers(config): def get_path_post_handlers(config):
return _get_handlers(config, 'path_post_handlers') return _get_handlers(config, "path_post_handlers")
def get_path_error_handlers(config): def get_path_error_handlers(config):
return _get_handlers(config, 'path_error_handlers') return _get_handlers(config, "path_error_handlers")
def get_deregister_pre_handlers(config): def get_deregister_pre_handlers(config):
...@@ -207,16 +203,16 @@ def get_deregister_post_handlers(config): ...@@ -207,16 +203,16 @@ def get_deregister_post_handlers(config):
def get_deregister_error_handlers(config): def get_deregister_error_handlers(config):
return _get_handlers(config, 'deregister_error_handlers') return _get_handlers(config, "deregister_error_handlers")
def get_deregister_path_pre_handlers(config): def get_deregister_path_pre_handlers(config):
return _get_handlers(config, 'deregister_path_pre_handlers') return _get_handlers(config, "deregister_path_pre_handlers")
def get_deregister_path_post_handlers(config): def get_deregister_path_post_handlers(config):
return _get_handlers(config, 'deregister_path_post_handlers') return _get_handlers(config, "deregister_path_post_handlers")
def get_deregister_path_error_handlers(config): def get_deregister_path_error_handlers(config):
return _get_handlers(config, 'deregister_path_error_handlers') return _get_handlers(config, "deregister_path_error_handlers")
...@@ -12,7 +12,6 @@ import shutil ...@@ -12,7 +12,6 @@ import shutil
from glob import glob from glob import glob
from fnmatch import fnmatch from fnmatch import fnmatch
import logging import logging
from urllib.parse import urlparse
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
...@@ -36,13 +35,13 @@ class RegistrationError(Exception): ...@@ -36,13 +35,13 @@ class RegistrationError(Exception):
pass pass
class Source: class Source(ABC):
def __init__(self, name: str = None, endpoint_url: str = ""): def __init__(self, name: str = None, endpoint_url: str = ""):
self.name = name self.name = name
self.endpoint_url = endpoint_url self.endpoint_url = endpoint_url
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<{self.__class__.__name__} name={self.name}>' return f"<{self.__class__.__name__} name={self.name}>"
@abstractmethod @abstractmethod
def get_container_and_path(self, path: str): def get_container_and_path(self, path: str):
...@@ -382,10 +381,8 @@ def get_source(config: dict, item: "Item") -> Source: ...@@ -382,10 +381,8 @@ def get_source(config: dict, item: "Item") -> Source:
# no source found # no source found
raise RegistrationError(f"Could not find a suitable source for {item!r}") raise RegistrationError(f"Could not find a suitable source for {item!r}")
return SOURCE_TYPES[cfg_source['type']]( return SOURCE_TYPES[cfg_source["type"]](
cfg_source['name'], cfg_source["name"], *cfg_source.get("args", []), **cfg_source.get("kwargs", {})
*cfg_source.get('args', []),
**cfg_source.get('kwargs', {})
) )
...@@ -402,20 +399,18 @@ def get_source_from_path(config, path: str) -> Optional[Source]: ...@@ -402,20 +399,18 @@ def get_source_from_path(config, path: str) -> Optional[Source]:
Returns: Returns:
Source: source of the data Source: source of the data
""" """
cfg_sources = config['sources'] cfg_sources = config["sources"]
for cfg_source in cfg_sources: for cfg_source in cfg_sources:
if cfg_source.get('filter'): if cfg_source.get("filter"):
f = cfg_source.get('filter') f = cfg_source.get("filter")
if any(filter(lambda item: re.match(f, item), path)): if any(filter(lambda item: re.match(f, item), path)):
break break
else: else:
break break
else: else:
# no source found # no source found
raise RegistrationError( raise RegistrationError(f"Could not find a suitable source for {path!r}")
f'Could not find a suitable source for {path!r}'
)
return SOURCE_TYPES[cfg_source["type"]]( return SOURCE_TYPES[cfg_source["type"]](
cfg_source["name"], *cfg_source.get("args", []), **cfg_source.get("kwargs", {}) cfg_source["name"], *cfg_source.get("args", []), **cfg_source.get("kwargs", {})
......
...@@ -41,10 +41,7 @@ def parse_metadata_schema( ...@@ -41,10 +41,7 @@ def parse_metadata_schema(
) -> dict: ) -> dict:
out = {} out = {}
for key, param in schema.items(): for key, param in schema.items():
values = tree.xpath( values = tree.xpath(param.xpath, namespaces=param.namespaces or namespaces)
param.xpath,
namespaces=param.namespaces or namespaces
)
if param.multi: if param.multi:
value = [param.parser(v) if param.parser else v for v in values] value = [param.parser(v) if param.parser else v for v in values]
else: else:
......
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