/
coregistration.py
115 lines (96 loc) · 3.61 KB
/
coregistration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# coding: utf8
#
# Copyright (c) 2022 Centre National d'Etudes Spatiales (CNES).
#
# This file is part of demcompare
# (see https://github.com/CNES/demcompare).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
This module contains the Coregistration class factory.
This main API Coregistration class generates an object linked
with coregistration configuration "method_name"
(from registered CoregistrationTemplate class)
"""
# Standard imports
import logging
from typing import Any, Dict
from ..internal_typing import ConfigType
class Coregistration:
"""
Coregistration factory:
A class designed for registered all available coregistration methods
and instantiate them when needed.
"""
# Dict (method_name: str, class: object) containing registered
# available coregistration methods
available_coregistrations: Dict[str, Any] = {}
default_application = "nuth_kaab_internal"
def __new__(cls, cfg: ConfigType = None):
"""
Return a CoregistrationTemplate child instance
associated with the "method_name" given in the configuration
through create_coreg local method for clarity.
:param cfg: JSON configuration {'method_name': value}
:type cfg: ConfigType
"""
return cls.create_coreg(cfg)
@classmethod
def create_coreg(cls, cfg: ConfigType = None):
"""
Factory command to create the coregistration from method_name
Return a CoregistrationTemplate child instance
associated with the "method_name" given in the configuration
:param cfg: configuration {'method_name': value}
:type cfg: ConfigType
"""
# If no cfg is given, use default_application
coreg_method = cls.default_application
if bool(cfg):
coreg_method = cfg["method_name"]
try:
coreg_class = cls.available_coregistrations[coreg_method]
coreg = coreg_class(cfg)
logging.info("Coregistration method name: %s", coreg_method)
except KeyError:
logging.error(
"No coregistration method named %s supported", coreg_method
)
raise
return coreg
@classmethod
def print_coregistration_methods(cls):
"""
Print all registered applications
"""
for coreg_method_name in cls.available_coregistrations:
print(coreg_method_name)
@classmethod
def register(cls, coreg_method_name: str):
"""
Allows to register the CoregistrationTemplate subclass in the factory
with its coregistration method_name through decorator
:param coreg_method_name: the subclass name to be registered
:type coreg_method_name: string
"""
def decorator(coreg_subclass):
"""
Register the coregistration subclass in the available methods
:param coreg_subclass: the subclass to be registered
:type coreg_subclass: object
"""
cls.available_coregistrations[coreg_method_name] = coreg_subclass
return coreg_subclass
return decorator