forked from amundsen-io/amundsendatabuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_variable_substitution_transformer.py
More file actions
39 lines (26 loc) · 1.09 KB
/
template_variable_substitution_transformer.py
File metadata and controls
39 lines (26 loc) · 1.09 KB
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
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import logging
from pyhocon import ConfigTree # noqa: F401
from typing import Any, Dict # noqa: F401
from databuilder.transformer.base_transformer import Transformer
TEMPLATE = 'template'
FIELD_NAME = 'field_name' # field name to UPSERT
LOGGER = logging.getLogger(__name__)
class TemplateVariableSubstitutionTransformer(Transformer):
"""
Add/Replace field in Dict by string.format based on given template and provide record Dict as a template parameter
https://docs.python.org/3.4/library/string.html#string.Formatter.format
"""
def init(self, conf):
# type: (ConfigTree) -> None
self._template = conf.get_string(TEMPLATE)
self._field_name = conf.get_string(FIELD_NAME)
def transform(self, record):
# type: (Dict[str, Any]) -> Dict[str, Any]
val = self._template.format(**record)
record[self._field_name] = val
return record
def get_scope(self):
# type: () -> str
return 'transformer.template_variable_substitution'