-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathcapabilities.py
216 lines (165 loc) · 5.83 KB
/
capabilities.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Module with helpers to declare capabilities and plugin behavior."""
from enum import Enum, EnumMeta
from typing import Any, Optional
from warnings import warn
from singer_sdk.typing import (
BooleanType,
IntegerType,
ObjectType,
PropertiesList,
Property,
)
# Default JSON Schema to support config for built-in capabilities:
STREAM_MAPS_CONFIG = PropertiesList(
Property(
"stream_maps",
ObjectType(),
description="Config object for stream maps capability.",
),
Property(
"stream_map_config",
ObjectType(),
description="User-defined config values to be used within map expressions.",
),
).to_dict()
FLATTENING_CONFIG = PropertiesList(
Property(
"flattening_enabled",
BooleanType(),
description=(
"'True' to enable schema flattening and automatically expand nested "
"properties."
),
),
Property(
"flattening_max_depth",
IntegerType(),
description="The max depth to flatten schemas.",
),
).to_dict()
class DeprecatedEnum(Enum):
"""Base class for capabilities enumeration."""
def __new__(cls, value: Any, deprecation: Optional[str] = None) -> "DeprecatedEnum":
"""Create a new enum member.
Args:
value: Enum member value.
deprecation: Deprecation message.
Returns:
An enum member value.
"""
member: "DeprecatedEnum" = object.__new__(cls)
member._value_ = value
member._deprecation = deprecation
return member
@property
def deprecation_message(self) -> Optional[str]:
"""Get deprecation message.
Returns:
Deprecation message.
"""
self._deprecation: Optional[str]
return self._deprecation
def emit_warning(self) -> None:
"""Emit deprecation warning."""
warn(
f"{self.name} is deprecated. {self.deprecation_message}",
DeprecationWarning,
stacklevel=3,
)
class DeprecatedEnumMeta(EnumMeta):
"""Metaclass for enumeration with deprecation support."""
def __getitem__(self, name: str) -> Any:
"""Retrieve mapping item.
Args:
name: Item name.
Returns:
Enum member.
"""
obj: Enum = super().__getitem__(name)
if isinstance(obj, DeprecatedEnum) and obj.deprecation_message:
obj.emit_warning()
return obj
def __getattribute__(cls, name: str) -> Any:
"""Retrieve enum attribute.
Args:
name: Attribute name.
Returns:
Attribute.
"""
obj = super().__getattribute__(name)
if isinstance(obj, DeprecatedEnum) and obj.deprecation_message:
obj.emit_warning()
return obj
def __call__(self, *args: Any, **kwargs: Any) -> Enum:
"""Call enum member.
Args:
args: Positional arguments.
kwargs: Keyword arguments.
Returns:
Enum member.
"""
obj: Enum = super().__call__(*args, **kwargs)
if isinstance(obj, DeprecatedEnum) and obj.deprecation_message:
obj.emit_warning()
return obj
class CapabilitiesEnum(DeprecatedEnum, metaclass=DeprecatedEnumMeta):
"""Base capabilities enumeration."""
def __str__(self) -> str:
"""String representation.
Returns:
Stringified enum value.
"""
return str(self.value)
def __repr__(self) -> str:
"""String representation.
Returns:
Stringified enum value.
"""
return str(self.value)
class PluginCapabilities(CapabilitiesEnum):
"""Core capabilities which can be supported by taps and targets."""
#: Support plugin capability and setting discovery.
ABOUT = "about"
#: Support :doc:`inline stream map transforms</stream_maps>`.
STREAM_MAPS = "stream-maps"
#: Support schema flattening, aka denesting of complex properties.
FLATTENING = "schema-flattening"
#: Support the
#: `ACTIVATE_VERSION <https://hub.meltano.com/singer/docs#activate-version>`_
#: extension.
ACTIVATE_VERSION = "activate-version"
#: Input and output from
#: `batched files <https://hub.meltano.com/singer/docs#batch>`_.
#: A.K.A ``FAST_SYNC``.
BATCH = "batch"
class TapCapabilities(CapabilitiesEnum):
"""Tap-specific capabilities."""
#: Generate a catalog with `--discover`.
DISCOVER = "discover"
#: Accept input catalog, apply metadata and selection rules.
CATALOG = "catalog"
#: Incremental refresh by means of state tracking.
STATE = "state"
#: Automatic connectivity and stream init test via :ref:`--test<Test connectivity>`.
TEST = "test"
#: Support for ``replication_method: LOG_BASED``. You can read more about this
#: feature in `MeltanoHub <https://hub.meltano.com/singer/docs#log-based>`_.
LOG_BASED = "log-based"
#: Deprecated. Please use :attr:`~TapCapabilities.CATALOG` instead.
PROPERTIES = "properties", "Please use CATALOG instead."
class TargetCapabilities(CapabilitiesEnum):
"""Target-specific capabilities."""
#: Allows a ``soft_delete=True`` config option.
#: Requires a tap stream supporting :attr:`PluginCapabilities.ACTIVATE_VERSION`
#: and/or :attr:`TapCapabilities.LOG_BASED`.
SOFT_DELETE = "soft-delete"
#: Allows a ``hard_delete=True`` config option.
#: Requires a tap stream supporting :attr:`PluginCapabilities.ACTIVATE_VERSION`
#: and/or :attr:`TapCapabilities.LOG_BASED`.
HARD_DELETE = "hard-delete"
#: Fail safe for unknown JSON Schema types.
DATATYPE_FAILSAFE = "datatype-failsafe"
#: Allow denesting complex properties.
RECORD_FLATTENING = "record-flattening"
#: Allow setting the target schema.
TARGET_SCHEMA = "target-schema"