-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexport_models.py
More file actions
114 lines (92 loc) · 4.67 KB
/
Copy pathexport_models.py
File metadata and controls
114 lines (92 loc) · 4.67 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
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
# Export dbt models to Excel files.
#
# Run `python scripts/export_models.py --help` for details.
import argparse
from utils import constants
from utils.export import export_models
from utils.helpers import create_logger
CLI_DESCRIPTION = """Export dbt models to Excel files.
Expects dependencies from [project].dependencies (dbt dependencies) and [project.optional-dependencies].dbt_tests (script dependencies) be installed.
A few configuration values can be set on any model to support exporting:
* config.meta.export_name (optional): The base name of the output file that will be generated. File extensions are not necessary since all
models are exported as .xlsx files. If unset, defaults to the name of the model.
* config.meta.export_template (optional): Configs that apply to an optional Excel template that the script will populate with data. Attributes include:
* name (optional): The base name of the template file. File extensions are not necessary since all templates are assumed to be .xlsx files. Templates should
be stored in the export/templates/ directory and should include header rows. If unset, will search for a template with the same name as the
model; if no template is found or if the attribute is not present, defaults to a simple layout with filterable columns and striped rows.
* start_row (optional): The 1-indexed position of the row in the sheet that should be the first non-header row, i.e. the start row for the data.
* add_table (optional): Whether to add a data table for sorting and filtering. Defaults to True.
* sheet_name (optional): The name of the sheet in the workbook to populate with data. Defaults to "Sheet1".
* config.meta.export_format (optional): Formatting to apply to the output workbook. Useful for specific types of formatting, like alignment
and number formats, that Excel can only apply after populating a template with data
* format_blanks_as_empty_string (optional): When True, indicates to the script to export blanks as empty strings instead of nulls. Defaults to False.
""" # noqa: E501
CLI_EXAMPLE = """Example usage to output the 2024 non-tri town close QC report for Leyden, which is a non-tri town in 2024:
python scripts/export_models.py --selector select_qc_report_town_close_non_tri --where "township_code = '20' and taxyr = '2024'"
To output the 2024 tri town close QC report for Hyde Park, which is a tri town in 2024:
python scripts/export_models.py --selector select_qc_report_town_close_tri --where "township_code = '70' and taxyr = '2024'"
To output the 2024 AHSAP property report for Hyde Park:
python3 scripts/export_models.py --select qc.vw_change_in_ahsap_values --where "township_code = '70' and taxyr = '2024'"
""" # noqa: E501
def parse_args():
parser = argparse.ArgumentParser(
description=CLI_DESCRIPTION,
epilog=CLI_EXAMPLE,
# Parse the description and epilog as raw text so that newlines
# get preserved
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
*constants.TARGET_ARGUMENT_ARGS, **constants.TARGET_ARGUMENT_KWARGS
)
parser.add_argument(
*constants.REBUILD_ARGUMENT_ARGS, **constants.REBUILD_ARGUMENT_KWARGS
)
parser.add_argument(
*constants.SELECT_ARGUMENT_ARGS, **constants.SELECT_ARGUMENT_KWARGS
)
parser.add_argument(
*constants.SELECTOR_ARGUMENT_ARGS, **constants.SELECTOR_ARGUMENT_KWARGS
)
parser.add_argument(
"--where",
required=False,
help="SQL expression representing a WHERE clause to filter models",
)
parser.add_argument(
*constants.OUTPUT_DIR_ARGUMENT_ARGS,
**constants.OUTPUT_DIR_ARGUMENT_KWARGS,
)
parser.add_argument(
*constants.LOG_TO_FILE_ARGUMENT_ARGS,
**constants.LOG_TO_FILE_ARGUMENT_KWARGS,
)
parser.add_argument(
*constants.LOG_TO_CLOUDWATCH_GROUP_ARGUMENT_ARGS,
**constants.LOG_TO_CLOUDWATCH_GROUP_ARGUMENT_KWARGS,
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
logger = create_logger(
name=__name__,
log_file_path=args.log_to_file,
log_group_name=args.log_to_cloudwatch_group,
)
try:
logger.info("Starting model export")
export_models(
args.target,
args.select,
args.selector,
args.rebuild,
args.where,
args.output_dir,
)
logger.info("Export completed successfully.")
except Exception as e:
logger.error(
str(e) or f"{type(e).__name__} raised with no message",
exc_info=True,
)
raise