-
Notifications
You must be signed in to change notification settings - Fork 209
/
generic_extractor.py
52 lines (42 loc) · 1.45 KB
/
generic_extractor.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
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import importlib
from typing import Any, Iterable
from pyhocon import ConfigTree
from databuilder.extractor.base_extractor import Extractor
class GenericExtractor(Extractor):
"""
Extractor to extract any arbitrary values from users.
"""
EXTRACTION_ITEMS = 'extraction_items'
def init(self, conf: ConfigTree) -> None:
"""
Receives a list of dictionaries which is used for extraction
:param conf:
:return:
"""
self.conf = conf
self.values: Iterable[Any] = conf.get(GenericExtractor.EXTRACTION_ITEMS)
model_class = conf.get('model_class', None)
if model_class:
module_name, class_name = model_class.rsplit(".", 1)
mod = importlib.import_module(module_name)
self.model_class = getattr(mod, class_name)
results = [self.model_class(**result)
for result in self.values]
self._iter = iter(results)
else:
self._iter = iter(self.values)
def extract(self) -> Any:
"""
Fetch one sql result row, convert to {model_class} if specified before
returning.
:return:
"""
try:
result = next(self._iter)
return result
except StopIteration:
return None
def get_scope(self) -> str:
return 'extractor.generic'