-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_util.py
49 lines (39 loc) · 1.28 KB
/
sql_util.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
"""Utility for working with SQL files.
License:
BSD, see LICENSE.md
"""
import os
import jinja2
import const
def get_sql_file(filename, sql_dir=None, target_dir=None, additional_params=None):
"""Get contents of a SQL file with jinja applied.
Args:
filename: The name of the SQL file.
sql_dir: Optional subdirectory within the sql directory where this script can be found.
target_dir: Directory prefix to use or None if not required.
additional_params: Dictionary with additional jinja template values or None if no additional
params available.
Returns:
Contents of the requested SQL file after interpreting it as a jina template.
"""
if sql_dir:
template_path = os.path.join(
const.SQL_DIR,
sql_dir,
filename
)
else:
template_path = os.path.join(
const.SQL_DIR,
filename
)
all_params = {
'regions': const.REGIONS_INFO,
'target_dir': target_dir
}
if additional_params:
all_params.update(additional_params)
with open(template_path) as f:
template = jinja2.Environment(loader=jinja2.BaseLoader()).from_string(f.read())
rendered = template.render(**all_params)
return rendered